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

Support multiple values for HTTP header with same key #411

Merged
merged 5 commits into from
Oct 12, 2023
Merged

Conversation

qnga
Copy link
Member

@qnga qnga commented Oct 5, 2023

Deprecated

Shared

  • DefaultHttClient.additionalHeaders is deprecated. Set all the headers when creating a new HttpRequest, or modify outgoing requests in DefaultHttpClient.Callback.onStartRequest().

@qnga qnga requested a review from mickael-menu October 5, 2023 14:25
Copy link
Member

@mickael-menu mickael-menu left a comment

Choose a reason for hiding this comment

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

I think we should also modify HttpHeaders.getAll() to split the values by , here:

As the caller is using getAll() instead of get(), we can infer that the header supports multiple values and so should be split by , to expand all the values into a single List<String>.

@qnga
Copy link
Member Author

qnga commented Oct 7, 2023

That's right. The get method could become confusing then though: the caller might call it because he's interested only in the first value, not because he knows that he header can't contain multiple values. So get would require him to split values while getAll would not. What do you think?

@qnga
Copy link
Member Author

qnga commented Oct 7, 2023

Actually, public methods are valueForHeader and valuesForHeader. I wonder if the best solution wouldn't be that valueForHeader must be used for single-value headers and valuesForHeader for multiple-value headers. Alternatively, we could leave all splitting to callers as we do now in my PR (that's the OkHttp strategy for instance).

@mickael-menu
Copy link
Member

I wonder if the best solution wouldn't be that valueForHeader must be used for single-value headers and valuesForHeader for multiple-value headers.

That's what I had in mind. But we could also have valueForHeader join all the field values with a comma if there are several. If the caller is interested in the first value, they should use valuesForHeader().first.

Unfortunately we can't preemptively split the values as some single-value headers are allowed to contain a comma, e.g.

Expires: Wed, 21 Oct 2015 07:28:00 GMT

@qnga
Copy link
Member Author

qnga commented Oct 7, 2023

Unfortunately, even single-value headers can have multiple occurrences. For instance, I'm afraid that none of both methods would be suitable for reading headers such as Set-Cookie.
valueForHeader would return either only the first occurrence or all the values concatenated in a form difficult to split back. On the other hand, valuesForHeader would dangerously split values at commas.

OkHttp's strategy seems the wisest choice to me.

@mickael-menu
Copy link
Member

You're right, HTTP headers are a mess! Which strategy OkHttp uses?

@qnga
Copy link
Member Author

qnga commented Oct 7, 2023

It is the caller’s responsibility to detect and split on commas if their field permits multiple values.

Actually they still offer a helper to get the raw value of the last header with a given name.

In our case, that would mean offering headers(name: String): List<String> and header(name: String): String? (I feel like value could imply true single value). This way, a caller interested in only one value of a header not allowing comma-separated list would only have to call header(name: String).

@mickael-menu
Copy link
Member

Sounds good. With header() returning the last value for the name, instead of the first?

@qnga
Copy link
Member Author

qnga commented Oct 9, 2023

Yes! I updated the interface.

@qnga qnga requested a review from mickael-menu October 9, 2023 04:59
@mickael-menu mickael-menu merged commit 0a6848e into v3 Oct 12, 2023
3 checks passed
@mickael-menu mickael-menu deleted the update-http branch October 12, 2023 09:31
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.

2 participants