Skip to content

Commit 3a7cb8f

Browse files
committed
Fix for URLSession redirect does not inherit request timeout value[SR-2682]
1 parent 226e078 commit 3a7cb8f

File tree

3 files changed

+44
-5
lines changed

3 files changed

+44
-5
lines changed

Foundation/NSURLSession/http/EasyHandle.swift

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -240,9 +240,18 @@ 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 getTimetaken() -> Double {
248+
let timeTaken = UnsafeMutablePointer<Double>.allocate(capacity: 8)
249+
CFURLSession_easy_getinfo_double(rawHandle, CFURLSessionInfoTOTAL_TIME, UnsafeMutablePointer<Double>(timeTaken))
250+
let timeSpent = timeTaken.pointee
251+
timeTaken.deallocate(capacity: 8)
252+
return timeSpent
253+
}
254+
246255
}
247256

248257
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.getTimetaken() / 1000
905+
request.timeoutInterval = fromRequest.timeoutInterval - timeSpent
904906
return request
905907
}
906908
}

TestFoundation/TestNSURLSession.swift

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,9 @@ class TestURLSession : XCTestCase {
3737
("test_verifyRequestHeaders", test_verifyRequestHeaders),
3838
("test_verifyHttpAdditionalHeaders", test_verifyHttpAdditionalHeaders),
3939
("test_timeoutInterval", test_timeoutInterval),
40-
("test_customProtocol", test_customProtocol),
41-
("test_httpRedirection", test_httpRedirection),
40+
("test_customProtocol", test_customProtocol),
41+
("test_httpRedirection", test_httpRedirection),
42+
("test_httpRedirectionTimeout", test_httpRedirectionTimeout),
4243
]
4344
}
4445

@@ -415,7 +416,7 @@ class TestURLSession : XCTestCase {
415416
let serverReady = ServerSemaphore()
416417
globalDispatchQueue.async {
417418
do {
418-
try self.runServer(with: serverReady, startDelay: 3, sendDelay: 5, bodyChunks: 3)
419+
try self.runServer(with: serverReady, sendDelay: 5, bodyChunks: 1)
419420
} catch {
420421
XCTAssertTrue(true)
421422
return
@@ -485,6 +486,33 @@ class TestURLSession : XCTestCase {
485486
d.run(with: url)
486487
waitForExpectations(timeout: 12)
487488
}
489+
490+
func test_httpRedirectionTimeout() {
491+
let serverReady = ServerSemaphore()
492+
globalDispatchQueue.async {
493+
do {
494+
try self.runServer(with: serverReady, sendDelay: 2, bodyChunks: 1)
495+
} catch {
496+
XCTAssertTrue(true)
497+
return
498+
}
499+
}
500+
serverReady.wait()
501+
var req = URLRequest(url: URL(string: "http://127.0.0.1:\(serverPort)/UnitedStates")!)
502+
req.timeoutInterval = 3
503+
let config = URLSessionConfiguration.default
504+
var expect = expectation(description: "download task with handler")
505+
let session = URLSession(configuration: config, delegate: nil, delegateQueue: nil)
506+
let task = session.dataTask(with: req) { data, response, error in
507+
defer { expect.fulfill() }
508+
if let e = error as? URLError {
509+
XCTAssertEqual(e.code, .timedOut, "Unexpected error code")
510+
return
511+
}
512+
}
513+
task.resume()
514+
waitForExpectations(timeout: 12)
515+
}
488516
}
489517

490518
class SessionDelegate: NSObject, URLSessionDelegate {

0 commit comments

Comments
 (0)