Another small refactor to Allow handling.

This commit is contained in:
flash 2025-03-07 20:17:58 +00:00
parent 14464c2835
commit eb73a41b88
Signed by: flash
GPG key ID: 2C9C2C574D47FE3E
3 changed files with 20 additions and 24 deletions

View file

@ -1 +1 @@
0.2503.72005
0.2503.72017

View file

@ -146,6 +146,8 @@ final class HttpResponseBuilder extends HttpMessageBuilder {
* @param string[] $methods Allowed methods.
*/
public function setAllow(array $methods): void {
$methods = array_unique($methods);
sort($methods);
$this->setHeader('Allow', implode(', ', $methods));
}

View file

@ -223,33 +223,27 @@ class Router implements RequestHandlerInterface {
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($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;
$context->response->reasonPhrase = '';
$context->response->setAllow($getAllowedMethods());
return $context->response->toResponse();
} elseif($context->request->method === 'HEAD' && array_key_exists('GET', $methods)) {
if($context->request->method === 'HEAD' && array_key_exists('GET', $methods)) {
$methods['HEAD'] = $methods['GET'];
} 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->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();
}
}