Repository navigation
Keep the raw URI when adding a Range header - #2393
Open
pavel-ptashyts wants to merge 1 commit into
Open
pavel-ptashyts wants to merge 1 commit into
pavel-ptashyts wants to merge 1 commit into
Conversation
Request.toBuilder() always uses the fixing URI encoder, so a request
built with URL encoding disabled, such as http://localhost/a|b?q={x},
went out as /a%7Cb?q=%7Bx%7D once a Range header was added, either for
its range offset in DefaultAsyncHttpClient.preProcessRequest or by
ResumableAsyncHandler.adjustRequestRange. The latter also rebuilt the
request on every call, even when it set no header.
Both paths now copy a DefaultRequest as toBuilder() does, but with URL
encoding disabled: its URI is already encoded, so it is kept as built,
and build() still validates and fills in everything it did before.
adjustRequestRange returns the request as is when it needs no header,
unless it is a DefaultRequest from the public constructor with a null
charset or null lists, which is still rebuilt so build() fills them
in. Subclasses and other Request implementations still go through
toBuilder(). No public API change.
Claude Code on behalf of Pavel Ptashyts
Co-Authored-By: Claude Opus 5.5 <[email protected]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Request.toBuilder()always uses the fixing URI encoder. So a request built with URL encoding disabled is sent re-encoded as soon as aRangeheader is added to it, on either of the two paths that add one:DefaultAsyncHttpClient.preProcessRequest, for a request with a range offset, rantoBuilder(),setHeader("Range", ...)andbuild().ResumableAsyncHandler.adjustRequestRangedid the same, and rebuilt the request on every call, also when it set no header.This is the same
toBuilder()problem that #2384 fixed for stored cookies; these two paths were listed there under "Not changed here".Changes:
preProcessRequestsets the range through a new package-privatewithRangeHeader. ADefaultRequestis copied through a new package-privateRequestBuilder(Request, boolean disableUrlEncoding, boolean validateHeaders)constructor with(request, true, false). That is whattoBuilder()does (RequestBuilderBase(prototype, false, false)) with only the encoder changed: the prototype's URI is already encoded, and the raw encoder returns it unchanged. Everything elsebuild()does (scheme check, charset, empty lists for a hand-built request) still happens.adjustRequestRangereturns the request itself when it needs noRangeheader (it has one, or no bytes were transferred yet). Otherwise aDefaultRequestis copied the same way, by a private nestedRequestBuilderBasesubclass, since the newRequestBuilderconstructor is not visible fromhandler.resumable.DefaultRequestand otherRequestimplementations still go throughtoBuilder()on both paths, as they may override what it copies.Unlike #2384 this does not share fields with the original request: the range-offset path is not a per-request path (it runs only for requests with a range offset), so keeping
build()in it is simpler than reproducing whatbuild()checks and fills in.Behaviour notes
Rangesemantics as before: the resumable handler sets it only when the request has none, and the range offset then replaces any earlier value (all values, assetHeaderdid).adjustRequestRangenow returns the same instance when it adds nothing, so such a request goes on exactly as it does without a resumable handler. Its callers (preProcessRequest,ResumableIOExceptionFilter) do not depend on identity. One exception keeps the old normalisation: aDefaultRequestbuilt through its public constructor with a null charset or a null cookie, form param or body part list is still rebuilt, sobuild()fills those in as before. Otherbuild()-only effects, such as the scheme check, do not run on this no-op path, just as they do not without the handler.RequestBuilderconstructor andwithRangeHeaderare package-private, the nested builder is private. Revapi passes.Also found, not changed here
executeRequestcallspreProcessRequestonly when the client has at least one request filter (thenoRequestFiltersfast path, since d61fb56 for #706). Without a request filter,setRangeOffsetis ignored andResumableAsyncHandleradds noRangeheader to the first request: onmaina request withsetRangeOffset(5)on a client without filters reaches the server with noRangeheader. That is a separate bug for a separate change. The wire tests here add a pass-through request filter to reach the path.Tests
DefaultAsyncHttpClientTest, over a loopbackServerSocketthat records the request head:Range: bytes=5-and its other headers, and the caller's request has noRangeheader afterwards (fails onmainwithGET /a%7Cb?q=%7Bx%7D);ResumableAsyncHandlerwith a stored offset, without and with a range offset (the offset replaces the handler'sRange);Rangevalues;DefaultRequestgoes through itstoBuilder().The raw socket is used because these tests assert the request-target bytes as sent. The Jetty server in
org.asynchttpclient.testserverexposes the decoded path (getPathInfo), which reads the same for/a|band/a%7Cb.CrossHostReplayTestand other tests use the same pattern.Calling
withRangeHeaderdirectly:toBuilder()produces, checked reflectively;toBuilder()(aDefaultRequestwith non-validatingReadOnlyHttpHeadersholding an illegal value and an illegal name keeps both);DefaultRequestwith null lists and charset gets them filled in, with its raw URI kept;DefaultRequestwith anftpURI is still rejected.ResumableAsyncHandlerTest:mainwithhttp://localhost/a%7Cb?q=%7Bx%7D);Rangeheader already set, the request is returned as is (both fail onmain);toBuilder()produces;DefaultRequestwith any one of the four fields null is rebuilt and gets it filled in, and with all four null its raw URL is kept, with and without aRangeheader added;DefaultRequestgoes through itstoBuilder(), also when it is rebuilt only for a null charset;Rangeheader is kept when a hand-built request is rebuilt;DefaultRequestwith anftpURI is still rejected when it is rebuilt../mvnw clean verify -Dgpg.skip=truepasses on JDK 17 (2083 tests; no JDK 11 and no gpg on the host); CI covers JDK 11.Second-agent review
Both reviewers ran from fresh CLI sessions, read-only, with the intent and constraints in the prompt.
AGENTS.md, ongit diff upstream/main...HEAD)gpt-6-astraclaude-opus-5-5Nine rounds in all, the commit amended before the push after each round with findings. Fixed from earlier rounds:
InetAddress.getLoopbackAddress()but sent to127.0.0.1, which fails where IPv6 is preferred; the server now binds to127.0.0.1(round 1, guided);DefaultRequest.withHeaders, aswithCookiesdoes. Rounds 1 to 5 found what that skipped frombuild(): the header copy validated headers thattoBuilder()copied without validation (the baseHttpHeaders.copy()builds a validatingDefaultHttpHeaders), null lists and a null charset of a hand-built request were no longer filled in, and the scheme check no longer ran. The range-offset path now keepsbuild(), with only the encoder changed;0x01byte in a test string literal is now written as\u0001(round 3, independent);Rangeheader kept when a hand-built request is rebuilt (round 7), and for aDefaultRequestsubclass with a null charset rebuilt through itstoBuilder()and the scheme check on the resumable rebuild path (round 8, independent).Not changed:
adjustRequestRangeskipsbuild()-only effects such as the scheme check or deriving the charset fromContent-Typefor a hand-built request with non-null fields (rounds 4 and 5, independent, risk and nit). Not rebuilding there is the point of the change, and the request then goes on exactly as without a resumable handler.Claude Code on behalf of Pavel Ptashyts
🤖 Generated with Claude Code