1
0
mirror of https://github.com/flarum/core.git synced 2025-01-18 06:38:25 +01:00

Update Stratigility, use http-interop middleware

This commit is contained in:
Franz Liedke 2018-03-18 13:52:16 +01:00
parent d5ef9486d0
commit 9d30be1617
No known key found for this signature in database
GPG Key ID: 9A0231A879B055F4
19 changed files with 72 additions and 101 deletions

View File

@ -54,7 +54,8 @@
"s9e/text-formatter": "^0.8.1",
"tobscure/json-api": "^0.3.0",
"zendframework/zend-diactoros": "^1.6",
"zendframework/zend-stratigility": "^1.3"
"zendframework/zend-stratigility": "^2.2",
"http-interop/http-middleware": "^0.4.0"
},
"require-dev": {
"mockery/mockery": "^0.9.4",

View File

@ -46,7 +46,6 @@ class AdminServiceProvider extends AbstractServiceProvider
$this->app->singleton('flarum.admin.middleware', function ($app) {
$pipe = new MiddlewarePipe;
$pipe->raiseThrowables();
// All requests should first be piped through our global error handler
$debugMode = ! $app->isUpToDate() || $app->inDebugMode();

View File

@ -12,21 +12,18 @@
namespace Flarum\Admin\Middleware;
use Flarum\User\AssertPermissionTrait;
use Psr\Http\Message\ResponseInterface as Response;
use Interop\Http\ServerMiddleware\DelegateInterface;
use Interop\Http\ServerMiddleware\MiddlewareInterface;
use Psr\Http\Message\ServerRequestInterface as Request;
use Zend\Stratigility\MiddlewareInterface;
class RequireAdministrateAbility implements MiddlewareInterface
{
use AssertPermissionTrait;
/**
* {@inheritdoc}
*/
public function __invoke(Request $request, Response $response, callable $out = null)
public function process(Request $request, DelegateInterface $delegate)
{
$this->assertAdmin($request->getAttribute('actor'));
return $out ? $out($request, $response) : $response;
return $delegate->process($request);
}
}

View File

@ -53,7 +53,6 @@ class ApiServiceProvider extends AbstractServiceProvider
$this->app->singleton('flarum.api.middleware', function ($app) {
$pipe = new MiddlewarePipe;
$pipe->raiseThrowables();
$pipe->pipe($app->make(HandleErrors::class));

View File

@ -11,18 +11,15 @@
namespace Flarum\Api\Middleware;
use Psr\Http\Message\ResponseInterface as Response;
use Interop\Http\ServerMiddleware\DelegateInterface;
use Interop\Http\ServerMiddleware\MiddlewareInterface;
use Psr\Http\Message\ServerRequestInterface as Request;
use Zend\Stratigility\MiddlewareInterface;
class FakeHttpMethods implements MiddlewareInterface
{
const HEADER_NAME = 'x-http-method-override';
/**
* {@inheritdoc}
*/
public function __invoke(Request $request, Response $response, callable $out = null)
public function process(Request $request, DelegateInterface $delegate)
{
if ($request->getMethod() === 'POST' && $request->hasHeader(self::HEADER_NAME)) {
$fakeMethod = $request->getHeaderLine(self::HEADER_NAME);
@ -30,6 +27,6 @@ class FakeHttpMethods implements MiddlewareInterface
$request = $request->withMethod(strtoupper($fakeMethod));
}
return $out ? $out($request, $response) : $response;
return $delegate->process($request);
}
}

View File

@ -13,10 +13,12 @@ namespace Flarum\Api\Middleware;
use Exception;
use Flarum\Api\ErrorHandler;
use Interop\Http\ServerMiddleware\DelegateInterface;
use Interop\Http\ServerMiddleware\MiddlewareInterface;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
class HandleErrors
class HandleErrors implements MiddlewareInterface
{
/**
* @var ErrorHandler
@ -35,14 +37,13 @@ class HandleErrors
* Catch all errors that happen during further middleware execution.
*
* @param Request $request
* @param Response $response
* @param callable $out
* @param DelegateInterface $delegate
* @return Response
*/
public function __invoke(Request $request, Response $response, callable $out = null)
public function process(Request $request, DelegateInterface $delegate)
{
try {
return $out($request, $response);
return $delegate->process($request);
} catch (Exception $e) {
return $this->errorHandler->handle($e);
}

View File

@ -38,7 +38,7 @@ class ConfigureMiddleware
$this->stackName = $stackName;
}
public function pipe(callable $middleware)
public function pipe($middleware)
{
$this->pipe->pipe($middleware);
}

View File

@ -50,7 +50,6 @@ class ForumServiceProvider extends AbstractServiceProvider
$this->app->singleton('flarum.forum.middleware', function ($app) {
$pipe = new MiddlewarePipe;
$pipe->raiseThrowables();
// All requests should first be piped through our global error handler
$debugMode = ! $app->isUpToDate() || $app->inDebugMode();

View File

@ -14,18 +14,15 @@ namespace Flarum\Http\Middleware;
use Flarum\Api\ApiKey;
use Flarum\Http\AccessToken;
use Flarum\User\User;
use Psr\Http\Message\ResponseInterface as Response;
use Interop\Http\ServerMiddleware\DelegateInterface;
use Interop\Http\ServerMiddleware\MiddlewareInterface;
use Psr\Http\Message\ServerRequestInterface as Request;
use Zend\Stratigility\MiddlewareInterface;
class AuthenticateWithHeader implements MiddlewareInterface
{
const TOKEN_PREFIX = 'Token ';
/**
* {@inheritdoc}
*/
public function __invoke(Request $request, Response $response, callable $out = null)
public function process(Request $request, DelegateInterface $delegate)
{
$headerLine = $request->getHeaderLine('authorization');
@ -53,7 +50,7 @@ class AuthenticateWithHeader implements MiddlewareInterface
}
}
return $out ? $out($request, $response) : $response;
return $delegate->process($request);
}
private function getUser($string)

View File

@ -13,17 +13,14 @@ namespace Flarum\Http\Middleware;
use Flarum\User\Guest;
use Flarum\User\User;
use Psr\Http\Message\ResponseInterface as Response;
use Interop\Http\ServerMiddleware\DelegateInterface;
use Interop\Http\ServerMiddleware\MiddlewareInterface;
use Psr\Http\Message\ServerRequestInterface as Request;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Zend\Stratigility\MiddlewareInterface;
class AuthenticateWithSession implements MiddlewareInterface
{
/**
* {@inheritdoc}
*/
public function __invoke(Request $request, Response $response, callable $out = null)
public function process(Request $request, DelegateInterface $delegate)
{
$session = $request->getAttribute('session');
@ -33,7 +30,7 @@ class AuthenticateWithSession implements MiddlewareInterface
$request = $request->withAttribute('actor', $actor);
return $out ? $out($request, $response) : $response;
return $delegate->process($request);
}
private function getActor(SessionInterface $session)

View File

@ -15,20 +15,17 @@ use Flarum\Http\AccessToken;
use Flarum\User\AuthToken;
use Flarum\User\EmailToken;
use Flarum\User\PasswordToken;
use Psr\Http\Message\ResponseInterface as Response;
use Interop\Http\ServerMiddleware\DelegateInterface;
use Interop\Http\ServerMiddleware\MiddlewareInterface;
use Psr\Http\Message\ServerRequestInterface as Request;
use Zend\Stratigility\MiddlewareInterface;
class CollectGarbage implements MiddlewareInterface
{
/**
* {@inheritdoc}
*/
public function __invoke(Request $request, Response $response, callable $out = null)
public function process(Request $request, DelegateInterface $delegate)
{
$this->collectGarbageSometimes();
return $out ? $out($request, $response) : $response;
return $delegate->process($request);
}
private function collectGarbageSometimes()

View File

@ -15,10 +15,12 @@ use FastRoute\Dispatcher;
use Flarum\Http\Exception\MethodNotAllowedException;
use Flarum\Http\Exception\RouteNotFoundException;
use Flarum\Http\RouteCollection;
use Interop\Http\ServerMiddleware\DelegateInterface;
use Interop\Http\ServerMiddleware\MiddlewareInterface;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
class DispatchRoute
class DispatchRoute implements MiddlewareInterface
{
/**
* @var RouteCollection
@ -44,13 +46,12 @@ class DispatchRoute
* Dispatch the given request to our route collection.
*
* @param Request $request
* @param Response $response
* @param callable $out
* @param DelegateInterface $delegate
* @return Response
* @throws MethodNotAllowedException
* @throws RouteNotFoundException
*/
public function __invoke(Request $request, Response $response, callable $out = null)
public function process(Request $request, DelegateInterface $delegate)
{
$method = $request->getMethod();
$uri = $request->getUri()->getPath() ?: '/';

View File

@ -14,14 +14,17 @@ namespace Flarum\Http\Middleware;
use Exception;
use Flarum\Settings\SettingsRepositoryInterface;
use Franzl\Middleware\Whoops\ErrorMiddleware as WhoopsMiddleware;
use Franzl\Middleware\Whoops\WhoopsRunner;
use Illuminate\Contracts\View\Factory as ViewFactory;
use Interop\Http\ServerMiddleware\DelegateInterface;
use Interop\Http\ServerMiddleware\MiddlewareInterface;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Log\LoggerInterface;
use Symfony\Component\Translation\TranslatorInterface;
use Zend\Diactoros\Response\HtmlResponse;
class HandleErrors
class HandleErrors implements MiddlewareInterface
{
/**
* @var ViewFactory
@ -68,20 +71,23 @@ class HandleErrors
* Catch all errors that happen during further middleware execution.
*
* @param Request $request
* @param Response $response
* @param callable $out
* @param DelegateInterface $delegate
* @return Response
*/
public function __invoke(Request $request, Response $response, callable $out = null)
public function process(Request $request, DelegateInterface $delegate)
{
try {
return $out($request, $response);
return $delegate->process($request);
} catch (Exception $e) {
return $this->formatException($e, $request, $response, $out);
if ($this->debug) {
return WhoopsRunner::handle($e, $request);
} else {
return $this->formatException($e);
}
}
}
protected function formatException(Exception $error, Request $request, Response $response, callable $out = null)
protected function formatException(Exception $error)
{
$status = 500;
$errorCode = $error->getCode();
@ -92,12 +98,6 @@ class HandleErrors
$status = $errorCode;
}
if ($this->debug) {
$whoops = new WhoopsMiddleware;
return $whoops($error, $request, $response, $out);
}
// Log the exception (with trace)
$this->logger->debug($error);

View File

@ -11,16 +11,13 @@
namespace Flarum\Http\Middleware;
use Psr\Http\Message\ResponseInterface as Response;
use Interop\Http\ServerMiddleware\DelegateInterface;
use Interop\Http\ServerMiddleware\MiddlewareInterface;
use Psr\Http\Message\ServerRequestInterface as Request;
use Zend\Stratigility\MiddlewareInterface;
class ParseJsonBody implements MiddlewareInterface
{
/**
* {@inheritdoc}
*/
public function __invoke(Request $request, Response $response, callable $out = null)
public function process(Request $request, DelegateInterface $delegate)
{
if (str_contains($request->getHeaderLine('content-type'), 'json')) {
$input = json_decode($request->getBody(), true);
@ -28,6 +25,6 @@ class ParseJsonBody implements MiddlewareInterface
$request = $request->withParsedBody($input ?: []);
}
return $out ? $out($request, $response) : $response;
return $delegate->process($request);
}
}

View File

@ -12,16 +12,13 @@
namespace Flarum\Http\Middleware;
use Flarum\Http\AccessToken;
use Psr\Http\Message\ResponseInterface as Response;
use Interop\Http\ServerMiddleware\DelegateInterface;
use Interop\Http\ServerMiddleware\MiddlewareInterface;
use Psr\Http\Message\ServerRequestInterface as Request;
use Zend\Stratigility\MiddlewareInterface;
class RememberFromCookie implements MiddlewareInterface
{
/**
* {@inheritdoc}
*/
public function __invoke(Request $request, Response $response, callable $out = null)
public function process(Request $request, DelegateInterface $delegate)
{
$id = array_get($request->getCookieParams(), 'flarum_remember');
@ -36,6 +33,6 @@ class RememberFromCookie implements MiddlewareInterface
}
}
return $out ? $out($request, $response) : $response;
return $delegate->process($request);
}
}

View File

@ -12,9 +12,9 @@
namespace Flarum\Http\Middleware;
use Flarum\Locale\LocaleManager;
use Psr\Http\Message\ResponseInterface as Response;
use Interop\Http\ServerMiddleware\DelegateInterface;
use Interop\Http\ServerMiddleware\MiddlewareInterface;
use Psr\Http\Message\ServerRequestInterface as Request;
use Zend\Stratigility\MiddlewareInterface;
class SetLocale implements MiddlewareInterface
{
@ -31,10 +31,7 @@ class SetLocale implements MiddlewareInterface
$this->locales = $locales;
}
/**
* {@inheritdoc}
*/
public function __invoke(Request $request, Response $response, callable $out = null)
public function process(Request $request, DelegateInterface $delegate)
{
$actor = $request->getAttribute('actor');
@ -48,6 +45,6 @@ class SetLocale implements MiddlewareInterface
$this->locales->setLocale($locale);
}
return $out ? $out($request, $response) : $response;
return $delegate->process($request);
}
}

View File

@ -13,9 +13,9 @@ namespace Flarum\Http\Middleware;
use Illuminate\Contracts\View\Factory as ViewFactory;
use Illuminate\Support\ViewErrorBag;
use Psr\Http\Message\ResponseInterface as Response;
use Interop\Http\ServerMiddleware\DelegateInterface;
use Interop\Http\ServerMiddleware\MiddlewareInterface;
use Psr\Http\Message\ServerRequestInterface as Request;
use Zend\Stratigility\MiddlewareInterface;
/**
* Inspired by Illuminate\View\Middleware\ShareErrorsFromSession.
@ -37,10 +37,7 @@ class ShareErrorsFromSession implements MiddlewareInterface
$this->view = $view;
}
/**
* {@inheritdoc}
*/
public function __invoke(Request $request, Response $response, callable $out = null)
public function process(Request $request, DelegateInterface $delegate)
{
$session = $request->getAttribute('session');
@ -57,6 +54,6 @@ class ShareErrorsFromSession implements MiddlewareInterface
$session->remove('errors');
return $out ? $out($request, $response) : $response;
return $delegate->process($request);
}
}

View File

@ -14,11 +14,12 @@ namespace Flarum\Http\Middleware;
use Dflydev\FigCookies\FigResponseCookies;
use Flarum\Http\CookieFactory;
use Illuminate\Support\Str;
use Interop\Http\ServerMiddleware\DelegateInterface;
use Interop\Http\ServerMiddleware\MiddlewareInterface;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Zend\Stratigility\MiddlewareInterface;
class StartSession implements MiddlewareInterface
{
@ -37,16 +38,13 @@ class StartSession implements MiddlewareInterface
$this->cookie = $cookie;
}
/**
* {@inheritdoc}
*/
public function __invoke(Request $request, Response $response, callable $out = null)
public function process(Request $request, DelegateInterface $delegate)
{
$session = $this->startSession();
$request = $request->withAttribute('session', $session);
$response = $out ? $out($request, $response) : $response;
$response = $delegate->process($request);
$response = $this->withCsrfTokenHeader($response, $session);

View File

@ -18,13 +18,14 @@ use Flarum\Http\Middleware\HandleErrors;
use Flarum\Http\Middleware\StartSession;
use Flarum\Install\InstallServiceProvider;
use Flarum\Update\UpdateServiceProvider;
use Interop\Http\ServerMiddleware\MiddlewareInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Zend\Diactoros\Response\HtmlResponse;
use Zend\Diactoros\Server as DiactorosServer;
use Zend\Stratigility\MiddlewareInterface;
use Zend\Stratigility\MiddlewarePipe;
use Zend\Stratigility\NoopFinalHandler;
use function Zend\Stratigility\path;
class Server
{
@ -76,7 +77,6 @@ class Server
protected function getMiddleware($requestPath)
{
$pipe = new MiddlewarePipe;
$pipe->raiseThrowables();
if (! $this->app->isInstalled()) {
return $this->getInstallerMiddleware($pipe);
@ -95,11 +95,11 @@ class Server
$forum = parse_url($this->app->url(''), PHP_URL_PATH) ?: '/';
if ($this->pathStartsWith($requestPath, $api)) {
$pipe->pipe($api, $this->app->make('flarum.api.middleware'));
$pipe->pipe(path($api, $this->app->make('flarum.api.middleware')));
} elseif ($this->pathStartsWith($requestPath, $admin)) {
$pipe->pipe($admin, $this->app->make('flarum.admin.middleware'));
$pipe->pipe(path($admin, $this->app->make('flarum.admin.middleware')));
} else {
$pipe->pipe($forum, $this->app->make('flarum.forum.middleware'));
$pipe->pipe(path($forum, $this->app->make('flarum.forum.middleware')));
}
return $pipe;