2016-08-21 14:52:15 +03:00
|
|
|
<?php
|
2017-10-12 11:13:20 +03:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2016-08-21 14:52:15 +03:00
|
|
|
namespace Shlinkio\Shlink\Rest\Middleware;
|
|
|
|
|
2017-03-25 11:22:00 +03:00
|
|
|
use Fig\Http\Message\RequestMethodInterface;
|
|
|
|
use Interop\Http\ServerMiddleware\DelegateInterface;
|
|
|
|
use Interop\Http\ServerMiddleware\MiddlewareInterface;
|
2016-08-21 14:52:15 +03:00
|
|
|
use Psr\Http\Message\ResponseInterface as Response;
|
|
|
|
use Psr\Http\Message\ServerRequestInterface as Request;
|
2017-12-30 23:35:26 +03:00
|
|
|
use Shlinkio\Shlink\Rest\Exception\RuntimeException;
|
2016-08-21 14:52:15 +03:00
|
|
|
|
2017-03-25 11:22:00 +03:00
|
|
|
class BodyParserMiddleware implements MiddlewareInterface, RequestMethodInterface
|
2016-08-21 14:52:15 +03:00
|
|
|
{
|
|
|
|
/**
|
2017-03-25 11:22:00 +03:00
|
|
|
* Process an incoming server request and return a response, optionally delegating
|
|
|
|
* to the next middleware component to create the response.
|
2016-08-21 14:52:15 +03:00
|
|
|
*
|
|
|
|
* @param Request $request
|
2017-03-25 11:22:00 +03:00
|
|
|
* @param DelegateInterface $delegate
|
|
|
|
*
|
|
|
|
* @return Response
|
2016-08-21 14:52:15 +03:00
|
|
|
*/
|
2017-03-25 11:22:00 +03:00
|
|
|
public function process(Request $request, DelegateInterface $delegate)
|
2016-08-21 14:52:15 +03:00
|
|
|
{
|
|
|
|
$method = $request->getMethod();
|
2016-10-22 23:17:04 +03:00
|
|
|
$currentParams = $request->getParsedBody();
|
|
|
|
|
|
|
|
// In requests that do not allow body or if the body has already been parsed, continue to next middleware
|
2017-12-30 23:35:26 +03:00
|
|
|
if (! empty($currentParams) || \in_array($method, [
|
2017-03-25 11:22:00 +03:00
|
|
|
self::METHOD_GET,
|
|
|
|
self::METHOD_HEAD,
|
2017-10-12 11:13:20 +03:00
|
|
|
self::METHOD_OPTIONS,
|
2017-03-25 11:22:00 +03:00
|
|
|
], true)) {
|
|
|
|
return $delegate->process($request);
|
2016-08-21 14:52:15 +03:00
|
|
|
}
|
|
|
|
|
2016-10-22 23:17:04 +03:00
|
|
|
// If the accepted content is JSON, try to parse the body from JSON
|
|
|
|
$contentType = $this->getRequestContentType($request);
|
2017-12-30 23:35:26 +03:00
|
|
|
if (\in_array($contentType, ['application/json', 'text/json', 'application/x-json'], true)) {
|
2017-03-25 11:22:00 +03:00
|
|
|
return $delegate->process($this->parseFromJson($request));
|
2016-10-22 23:17:04 +03:00
|
|
|
}
|
|
|
|
|
2017-03-25 11:22:00 +03:00
|
|
|
return $delegate->process($this->parseFromUrlEncoded($request));
|
2016-10-22 23:17:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Request $request
|
|
|
|
* @return string
|
|
|
|
*/
|
2017-12-30 23:35:26 +03:00
|
|
|
private function getRequestContentType(Request $request): string
|
2016-10-22 23:17:04 +03:00
|
|
|
{
|
2016-08-21 14:52:15 +03:00
|
|
|
$contentType = $request->getHeaderLine('Content-type');
|
2017-12-30 23:35:26 +03:00
|
|
|
$contentTypes = \explode(';', $contentType);
|
|
|
|
return \trim(\array_shift($contentTypes));
|
2016-10-22 23:17:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Request $request
|
|
|
|
* @return Request
|
2017-12-30 23:35:26 +03:00
|
|
|
* @throws RuntimeException
|
2016-10-22 23:17:04 +03:00
|
|
|
*/
|
2017-12-30 23:35:26 +03:00
|
|
|
private function parseFromJson(Request $request): Request
|
2016-10-22 23:17:04 +03:00
|
|
|
{
|
2016-08-21 14:52:15 +03:00
|
|
|
$rawBody = (string) $request->getBody();
|
2016-10-22 23:17:04 +03:00
|
|
|
if (empty($rawBody)) {
|
|
|
|
return $request;
|
|
|
|
}
|
|
|
|
|
2017-12-30 23:35:26 +03:00
|
|
|
$parsedJson = \json_decode($rawBody, true);
|
|
|
|
if (\json_last_error() !== JSON_ERROR_NONE) {
|
|
|
|
throw new RuntimeException(\sprintf('Error when parsing JSON request body: %s', \json_last_error_msg()));
|
2016-10-22 23:17:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return $request->withParsedBody($parsedJson);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Request $request
|
|
|
|
* @return Request
|
|
|
|
*/
|
2017-12-30 23:35:26 +03:00
|
|
|
private function parseFromUrlEncoded(Request $request): Request
|
2016-10-22 23:17:04 +03:00
|
|
|
{
|
|
|
|
$rawBody = (string) $request->getBody();
|
|
|
|
if (empty($rawBody)) {
|
|
|
|
return $request;
|
2016-08-21 14:52:15 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
$parsedBody = [];
|
|
|
|
parse_str($rawBody, $parsedBody);
|
2016-10-22 23:17:04 +03:00
|
|
|
|
|
|
|
return $request->withParsedBody($parsedBody);
|
2016-08-21 14:52:15 +03:00
|
|
|
}
|
|
|
|
}
|