Skip to content

Fix for URLSession redirect does not inherit request timeout value[SR… #1063

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
Jun 27, 2017
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
11 changes: 9 additions & 2 deletions Foundation/NSURLSession/http/EasyHandle.swift
Original file line number Diff line number Diff line change
Expand Up @@ -240,9 +240,16 @@ extension _EasyHandle {
try! CFURLSession_easy_setopt_int64(rawHandle, CFURLSessionOptionINFILESIZE_LARGE, length).asError()
}

func set(timeout value: Int) {
func set(timeout value: Int) {
try! CFURLSession_easy_setopt_long(rawHandle, CFURLSessionOptionTIMEOUT, value).asError()
}
}

func getTimeoutIntervalSpent() -> Double {
var timeSpent = Double()
CFURLSession_easy_getinfo_double(rawHandle, CFURLSessionInfoTOTAL_TIME, &timeSpent)
return timeSpent / 1000
}

}

fileprivate func printLibcurlDebug(handle: CFURLSessionEasyHandle, type: CInt, data: UnsafeMutablePointer<Int8>, size: Int, userInfo: UnsafeMutableRawPointer?) -> CInt {
Expand Down
2 changes: 2 additions & 0 deletions Foundation/NSURLSession/http/HTTPURLProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -901,6 +901,8 @@ internal extension _HTTPURLProtocol {
components.path = targetURL.relativeString
guard let urlString = components.string else { fatalError("Invalid URL") }
request.url = URL(string: urlString)
let timeSpent = easyHandle.getTimeoutIntervalSpent()
request.timeoutInterval = fromRequest.timeoutInterval - timeSpent
return request
}
}
Expand Down
18 changes: 18 additions & 0 deletions TestFoundation/TestNSURLSession.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class TestURLSession : XCTestCase {
("test_timeoutInterval", test_timeoutInterval),
("test_customProtocol", test_customProtocol),
("test_httpRedirection", test_httpRedirection),
("test_httpRedirectionTimeout", test_httpRedirectionTimeout),
]
}

Expand Down Expand Up @@ -351,6 +352,23 @@ class TestURLSession : XCTestCase {
d.run(with: url)
waitForExpectations(timeout: 12)
}

func test_httpRedirectionTimeout() {
var req = URLRequest(url: URL(string: "http://127.0.0.1:\(TestURLSession.serverPort)/UnitedStates")!)
req.timeoutInterval = 3
let config = URLSessionConfiguration.default
var expect = expectation(description: "download task with handler")
let session = URLSession(configuration: config, delegate: nil, delegateQueue: nil)
let task = session.dataTask(with: req) { data, response, error in
defer { expect.fulfill() }
if let e = error as? URLError {
XCTAssertEqual(e.code, .timedOut, "Unexpected error code")
return
}
}
task.resume()
waitForExpectations(timeout: 12)
}
}

class SessionDelegate: NSObject, URLSessionDelegate {
Expand Down