Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

Allows method emulation to be determined via X-HTTP-Method-Override request header #84

Merged
merged 2 commits into from Jan 20, 2012
Jump to file or symbol
Failed to load files and symbols.
+23 −13
Split
@@ -99,6 +99,8 @@ public function __construct()
if ($this->request->format and $this->request->body)
{
$this->request->body = $this->format->factory($this->request->body, $this->request->format)->to_array();
+ // Assign payload arguments to proper method container
+ $this->{'_'.$this->request->method.'_args'} = $this->request->body;
}
// Merge both for one mega-args variable
@@ -244,15 +246,15 @@ public function response($data = array(), $http_code = null)
{
// all the compression settings must be done before sending any headers
// if php is not handling the compression by itself
- if (@ini_get('zlib.output_compression') == FALSE) {
- // ob_gzhandler depends on zlib
- if (extension_loaded('zlib')) {
- // if the client supports GZIP compression
- if (isset($_SERVER['HTTP_ACCEPT_ENCODING']) AND strpos($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip') !== FALSE) {
- ob_start('ob_gzhandler');
- }
- }
- }
+ if (@ini_get('zlib.output_compression') == FALSE) {
+ // ob_gzhandler depends on zlib
+ if (extension_loaded('zlib')) {
+ // if the client supports GZIP compression
+ if (isset($_SERVER['HTTP_ACCEPT_ENCODING']) AND strpos($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip') !== FALSE) {
+ ob_start('ob_gzhandler');
+ }
+ }
+ }
is_numeric($http_code) OR $http_code = 200;
@@ -403,10 +405,18 @@ protected function _detect_output_format()
protected function _detect_method()
{
$method = strtolower($this->input->server('REQUEST_METHOD'));
+ $request_headers = apache_request_headers();
- if ($this->config->item('enable_emulate_request') && $this->input->post('_method'))
+ if ($this->config->item('enable_emulate_request'))
{
- $method = strtolower($this->input->post('_method'));
+ if ($this->input->post('_method'))
+ {
+ $method = strtolower($this->input->post('_method'));
+ }
+ else if (isset($request_headers['X-HTTP-Method-Override']))
+ {
+ $method = strtolower($request_headers['X-HTTP-Method-Override']);
+ }
}
if (in_array($method, array('get', 'delete', 'post', 'put')))
@@ -426,8 +436,8 @@ protected function _detect_method()
protected function _detect_api_key()
{
- // Get the api key name variable set in the rest config file
- $api_key_variable = config_item('rest_key_name');
+ // Get the api key name variable set in the rest config file
+ $api_key_variable = config_item('rest_key_name');
// Work out the name of the SERVER entry based on config
$key_name = 'HTTP_' . strtoupper(str_replace('-', '_', $api_key_variable));