Skip to content

[Foundation] Add KeepHeadersAfterDecompression app context switch to NSUrlSessionHandler#24957

Merged
rolfbjarne merged 1 commit intomainfrom
dev/rolf/handler-header-fallback
Mar 23, 2026
Merged

[Foundation] Add KeepHeadersAfterDecompression app context switch to NSUrlSessionHandler#24957
rolfbjarne merged 1 commit intomainfrom
dev/rolf/handler-header-fallback

Conversation

@rolfbjarne
Copy link
Copy Markdown
Member

Add an opt-out switch 'Foundation.NSUrlSessionHandler.KeepHeadersAfterDecompression'
that, when enabled, preserves the original Content-Encoding and Content-Length headers
on auto-decompressed responses instead of removing them.

This allows apps that depend on the original headers to restore the previous behavior.

…NSUrlSessionHandler

Add an opt-out switch 'Foundation.NSUrlSessionHandler.KeepHeadersAfterDecompression'
that, when enabled, preserves the original Content-Encoding and Content-Length headers
on auto-decompressed responses instead of removing them.

This allows apps that depend on the original headers to restore the previous behavior.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Adds an app-context switch to allow opting out of NSUrlSessionHandler’s header-stripping behavior after automatic response decompression, and introduces a regression test to validate the opt-out.

Changes:

  • Added Foundation.NSUrlSessionHandler.KeepHeadersAfterDecompression AppContext switch handling in NSUrlSessionHandler to preserve Content-Encoding/Content-Length after auto-decompression.
  • Added a new monotouch test to validate preserved headers when the switch is enabled.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
src/Foundation/NSUrlSessionHandler.cs Reads an AppContext switch to conditionally keep Content-Encoding/Content-Length headers on auto-decompressed responses.
tests/monotouch-test/System.Net.Http/NSUrlSessionHandlerTest.cs Adds a test that enables the switch and asserts the headers are preserved for a gzip response.

Comment on lines +104 to +134
AppContext.SetSwitch ("Foundation.NSUrlSessionHandler.KeepHeadersAfterDecompression", true);
try {
var done = TestRuntime.TryRunAsync (TimeSpan.FromSeconds (30), async () => {
using var handler = new NSUrlSessionHandler ();
using var client = new HttpClient (handler);
using var request = new HttpRequestMessage (HttpMethod.Get, $"{NetworkResources.Httpbin.Url}/gzip");
request.Headers.TryAddWithoutValidation ("Accept-Encoding", "gzip");
var response = await client.SendAsync (request, HttpCompletionOption.ResponseHeadersRead);

if (!response.IsSuccessStatusCode) {
Assert.Inconclusive ($"Request failed with status {response.StatusCode}");
return;
}

hasContentEncoding = response.Content.Headers.ContentEncoding.Count > 0;
hasContentLength = response.Content.Headers.ContentLength is not null;
body = await response.Content.ReadAsStringAsync ();
}, out var ex);

if (!done) {
TestRuntime.IgnoreInCI ("Transient network failure - ignore in CI");
Assert.Inconclusive ("Request timed out.");
}
TestRuntime.IgnoreInCIIfBadNetwork (ex);
Assert.IsNull (ex, $"Exception: {ex}");
Assert.IsTrue (hasContentEncoding, "Content-Encoding header should be preserved when KeepHeadersAfterDecompression is enabled");
Assert.IsTrue (hasContentLength, "Content-Length header should be preserved when KeepHeadersAfterDecompression is enabled");
Assert.IsTrue (body.Contains ("\"gzipped\"", StringComparison.OrdinalIgnoreCase), "Response body should contain decompressed gzip data");
} finally {
AppContext.SetSwitch ("Foundation.NSUrlSessionHandler.KeepHeadersAfterDecompression", false);
}
Copy link

Copilot AI Mar 20, 2026

Choose a reason for hiding this comment

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

This test mutates a process-wide AppContext switch and then unconditionally sets it to false in the finally block. If the switch was already set (e.g., by the test harness or another test), this will clobber the previous value. Capture the previous switch state/value (via AppContext.TryGetSwitch) before setting it, and restore that value in finally (and consider clearing the switch if it was not previously set).

Copilot uses AI. Check for mistakes.
@vs-mobiletools-engineering-service2
Copy link
Copy Markdown
Collaborator

✅ [CI Build #c8fe287] Build passed (Build packages) ✅

Pipeline on Agent
Hash: c8fe287207540ce8b55754e7cc95ab41bdda345c [PR build]

@vs-mobiletools-engineering-service2
Copy link
Copy Markdown
Collaborator

✅ [PR Build #c8fe287] Build passed (Detect API changes) ✅

Pipeline on Agent
Hash: c8fe287207540ce8b55754e7cc95ab41bdda345c [PR build]

@vs-mobiletools-engineering-service2
Copy link
Copy Markdown
Collaborator

✅ API diff for current PR / commit

NET (empty diffs)

✅ API diff vs stable

NET (empty diffs)

ℹ️ Generator diff

Generator Diff: vsdrops (html) vsdrops (raw diff) gist (raw diff) - Please review changes)

Pipeline on Agent
Hash: c8fe287207540ce8b55754e7cc95ab41bdda345c [PR build]

@vs-mobiletools-engineering-service2
Copy link
Copy Markdown
Collaborator

✅ [CI Build #c8fe287] Build passed (Build macOS tests) ✅

Pipeline on Agent
Hash: c8fe287207540ce8b55754e7cc95ab41bdda345c [PR build]

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2
Copy link
Copy Markdown
Collaborator

🚀 [CI Build #c8fe287] Test results 🚀

Test results

✅ All tests passed on VSTS: test results.

🎉 All 156 tests passed 🎉

Tests counts

✅ cecil: All 1 tests passed. Html Report (VSDrops) Download
✅ dotnettests (iOS): All 1 tests passed. Html Report (VSDrops) Download
✅ dotnettests (MacCatalyst): All 1 tests passed. Html Report (VSDrops) Download
✅ dotnettests (macOS): All 1 tests passed. Html Report (VSDrops) Download
✅ dotnettests (Multiple platforms): All 1 tests passed. Html Report (VSDrops) Download
✅ dotnettests (tvOS): All 1 tests passed. Html Report (VSDrops) Download
✅ framework: All 2 tests passed. Html Report (VSDrops) Download
✅ fsharp: All 4 tests passed. Html Report (VSDrops) Download
✅ generator: All 5 tests passed. Html Report (VSDrops) Download
✅ interdependent-binding-projects: All 4 tests passed. Html Report (VSDrops) Download
✅ introspection: All 6 tests passed. Html Report (VSDrops) Download
✅ linker: All 44 tests passed. Html Report (VSDrops) Download
✅ monotouch (iOS): All 11 tests passed. Html Report (VSDrops) Download
✅ monotouch (MacCatalyst): All 15 tests passed. Html Report (VSDrops) Download
✅ monotouch (macOS): All 12 tests passed. Html Report (VSDrops) Download
✅ monotouch (tvOS): All 11 tests passed. Html Report (VSDrops) Download
✅ msbuild: All 2 tests passed. Html Report (VSDrops) Download
✅ sharpie: All 1 tests passed. Html Report (VSDrops) Download
✅ windows: All 3 tests passed. [attempt 2] Html Report (VSDrops) Download
✅ xcframework: All 4 tests passed. Html Report (VSDrops) Download
✅ xtro: All 1 tests passed. Html Report (VSDrops) Download

macOS tests

✅ Tests on macOS Monterey (12): All 5 tests passed. Html Report (VSDrops) Download
✅ Tests on macOS Ventura (13): All 5 tests passed. Html Report (VSDrops) Download
✅ Tests on macOS Sonoma (14): All 5 tests passed. Html Report (VSDrops) Download
✅ Tests on macOS Sequoia (15): All 5 tests passed. Html Report (VSDrops) Download
✅ Tests on macOS Tahoe (26): All 5 tests passed. Html Report (VSDrops) Download

Pipeline on Agent
Hash: c8fe287207540ce8b55754e7cc95ab41bdda345c [PR build]

@rolfbjarne rolfbjarne merged commit 1fc82b6 into main Mar 23, 2026
52 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants