Skip to content

Commit 21726f0

Browse files
authored
Merge pull request #1650 from spevans/pr_http_redirect_port_fix
2 parents 707896f + 323f85a commit 21726f0

File tree

3 files changed

+27
-1
lines changed

3 files changed

+27
-1
lines changed

Foundation/URLSession/http/HTTPURLProtocol.swift

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,12 @@ internal extension _HTTPURLProtocol {
414414
var components = URLComponents()
415415
components.scheme = scheme
416416
components.host = host
417-
components.port = port
417+
// Use the original port if the new URL does not contain a host
418+
// ie Location: /foo => <original host>:<original port>/Foo
419+
// but Location: newhost/foo will ignore the original port
420+
if targetURL.host == nil {
421+
components.port = port
422+
}
418423
//The path must either begin with "/" or be an empty string.
419424
if targetURL.relativeString.first != "/" {
420425
components.path = "/" + targetURL.relativeString

TestFoundation/HTTPServer.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -522,6 +522,13 @@ public class TestURLSessionServer {
522522
let httpResponse = _HTTPResponse(response: .REDIRECT, headers: "Location: \(value)", body: text)
523523
return httpResponse
524524
}
525+
if uri == "/redirect-with-default-port" {
526+
let text = request.getCommaSeparatedHeaders()
527+
let host = request.headers[1].components(separatedBy: " ")[1]
528+
let ip = host.components(separatedBy: ":")[0]
529+
let httpResponse = _HTTPResponse(response: .REDIRECT, headers: "Location: http://\(ip)/redirected-with-default-port", body: text)
530+
return httpResponse
531+
}
525532
return _HTTPResponse(response: .OK, body: capitals[String(uri.dropFirst())]!)
526533
}
527534

TestFoundation/TestURLSession.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ class TestURLSession : LoopbackServerTest {
2929
("test_timeoutInterval", test_timeoutInterval),
3030
("test_httpRedirectionWithCompleteRelativePath", test_httpRedirectionWithCompleteRelativePath),
3131
("test_httpRedirectionWithInCompleteRelativePath", test_httpRedirectionWithInCompleteRelativePath),
32+
("test_httpRedirectionWithDefaultPort", test_httpRedirectionWithDefaultPort),
3233
("test_httpRedirectionTimeout", test_httpRedirectionTimeout),
3334
("test_http0_9SimpleResponses", test_http0_9SimpleResponses),
3435
("test_outOfRangeButCorrectlyFormattedHTTPCode", test_outOfRangeButCorrectlyFormattedHTTPCode),
@@ -345,6 +346,14 @@ class TestURLSession : LoopbackServerTest {
345346
waitForExpectations(timeout: 12)
346347
}
347348

349+
func test_httpRedirectionWithDefaultPort() {
350+
let urlString = "http://127.0.0.1:\(TestURLSession.serverPort)/redirect-with-default-port"
351+
let url = URL(string: urlString)!
352+
let d = HTTPRedirectionDataTask(with: expectation(description: "GET \(urlString): with HTTP redirection"))
353+
d.run(with: url)
354+
waitForExpectations(timeout: 12)
355+
}
356+
348357
// temporarily disabled (https://bugs.swift.org/browse/SR-5751)
349358
func test_httpRedirectionTimeout() {
350359
let urlString = "http://127.0.0.1:\(TestURLSession.serverPort)/UnitedStates"
@@ -915,6 +924,11 @@ extension HTTPRedirectionDataTask : URLSessionTaskDelegate {
915924
public func urlSession(_ session: URLSession, task: URLSessionTask, willPerformHTTPRedirection response: HTTPURLResponse, newRequest request: URLRequest, completionHandler: @escaping (URLRequest?) -> Void) {
916925
XCTAssertNotNil(response)
917926
XCTAssertEqual(302, response.statusCode, "HTTP response code is not 302")
927+
if let url = response.url, url.path.hasSuffix("/redirect-with-default-port") {
928+
XCTAssertEqual(request.url?.absoluteString, "http://127.0.0.1/redirected-with-default-port")
929+
// Dont follow the redirect as the test server is not running on port 80
930+
return
931+
}
918932
completionHandler(request)
919933
}
920934
}

0 commit comments

Comments
 (0)