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

Throw exception if you are using all wrong headers #2916

Merged
merged 1 commit into from Jul 17, 2021

Conversation

Nyholm
Copy link
Member

@Nyholm Nyholm commented Jul 17, 2021

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.

Copy link
Member

@GrahamCampbell GrahamCampbell left a 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>.

@Nyholm
Copy link
Member Author

Nyholm commented Jul 17, 2021

Yeah. I agree. But it is a bit tricky.

Let's discuss that further on the psr7 repo

@Nyholm Nyholm merged commit a2b8dd1 into guzzle:master Jul 17, 2021
@@ -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);
Copy link
Contributor

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.

Copy link
Member

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.

Copy link
Contributor

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.

throw new InvalidArgumentException('sink must not be a boolean');

and

guzzle/src/Client.php

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.');
}

Copy link
Contributor

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.

Copy link
Member

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? :)

Copy link
Contributor

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.

Copy link
Contributor

Choose a reason for hiding this comment

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

PR is up: #2942

TimWolla added a commit to WoltLab/guzzle that referenced this pull request Oct 13, 2021
… 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
GrahamCampbell pushed a commit that referenced this pull request Oct 17, 2021
… 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
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.

strtolower() expects parameter 1 to be string, int given
3 participants