Skip to content

Commit 87d7210

Browse files
authored
Merge pull request #2746 from apple/revert-2744-pr_url_redirect_fixes
Revert "URLSession: Fix redirection response handling"
2 parents c452e23 + c2f1c2c commit 87d7210

File tree

3 files changed

+35
-272
lines changed

3 files changed

+35
-272
lines changed

Sources/FoundationNetworking/URLSession/HTTP/HTTPURLProtocol.swift

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -401,8 +401,9 @@ internal class _HTTPURLProtocol: _NativeProtocol {
401401
easyHandle.timeoutTimer = _TimeoutSource(queue: task.workQueue, milliseconds: timeoutInterval, handler: timeoutHandler)
402402
easyHandle.set(automaticBodyDecompression: true)
403403
easyHandle.set(requestMethod: request.httpMethod ?? "GET")
404-
// Always set the status as it may change if a HEAD is converted to a GET.
405-
easyHandle.set(noBody: request.httpMethod == "HEAD")
404+
if request.httpMethod == "HEAD" {
405+
easyHandle.set(noBody: true)
406+
}
406407
}
407408

408409
/// What action to take
@@ -590,7 +591,7 @@ internal extension _HTTPURLProtocol {
590591
// For now, we'll notify the delegate, but won't pause the transfer,
591592
// and we'll disregard the completion handler:
592593
switch response.statusCode {
593-
case 301, 302, 303, 305...308:
594+
case 301, 302, 303, 307:
594595
break
595596
default:
596597
self.client?.urlProtocol(self, didReceive: response, cacheStoragePolicy: .notAllowed)
@@ -618,26 +619,19 @@ internal extension _HTTPURLProtocol {
618619
return nil
619620
}
620621

621-
let method = fromRequest.httpMethod ?? "GET"
622622
// Check for a redirect:
623623
switch response.statusCode {
624-
case 301, 302:
625-
// Change "POST" into "GET" but leave other methods unchanged:
626-
let newMethod = (method == "POST") ? "GET" : method
627-
return (newMethod, targetURL)
628-
629-
case 303:
630-
return ("GET", targetURL)
631-
632-
case 305...308:
633-
// Re-use existing method:
634-
return (method, targetURL)
635-
636-
default:
637-
return nil
624+
//TODO: Should we do this for 300 "Multiple Choices", too?
625+
case 301, 302, 303:
626+
// Change into "GET":
627+
return ("GET", targetURL)
628+
case 307:
629+
// Re-use existing method:
630+
return (fromRequest.httpMethod ?? "GET", targetURL)
631+
default:
632+
return nil
638633
}
639634
}
640-
641635
guard let (method, targetURL) = methodAndURL() else { return nil }
642636
var request = fromRequest
643637
request.httpMethod = method

Sources/FoundationNetworking/URLSession/NativeProtocol.swift

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -102,13 +102,6 @@ internal class _NativeProtocol: URLProtocol, _EasyHandleDelegate {
102102
if let response = validateHeaderComplete(transferState:ts) {
103103
ts.response = response
104104
}
105-
106-
// Note this excludes code 300 which should return the response of the redirect and not follow it.
107-
// For other redirect codes dont notify the delegate of the data received in the redirect response.
108-
if let httpResponse = ts.response as? HTTPURLResponse, 301...308 ~= httpResponse.statusCode {
109-
return .proceed
110-
}
111-
112105
notifyDelegate(aboutReceivedData: data)
113106
internalState = .transferInProgress(ts.byAppending(bodyData: data))
114107
return .proceed

0 commit comments

Comments
 (0)