Skip to content

Revert "URLSession: Fix redirection response handling" #2746

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 28, 2020
Merged
Show file tree
Hide file tree
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
32 changes: 13 additions & 19 deletions Sources/FoundationNetworking/URLSession/HTTP/HTTPURLProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -401,8 +401,9 @@ internal class _HTTPURLProtocol: _NativeProtocol {
easyHandle.timeoutTimer = _TimeoutSource(queue: task.workQueue, milliseconds: timeoutInterval, handler: timeoutHandler)
easyHandle.set(automaticBodyDecompression: true)
easyHandle.set(requestMethod: request.httpMethod ?? "GET")
// Always set the status as it may change if a HEAD is converted to a GET.
easyHandle.set(noBody: request.httpMethod == "HEAD")
if request.httpMethod == "HEAD" {
easyHandle.set(noBody: true)
}
}

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

let method = fromRequest.httpMethod ?? "GET"
// Check for a redirect:
switch response.statusCode {
case 301, 302:
// Change "POST" into "GET" but leave other methods unchanged:
let newMethod = (method == "POST") ? "GET" : method
return (newMethod, targetURL)

case 303:
return ("GET", targetURL)

case 305...308:
// Re-use existing method:
return (method, targetURL)

default:
return nil
//TODO: Should we do this for 300 "Multiple Choices", too?
case 301, 302, 303:
// Change into "GET":
return ("GET", targetURL)
case 307:
// Re-use existing method:
return (fromRequest.httpMethod ?? "GET", targetURL)
default:
return nil
}
}

guard let (method, targetURL) = methodAndURL() else { return nil }
var request = fromRequest
request.httpMethod = method
Expand Down
7 changes: 0 additions & 7 deletions Sources/FoundationNetworking/URLSession/NativeProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -102,13 +102,6 @@ internal class _NativeProtocol: URLProtocol, _EasyHandleDelegate {
if let response = validateHeaderComplete(transferState:ts) {
ts.response = response
}

// Note this excludes code 300 which should return the response of the redirect and not follow it.
// For other redirect codes dont notify the delegate of the data received in the redirect response.
if let httpResponse = ts.response as? HTTPURLResponse, 301...308 ~= httpResponse.statusCode {
return .proceed
}

notifyDelegate(aboutReceivedData: data)
internalState = .transferInProgress(ts.byAppending(bodyData: data))
return .proceed
Expand Down
Loading