Skip to content

Commit 597804f

Browse files
committed
Fix for URLSession redirect does not inherit request timeout value[SR-2682]
1 parent 3c8d51e commit 597804f

File tree

3 files changed

+29
-2
lines changed

3 files changed

+29
-2
lines changed

Foundation/NSURLSession/http/EasyHandle.swift

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -240,9 +240,16 @@ extension _EasyHandle {
240240
try! CFURLSession_easy_setopt_int64(rawHandle, CFURLSessionOptionINFILESIZE_LARGE, length).asError()
241241
}
242242

243-
func set(timeout value: Int) {
243+
func set(timeout value: Int) {
244244
try! CFURLSession_easy_setopt_long(rawHandle, CFURLSessionOptionTIMEOUT, value).asError()
245-
}
245+
}
246+
247+
func getTimeoutIntervalSpent() -> Double {
248+
var timeSpent = Double()
249+
CFURLSession_easy_getinfo_double(rawHandle, CFURLSessionInfoTOTAL_TIME, &timeSpent)
250+
return timeSpent / 1000
251+
}
252+
246253
}
247254

248255
fileprivate func printLibcurlDebug(handle: CFURLSessionEasyHandle, type: CInt, data: UnsafeMutablePointer<Int8>, size: Int, userInfo: UnsafeMutableRawPointer?) -> CInt {

Foundation/NSURLSession/http/HTTPURLProtocol.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -901,6 +901,8 @@ internal extension _HTTPURLProtocol {
901901
components.path = targetURL.relativeString
902902
guard let urlString = components.string else { fatalError("Invalid URL") }
903903
request.url = URL(string: urlString)
904+
let timeSpent = easyHandle.getTimeoutIntervalSpent()
905+
request.timeoutInterval = fromRequest.timeoutInterval - timeSpent
904906
return request
905907
}
906908
}

TestFoundation/TestNSURLSession.swift

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ class TestURLSession : XCTestCase {
3939
("test_timeoutInterval", test_timeoutInterval),
4040
("test_customProtocol", test_customProtocol),
4141
("test_httpRedirection", test_httpRedirection),
42+
("test_httpRedirectionTimeout", test_httpRedirectionTimeout),
4243
]
4344
}
4445

@@ -351,6 +352,23 @@ class TestURLSession : XCTestCase {
351352
d.run(with: url)
352353
waitForExpectations(timeout: 12)
353354
}
355+
356+
func test_httpRedirectionTimeout() {
357+
var req = URLRequest(url: URL(string: "http://127.0.0.1:\(TestURLSession.serverPort)/UnitedStates")!)
358+
req.timeoutInterval = 3
359+
let config = URLSessionConfiguration.default
360+
var expect = expectation(description: "download task with handler")
361+
let session = URLSession(configuration: config, delegate: nil, delegateQueue: nil)
362+
let task = session.dataTask(with: req) { data, response, error in
363+
defer { expect.fulfill() }
364+
if let e = error as? URLError {
365+
XCTAssertEqual(e.code, .timedOut, "Unexpected error code")
366+
return
367+
}
368+
}
369+
task.resume()
370+
waitForExpectations(timeout: 12)
371+
}
354372
}
355373

356374
class SessionDelegate: NSObject, URLSessionDelegate {

0 commit comments

Comments
 (0)