Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Read content type in a more compatible way.
Not all webservers set CONTENT_TYPE. The built-in PHP webserver for
example sets HTTP_CONTENT_TYPE instead. Add a public method to the
request object to smooth over this difference.

Refs #6051
  • Loading branch information
markstory committed Mar 12, 2015
1 parent c644474 commit 56634c7
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
16 changes: 15 additions & 1 deletion src/Network/Request.php
Expand Up @@ -276,7 +276,7 @@ protected function _processPost($data)
{
$method = $this->env('REQUEST_METHOD');
if (in_array($method, ['PUT', 'DELETE', 'PATCH']) &&
strpos($this->env('CONTENT_TYPE'), 'application/x-www-form-urlencoded') === 0
strpos($this->contentType(), 'application/x-www-form-urlencoded') === 0
) {
$data = $this->input();
parse_str($data, $data);
Expand Down Expand Up @@ -472,6 +472,20 @@ protected function _processFileData($data, $post, $path = '', $field = '')
return $data;
}

/**
* Get the content type used in this request.
*
* @return string
*/
public function contentType()
{
$type = $this->env('CONTENT_TYPE');
if ($type) {
return $type;
}
return $this->env('HTTP_CONTENT_TYPE');
}

/**
* Returns the instance of the Session object for this request
*
Expand Down
16 changes: 16 additions & 0 deletions tests/TestCase/Network/RequestTest.php
Expand Up @@ -2398,6 +2398,22 @@ public function testSession()
$this->assertEquals($session, $request->session());
}

/**
* Test the content type method.
*
* @return void
*/
public function testContentType()
{
$_SERVER['HTTP_CONTENT_TYPE'] = 'application/json';
$request = Request::createFromGlobals();
$this->assertEquals('application/json', $request->contentType());

$_SERVER['CONTENT_TYPE'] = 'application/xml';
$request = Request::createFromGlobals();
$this->assertEquals('application/xml', $request->contentType(), 'prefer non http header.');
}

/**
* loadEnvironment method
*
Expand Down

4 comments on commit 56634c7

@challgren
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This bug is also present in 2.8.0

@markstory
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@challgren File an issue or open a pull request please. Commit comments get lost/forgotten about too easily.

@challgren
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Already did waiting for #8268 to be merged

@markstory
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks 👍

Please sign in to comment.