-
Notifications
You must be signed in to change notification settings - Fork 1.1k
KTOR-6508: Skip Content-Length header for GET, HEAD and OPTIONS #4895
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
Conversation
## Walkthrough
This change introduces a unified approach to HTTP header handling across multiple Ktor client engines by replacing the `mergeHeaders` utility with a new `forEachHeader` extension function on `HttpRequestData`. It also adds a `supportsRequestBody` property to `HttpMethod` to generalize logic for determining if an HTTP method allows a request body, refactoring several engines and tests accordingly.
## Changes
| Files/Paths (grouped) | Change Summary |
|--------------------------------------------------------------------------------------------------------------------|---------------------------------------------------------------------------------------------------------------|
| ktor-client/ktor-client-core/common/src/io/ktor/client/request/HttpRequest.kt<br>ktor-client/ktor-client-core/api/ktor-client-core.api<br>ktor-client/ktor-client-core/api/ktor-client-core.klib.api | Added `forEachHeader` extension function for unified header iteration and exposed it in public API. |
| ktor-http/common/src/io/ktor/http/HttpMethod.kt<br>ktor-http/api/ktor-http.api<br>ktor-http/api/ktor-http.klib.api | Added `supportsRequestBody` property to `HttpMethod` for checking if a method allows a request body. |
| ktor-client/ktor-client-android/jvm/src/io/ktor/client/engine/android/AndroidClientEngine.kt<br>ktor-client/ktor-client-core/js/src/io/ktor/client/engine/js/JsUtils.kt<br>ktor-client/ktor-client-core/wasmJs/src/io/ktor/client/engine/js/JsUtils.kt<br>ktor-client/ktor-client-curl/desktop/src/io/ktor/client/engine/curl/internal/CurlAdapters.kt<br>ktor-client/ktor-client-darwin-legacy/darwin/src/io/ktor/client/engine/darwin/internal/legacy/DarwinLegacyRequestUtils.kt<br>ktor-client/ktor-client-darwin/darwin/src/io/ktor/client/engine/darwin/internal/DarwinRequestUtils.kt<br>ktor-client/ktor-client-jetty-jakarta/jvm/src/io/ktor/client/engine/jetty/jakarta/JettyHttpRequest.kt<br>ktor-client/ktor-client-jetty/jvm/src/io/ktor/client/engine/jetty/JettyHttpRequest.kt<br>ktor-client/ktor-client-okhttp/jvm/src/io/ktor/client/engine/okhttp/OkHttpEngine.kt<br>ktor-client/ktor-client-winhttp/windows/src/io/ktor/client/engine/winhttp/internal/WinHttpRequestProducer.kt | Refactored to use `forEachHeader` for header processing, replacing `mergeHeaders` and simplifying header logic. |
| ktor-client/ktor-client-apache/jvm/src/io/ktor/client/engine/apache/ApacheRequestProducer.kt<br>ktor-client/ktor-client-apache5/jvm/src/io/ktor/client/engine/apache5/ApacheRequestProducer.kt<br>ktor-client/ktor-client-cio/common/src/io/ktor/client/engine/cio/utils.kt | Refactored request body logic to use `supportsRequestBody` for method checks, replacing explicit method lists. |
| ktor-client/ktor-client-tests/common/test/io/ktor/client/tests/ClientHeadersTest.kt | Removed most client exclusions from tests, allowing broader test coverage and updated comment for Java bug. |
## Possibly related PRs
- #4787: Modifies HTTP method request body support logic using `supportsRequestBody`, closely related to this PR's refactoring of method checks and header handling.
## Suggested reviewers
- e5l
- bjhham 📜 Recent review detailsConfiguration used: CodeRabbit UI 📒 Files selected for processing (20)
🚧 Files skipped from review as they are similar to previous changes (20)
✨ Finishing Touches
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed help? Create a ticket on our support page for assistance with any issues or questions. Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
217b6c9
to
5fda3cb
Compare
5fda3cb
to
a3e6c74
Compare
/** | ||
* Merges request [headers] and [body] headers according to [OutgoingContent] properties | ||
* and calls [block] for each header. | ||
* | ||
* [Report a problem](https://ktor.io/feedback/?fqname=io.ktor.client.request.forEachHeader) | ||
* | ||
* @see mergeHeaders | ||
*/ | ||
@InternalAPI | ||
public inline fun HttpRequestData.forEachHeader( | ||
crossinline block: (key: String, value: String) -> Unit | ||
) { |
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.
New API here
/** | ||
* Returns `true` if this request method can have a request body. | ||
*/ | ||
@InternalAPI | ||
public val HttpMethod.supportsRequestBody: Boolean | ||
get() = this !in REQUESTS_WITHOUT_BODY |
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.
And here
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.
Nice 👍
a3e6c74
to
902b9f5
Compare
Subsystem
Client
Motivation
KTOR-6508 The "Content-Length: 0" header is added for GET requests sent to some servers
Solution
The root cause is that we add "Content-Length" header inside
mergeHeaders
function ifcontentLength != null
without checking HTTP method. The problem is we don't have an access to HTTP method insidemergeHeaders
, so I introducedHttpRequestData.forEachHeader
which merges headers and iterates over them taking HTTP method into account.