[Routing] changed HTTP method to always be uppercased (to be consistent with HttpFoundation/Request)

This commit is contained in:
Fabien Potencier 2011-06-04 18:59:57 +02:00
parent 736c27e0c9
commit c561f4f0c0
10 changed files with 45 additions and 41 deletions

View File

@ -628,6 +628,8 @@ class Request
/**
* Gets the request method.
*
* The method is always an uppercased string.
*
* @return string The request method
*/
public function getMethod()

View File

@ -24,7 +24,7 @@ class MethodNotAllowedException extends \RuntimeException implements ExceptionIn
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);
}

View File

@ -70,10 +70,10 @@ class ApacheMatcherDumper extends MatcherDumper
// method mismatch
if ($req = $route->getRequirement('_method')) {
$methods = explode('|', strtolower($req));
$methods = explode('|', strtoupper($req));
// GET and HEAD are equivalent
if (in_array('get', $methods) && !in_array('head', $methods)) {
$methods[] = 'head';
if (in_array('GET', $methods) && !in_array('HEAD', $methods)) {
$methods[] = 'HEAD';
}
$allow = array();
foreach ($methods as $method) {

View File

@ -137,10 +137,10 @@ EOF;
EOF;
if ($req = $route->getRequirement('_method')) {
$methods = explode('|', strtolower($req));
$methods = explode('|', strtoupper($req));
// GET and HEAD are equivalent
if (in_array('get', $methods) && !in_array('head', $methods)) {
$methods[] = 'head';
if (in_array('GET', $methods) && !in_array('HEAD', $methods)) {
$methods[] = 'HEAD';
}
if (1 === count($methods)) {
$code[] = <<<EOF

View File

@ -79,7 +79,7 @@ class UrlMatcher implements UrlMatcherInterface
}
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();
}
@ -112,11 +112,11 @@ class UrlMatcher implements UrlMatcherInterface
// check HTTP method requirement
if ($req = $route->getRequirement('_method')) {
// HEAD and GET are equivalent as per RFC
if ('head' === $method = $this->context->getMethod()) {
$method = 'get';
if ('HEAD' === $method = $this->context->getMethod()) {
$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);
continue;

View File

@ -36,10 +36,10 @@ class RequestContext
* @param integer $httpPort The HTTP 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->method = strtolower($method);
$this->method = strtoupper($method);
$this->host = $host;
$this->scheme = strtolower($scheme);
$this->httpPort = $httpPort;
@ -70,6 +70,8 @@ class RequestContext
/**
* Gets the HTTP method.
*
* The method is always an uppercased string.
*
* @return string The HTTP method
*/
public function getMethod()
@ -84,7 +86,7 @@ class RequestContext
*/
public function setMethod($method)
{
$this->method = strtolower($method);
$this->method = strtoupper($method);
}
/**

View File

@ -8,15 +8,15 @@ RewriteRule .* app.php [QSA,L,E=_ROUTING__route:foo,E=_ROUTING_bar:%1,E=_ROUTING
# bar
RewriteCond %{REQUEST_URI} ^/bar/([^/]+?)$
RewriteCond %{REQUEST_METHOD} !^(get|head)$ [NC]
RewriteRule .* - [S=1,E=_ROUTING__allow_get:1,E=_ROUTING__allow_head:1]
RewriteCond %{REQUEST_METHOD} !^(GET|HEAD)$ [NC]
RewriteRule .* - [S=1,E=_ROUTING__allow_GET:1,E=_ROUTING__allow_HEAD:1]
RewriteCond %{REQUEST_URI} ^/bar/([^/]+?)$
RewriteRule .* app.php [QSA,L,E=_ROUTING__route:bar,E=_ROUTING_foo:%1]
# baragain
RewriteCond %{REQUEST_URI} ^/baragain/([^/]+?)$
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]
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]
RewriteCond %{REQUEST_URI} ^/baragain/([^/]+?)$
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
RewriteCond %{REQUEST_URI} ^/test/([^/]+?)/$
RewriteCond %{REQUEST_METHOD} !^(post)$ [NC]
RewriteRule .* - [S=2,E=_ROUTING__allow_post:1]
RewriteCond %{REQUEST_METHOD} !^(POST)$ [NC]
RewriteRule .* - [S=2,E=_ROUTING__allow_POST:1]
RewriteCond %{REQUEST_URI} ^/test/([^/]+?)$
RewriteRule .* $0/ [QSA,L,R=301]
RewriteCond %{REQUEST_URI} ^/test/([^/]+?)/$
RewriteRule .* app.php [QSA,L,E=_ROUTING__route:baz5,E=_ROUTING_foo:%1]
# 405 Method Not Allowed
RewriteCond %{_ROUTING__allow_get} !-z [OR]
RewriteCond %{_ROUTING__allow_head} !-z [OR]
RewriteCond %{_ROUTING__allow_post} !-z
RewriteCond %{_ROUTING__allow_GET} !-z [OR]
RewriteCond %{_ROUTING__allow_HEAD} !-z [OR]
RewriteCond %{_ROUTING__allow_POST} !-z
RewriteRule .* app.php [QSA,L]

View File

@ -31,8 +31,8 @@ class ProjectUrlMatcher extends Symfony\Component\Routing\Matcher\UrlMatcher
// bar
if (0 === strpos($pathinfo, '/bar') && preg_match('#^/bar/(?P<foo>[^/]+?)$#x', $pathinfo, $matches)) {
if (!in_array($this->context->getMethod(), array('get', 'head'))) {
$allow = array_merge($allow, array('get', 'head'));
if (!in_array($this->context->getMethod(), array('GET', 'HEAD'))) {
$allow = array_merge($allow, array('GET', 'HEAD'));
goto not_bar;
}
$matches['_route'] = 'bar';
@ -42,8 +42,8 @@ class ProjectUrlMatcher extends Symfony\Component\Routing\Matcher\UrlMatcher
// barhead
if (0 === strpos($pathinfo, '/barhead') && preg_match('#^/barhead/(?P<foo>[^/]+?)$#x', $pathinfo, $matches)) {
if (!in_array($this->context->getMethod(), array('get', 'head'))) {
$allow = array_merge($allow, array('get', 'head'));
if (!in_array($this->context->getMethod(), array('GET', 'HEAD'))) {
$allow = array_merge($allow, array('GET', 'HEAD'));
goto not_barhead;
}
$matches['_route'] = 'barhead';
@ -74,8 +74,8 @@ class ProjectUrlMatcher extends Symfony\Component\Routing\Matcher\UrlMatcher
// baz5
if (0 === strpos($pathinfo, '/test') && preg_match('#^/test/(?P<foo>[^/]+?)/$#x', $pathinfo, $matches)) {
if ($this->context->getMethod() != 'post') {
$allow[] = 'post';
if ($this->context->getMethod() != 'POST') {
$allow[] = 'POST';
goto not_baz5;
}
$matches['_route'] = 'baz5';
@ -85,8 +85,8 @@ class ProjectUrlMatcher extends Symfony\Component\Routing\Matcher\UrlMatcher
// baz.baz6
if (0 === strpos($pathinfo, '/test') && preg_match('#^/test/(?P<foo>[^/]+?)/$#x', $pathinfo, $matches)) {
if ($this->context->getMethod() != 'put') {
$allow[] = 'put';
if ($this->context->getMethod() != 'PUT') {
$allow[] = 'PUT';
goto not_bazbaz6;
}
$matches['_route'] = 'baz.baz6';

View File

@ -31,8 +31,8 @@ class ProjectUrlMatcher extends Symfony\Tests\Component\Routing\Fixtures\Redirec
// bar
if (0 === strpos($pathinfo, '/bar') && preg_match('#^/bar/(?P<foo>[^/]+?)$#x', $pathinfo, $matches)) {
if (!in_array($this->context->getMethod(), array('get', 'head'))) {
$allow = array_merge($allow, array('get', 'head'));
if (!in_array($this->context->getMethod(), array('GET', 'HEAD'))) {
$allow = array_merge($allow, array('GET', 'HEAD'));
goto not_bar;
}
$matches['_route'] = 'bar';
@ -42,8 +42,8 @@ class ProjectUrlMatcher extends Symfony\Tests\Component\Routing\Fixtures\Redirec
// barhead
if (0 === strpos($pathinfo, '/barhead') && preg_match('#^/barhead/(?P<foo>[^/]+?)$#x', $pathinfo, $matches)) {
if (!in_array($this->context->getMethod(), array('get', 'head'))) {
$allow = array_merge($allow, array('get', 'head'));
if (!in_array($this->context->getMethod(), array('GET', 'HEAD'))) {
$allow = array_merge($allow, array('GET', 'HEAD'));
goto not_barhead;
}
$matches['_route'] = 'barhead';
@ -80,8 +80,8 @@ class ProjectUrlMatcher extends Symfony\Tests\Component\Routing\Fixtures\Redirec
// baz5
if (0 === strpos($pathinfo, '/test') && preg_match('#^/test/(?P<foo>[^/]+?)/?$#x', $pathinfo, $matches)) {
if ($this->context->getMethod() != 'post') {
$allow[] = 'post';
if ($this->context->getMethod() != 'POST') {
$allow[] = 'POST';
goto not_baz5;
}
if (substr($pathinfo, -1) !== '/') {
@ -94,8 +94,8 @@ class ProjectUrlMatcher extends Symfony\Tests\Component\Routing\Fixtures\Redirec
// baz.baz6
if (0 === strpos($pathinfo, '/test') && preg_match('#^/test/(?P<foo>[^/]+?)/?$#x', $pathinfo, $matches)) {
if ($this->context->getMethod() != 'put') {
$allow[] = 'put';
if ($this->context->getMethod() != 'PUT') {
$allow[] = 'PUT';
goto not_bazbaz6;
}
if (substr($pathinfo, -1) !== '/') {

View File

@ -40,7 +40,7 @@ class UrlMatcherTest extends \PHPUnit_Framework_TestCase
$matcher->match('/foo');
$this->fail();
} 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');
$this->fail();
} catch (MethodNotAllowedException $e) {
$this->assertEquals(array('post', 'put', 'delete'), $e->getAllowedMethods());
$this->assertEquals(array('POST', 'PUT', 'DELETE'), $e->getAllowedMethods());
}
}