[Mono.Android] Preserve HEAD requests on redirect in AndroidMessageHandler#12102
Conversation
Keep HEAD unchanged when following 301, 302, and 303 redirects, and cover the 302 case with a loopback HTTP server regression test. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: fce7d2e8-a9c5-4e88-9cdc-4c8662672dd9
There was a problem hiding this comment.
Pull request overview
This PR fixes redirect handling in AndroidMessageHandler so that HTTP HEAD requests are not incorrectly rewritten to GET when following 301/302/303 redirects. This aligns redirect behavior with expected semantics for HEAD (no response body) and avoids accidentally downloading content after a redirect.
Changes:
- Update redirect logic to preserve
HEADacross 301/302/303 while keeping the existing “convert non-GET to GET” behavior for other methods. - Extend the local HTTP test server with a
/headendpoint that only accepts theHEADwire method. - Add a regression test that validates
HEADis preserved across a 302 redirect.
Show a summary per file
| File | Description |
|---|---|
src/Mono.Android/Xamarin.Android.Net/AndroidMessageHandler.cs |
Adjusts 301/302/303 redirect handling to keep HEAD unchanged while still converting other non-GET methods to GET. |
tests/Mono.Android-Tests/Mono.Android-Tests/Xamarin.Android.Net/LocalTestServers.cs |
Adds a /head route used by redirect tests to ensure the follow-up request remains HEAD. |
tests/Mono.Android-Tests/Mono.Android-Tests/Xamarin.Android.Net/AndroidMessageHandlerTests.cs |
Adds a regression test asserting HEAD is preserved and the final RequestUri matches the redirect target. |
Copilot's findings
- Files reviewed: 3/3 changed files
- Comments generated: 0
|
/review |
|
✅ Android PR Reviewer completed successfully! |
There was a problem hiding this comment.
✅ LGTM
Summary: Small, focused fix that stops AndroidMessageHandler from rewriting HEAD requests to GET when following 301/302/303 redirects. The change targets the root cause and now matches the BCL SocketsHttpHandler semantics (only non-GET/HEAD methods are downgraded to GET on these codes).
Strengths:
- Correct RFC/BCL-aligned behavior — GET and HEAD are both preserved; other methods still convert to GET.
- Good regression test using a loopback server whose
/headendpoint returns405unless the wire method is genuinelyHEAD, so an incorrect GET downgrade fails both the status-code and method assertions. - Minimal, well-scoped diff with no collateral changes.
Issue counts: ❌ 0 ·
No blocking issues found.
Generated by Android PR Reviewer for #12102 · 61 AIC · ⌖ 12.9 AIC · ⊞ 6.8K
Comment /review to run again
AndroidMessageHandlerconverted every non-GET request to GET when following 301, 302, or 303 redirects. This incorrectly changed HEAD requests and could cause the redirected resource body to be downloaded.Preserve HEAD across these redirects while retaining the existing conversion behavior for other methods.
Fixes #12103
The regression test uses a local HTTP server that returns a 302 redirect and rejects the follow-up request unless its wire method is HEAD. The test passes on
emulator-5600.