-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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
Throw exception if you are using all wrong headers #2916
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be also good to add some additional validation in the psr7 class to ensure headers are actually of type array<string, string>
.
Yeah. I agree. But it is a bit tricky. Let's discuss that further on the psr7 repo |
@@ -344,6 +345,9 @@ private function applyOptions(RequestInterface $request, array &$options): Reque | |||
]; | |||
|
|||
if (isset($options['headers'])) { | |||
if (array_keys($options['headers']) === range(0, count($options['headers']) - 1)) { | |||
throw new RequestException('The headers array must have header name as keys.', $request); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My understanding is that this PR is not yet in any release: Should this exception be changed to InvalidArgumentException
instead? I seems to be more fitting.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this was chosen for consistency with what similar code here does. I'm open to review this for Guzzle 8.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking further below within this method I am just seeing InvalidArgumentException
being thrown. That was my primary motivation to suggest the change.
e.g.
Line 437 in e38c668
throw new InvalidArgumentException('sink must not be a boolean'); |
and
Lines 356 to 362 in e38c668
if (isset($options['multipart'])) { | |
throw new InvalidArgumentException('You cannot use ' | |
. 'form_params and multipart at the same time. Use the ' | |
. 'form_params option if you want to send application/' | |
. 'x-www-form-urlencoded requests, and the multipart ' | |
. 'option to send multipart/form-data requests.'); | |
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@GrahamCampbell Pinging in case you didn't see my response to make sure you see this before an eventual new release.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the ping (I had to clear all my notifications coz thousands built up and I was unable to prioritise effectively). This isn't tagged yet, right? What you are saying sounds reasonable to me. Are you able to send a PR over, so we can get this resolved before the 6.4.0 tag? :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, I will get right to it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
PR is up: #2942
… provided As discussed in PR guzzle#2916 after it was merged an `InvalidArgumentException` is more fitting, as passing an invalid `headers` array is a clear programming error that needs to be fixed and not caught. This is consistent with the validation of the other options, e.g. when using `multipart` and `form_params` at the same time. see guzzle#2916 see a2b8dd1
… provided (#2942) * Throw `InvalidArgumentException` when an incorrect `headers` array is provided As discussed in PR #2916 after it was merged an `InvalidArgumentException` is more fitting, as passing an invalid `headers` array is a clear programming error that needs to be fixed and not caught. This is consistent with the validation of the other options, e.g. when using `multipart` and `form_params` at the same time. see #2916 see a2b8dd1 * Remove obsolete import in Client.php / ClientTest.php
This will fix #2911
Note that this only thrown an exception if you are using all wrong headers. Ie, it check if you send a list or a map.