Skip to content

[Mono.Android] Preserve HEAD requests on redirect in AndroidMessageHandler#12102

Open
simonrozsival wants to merge 1 commit into
dotnet:mainfrom
simonrozsival:dev/simonrozsival/androidmessagehandler-redirect-head
Open

[Mono.Android] Preserve HEAD requests on redirect in AndroidMessageHandler#12102
simonrozsival wants to merge 1 commit into
dotnet:mainfrom
simonrozsival:dev/simonrozsival/androidmessagehandler-redirect-head

Conversation

@simonrozsival

Copy link
Copy Markdown
Member

AndroidMessageHandler converted 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.

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
Copilot AI review requested due to automatic review settings July 14, 2026 15:21

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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 HEAD across 301/302/303 while keeping the existing “convert non-GET to GET” behavior for other methods.
  • Extend the local HTTP test server with a /head endpoint that only accepts the HEAD wire method.
  • Add a regression test that validates HEAD is 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

@simonrozsival simonrozsival changed the title [AndroidMessageHandler] Preserve HEAD requests on redirect [Mono.Android] Preserve HEAD requests on redirect in AndroidMessageHandler Jul 14, 2026
@simonrozsival

Copy link
Copy Markdown
Member Author

/review

@github-actions

github-actions Bot commented Jul 14, 2026

Copy link
Copy Markdown
Contributor

Android PR Reviewer completed successfully!

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

✅ 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 /head endpoint returns 405 unless the wire method is genuinely HEAD, so an incorrect GET downgrade fails both the status-code and method assertions.
  • Minimal, well-scoped diff with no collateral changes.

Issue counts: ❌ 0 · ⚠️ 0 · 💡 2 (both optional — see inline comments: consider parameterizing the test across 301/302/303).

No blocking issues found.

Generated by Android PR Reviewer for #12102 · 61 AIC · ⌖ 12.9 AIC · ⊞ 6.8K
Comment /review to run again

Comment thread src/Mono.Android/Xamarin.Android.Net/AndroidMessageHandler.cs
@simonrozsival simonrozsival added the ready-to-review This PR is ready to review/merge, I think any CI failures are just flaky (ignorable). label Jul 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ready-to-review This PR is ready to review/merge, I think any CI failures are just flaky (ignorable).

Projects

None yet

Development

Successfully merging this pull request may close these issues.

HttpClient converts HEAD requests to GET request

2 participants