Repository navigation
fix(http): resolve relative Location headers per RFC 3986 without path duplication - #2254
Conversation
…out string concatenation Remove manual string concatenation of request URL and Location header in RedirectHandler.getRedirect(). Relying on HttpUrl.resolve() conforms to RFC 7231 section 7.1.2 and RFC 3986 section 5.2, correctly resolving absolute-path and relative-path redirects without path duplication or query string corruption. Closes microsoft#2244 Signed-off-by: Hardik Kaurani <hardikkaurani1@gmail.com>
Add unit tests verifying that root-relative Location headers (e.g. /v2.0/users) resolve directly against the scheme and authority without duplicating parent path segments from the original request URL. Signed-off-by: Hardik Kaurani <hardikkaurani1@gmail.com>
…query strings Add unit tests verifying that original request query strings do not corrupt destination path segments during relative redirects, and that redirect target query parameters are preserved as required by RFC 3986. Signed-off-by: Hardik Kaurani <hardikkaurani1@gmail.com>
…t be resolved When isRedirected evaluates to true but getRedirect cannot build a followup request (e.g. malformed Location, unsupported protocol, or empty URI), set shouldRedirect to false to terminate the loop cleanly and return the received response rather than infinitely re-executing the same request. Signed-off-by: Hardik Kaurani <hardikkaurani1@gmail.com>
Guard against empty or whitespace-only Location headers in both isRedirected() and getRedirect(), preventing spurious redirect handling when servers return blank Location header values. Signed-off-by: Hardik Kaurani <hardikkaurani1@gmail.com>
…ders Add unit test coverage asserting that empty and blank Location headers are not treated as redirects in isRedirected() and return null in getRedirect(), and that malformed or unsupported URI schemes return null. Signed-off-by: Hardik Kaurani <hardikkaurani1@gmail.com>
…gin relative redirects Add unit test asserting that Authorization and Cookie headers are preserved when following same-origin relative redirects. Signed-off-by: Hardik Kaurani <hardikkaurani1@gmail.com>
…edirects Add an end-to-end integration test verifying that a 302 response with a root-relative Location header (/v2/resource) executes through OkHttpClient and RedirectHandler middleware pipeline to the expected target path. Signed-off-by: Hardik Kaurani <hardikkaurani1@gmail.com>
|
Following up on #2244, this PR addresses the relative redirect URL resolution issue in Previously, Key changes:
All local checks ( |
…schemes Signed-off-by: Hardik Kaurani <hardikkaurani1@gmail.com>
|
I've addressed the review feedback in commit
All tests ( |

Closes #2244
Problem
In
RedirectHandler.java, when handling a redirect response with a relativeLocationheader,getRedirect()performed manual URL string concatenation:Per RFC 7231 §7.1.2 and RFC 3986 §5.2, a relative URI reference starting with a single slash is an absolute-path reference that must be resolved against the scheme and authority (host and port) of the base URI. Concatenating
request.url() + locationcauses:https://example.com/v1.0/userswithLocation: /v2.0/userswas concatenated tohttps://example.com/v1.0/users/v2.0/users.https://example.com/v1.0/users?top=10withLocation: /v2.0/userswas concatenated tohttps://example.com/v1.0/users?top=10/v2.0/users, corrupting the path into the query parameter value.intercept(), ifisRedirected()returned true butgetRedirect()returnednull(e.g. malformed or blank Location header), thewhile (shouldRedirect)loop failed to terminateshouldRedirect, resulting in re-executing the identical request repeatedly.Changes
RedirectHandler.getRedirect():Allow OkHttp's built-in
request.url().resolve(location)to resolve relative references directly according to RFC 3986.intercept():When
followup == null, setshouldRedirect = falseso unresolvable redirects terminate the loop immediately and return the received response to the caller.In
isRedirected(), treat empty or whitespace-only Location headers as non-redirects (return false). IngetRedirect(), trim the location header and returnnullif blank.Tests
Added comprehensive test coverage in
RedirectHandlerTests.java:/v2.0/usersagainst/v1.0/users).detailsagainst/v1.0/users/item).isRedirectedandgetRedirect.mailto:,javascript:) returningnull.Authorization,Cookie) retention on same-origin relative redirects.MockWebServerredirect execution verifying full pipeline flow through OkHttp interceptors.Local validation:
./gradlew :components:http:okHttp:checkpassed cleanly (including tests, Spotless, and SpotBugs).