Skip to content
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

Enable clients to call URLs that include %2F as an escaped backslash #201

Merged
merged 2 commits into from
Apr 15, 2020
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
24 changes: 1 addition & 23 deletions Sources/AsyncHTTPClient/HTTPHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -464,33 +464,11 @@ extension URL {
if self.path.isEmpty {
return "/"
}
return self.path.addingPercentEncoding(withAllowedCharacters: .urlPathAllowed) ?? self.path
}

var pathHasTrailingSlash: Bool {
if #available(OSX 10.11, iOS 9.0, tvOS 9.0, watchOS 2.0, *) {
return self.hasDirectoryPath
} else {
// Most platforms should use `self.hasDirectoryPath`, but on older darwin platforms
// we have this approximation instead.
let url = self.absoluteString

var pathEndIndex = url.index(before: url.endIndex)
if let queryIndex = url.firstIndex(of: "?") {
pathEndIndex = url.index(before: queryIndex)
} else if let fragmentIndex = url.suffix(from: url.firstIndex(of: "@") ?? url.startIndex).lastIndex(of: "#") {
pathEndIndex = url.index(before: fragmentIndex)
}

return url[pathEndIndex] == "/"
}
return URLComponents(url: self, resolvingAgainstBaseURL: false)?.percentEncodedPath ?? self.path
}

var uri: String {
var uri = self.percentEncodedPath
if self.pathHasTrailingSlash, uri != "/" {
Copy link
Contributor Author

@iainsmith iainsmith Apr 14, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So it seems this case is handled correctly through the URLComponenets.percentEncodedPath approach. This behaviour looks well tested in testRequestURITrailingSlash which should now be passing (at least on iOS and macOS).

uri += "/"
}

if let query = self.query {
uri += "?" + query
Expand Down
3 changes: 3 additions & 0 deletions Tests/AsyncHTTPClientTests/HTTPClientInternalTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,9 @@ class HTTPClientInternalTests: XCTestCase {

let request11 = try Request(url: "https://someserver.com/some%20path")
XCTAssertEqual(request11.url.uri, "/some%20path")

let request12 = try Request(url: "https://someserver.com/some%2Fpathsegment1/pathsegment2")
XCTAssertEqual(request12.url.uri, "/some%2Fpathsegment1/pathsegment2")
}

func testChannelAndDelegateOnDifferentEventLoops() throws {
Expand Down
18 changes: 12 additions & 6 deletions Tests/AsyncHTTPClientTests/HTTPClientTestUtils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -394,8 +394,8 @@ internal final class HttpBinHandler: ChannelInboundHandler {
switch self.unwrapInboundIn(data) {
case .head(let req):
self.parseAndSetOptions(from: req)
let url = URL(string: req.uri)!
switch url.path {
let urlComponents = URLComponents(string: req.uri)!
switch urlComponents.percentEncodedPath {
case "/":
var headers = HTTPHeaders()
headers.add(name: "X-Is-This-Slash", value: "Yes")
Expand Down Expand Up @@ -429,13 +429,13 @@ internal final class HttpBinHandler: ChannelInboundHandler {
self.resps.append(HTTPResponseBuilder(status: .found, headers: headers))
return
case "/redirect/https":
let port = self.value(for: "port", from: url.query!)
let port = self.value(for: "port", from: urlComponents.query!)
var headers = HTTPHeaders()
headers.add(name: "Location", value: "https://localhost:\(port)/ok")
self.resps.append(HTTPResponseBuilder(status: .found, headers: headers))
return
case "/redirect/loopback":
let port = self.value(for: "port", from: url.query!)
let port = self.value(for: "port", from: urlComponents.query!)
var headers = HTTPHeaders()
headers.add(name: "Location", value: "http://127.0.0.1:\(port)/echohostheader")
self.resps.append(HTTPResponseBuilder(status: .found, headers: headers))
Expand All @@ -450,8 +450,14 @@ internal final class HttpBinHandler: ChannelInboundHandler {
headers.add(name: "Location", value: "/redirect/infinite1")
self.resps.append(HTTPResponseBuilder(status: .found, headers: headers))
return
// Since this String is taken from URL.path, the percent encoding has been removed
case "/percent encoded":
case "/percent%20encoded":
if req.method != .GET {
self.resps.append(HTTPResponseBuilder(status: .methodNotAllowed))
return
}
self.resps.append(HTTPResponseBuilder(status: .ok))
return
case "/percent%2Fencoded/hello":
if req.method != .GET {
self.resps.append(HTTPResponseBuilder(status: .methodNotAllowed))
return
Expand Down
1 change: 1 addition & 0 deletions Tests/AsyncHTTPClientTests/HTTPClientTests+XCTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ extension HTTPClientTests {
("testHttpRedirect", testHttpRedirect),
("testHttpHostRedirect", testHttpHostRedirect),
("testPercentEncoded", testPercentEncoded),
("testPercentEncodedBackslash", testPercentEncodedBackslash),
("testMultipleContentLengthHeaders", testMultipleContentLengthHeaders),
("testStreaming", testStreaming),
("testRemoteClose", testRemoteClose),
Expand Down
12 changes: 12 additions & 0 deletions Tests/AsyncHTTPClientTests/HTTPClientTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,18 @@ class HTTPClientTests: XCTestCase {
XCTAssertEqual(.ok, response.status)
}

func testPercentEncodedBackslash() throws {
let httpBin = HTTPBin()
let httpClient = HTTPClient(eventLoopGroupProvider: .createNew)
defer {
XCTAssertNoThrow(try httpClient.syncShutdown(requiresCleanClose: true))
XCTAssertNoThrow(try httpBin.shutdown())
}

let response = try httpClient.get(url: "http://localhost:\(httpBin.port)/percent%2Fencoded/hello").wait()
XCTAssertEqual(.ok, response.status)
}

func testMultipleContentLengthHeaders() throws {
let httpClient = HTTPClient(eventLoopGroupProvider: .createNew)
defer {
Expand Down