Skip to content

fix(okhttp): allow explicit Content-Length headers on multipart parts - #9816

Open
Amir-Golmoradi wants to merge 5 commits into
lysine-dev:mainfrom
Amir-Golmoradi:fix-multipart-content-length
Open

Amir-Golmoradi wants to merge 5 commits into
lysine-dev:mainfrom
Amir-Golmoradi:fix-multipart-content-length

Conversation

@Amir-Golmoradi

Copy link
Copy Markdown
Contributor

Fixes #9814

#7319 stopped adding Content-Length to multipart parts automatically, but Part.create still 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-Length headers 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 full check pass.

@Amir-Golmoradi

Copy link
Copy Markdown
Contributor Author

@yschimke, could you take a look when you have a chance? You reviewed #7319, which this follows up on.

body: RequestBody,
): Part {
require(headers?.get("Content-Type") == null) { "Unexpected header: Content-Type" }
require(headers?.get("Content-Length") == null) { "Unexpected header: Content-Length" }

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

if present do we need to validate that it matches body.contentLength()?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

What's ensuring "that the multipart content length matches the bytes written", isn't that just because you setup the test as such?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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 yschimke left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I'd like to understand the original request first.

@yschimke yschimke left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

not clear what the changes are but using this to mark it as not quite ready to land.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

Cannot add Content-Length header to a multipart part

2 participants