Allow HTTP method overriding.

pull/57/merge
Mike Cao 11 years ago
parent 86d1ea20e4
commit 5ba910981f

@ -132,23 +132,23 @@ class Request {
// Default properties // Default properties
if (empty($config)) { if (empty($config)) {
$config = array( $config = array(
'url' => $this->server('REQUEST_URI', '/'), 'url' => $this->getVar('REQUEST_URI', '/'),
'base' => str_replace(array('\\',' '), array('/','%20'), dirname($this->server('SCRIPT_NAME'))), 'base' => str_replace(array('\\',' '), array('/','%20'), dirname($this->getVar('SCRIPT_NAME'))),
'method' => $this->server('REQUEST_METHOD', 'GET'), 'method' => $this->getMethod(),
'referrer' => $this->server('HTTP_REFERER'), 'referrer' => $this->getVar('HTTP_REFERER'),
'ip' => $this->server('REMOTE_ADDR'), 'ip' => $this->getVar('REMOTE_ADDR'),
'ajax' => $this->server('HTTP_X_REQUESTED_WITH') == 'XMLHttpRequest', 'ajax' => $this->getVar('HTTP_X_REQUESTED_WITH') == 'XMLHttpRequest',
'scheme' => $this->server('SERVER_PROTOCOL', 'HTTP/1.1'), 'scheme' => $this->getVar('SERVER_PROTOCOL', 'HTTP/1.1'),
'user_agent' => $this->server('HTTP_USER_AGENT'), 'user_agent' => $this->getVar('HTTP_USER_AGENT'),
'body' => file_get_contents('php://input'), 'body' => file_get_contents('php://input'),
'type' => $this->server('CONTENT_TYPE'), 'type' => $this->getVar('CONTENT_TYPE'),
'length' => $this->server('CONTENT_LENGTH', 0), 'length' => $this->getVar('CONTENT_LENGTH', 0),
'query' => new Collection($_GET), 'query' => new Collection($_GET),
'data' => new Collection($_POST), 'data' => new Collection($_POST),
'cookies' => new Collection($_COOKIE), 'cookies' => new Collection($_COOKIE),
'files' => new Collection($_FILES), 'files' => new Collection($_FILES),
'secure' => $this->server('HTTPS', 'off') != 'off', 'secure' => $this->getVar('HTTPS', 'off') != 'off',
'accept' => $this->server('HTTP_ACCEPT'), 'accept' => $this->getVar('HTTP_ACCEPT'),
'proxy_ip' => $this->getProxyIpAddress() 'proxy_ip' => $this->getProxyIpAddress()
); );
} }
@ -197,6 +197,22 @@ class Request {
return $params; 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. * 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 $var Variable name
* @param string $default Default value to substitute * @param string $default Default value to substitute
* @return string Server variable value * @return string Server variable value
*/ */
private function server($var, $default='') { private function getVar($var, $default = '') {
return isset($_SERVER[$var]) ? $_SERVER[$var] : $default; return isset($_SERVER[$var]) ? $_SERVER[$var] : $default;
} }
} }

@ -80,4 +80,20 @@ class RequestTest extends PHPUnit_Framework_TestCase
$this->assertEquals(1, $request->cookies->q); $this->assertEquals(1, $request->cookies->q);
$this->assertEquals(1, $request->files->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);
}
} }

Loading…
Cancel
Save