Skip to content

SR-7196: Fix for Redirects crash in HTTPURLProtocol #1486

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
Apr 18, 2018
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
8 changes: 7 additions & 1 deletion Foundation/URLSession/http/HTTPURLProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,13 @@ internal extension _HTTPURLProtocol {
var components = URLComponents()
components.scheme = scheme
components.host = host
components.path = targetURL.relativeString
//The path must either begin with "/" or be an empty string.
if targetURL.relativeString.first != "/" {
components.path = "/" + targetURL.relativeString
} else {
components.path = targetURL.relativeString
}

guard let urlString = components.string else { fatalError("Invalid URL") }
request.url = URL(string: urlString)
let timeSpent = easyHandle.getTimeoutIntervalSpent()
Expand Down
9 changes: 9 additions & 0 deletions TestFoundation/HTTPServer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,8 @@ public class TestURLSessionServer {
"Italy": "Rome",
"USA": "Washington, D.C.",
"UnitedStates": "USA",
"UnitedKingdom": "UK",
"UK": "London",
"country.txt": "A country is a region that is identified as a distinct national entity in political geography"]
let httpServer: _HTTPServer
let startDelay: TimeInterval?
Expand Down Expand Up @@ -432,6 +434,13 @@ public class TestURLSessionServer {
return _HTTPResponse(response: .OK, body: dtd)
}

if uri == "/UnitedKingdom" {
let value = capitals[String(uri.dropFirst())]!
let text = request.getCommaSeparatedHeaders()
//Response header with only path to the location to redirect.
let httpResponse = _HTTPResponse(response: .REDIRECT, headers: "Location: \(value)", body: text)
return httpResponse
}
return _HTTPResponse(response: .OK, body: capitals[String(uri.dropFirst())]!)
}

Expand Down
13 changes: 11 additions & 2 deletions TestFoundation/TestURLSession.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ class TestURLSession : LoopbackServerTest {
("test_verifyRequestHeaders", test_verifyRequestHeaders),
("test_verifyHttpAdditionalHeaders", test_verifyHttpAdditionalHeaders),
("test_timeoutInterval", test_timeoutInterval),
("test_httpRedirection", test_httpRedirection),
("test_httpRedirectionWithCompleteRelativePath", test_httpRedirectionWithCompleteRelativePath),
//("test_httpRedirectionWithInCompleteRelativePath", test_httpRedirectionWithInCompleteRelativePath), /* temporarily disabled. Needs HTTPServer rework */
//("test_httpRedirectionTimeout", test_httpRedirectionTimeout), /* temporarily disabled (https://bugs.swift.org/browse/SR-5751) */
("test_http0_9SimpleResponses", test_http0_9SimpleResponses),
("test_outOfRangeButCorrectlyFormattedHTTPCode", test_outOfRangeButCorrectlyFormattedHTTPCode),
Expand Down Expand Up @@ -325,14 +326,22 @@ class TestURLSession : LoopbackServerTest {
waitForExpectations(timeout: 30)
}

func test_httpRedirection() {
func test_httpRedirectionWithCompleteRelativePath() {
let urlString = "http://127.0.0.1:\(TestURLSession.serverPort)/UnitedStates"
let url = URL(string: urlString)!
let d = HTTPRedirectionDataTask(with: expectation(description: "GET \(urlString): with HTTP redirection"))
d.run(with: url)
waitForExpectations(timeout: 12)
}

func test_httpRedirectionWithInCompleteRelativePath() {
let urlString = "http://127.0.0.1:\(TestURLSession.serverPort)/UnitedKingdom"
let url = URL(string: urlString)!
let d = HTTPRedirectionDataTask(with: expectation(description: "GET \(urlString): with HTTP redirection"))
d.run(with: url)
waitForExpectations(timeout: 12)
}

/*
// temporarily disabled (https://bugs.swift.org/browse/SR-5751)
func test_httpRedirectionTimeout() {
Expand Down