[Routing] changed HTTP method to always be uppercased (to be consistent with HttpFoundation/Request)
This commit is contained in:
parent
736c27e0c9
commit
c561f4f0c0
@ -628,6 +628,8 @@ class Request
|
|||||||
/**
|
/**
|
||||||
* Gets the request method.
|
* Gets the request method.
|
||||||
*
|
*
|
||||||
|
* The method is always an uppercased string.
|
||||||
|
*
|
||||||
* @return string The request method
|
* @return string The request method
|
||||||
*/
|
*/
|
||||||
public function getMethod()
|
public function getMethod()
|
||||||
|
@ -24,7 +24,7 @@ class MethodNotAllowedException extends \RuntimeException implements ExceptionIn
|
|||||||
|
|
||||||
public function __construct(array $allowedMethods, $message = null, $code = 0, \Exception $previous = null)
|
public function __construct(array $allowedMethods, $message = null, $code = 0, \Exception $previous = null)
|
||||||
{
|
{
|
||||||
$this->allowedMethods = $allowedMethods;
|
$this->allowedMethods = array_map('strtoupper', $allowedMethods);
|
||||||
|
|
||||||
parent::__construct($message, $code, $previous);
|
parent::__construct($message, $code, $previous);
|
||||||
}
|
}
|
||||||
|
@ -70,10 +70,10 @@ class ApacheMatcherDumper extends MatcherDumper
|
|||||||
|
|
||||||
// method mismatch
|
// method mismatch
|
||||||
if ($req = $route->getRequirement('_method')) {
|
if ($req = $route->getRequirement('_method')) {
|
||||||
$methods = explode('|', strtolower($req));
|
$methods = explode('|', strtoupper($req));
|
||||||
// GET and HEAD are equivalent
|
// GET and HEAD are equivalent
|
||||||
if (in_array('get', $methods) && !in_array('head', $methods)) {
|
if (in_array('GET', $methods) && !in_array('HEAD', $methods)) {
|
||||||
$methods[] = 'head';
|
$methods[] = 'HEAD';
|
||||||
}
|
}
|
||||||
$allow = array();
|
$allow = array();
|
||||||
foreach ($methods as $method) {
|
foreach ($methods as $method) {
|
||||||
|
@ -137,10 +137,10 @@ EOF;
|
|||||||
EOF;
|
EOF;
|
||||||
|
|
||||||
if ($req = $route->getRequirement('_method')) {
|
if ($req = $route->getRequirement('_method')) {
|
||||||
$methods = explode('|', strtolower($req));
|
$methods = explode('|', strtoupper($req));
|
||||||
// GET and HEAD are equivalent
|
// GET and HEAD are equivalent
|
||||||
if (in_array('get', $methods) && !in_array('head', $methods)) {
|
if (in_array('GET', $methods) && !in_array('HEAD', $methods)) {
|
||||||
$methods[] = 'head';
|
$methods[] = 'HEAD';
|
||||||
}
|
}
|
||||||
if (1 === count($methods)) {
|
if (1 === count($methods)) {
|
||||||
$code[] = <<<EOF
|
$code[] = <<<EOF
|
||||||
|
@ -79,7 +79,7 @@ class UrlMatcher implements UrlMatcherInterface
|
|||||||
}
|
}
|
||||||
|
|
||||||
throw 0 < count($this->allow)
|
throw 0 < count($this->allow)
|
||||||
? new MethodNotAllowedException(array_unique(array_map('strtolower', $this->allow)))
|
? new MethodNotAllowedException(array_unique(array_map('strtoupper', $this->allow)))
|
||||||
: new ResourceNotFoundException();
|
: new ResourceNotFoundException();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -112,11 +112,11 @@ class UrlMatcher implements UrlMatcherInterface
|
|||||||
// check HTTP method requirement
|
// check HTTP method requirement
|
||||||
if ($req = $route->getRequirement('_method')) {
|
if ($req = $route->getRequirement('_method')) {
|
||||||
// HEAD and GET are equivalent as per RFC
|
// HEAD and GET are equivalent as per RFC
|
||||||
if ('head' === $method = $this->context->getMethod()) {
|
if ('HEAD' === $method = $this->context->getMethod()) {
|
||||||
$method = 'get';
|
$method = 'GET';
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!in_array($method, $req = explode('|', strtolower($req)))) {
|
if (!in_array($method, $req = explode('|', strtoupper($req)))) {
|
||||||
$this->allow = array_merge($this->allow, $req);
|
$this->allow = array_merge($this->allow, $req);
|
||||||
|
|
||||||
continue;
|
continue;
|
||||||
|
@ -36,10 +36,10 @@ class RequestContext
|
|||||||
* @param integer $httpPort The HTTP port
|
* @param integer $httpPort The HTTP port
|
||||||
* @param integer $httpsPort The HTTPS port
|
* @param integer $httpsPort The HTTPS port
|
||||||
*/
|
*/
|
||||||
public function __construct($baseUrl = '', $method = 'get', $host = 'localhost', $scheme = 'http', $httpPort = 80, $httpsPort = 443)
|
public function __construct($baseUrl = '', $method = 'GET', $host = 'localhost', $scheme = 'http', $httpPort = 80, $httpsPort = 443)
|
||||||
{
|
{
|
||||||
$this->baseUrl = $baseUrl;
|
$this->baseUrl = $baseUrl;
|
||||||
$this->method = strtolower($method);
|
$this->method = strtoupper($method);
|
||||||
$this->host = $host;
|
$this->host = $host;
|
||||||
$this->scheme = strtolower($scheme);
|
$this->scheme = strtolower($scheme);
|
||||||
$this->httpPort = $httpPort;
|
$this->httpPort = $httpPort;
|
||||||
@ -70,6 +70,8 @@ class RequestContext
|
|||||||
/**
|
/**
|
||||||
* Gets the HTTP method.
|
* Gets the HTTP method.
|
||||||
*
|
*
|
||||||
|
* The method is always an uppercased string.
|
||||||
|
*
|
||||||
* @return string The HTTP method
|
* @return string The HTTP method
|
||||||
*/
|
*/
|
||||||
public function getMethod()
|
public function getMethod()
|
||||||
@ -84,7 +86,7 @@ class RequestContext
|
|||||||
*/
|
*/
|
||||||
public function setMethod($method)
|
public function setMethod($method)
|
||||||
{
|
{
|
||||||
$this->method = strtolower($method);
|
$this->method = strtoupper($method);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -8,15 +8,15 @@ RewriteRule .* app.php [QSA,L,E=_ROUTING__route:foo,E=_ROUTING_bar:%1,E=_ROUTING
|
|||||||
|
|
||||||
# bar
|
# bar
|
||||||
RewriteCond %{REQUEST_URI} ^/bar/([^/]+?)$
|
RewriteCond %{REQUEST_URI} ^/bar/([^/]+?)$
|
||||||
RewriteCond %{REQUEST_METHOD} !^(get|head)$ [NC]
|
RewriteCond %{REQUEST_METHOD} !^(GET|HEAD)$ [NC]
|
||||||
RewriteRule .* - [S=1,E=_ROUTING__allow_get:1,E=_ROUTING__allow_head:1]
|
RewriteRule .* - [S=1,E=_ROUTING__allow_GET:1,E=_ROUTING__allow_HEAD:1]
|
||||||
RewriteCond %{REQUEST_URI} ^/bar/([^/]+?)$
|
RewriteCond %{REQUEST_URI} ^/bar/([^/]+?)$
|
||||||
RewriteRule .* app.php [QSA,L,E=_ROUTING__route:bar,E=_ROUTING_foo:%1]
|
RewriteRule .* app.php [QSA,L,E=_ROUTING__route:bar,E=_ROUTING_foo:%1]
|
||||||
|
|
||||||
# baragain
|
# baragain
|
||||||
RewriteCond %{REQUEST_URI} ^/baragain/([^/]+?)$
|
RewriteCond %{REQUEST_URI} ^/baragain/([^/]+?)$
|
||||||
RewriteCond %{REQUEST_METHOD} !^(get|post|head)$ [NC]
|
RewriteCond %{REQUEST_METHOD} !^(GET|POST|HEAD)$ [NC]
|
||||||
RewriteRule .* - [S=1,E=_ROUTING__allow_get:1,E=_ROUTING__allow_post:1,E=_ROUTING__allow_head:1]
|
RewriteRule .* - [S=1,E=_ROUTING__allow_GET:1,E=_ROUTING__allow_POST:1,E=_ROUTING__allow_HEAD:1]
|
||||||
RewriteCond %{REQUEST_URI} ^/baragain/([^/]+?)$
|
RewriteCond %{REQUEST_URI} ^/baragain/([^/]+?)$
|
||||||
RewriteRule .* app.php [QSA,L,E=_ROUTING__route:baragain,E=_ROUTING_foo:%1]
|
RewriteRule .* app.php [QSA,L,E=_ROUTING__route:baragain,E=_ROUTING_foo:%1]
|
||||||
|
|
||||||
@ -42,15 +42,15 @@ RewriteRule .* app.php [QSA,L,E=_ROUTING__route:baz4,E=_ROUTING_foo:%1]
|
|||||||
|
|
||||||
# baz5
|
# baz5
|
||||||
RewriteCond %{REQUEST_URI} ^/test/([^/]+?)/$
|
RewriteCond %{REQUEST_URI} ^/test/([^/]+?)/$
|
||||||
RewriteCond %{REQUEST_METHOD} !^(post)$ [NC]
|
RewriteCond %{REQUEST_METHOD} !^(POST)$ [NC]
|
||||||
RewriteRule .* - [S=2,E=_ROUTING__allow_post:1]
|
RewriteRule .* - [S=2,E=_ROUTING__allow_POST:1]
|
||||||
RewriteCond %{REQUEST_URI} ^/test/([^/]+?)$
|
RewriteCond %{REQUEST_URI} ^/test/([^/]+?)$
|
||||||
RewriteRule .* $0/ [QSA,L,R=301]
|
RewriteRule .* $0/ [QSA,L,R=301]
|
||||||
RewriteCond %{REQUEST_URI} ^/test/([^/]+?)/$
|
RewriteCond %{REQUEST_URI} ^/test/([^/]+?)/$
|
||||||
RewriteRule .* app.php [QSA,L,E=_ROUTING__route:baz5,E=_ROUTING_foo:%1]
|
RewriteRule .* app.php [QSA,L,E=_ROUTING__route:baz5,E=_ROUTING_foo:%1]
|
||||||
|
|
||||||
# 405 Method Not Allowed
|
# 405 Method Not Allowed
|
||||||
RewriteCond %{_ROUTING__allow_get} !-z [OR]
|
RewriteCond %{_ROUTING__allow_GET} !-z [OR]
|
||||||
RewriteCond %{_ROUTING__allow_head} !-z [OR]
|
RewriteCond %{_ROUTING__allow_HEAD} !-z [OR]
|
||||||
RewriteCond %{_ROUTING__allow_post} !-z
|
RewriteCond %{_ROUTING__allow_POST} !-z
|
||||||
RewriteRule .* app.php [QSA,L]
|
RewriteRule .* app.php [QSA,L]
|
||||||
|
@ -31,8 +31,8 @@ class ProjectUrlMatcher extends Symfony\Component\Routing\Matcher\UrlMatcher
|
|||||||
|
|
||||||
// bar
|
// bar
|
||||||
if (0 === strpos($pathinfo, '/bar') && preg_match('#^/bar/(?P<foo>[^/]+?)$#x', $pathinfo, $matches)) {
|
if (0 === strpos($pathinfo, '/bar') && preg_match('#^/bar/(?P<foo>[^/]+?)$#x', $pathinfo, $matches)) {
|
||||||
if (!in_array($this->context->getMethod(), array('get', 'head'))) {
|
if (!in_array($this->context->getMethod(), array('GET', 'HEAD'))) {
|
||||||
$allow = array_merge($allow, array('get', 'head'));
|
$allow = array_merge($allow, array('GET', 'HEAD'));
|
||||||
goto not_bar;
|
goto not_bar;
|
||||||
}
|
}
|
||||||
$matches['_route'] = 'bar';
|
$matches['_route'] = 'bar';
|
||||||
@ -42,8 +42,8 @@ class ProjectUrlMatcher extends Symfony\Component\Routing\Matcher\UrlMatcher
|
|||||||
|
|
||||||
// barhead
|
// barhead
|
||||||
if (0 === strpos($pathinfo, '/barhead') && preg_match('#^/barhead/(?P<foo>[^/]+?)$#x', $pathinfo, $matches)) {
|
if (0 === strpos($pathinfo, '/barhead') && preg_match('#^/barhead/(?P<foo>[^/]+?)$#x', $pathinfo, $matches)) {
|
||||||
if (!in_array($this->context->getMethod(), array('get', 'head'))) {
|
if (!in_array($this->context->getMethod(), array('GET', 'HEAD'))) {
|
||||||
$allow = array_merge($allow, array('get', 'head'));
|
$allow = array_merge($allow, array('GET', 'HEAD'));
|
||||||
goto not_barhead;
|
goto not_barhead;
|
||||||
}
|
}
|
||||||
$matches['_route'] = 'barhead';
|
$matches['_route'] = 'barhead';
|
||||||
@ -74,8 +74,8 @@ class ProjectUrlMatcher extends Symfony\Component\Routing\Matcher\UrlMatcher
|
|||||||
|
|
||||||
// baz5
|
// baz5
|
||||||
if (0 === strpos($pathinfo, '/test') && preg_match('#^/test/(?P<foo>[^/]+?)/$#x', $pathinfo, $matches)) {
|
if (0 === strpos($pathinfo, '/test') && preg_match('#^/test/(?P<foo>[^/]+?)/$#x', $pathinfo, $matches)) {
|
||||||
if ($this->context->getMethod() != 'post') {
|
if ($this->context->getMethod() != 'POST') {
|
||||||
$allow[] = 'post';
|
$allow[] = 'POST';
|
||||||
goto not_baz5;
|
goto not_baz5;
|
||||||
}
|
}
|
||||||
$matches['_route'] = 'baz5';
|
$matches['_route'] = 'baz5';
|
||||||
@ -85,8 +85,8 @@ class ProjectUrlMatcher extends Symfony\Component\Routing\Matcher\UrlMatcher
|
|||||||
|
|
||||||
// baz.baz6
|
// baz.baz6
|
||||||
if (0 === strpos($pathinfo, '/test') && preg_match('#^/test/(?P<foo>[^/]+?)/$#x', $pathinfo, $matches)) {
|
if (0 === strpos($pathinfo, '/test') && preg_match('#^/test/(?P<foo>[^/]+?)/$#x', $pathinfo, $matches)) {
|
||||||
if ($this->context->getMethod() != 'put') {
|
if ($this->context->getMethod() != 'PUT') {
|
||||||
$allow[] = 'put';
|
$allow[] = 'PUT';
|
||||||
goto not_bazbaz6;
|
goto not_bazbaz6;
|
||||||
}
|
}
|
||||||
$matches['_route'] = 'baz.baz6';
|
$matches['_route'] = 'baz.baz6';
|
||||||
|
@ -31,8 +31,8 @@ class ProjectUrlMatcher extends Symfony\Tests\Component\Routing\Fixtures\Redirec
|
|||||||
|
|
||||||
// bar
|
// bar
|
||||||
if (0 === strpos($pathinfo, '/bar') && preg_match('#^/bar/(?P<foo>[^/]+?)$#x', $pathinfo, $matches)) {
|
if (0 === strpos($pathinfo, '/bar') && preg_match('#^/bar/(?P<foo>[^/]+?)$#x', $pathinfo, $matches)) {
|
||||||
if (!in_array($this->context->getMethod(), array('get', 'head'))) {
|
if (!in_array($this->context->getMethod(), array('GET', 'HEAD'))) {
|
||||||
$allow = array_merge($allow, array('get', 'head'));
|
$allow = array_merge($allow, array('GET', 'HEAD'));
|
||||||
goto not_bar;
|
goto not_bar;
|
||||||
}
|
}
|
||||||
$matches['_route'] = 'bar';
|
$matches['_route'] = 'bar';
|
||||||
@ -42,8 +42,8 @@ class ProjectUrlMatcher extends Symfony\Tests\Component\Routing\Fixtures\Redirec
|
|||||||
|
|
||||||
// barhead
|
// barhead
|
||||||
if (0 === strpos($pathinfo, '/barhead') && preg_match('#^/barhead/(?P<foo>[^/]+?)$#x', $pathinfo, $matches)) {
|
if (0 === strpos($pathinfo, '/barhead') && preg_match('#^/barhead/(?P<foo>[^/]+?)$#x', $pathinfo, $matches)) {
|
||||||
if (!in_array($this->context->getMethod(), array('get', 'head'))) {
|
if (!in_array($this->context->getMethod(), array('GET', 'HEAD'))) {
|
||||||
$allow = array_merge($allow, array('get', 'head'));
|
$allow = array_merge($allow, array('GET', 'HEAD'));
|
||||||
goto not_barhead;
|
goto not_barhead;
|
||||||
}
|
}
|
||||||
$matches['_route'] = 'barhead';
|
$matches['_route'] = 'barhead';
|
||||||
@ -80,8 +80,8 @@ class ProjectUrlMatcher extends Symfony\Tests\Component\Routing\Fixtures\Redirec
|
|||||||
|
|
||||||
// baz5
|
// baz5
|
||||||
if (0 === strpos($pathinfo, '/test') && preg_match('#^/test/(?P<foo>[^/]+?)/?$#x', $pathinfo, $matches)) {
|
if (0 === strpos($pathinfo, '/test') && preg_match('#^/test/(?P<foo>[^/]+?)/?$#x', $pathinfo, $matches)) {
|
||||||
if ($this->context->getMethod() != 'post') {
|
if ($this->context->getMethod() != 'POST') {
|
||||||
$allow[] = 'post';
|
$allow[] = 'POST';
|
||||||
goto not_baz5;
|
goto not_baz5;
|
||||||
}
|
}
|
||||||
if (substr($pathinfo, -1) !== '/') {
|
if (substr($pathinfo, -1) !== '/') {
|
||||||
@ -94,8 +94,8 @@ class ProjectUrlMatcher extends Symfony\Tests\Component\Routing\Fixtures\Redirec
|
|||||||
|
|
||||||
// baz.baz6
|
// baz.baz6
|
||||||
if (0 === strpos($pathinfo, '/test') && preg_match('#^/test/(?P<foo>[^/]+?)/?$#x', $pathinfo, $matches)) {
|
if (0 === strpos($pathinfo, '/test') && preg_match('#^/test/(?P<foo>[^/]+?)/?$#x', $pathinfo, $matches)) {
|
||||||
if ($this->context->getMethod() != 'put') {
|
if ($this->context->getMethod() != 'PUT') {
|
||||||
$allow[] = 'put';
|
$allow[] = 'PUT';
|
||||||
goto not_bazbaz6;
|
goto not_bazbaz6;
|
||||||
}
|
}
|
||||||
if (substr($pathinfo, -1) !== '/') {
|
if (substr($pathinfo, -1) !== '/') {
|
||||||
|
@ -40,7 +40,7 @@ class UrlMatcherTest extends \PHPUnit_Framework_TestCase
|
|||||||
$matcher->match('/foo');
|
$matcher->match('/foo');
|
||||||
$this->fail();
|
$this->fail();
|
||||||
} catch (MethodNotAllowedException $e) {
|
} catch (MethodNotAllowedException $e) {
|
||||||
$this->assertEquals(array('post'), $e->getAllowedMethods());
|
$this->assertEquals(array('POST'), $e->getAllowedMethods());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -65,7 +65,7 @@ class UrlMatcherTest extends \PHPUnit_Framework_TestCase
|
|||||||
$matcher->match('/foo');
|
$matcher->match('/foo');
|
||||||
$this->fail();
|
$this->fail();
|
||||||
} catch (MethodNotAllowedException $e) {
|
} catch (MethodNotAllowedException $e) {
|
||||||
$this->assertEquals(array('post', 'put', 'delete'), $e->getAllowedMethods());
|
$this->assertEquals(array('POST', 'PUT', 'DELETE'), $e->getAllowedMethods());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user