Browse Source

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

Fabien Potencier 14 years ago
parent
commit
c561f4f0c0

+ 2 - 0
src/Symfony/Component/HttpFoundation/Request.php

@@ -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()

+ 1 - 1
src/Symfony/Component/Routing/Exception/MethodNotAllowedException.php

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

+ 3 - 3
src/Symfony/Component/Routing/Matcher/Dumper/ApacheMatcherDumper.php

@@ -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) {

+ 3 - 3
src/Symfony/Component/Routing/Matcher/Dumper/PhpMatcherDumper.php

@@ -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

+ 4 - 4
src/Symfony/Component/Routing/Matcher/UrlMatcher.php

@@ -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;

+ 5 - 3
src/Symfony/Component/Routing/RequestContext.php

@@ -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);
     }
 
     /**

+ 9 - 9
tests/Symfony/Tests/Component/Routing/Fixtures/dumper/url_matcher1.apache

@@ -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]

+ 8 - 8
tests/Symfony/Tests/Component/Routing/Fixtures/dumper/url_matcher1.php

@@ -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';

+ 8 - 8
tests/Symfony/Tests/Component/Routing/Fixtures/dumper/url_matcher2.php

@@ -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) !== '/') {

+ 2 - 2
tests/Symfony/Tests/Component/Routing/Matcher/UrlMatcherTest.php

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