Skip to content

GitHub Issue #177: make reading from php://input configurable#181

Merged
rokob merged 12 commits intorollbar:masterfrom
ArturMoczulski:github-177-phpinput
Jun 13, 2017
Merged

GitHub Issue #177: make reading from php://input configurable#181
rokob merged 12 commits intorollbar:masterfrom
ArturMoczulski:github-177-phpinput

Conversation

@ArturMoczulski
Copy link
Copy Markdown
Contributor

In response to feedback from #177 I decided to make reading the raw request body from php://input disabled by default and configurable through include_raw_request_body.

Unfortunately, I don't see any way to overcome PHP < 5.6 limitation of single read from php://input so I combined making it configurable and also saving the content of the stream for later use in $_SERVER['php://input']. This is not perfect, but I don't think we can do anything more for < 5.6 users due to PHP limitations.

Comment thread src/DataBuilder.php Outdated

protected function setIncludeRawRequestBody($config)
{
$fromConfig = $this->tryGet($config, 'include_raW_request_body');
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

s/include_raW_request_body/include_raw_request_body/

Comment thread README.md
If you still want to read the request body for your PUT requests Rollbar SDK saves
the content of php://input in $_SERVER['php://input']

Default: `false`
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Is this something that most people assume should be there though? I feel like the default should be what we have been doing so far and allow people using < 5.6 to deal with the problem. But I am not sure, it is a shitty situation that I don't see a better solution for.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This alters the default behavior of PHP in what I would call an unexpected place. Because of this I would opt for keeping this disabled by default.

Comment thread src/DataBuilder.php Outdated
if (!$this->requestBody) {
if (!$this->requestBody && $this->includeRawRequestBody) {
$this->requestBody = file_get_contents("php://input");
$_SERVER['php://input'] = $this->requestBody;
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Saving request body into $_SERVER make sense to keep only for old PHP versions due to performance reason. And even withing old PHP versions it's a huge hack, IMO, to build the logic of own application based on some workaround behavior of vendor logging library. I would recommend to just remove this line.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@rokob For the same reasons I opt in for include_raw_request_body to be disabled by default.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

What if we did

if (version_compare(PHP_VERSION, '5.6.0') < 0) {
    $_SERVER['php://input'] = $this->requestBody;
}

At least then we only do this hack when we need it.

@coryvirok coryvirok added the Type: Bug Fix a component so that its behavior aligns with its documentation. label Jun 7, 2017
Comment thread README.md
@see http://php.net/manual/pl/wrappers.php.php#refsect2-wrappers.php-unknown-unknown-descriptiop
If you still want to read the request body for your PUT requests Rollbar SDK saves
the content of php://input in $_SERVER['php://input']

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Let's mention the default being false for this

Comment thread src/DataBuilder.php Outdated
if (!$this->requestBody) {
if (!$this->requestBody && $this->rawRequestBody) {
$this->requestBody = file_get_contents("php://input");
$_SERVER['php://input'] = $this->requestBody;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Let's do something like to only do this when absolutely necessary

if (version_compare(PHP_VERSION, '5.6.0') < 0) {
    $_SERVER['php://input'] = $this->requestBody;
}

Comment thread src/Defaults.php Outdated

public function rawRequestBody($rawRequestBody = null)
{
return $rawRequestBody ? $rawRequestBody : $this->defaultRawRequestBody;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

We should be testing for $rawRequestBody !== null

Comment thread tests/DataBuilderTest.php Outdated
$requestBody = $output->getRequest()->getBody();

$this->assertEquals($streamInput, $requestBody);
$this->assertEquals($streamInput, $_SERVER['php://input']);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Only do this check on less than 5.6

@rokob
Copy link
Copy Markdown
Contributor

rokob commented Jun 12, 2017

Other than the comments this looks good

@rokob
Copy link
Copy Markdown
Contributor

rokob commented Jun 13, 2017

This LGTM but there is still an issue with the tests taking too long, I'm going to merge this and deal with the test timeouts elsewhere.

@rokob rokob merged commit 704e7b6 into rollbar:master Jun 13, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Type: Bug Fix a component so that its behavior aligns with its documentation.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants