Another small refactor to Allow handling.
This commit is contained in:
parent
14464c2835
commit
eb73a41b88
3 changed files with 20 additions and 24 deletions
2
VERSION
2
VERSION
|
@ -1 +1 @@
|
||||||
0.2503.72005
|
0.2503.72017
|
||||||
|
|
|
@ -146,6 +146,8 @@ final class HttpResponseBuilder extends HttpMessageBuilder {
|
||||||
* @param string[] $methods Allowed methods.
|
* @param string[] $methods Allowed methods.
|
||||||
*/
|
*/
|
||||||
public function setAllow(array $methods): void {
|
public function setAllow(array $methods): void {
|
||||||
|
$methods = array_unique($methods);
|
||||||
|
sort($methods);
|
||||||
$this->setHeader('Allow', implode(', ', $methods));
|
$this->setHeader('Allow', implode(', ', $methods));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -223,33 +223,27 @@ class Router implements RequestHandlerInterface {
|
||||||
return $context->response->toResponse();
|
return $context->response->toResponse();
|
||||||
}
|
}
|
||||||
|
|
||||||
$getAllowedMethods = function() use ($methods) {
|
|
||||||
$allow = array_keys($methods);
|
|
||||||
$allow[] = 'OPTIONS';
|
|
||||||
if(in_array('GET', $allow))
|
|
||||||
$allow[] = 'HEAD';
|
|
||||||
|
|
||||||
$allow = array_unique($allow);
|
|
||||||
sort($allow);
|
|
||||||
|
|
||||||
return $allow;
|
|
||||||
};
|
|
||||||
|
|
||||||
if(!array_key_exists($context->request->method, $methods)) {
|
if(!array_key_exists($context->request->method, $methods)) {
|
||||||
if($context->request->method === 'OPTIONS') {
|
if($context->request->method === 'HEAD' && array_key_exists('GET', $methods)) {
|
||||||
// this should include CORS stuff
|
|
||||||
// should have an entry in RouteInfo which get aggregated here, but also attributes
|
|
||||||
$context->response->statusCode = 204;
|
|
||||||
$context->response->reasonPhrase = '';
|
|
||||||
$context->response->setAllow($getAllowedMethods());
|
|
||||||
return $context->response->toResponse();
|
|
||||||
} elseif($context->request->method === 'HEAD' && array_key_exists('GET', $methods)) {
|
|
||||||
$methods['HEAD'] = $methods['GET'];
|
$methods['HEAD'] = $methods['GET'];
|
||||||
} else {
|
} else {
|
||||||
$context->response->statusCode = 405;
|
$allow = array_keys($methods);
|
||||||
|
$allow[] = 'OPTIONS';
|
||||||
|
if(in_array('GET', $allow))
|
||||||
|
$allow[] = 'HEAD';
|
||||||
|
|
||||||
|
$context->response->setAllow($allow);
|
||||||
$context->response->reasonPhrase = '';
|
$context->response->reasonPhrase = '';
|
||||||
$context->response->setAllow($getAllowedMethods());
|
|
||||||
$this->errorHandler->handle($context);
|
if($context->request->method === 'OPTIONS') {
|
||||||
|
// this should include CORS stuff
|
||||||
|
// should have an entry in RouteInfo which get aggregated here, but also attributes
|
||||||
|
$context->response->statusCode = 204;
|
||||||
|
} else {
|
||||||
|
$context->response->statusCode = 405;
|
||||||
|
$this->errorHandler->handle($context);
|
||||||
|
}
|
||||||
|
|
||||||
return $context->response->toResponse();
|
return $context->response->toResponse();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue