Skip to content

Fix headers parsing of HttpClient - #120

Merged
maciejlew merged 1 commit into
smsapi:masterfrom
myworkout:fix-headers-parsing
Mar 23, 2022
Merged

Fix headers parsing of HttpClient#120
maciejlew merged 1 commit into
smsapi:masterfrom
myworkout:fix-headers-parsing

Conversation

@kyryl-bogach

Copy link
Copy Markdown
Contributor

Reproducing the error:

By calling the sendSms method of this library like this:

sendSms(SendSmsBag::withMessage($phone, $message));

There is a change to have this exception:
image

When the header string contains a space in the end (notice the space in the end after HTTP/2 200).


Fixing the error:

This issue comes from HttpClient::execute that does not properly trim the header. What it is doing is only removing empty lines (with array_filter(explode("\n", $headerString), 'trim');). Now it will keep removing the empty lines but also it will trim the lines so that this exception is not thrown.

$headerString = "HTTP/2 200 \r
date: Tue, 22 Mar 2022 12:01:26 GMT\r
server: Apache\r
cache-control: no-cache, private\r
x-correlation-id: hc6f314f-e3d2-2b25-911e-e912e62aae1c\r
x-request-id: 14c12fa5-b6a8-2a83-a875-8c4c3841e4ed\r
cache-control: max-age=0\r
expires: Tue, 22 Mar 2022 12:01:26 GMT\r
content-type: application/json; charset=utf-8\r

// notice these new lines (which are taken care of by the array_filter)

";

.........

Expected result:
[
     "HTTP/2 200",
     "date: Tue, 22 Mar 2022 12:01:26 GMT",
     "server: Apache",
     "cache-control: no-cache, private",
     "x-correlation-id: hc6f314f-e3d2-2b25-911e-e912e62aae1c",
     "x-request-id: 14c12fa5-b6a8-2a83-a875-8c4c3841e4ed",
     "cache-control: max-age=0",
     "expires: Tue, 22 Mar 2022 12:01:26 GMT",
     "content-type: application/json; charset=utf-8",
]

Actual result:
[
     "HTTP/2 200 ",
     "date: Tue, 22 Mar 2022 12:01:26 GMT",
     "server: Apache",
     "cache-control: no-cache, private",
     "x-correlation-id: hc6f314f-e3d2-2b25-911e-e912e62aae1c",
     "x-request-id: 14c12fa5-b6a8-2a83-a875-8c4c3841e4ed",
     "cache-control: max-age=0",
     "expires: Tue, 22 Mar 2022 12:01:26 GMT",
     "content-type: application/json; charset=utf-8",
   ]

Where the error comes from:

Why do we have this error now but not before?

Because guzzle/psr7 updated a regex that was checking valid headers: https://github.com/guzzle/psr7/blob/master/src/MessageTrait.php

This has been released in 2.1.2 guzzle/psr7#492

@michalkortas

Copy link
Copy Markdown

Hi, I have the same bug.

@maciejlew
maciejlew self-requested a review March 23, 2022 08:26
@maciejlew
maciejlew merged commit 613e62f into smsapi:master Mar 23, 2022
maciejlew added a commit that referenced this pull request Mar 23, 2022
@kyryl-bogach

Copy link
Copy Markdown
Contributor Author

Thank you @maciejlew! 🙂

@kyryl-bogach
kyryl-bogach deleted the fix-headers-parsing branch March 23, 2022 11:35
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