Skip to content

Commit

Permalink
[Routing] changed HTTP method to always be uppercased (to be consiste…
Browse files Browse the repository at this point in the history
…nt with HttpFoundation/Request)
  • Loading branch information
fabpot committed Jun 4, 2011
1 parent 736c27e commit c561f4f
Show file tree
Hide file tree
Showing 10 changed files with 45 additions and 41 deletions.
2 changes: 2 additions & 0 deletions src/Symfony/Component/HttpFoundation/Request.php
Expand Up @@ -628,6 +628,8 @@ public function setMethod($method)
/**
* Gets the request method.
*
* The method is always an uppercased string.
*
* @return string The request method
*/
public function getMethod()
Expand Down
Expand Up @@ -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);
}
Expand Down
Expand Up @@ -70,10 +70,10 @@ public function dump(array $options = array())

// 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) {
Expand Down
Expand Up @@ -137,10 +137,10 @@ private function compileRoute(Route $route, $name, $supportsRedirections)
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
Expand Down
8 changes: 4 additions & 4 deletions src/Symfony/Component/Routing/Matcher/UrlMatcher.php
Expand Up @@ -79,7 +79,7 @@ public function match($pathinfo)
}

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();
}

Expand Down Expand Up @@ -112,11 +112,11 @@ protected function matchCollection($pathinfo, RouteCollection $routes)
// 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;
Expand Down
8 changes: 5 additions & 3 deletions src/Symfony/Component/Routing/RequestContext.php
Expand Up @@ -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;
Expand Down Expand Up @@ -70,6 +70,8 @@ public function setBaseUrl($baseUrl)
/**
* Gets the HTTP method.
*
* The method is always an uppercased string.
*
* @return string The HTTP method
*/
public function getMethod()
Expand All @@ -84,7 +86,7 @@ public function getMethod()
*/
public function setMethod($method)
{
$this->method = strtolower($method);
$this->method = strtoupper($method);
}

/**
Expand Down
Expand Up @@ -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]

Expand All @@ -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]
Expand Up @@ -31,8 +31,8 @@ public function match($pathinfo)

// 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';
Expand All @@ -42,8 +42,8 @@ public function match($pathinfo)

// 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';
Expand Down Expand Up @@ -74,8 +74,8 @@ public function match($pathinfo)

// 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';
Expand All @@ -85,8 +85,8 @@ public function match($pathinfo)

// 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';
Expand Down
Expand Up @@ -31,8 +31,8 @@ public function match($pathinfo)

// 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';
Expand All @@ -42,8 +42,8 @@ public function match($pathinfo)

// 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';
Expand Down Expand Up @@ -80,8 +80,8 @@ public function match($pathinfo)

// 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) !== '/') {
Expand All @@ -94,8 +94,8 @@ public function match($pathinfo)

// 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) !== '/') {
Expand Down
Expand Up @@ -40,7 +40,7 @@ public function testMethodNotAllowed()
$matcher->match('/foo');
$this->fail();
} catch (MethodNotAllowedException $e) {
$this->assertEquals(array('post'), $e->getAllowedMethods());
$this->assertEquals(array('POST'), $e->getAllowedMethods());
}
}

Expand All @@ -65,7 +65,7 @@ public function testMethodNotAllowedAggregatesAllowedMethods()
$matcher->match('/foo');
$this->fail();
} catch (MethodNotAllowedException $e) {
$this->assertEquals(array('post', 'put', 'delete'), $e->getAllowedMethods());
$this->assertEquals(array('POST', 'PUT', 'DELETE'), $e->getAllowedMethods());
}
}

Expand Down

0 comments on commit c561f4f

Please sign in to comment.