Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 20 additions & 8 deletions src/Foundation/NSUrlSessionHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -212,22 +212,26 @@ InflightData GetInflightData (NSUrlSessionTask task)
var inflight = default (InflightData);

lock (sessionHandler.inflightRequestsLock)
if (sessionHandler.inflightRequests.TryGetValue (task, out inflight))
if (sessionHandler.inflightRequests.TryGetValue (task, out inflight)) {
// ensure that we did not cancel the request, if we did, do cancel the task
if (inflight.CancellationToken.IsCancellationRequested)
task?.Cancel ();
return inflight;
}

// if we did not manage to get the inflight data, we either got an error or have been canceled, lets cancel the task, that will execute DidCompleteWithError
task?.Cancel ();
return null;
}

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

try {
// ensure that we did not cancel the request, if we did, do cancel the task
if (inflight.CancellationToken.IsCancellationRequested) {
dataTask.Cancel ();
}
if (inflight == null)
return;

try {
var urlResponse = (NSHttpUrlResponse)response;
var status = (int)urlResponse.StatusCode;

Expand Down Expand Up @@ -279,6 +283,9 @@ public override void DidReceiveData (NSUrlSession session, NSUrlSessionDataTask
{
var inflight = GetInflightData (dataTask);

if (inflight == null)
return;

inflight.Stream.Add (data);
SetResponse (inflight);
}
Expand Down Expand Up @@ -338,6 +345,11 @@ public override void WillPerformHttpRedirection (NSUrlSession session, NSUrlSess

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

if (inflight == null)
return;

// case for the basic auth failing up front. As per apple documentation:
// The URL Loading System is designed to handle various aspects of the HTTP protocol for you. As a result, you should not modify the following headers using
// the addValue(_:forHTTPHeaderField:) or setValue(_:forHTTPHeaderField:) methods:
Expand All @@ -352,7 +364,7 @@ public override void DidReceiveChallenge (NSUrlSession session, NSUrlSessionTask
// header, it means that we do not have the correct credentials, in any other case just do what it is expected.

if (challenge.PreviousFailureCount == 0) {
var authHeader = GetInflightData (task)?.Request?.Headers?.Authorization;
var authHeader = inflight.Request?.Headers?.Authorization;
if (!(string.IsNullOrEmpty (authHeader?.Scheme) && string.IsNullOrEmpty (authHeader?.Parameter))) {
completionHandler (NSUrlSessionAuthChallengeDisposition.RejectProtectionSpace, null);
return;
Expand All @@ -363,7 +375,7 @@ public override void DidReceiveChallenge (NSUrlSession session, NSUrlSessionTask
if (sessionHandler.Credentials != null) {
var credentialsToUse = sessionHandler.Credentials as NetworkCredential;
if (credentialsToUse == null) {
var uri = GetInflightData (task).Request.RequestUri;
var uri = inflight.Request.RequestUri;
credentialsToUse = sessionHandler.Credentials.GetCredential (uri, "NTLM");
}
var credential = new NSUrlCredential (credentialsToUse.UserName, credentialsToUse.Password, NSUrlCredentialPersistence.ForSession);
Expand Down