Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MAINTENANCE] Use Middleware for PageViewProxy #1004

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Classes/Controller/AbstractController.php
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ protected function configureProxyUrl(string &$url): void
->setCreateAbsoluteUri(!empty($this->settings['forceAbsoluteUrl']))
->setArguments(
[
'eID' => 'tx_dlf_pageview_proxy',
'middleware' => 'page-view-proxy',
'url' => $url,
'uHash' => GeneralUtility::hmac($url, 'PageViewProxy')
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,22 @@
* LICENSE.txt file that was distributed with this source code.
*/

namespace Kitodo\Dlf\Eid;
namespace Kitodo\Dlf\Middleware;

use Kitodo\Dlf\Common\Helper;
use Kitodo\Dlf\Common\StdOutStream;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
use TYPO3\CMS\Core\Configuration\ExtensionConfiguration;
use TYPO3\CMS\Core\Http\JsonResponse;
use TYPO3\CMS\Core\Http\RequestFactory;
use TYPO3\CMS\Core\Http\Response;
use TYPO3\CMS\Core\Utility\GeneralUtility;

/**
* eID image proxy for plugin 'Page View' of the 'dlf' extension
* Image proxy Middleware for plugin 'Page View' of the 'dlf' extension
*
* Supported query parameters:
* - `url` (mandatory): The URL to be proxied
Expand All @@ -34,7 +36,7 @@
*
* @access public
*/
class PageViewProxy
class PageViewProxy implements MiddlewareInterface
{
/**
* @access protected
Expand All @@ -61,15 +63,51 @@ public function __construct()
$this->extConf = GeneralUtility::makeInstance(ExtensionConfiguration::class)->get('dlf');
}

/**
* The process method of the middleware.
*
* @access public
*
* @param ServerRequestInterface $request
* @param RequestHandlerInterface $handler
*
* @return ResponseInterface
*/
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
$response = $handler->handle($request);
// Get input parameters and decrypt core name.
$parameters = $request->getQueryParams();
if (!isset($parameters['middleware']) || ($parameters['middleware'] != 'page-view-proxy')) {
return $response;
}

switch ($request->getMethod()) {
case 'OPTIONS':
return $this->handleOptions($request);

case 'GET':
return $this->handleGet($request);

case 'HEAD':
return $this->handleHead($request);

default:
// 405 Method Not Allowed
return GeneralUtility::makeInstance(Response::class)
->withStatus(405);
}
}

/**
* Return a response that is derived from $response and contains CORS
* headers to be sent to the client.
*
*
* @access protected
*
* @param ResponseInterface $response
* @param ServerRequestInterface $request The incoming request.
*
*
* @return ResponseInterface
*/
protected function withCorsResponseHeaders(
Expand Down Expand Up @@ -223,32 +261,4 @@ protected function handleGet(ServerRequestInterface $request): ResponseInterface

return $this->withCorsResponseHeaders($clientResponse, $request);
}

/**
* The main method of the eID script
*
* @access public
*
* @param ServerRequestInterface $request
*
* @return ResponseInterface
*/
public function main(ServerRequestInterface $request): ResponseInterface
{
switch ($request->getMethod()) {
case 'OPTIONS':
return $this->handleOptions($request);

case 'GET':
return $this->handleGet($request);

case 'HEAD':
return $this->handleHead($request);

default:
// 405 Method Not Allowed
return GeneralUtility::makeInstance(Response::class)
->withStatus(405);
}
}
}
5 changes: 5 additions & 0 deletions Configuration/RequestMiddlewares.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@
*/

return [
'backend' => [
'page-view-proxy' => [
'target' => \Kitodo\Dlf\Middleware\PageViewProxy::class,
],
],
'frontend' => [
'dlf/search-in-document' => [
'target' => \Kitodo\Dlf\Middleware\SearchInDocument::class,
Expand Down
2 changes: 1 addition & 1 deletion Tests/Functional/Api/PageViewProxyDisabledTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class PageViewProxyDisabledTest extends FunctionalTestCase

protected function queryProxy(array $query, string $method = 'GET')
{
$query['eID'] = 'tx_dlf_pageview_proxy';
$query['middleware'] = 'page-view-proxy';

return $this->httpClient->request($method, '', [
'query' => $query,
Expand Down
3 changes: 1 addition & 2 deletions Tests/Functional/Api/PageViewProxyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Kitodo\Dlf\Tests\Functional\Api;

use GuzzleHttp\Client as HttpClient;
use Kitodo\Dlf\Tests\Functional\FunctionalTestCase;
use TYPO3\CMS\Core\Utility\GeneralUtility;

Expand All @@ -19,7 +18,7 @@ protected function getDlfConfiguration()

protected function queryProxy(array $query, string $method = 'GET')
{
$query['eID'] = 'tx_dlf_pageview_proxy';
$query['middleware'] = 'page-view-proxy';

return $this->httpClient->request($method, '', [
'query' => $query,
Expand Down
4 changes: 0 additions & 4 deletions ext_localconf.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,6 @@
$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tcemain.php']['processDatamapClass'][] = \Kitodo\Dlf\Hooks\DataHandler::class;
$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tcemain.php']['processCmdmapClass'][] = \Kitodo\Dlf\Hooks\DataHandler::class;
$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['dlf/Classes/Common/MetsDocument.php']['hookClass'][] = \Kitodo\Dlf\Hooks\KitodoProductionHacks::class;
// Register AJAX eID handlers.
if ($GLOBALS['TYPO3_CONF_VARS']['EXTENSIONS']['dlf']['enableInternalProxy'] ?? false) {
$GLOBALS['TYPO3_CONF_VARS']['FE']['eID_include']['tx_dlf_pageview_proxy'] = \Kitodo\Dlf\Eid\PageViewProxy::class . '::main';
}
// Use Caching Framework for Solr queries
if (!is_array($GLOBALS['TYPO3_CONF_VARS']['SYS']['caching']['cacheConfigurations']['tx_dlf_solr'])) {
$GLOBALS['TYPO3_CONF_VARS']['SYS']['caching']['cacheConfigurations']['tx_dlf_solr'] = [];
Expand Down
Loading