diff --git a/flight/net/Request.php b/flight/net/Request.php index 8e73907..1947b37 100644 --- a/flight/net/Request.php +++ b/flight/net/Request.php @@ -132,23 +132,23 @@ class Request { // Default properties if (empty($config)) { $config = array( - 'url' => $this->server('REQUEST_URI', '/'), - 'base' => str_replace(array('\\',' '), array('/','%20'), dirname($this->server('SCRIPT_NAME'))), - 'method' => $this->server('REQUEST_METHOD', 'GET'), - 'referrer' => $this->server('HTTP_REFERER'), - 'ip' => $this->server('REMOTE_ADDR'), - 'ajax' => $this->server('HTTP_X_REQUESTED_WITH') == 'XMLHttpRequest', - 'scheme' => $this->server('SERVER_PROTOCOL', 'HTTP/1.1'), - 'user_agent' => $this->server('HTTP_USER_AGENT'), + 'url' => $this->getVar('REQUEST_URI', '/'), + 'base' => str_replace(array('\\',' '), array('/','%20'), dirname($this->getVar('SCRIPT_NAME'))), + 'method' => $this->getMethod(), + 'referrer' => $this->getVar('HTTP_REFERER'), + 'ip' => $this->getVar('REMOTE_ADDR'), + 'ajax' => $this->getVar('HTTP_X_REQUESTED_WITH') == 'XMLHttpRequest', + 'scheme' => $this->getVar('SERVER_PROTOCOL', 'HTTP/1.1'), + 'user_agent' => $this->getVar('HTTP_USER_AGENT'), 'body' => file_get_contents('php://input'), - 'type' => $this->server('CONTENT_TYPE'), - 'length' => $this->server('CONTENT_LENGTH', 0), + 'type' => $this->getVar('CONTENT_TYPE'), + 'length' => $this->getVar('CONTENT_LENGTH', 0), 'query' => new Collection($_GET), 'data' => new Collection($_POST), 'cookies' => new Collection($_COOKIE), 'files' => new Collection($_FILES), - 'secure' => $this->server('HTTPS', 'off') != 'off', - 'accept' => $this->server('HTTP_ACCEPT'), + 'secure' => $this->getVar('HTTPS', 'off') != 'off', + 'accept' => $this->getVar('HTTP_ACCEPT'), 'proxy_ip' => $this->getProxyIpAddress() ); } @@ -197,6 +197,22 @@ class Request { return $params; } + /** + * Gets the request method. + * + * @return string + */ + private function getMethod() { + if (isset($_SERVER['HTTP_X_HTTP_METHOD_OVERRIDE'])) { + return $_SERVER['HTTP_X_HTTP_METHOD_OVERRIDE']; + } + elseif (isset($_REQUEST['_method'])) { + return $_REQUEST['_method']; + } + + return $this->getVar('REQUEST_METHOD', 'GET'); + } + /** * Gets the real remote IP address. * @@ -227,13 +243,13 @@ class Request { } /** - * Get variable from $_SERVER using $default if not provided + * Gets a variable from $_SERVER using $default if not provided. * * @param string $var Variable name * @param string $default Default value to substitute * @return string Server variable value */ - private function server($var, $default='') { + private function getVar($var, $default = '') { return isset($_SERVER[$var]) ? $_SERVER[$var] : $default; } } diff --git a/tests/RequestTest.php b/tests/RequestTest.php index 1b6acae..ca32829 100644 --- a/tests/RequestTest.php +++ b/tests/RequestTest.php @@ -80,4 +80,20 @@ class RequestTest extends PHPUnit_Framework_TestCase $this->assertEquals(1, $request->cookies->q); $this->assertEquals(1, $request->files->q); } + + function testMethodOverrideWithHeader() { + $_SERVER['HTTP_X_HTTP_METHOD_OVERRIDE'] = 'PUT'; + + $request = new \flight\net\Request(); + + $this->assertEquals('PUT', $request->method); + } + + function testMethodOverrideWithPost() { + $_REQUEST['_method'] = 'PUT'; + + $request = new \flight\net\Request(); + + $this->assertEquals('PUT', $request->method); + } }