Repository navigation
fix(okhttp): allow explicit Content-Length headers on multipart parts - #9816
Amir-Golmoradi wants to merge 5 commits into
Conversation
| body: RequestBody, | ||
| ): Part { | ||
| require(headers?.get("Content-Type") == null) { "Unexpected header: Content-Type" } | ||
| require(headers?.get("Content-Length") == null) { "Unexpected header: Content-Length" } |
There was a problem hiding this comment.
if present do we need to validate that it matches body.contentLength()?
There was a problem hiding this comment.
I think so when body.contentLength() is known, to avoid sending a contradictory value. When it returns -1, we can’t validate it upfront. Would you still allow an explicit header in that case?
|
|
||
| /** | ||
| * Verifies that an explicit part-level Content-Length header is preserved | ||
| * and that the multipart content length matches the bytes written. |
There was a problem hiding this comment.
What's ensuring "that the multipart content length matches the bytes written", isn't that just because you setup the test as such?
There was a problem hiding this comment.
Yes, the test uses a body with a known, correct length. The assertion is a regression check that the added header is accounted for in the total multipart length. It doesn’t establish a general guarantee that the reported length always matches the bytes written.
yschimke
left a comment
There was a problem hiding this comment.
I'd like to understand the original request first.
yschimke
left a comment
There was a problem hiding this comment.
not clear what the changes are but using this to mark it as not quite ready to land.
Fixes #9814
#7319 stopped adding
Content-Lengthto multipart parts automatically, butPart.createstill rejects it when supplied explicitly. Callers therefore have no way to include the header.I removed that check. The writer already handles caller-supplied headers and counts their bytes toward the total multipart length. Parts without an explicit header are unchanged.
The outer length is still calculated from each body’s
contentLength(). If a body returns-1, the multipart length remains unknown, regardless of the supplied header.Tests cover both the factory and builder paths, the serialized output, the total byte count, and an unknown-length body with an explicit header. The existing rejection test now verifies that the builder preserves the header.
This patch allows explicit
Content-Lengthheaders for all multipart types. It does not include the FORM-specific restriction or value validation proposed in the issue discussion.The multipart tests,
spotlessCheck, and the fullcheckpass.