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 2c34ae7 commit 6e95621
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 14 deletions.
2 changes: 1 addition & 1 deletion Exception/MethodNotAllowedException.php
Original file line number Diff line number Diff line change
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
6 changes: 3 additions & 3 deletions Matcher/Dumper/ApacheMatcherDumper.php
Original file line number Diff line number Diff line change
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
6 changes: 3 additions & 3 deletions Matcher/Dumper/PhpMatcherDumper.php
Original file line number Diff line number Diff line change
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 Matcher/UrlMatcher.php
Original file line number Diff line number Diff line change
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 RequestContext.php
Original file line number Diff line number Diff line change
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

0 comments on commit 6e95621

Please sign in to comment.