Skip to content
New issue

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

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean up handling of PUT data. #2

Merged
merged 1 commit into from
Oct 31, 2013
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 47 additions & 16 deletions HTTP/OAuth/Provider/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,16 +55,49 @@ class HTTP_OAuth_Provider_Request extends HTTP_OAuth_Message
*/
protected $method = '';

/**
* Body data from the incoming request (POST/PUT)
*
* @var string Raw body data from the incoming request (POST/PUT)
*/
protected $body = '';

/**
* Construct
*
* @param string $body optional. The HTTP request body. Use this if your
* framework automatically reads the php://input
* stream.
*
* @return void
*/
public function __construct($rawBodyData = '')
public function __construct($body = '')
{
$this->setHeaders();
$this->setBody($body);
$this->setParametersFromRequest();
$this->rawBodyData = $rawBodyData;
}

/**
* Sets the body data for this request
*
* This is useful if your framework automatically reads the php://input
* stream and your API uses PUT or POST data.
*
* @param string $body the HTTP request body.
*
* @return HTTP_OAuth_Provider_Request the current object, for fluent
* interface.
*/
public function setBody($body = '')
{
if (empty($body)) {
$this->body = file_get_contents('php://input');
} else {
$this->body = (string)$body;
}

return $this;
}

/**
Expand All @@ -84,7 +117,7 @@ public function setHeaders(array $headers = array())
} else if (is_array($this->peclHttpHeaders())) {
$this->debug('Using pecl_http to get request headers');
$this->headers = $this->peclHttpHeaders();
} else {
} else {
$this->debug('Using $_SERVER to get request headers');
foreach ($_SERVER as $name => $value) {
if (substr($name, 0, 5) == 'HTTP_') {
Expand Down Expand Up @@ -167,21 +200,19 @@ public function setParametersFromRequest()
}
}

if ($this->getRequestMethod() == 'POST' || $this->getRequestMethod() == 'PUT') {
if ($this->getRequestMethod() == 'POST') {
$this->debug('getting data from POST');
} else {
$this->debug('getting data from PUT');
}
if ($this->getRequestMethod() === 'POST' || $this->getRequestMethod() === 'PUT') {
$this->debug('getting x-www-form-urlencoded data from request body');

$contentType = substr($this->getHeader('Content-Type'), 0, 33);
if ($contentType !== 'application/x-www-form-urlencoded') {
throw new HTTP_OAuth_Provider_Exception_InvalidRequest('Invalid ' .
'content type for POST or PUT request');
throw new HTTP_OAuth_Provider_Exception_InvalidRequest(
'Invalid content type for POST or PUT request'
);
}

$params = array_merge(
$params,
$this->parseQueryString($this->getPostData())
$this->parseQueryString($this->getBody())
);
}

Expand Down Expand Up @@ -328,13 +359,13 @@ public function getHeaders()

// @codeCoverageIgnoreStart
/**
* Gets POST data
* Gets request body
*
* @return string Post data
* @return string request data
*/
protected function getPostData()
protected function getBody()
{
return !empty($this->rawBodyData) ? $this->rawBodyData : file_get_contents('php://input');
return $this->body;
}
// @codeCoverageIgnoreEnd

Expand Down