Skip to content

Commit ef31401

Browse files
[Mono.Android] AndroidMessageHandler should follow HTTP-308 redirects (#8951)
Fixes: #8946 Context: 1e5bfa3 For some reason we skipped [`HttpStatusCode.PermanentRedirect`][0] (HTTP-308) when handling redirects in 1e5bfa3. Update `AndroidMessageHandler.HandleRedirect()` to follow an HTTP redirect on HTTP-308 errors. [0]: https://learn.microsoft.com/dotnet/api/system.net.httpstatuscode?view=net-8.0#system-net-httpstatuscode-permanentredirect
1 parent 4edf89c commit ef31401

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

src/Mono.Android/Xamarin.Android.Net/AndroidMessageHandler.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -827,6 +827,7 @@ bool HandleRedirect (HttpStatusCode redirectCode, HttpURLConnection httpConnecti
827827
// what to do with the response
828828

829829
case HttpStatusCode.TemporaryRedirect: // 307
830+
case HttpStatusCode.PermanentRedirect: // 308
830831
break;
831832

832833
default:

tests/Mono.Android-Tests/Xamarin.Android.Net/AndroidMessageHandlerTests.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,20 @@ public async Task ServerCertificateCustomValidationCallback_Redirects ()
234234
Assert.IsTrue (result.IsSuccessStatusCode);
235235
}
236236

237+
[Test]
238+
public async Task AndroidMessageHandlerFollows308PermanentRedirect ()
239+
{
240+
int callbackCounter = 0;
241+
242+
var handler = new AndroidMessageHandler ();
243+
244+
var client = new HttpClient (handler);
245+
var result = await client.GetAsync ("https://httpbin.org/redirect-to?url=https://www.microsoft.com/&status_code=308");
246+
247+
Assert.IsTrue (result.IsSuccessStatusCode);
248+
Assert.AreEqual ("https://www.microsoft.com/", result.RequestMessage.RequestUri.ToString ());
249+
}
250+
237251
private async Task AssertRejectsRemoteCertificate (Func<Task> makeRequest)
238252
{
239253
// there is a difference between the exception that's thrown in the .NET build and the legacy Xamarin

0 commit comments

Comments
 (0)