Skip to content

Be more precise in recognising three-part curl response #102

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

Merged
merged 2 commits into from
Oct 8, 2019

Conversation

driehuis
Copy link
Contributor

On RHEL7, libcurl's curl_exec returns the data in two-part form when not using a proxy, and in three-part form when using a proxy (for example, when run with HTTPS_PROXY=myproxy:8080 in the PHP environment). The existing code presumes curl will only use the three part form with chunked transfers. This pull request proposes to better look at the first part to see if it is an isolated HTTP response line, indicating a three part response, or a multi-line response, indicating a two part response. Arguably, the strpos() is redundant, but given the vagaries of libcurl I'd rather be safe than sorry.

This fixes the side issue mentioned in #48 where the MessageBird PHP REST API reports "Got an invalid JSON response from the server." The issue could possibly be worked around by upgrading curl and/or its php wrapper, but that opens up another whole can of worms, and I'm not sure curl is wrong here in the first place.

@vanderwijk
Copy link

I'm using this modification in production, and for me it solved the "Got an invalid JSON response from the server" problem. Could this be merged so I can get rid of a private patch to make MesageBird work on Redhat 7?

Copy link
Contributor

@modprobe modprobe left a comment

Choose a reason for hiding this comment

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

Thanks for your contribution @driehuis! I left one small comment, once that's resolved we can get this show on the road 😎

And sorry for taking so long to take a look at this 🙏

@@ -219,7 +219,7 @@ public function performHttpRequest($method, $resourceName, $query = null, $body

// Split the header and body
$parts = explode("\r\n\r\n", $response, 3);
list($responseHeader, $responseBody) = ($parts[0] === 'HTTP/1.1 100 Continue') ? array ($parts[1], $parts[2]) : array ($parts[0], $parts[1]);
list($responseHeader, $responseBody) = (strpos($parts[0], "\n") == false && strpos($parts[0], 'HTTP/1.') == 0) ? array ($parts[1], $parts[2]) : array ($parts[0], $parts[1]);
Copy link
Contributor

Choose a reason for hiding this comment

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

Could you extract the condition into a separate variable like $isThreePartResponse for more readability?

Copy link
Contributor

Choose a reason for hiding this comment

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

Also, please use strict type comparison.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yup, agreed! I have updated the code.

…ct bits out of the result array. Use strict checking for type safety.
@modprobe modprobe merged commit 6a1666b into messagebird:master Oct 8, 2019
@modprobe
Copy link
Contributor

modprobe commented Oct 8, 2019

@driehuis @vanderwijk I just pushed v1.16.1 which includes this fix 🙂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants