Skip to content

Commit

Permalink
[Foundation] Always call the completion handler in a few callbacks in…
Browse files Browse the repository at this point in the history
… NSUrlSessionHandler. (#20326)

Fixes these warnings:

    [API] API MISUSE: NSURLSession delegate System_Net_Http_NSUrlSessionHandler_NSUrlSessionHandlerDelegate: <System_Net_Http_NSUrlSessionHandler_NSUrlSessionHandlerDelegate: 0x600000676c60> (0x600000676c60)
    [API] API MISUSE: dataTask:didReceiveResponse:completionHandler: completion handler not called

    [API] API MISUSE: NSURLSession delegate System_Net_Http_NSUrlSessionHandler_NSUrlSessionHandlerDelegate: <System_Net_Http_NSUrlSessionHandler_NSUrlSessionHandlerDelegate: 0x600000676c60> (0x600000676c60)
    [API] API MISUSE: dataTask:willCacheResponse:completionHandler: completion handler not called

    [API] API MISUSE: NSURLSession delegate System_Net_Http_NSUrlSessionHandler_NSUrlSessionHandlerDelegate: <System_Net_Http_NSUrlSessionHandler_NSUrlSessionHandlerDelegate: 0x6000002cffa0> (0x6000002cffa0)
    [API] API MISUSE: dataTask:willCacheResponse:completionHandler: completion handler not called

which is printed numerous times with the test case here:

#11799 (comment)
  • Loading branch information
rolfbjarne authored Mar 19, 2024
1 parent f8f6fe8 commit 4584da6
Showing 1 changed file with 41 additions and 3 deletions.
44 changes: 41 additions & 3 deletions src/Foundation/NSUrlSessionHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -852,11 +852,23 @@ void UpdateManagedCookieContainer (Uri absoluteUri, NSHttpCookie [] cookies)

[Preserve (Conditional = true)]
public override void DidReceiveResponse (NSUrlSession session, NSUrlSessionDataTask dataTask, NSUrlResponse response, Action<NSUrlSessionResponseDisposition> completionHandler)
{
try {
DidReceiveResponseImpl (session, dataTask, response, completionHandler);
} catch {
completionHandler (NSUrlSessionResponseDisposition.Cancel);
throw;
}
}

void DidReceiveResponseImpl (NSUrlSession session, NSUrlSessionDataTask dataTask, NSUrlResponse response, Action<NSUrlSessionResponseDisposition> completionHandler)
{
var inflight = GetInflightData (dataTask);

if (inflight is null)
if (inflight is null) {
completionHandler (NSUrlSessionResponseDisposition.Cancel);
return;
}

try {
var urlResponse = (NSHttpUrlResponse) response;
Expand Down Expand Up @@ -984,11 +996,24 @@ void SetResponse (InflightData inflight)

[Preserve (Conditional = true)]
public override void WillCacheResponse (NSUrlSession session, NSUrlSessionDataTask dataTask, NSCachedUrlResponse proposedResponse, Action<NSCachedUrlResponse> completionHandler)
{
try {
WillCacheResponseImpl (session, dataTask, proposedResponse, completionHandler);
} catch {
completionHandler (null!);
throw;
}
}

void WillCacheResponseImpl (NSUrlSession session, NSUrlSessionDataTask dataTask, NSCachedUrlResponse proposedResponse, Action<NSCachedUrlResponse> completionHandler)
{
var inflight = GetInflightData (dataTask);

if (inflight is null)
if (inflight is null) {
completionHandler (null!);
return;
}

// apple caches post request with a body, which should not happen. https://github.com/xamarin/maccore/issues/2571
var disableCache = sessionHandler.DisableCaching || (inflight.Request.Method == HttpMethod.Post && inflight.Request.Content is not null);
completionHandler (disableCache ? null! : proposedResponse);
Expand All @@ -1002,11 +1027,24 @@ public override void WillPerformHttpRedirection (NSUrlSession session, NSUrlSess

[Preserve (Conditional = true)]
public override void DidReceiveChallenge (NSUrlSession session, NSUrlSessionTask task, NSUrlAuthenticationChallenge challenge, Action<NSUrlSessionAuthChallengeDisposition, NSUrlCredential> completionHandler)
{
try {
DidReceiveChallengeImpl (session, task, challenge, completionHandler);
} catch {
completionHandler (NSUrlSessionAuthChallengeDisposition.CancelAuthenticationChallenge, null!);
throw;
}
}

void DidReceiveChallengeImpl (NSUrlSession session, NSUrlSessionTask task, NSUrlAuthenticationChallenge challenge, Action<NSUrlSessionAuthChallengeDisposition, NSUrlCredential> completionHandler)
{
var inflight = GetInflightData (task);

if (inflight is null)
if (inflight is null) {
// Request was probably cancelled
completionHandler (NSUrlSessionAuthChallengeDisposition.CancelAuthenticationChallenge, null!);
return;
}

// ToCToU for the callback
var trustCallbackForUrl = sessionHandler.TrustOverrideForUrl;
Expand Down

8 comments on commit 4584da6

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

@vs-mobiletools-engineering-service2
Copy link
Collaborator

Choose a reason for hiding this comment

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

📚 [CI Build] Artifacts 📚

Packages generated

View packages

Pipeline on Agent
Hash: [CI build]

Please sign in to comment.