From 28b56f05f710b37a04deef8df7bc6fe2954657b9 Mon Sep 17 00:00:00 2001 From: Nejc Vivod Date: Thu, 11 Jul 2019 10:20:30 +0200 Subject: [PATCH 1/4] fix for URLComponents returning nil * when path contains 'weird' characters (`[]`) URLComponents won't parse it, so the path will be nil * escaping the string with `urlQueryAllowed` character set fixes this issue --- XCode/Sources/HttpParser.swift | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/XCode/Sources/HttpParser.swift b/XCode/Sources/HttpParser.swift index 3558380b..34a1e86e 100644 --- a/XCode/Sources/HttpParser.swift +++ b/XCode/Sources/HttpParser.swift @@ -23,7 +23,8 @@ public class HttpParser { } let request = HttpRequest() request.method = statusLineTokens[0] - let urlComponents = URLComponents(string: statusLineTokens[1]) + let encodedPath = statusLineTokens[1].addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) ?? statusLineTokens[1] + let urlComponents = URLComponents(string: encodedPath) request.path = urlComponents?.path ?? "" request.queryParams = urlComponents?.queryItems?.map { ($0.name, $0.value ?? "") } ?? [] request.headers = try readHeaders(socket) From e89aec101fe302106f2a654ae125f51e5e5e03ca Mon Sep 17 00:00:00 2001 From: Nejc Vivod Date: Thu, 11 Jul 2019 12:58:28 +0200 Subject: [PATCH 2/4] add unit test for change to parser --- XCode/Tests/SwifterTestsHttpParser.swift | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/XCode/Tests/SwifterTestsHttpParser.swift b/XCode/Tests/SwifterTestsHttpParser.swift index 7e9c656f..8c9b1cb4 100644 --- a/XCode/Tests/SwifterTestsHttpParser.swift +++ b/XCode/Tests/SwifterTestsHttpParser.swift @@ -165,5 +165,17 @@ class SwifterTestsHttpParser: XCTestCase { resp = try? parser.readHttpRequest(TestSocket("GET / HTTP/1.0\nHeader1: 1\nHeader2: 2\nContent-Length: 0\n\n")) XCTAssertEqual(resp?.headers["header1"], "1", "Parser should extract multiple headers from the request.") XCTAssertEqual(resp?.headers["header2"], "2", "Parser should extract multiple headers from the request.") + + resp = try? parser.readHttpRequest(TestSocket("GET https://www.example.com/some/path?subscript_query[]=1&subscript_query[]=2 HTTP/1.0\nContent-Length: 10\n\n1234567890")) + let queryPairs = resp?.queryParams ?? [] + XCTAssertEqual(queryPairs.count, 2) + XCTAssertEqual(queryPairs.first?.0, "subscript_query[]") + XCTAssertEqual(queryPairs.first?.1, "1") + XCTAssertEqual(queryPairs.last?.0, "subscript_query[]") + XCTAssertEqual(queryPairs.last?.1, "2") + XCTAssertEqual(resp?.method, "GET", "Parser should extract HTTP method name from the status line.") + XCTAssertEqual(resp?.path, "/some/path", "Parser should extract HTTP path value from the status line.") + XCTAssertEqual(resp?.headers["content-length"], "10", "Parser should extract Content-Length header value.") + } } From 3c3663bcb6d58b189d7b1c0f24e7ab2c15ebc56e Mon Sep 17 00:00:00 2001 From: Nejc Vivod Date: Mon, 15 Jul 2019 11:32:41 +0200 Subject: [PATCH 3/4] update changelog & fix invalid test * added `update` entries to CHANGELOG.md describing fixed parsing & new test * fix invalid test, don't need the full domain in parser * ran `swift test --generate-linuxmain` --- CHANGELOG.md | 2 ++ XCode/Tests/SwifterTestsHttpParser.swift | 2 +- XCode/Tests/XCTestManifests.swift | 1 + 3 files changed, 4 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ed51e077..4d2214b4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -25,6 +25,8 @@ All notable changes to this project will be documented in this file. Changes not ## Changed - Set the version of the HTTP Server based in the project version in the **Info.plist** for macOS, iOS and tvOS platforms. ([#416](https://github.com/httpswift/swifter/pull/416)) by [@Vkt0r](https://github.com/Vkt0r) +- Update `HttpParser` so it percent-encodes the URL components before initializing `URLComponents` +- Update `SwifterTestsHttpParser` with a test for parsing bracketed query strings # [1.4.7] diff --git a/XCode/Tests/SwifterTestsHttpParser.swift b/XCode/Tests/SwifterTestsHttpParser.swift index 8c9b1cb4..4fef4135 100644 --- a/XCode/Tests/SwifterTestsHttpParser.swift +++ b/XCode/Tests/SwifterTestsHttpParser.swift @@ -166,7 +166,7 @@ class SwifterTestsHttpParser: XCTestCase { XCTAssertEqual(resp?.headers["header1"], "1", "Parser should extract multiple headers from the request.") XCTAssertEqual(resp?.headers["header2"], "2", "Parser should extract multiple headers from the request.") - resp = try? parser.readHttpRequest(TestSocket("GET https://www.example.com/some/path?subscript_query[]=1&subscript_query[]=2 HTTP/1.0\nContent-Length: 10\n\n1234567890")) + resp = try? parser.readHttpRequest(TestSocket("GET /some/path?subscript_query[]=1&subscript_query[]=2 HTTP/1.0\nContent-Length: 10\n\n1234567890")) let queryPairs = resp?.queryParams ?? [] XCTAssertEqual(queryPairs.count, 2) XCTAssertEqual(queryPairs.first?.0, "subscript_query[]") diff --git a/XCode/Tests/XCTestManifests.swift b/XCode/Tests/XCTestManifests.swift index 4147549b..7e13780e 100644 --- a/XCode/Tests/XCTestManifests.swift +++ b/XCode/Tests/XCTestManifests.swift @@ -97,6 +97,7 @@ extension SwifterTestsWebSocketSession { public func __allTests() -> [XCTestCaseEntry] { return [ + testCase(IOSafetyTests.__allTests__IOSafetyTests), testCase(MimeTypeTests.__allTests__MimeTypeTests), testCase(ServerThreadingTests.__allTests__ServerThreadingTests), testCase(SwifterTestsHttpParser.__allTests__SwifterTestsHttpParser), From 83bcc7a5e5ec705dc8c613b5f46680e93ddfff28 Mon Sep 17 00:00:00 2001 From: Nejc Vivod Date: Mon, 15 Jul 2019 16:58:34 +0200 Subject: [PATCH 4/4] update changelog + remove IOSafetyTests --- CHANGELOG.md | 4 ++-- XCode/Tests/XCTestManifests.swift | 1 - 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4d2214b4..24851939 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -25,8 +25,8 @@ All notable changes to this project will be documented in this file. Changes not ## Changed - Set the version of the HTTP Server based in the project version in the **Info.plist** for macOS, iOS and tvOS platforms. ([#416](https://github.com/httpswift/swifter/pull/416)) by [@Vkt0r](https://github.com/Vkt0r) -- Update `HttpParser` so it percent-encodes the URL components before initializing `URLComponents` -- Update `SwifterTestsHttpParser` with a test for parsing bracketed query strings +- Update `HttpParser` so it percent-encodes the URL components before initializing `URLComponents`. ([#423](https://github.com/httpswift/swifter/pull/423)) by [@nejcvivod](https://github.com/nejcvivod) +- Update `SwifterTestsHttpParser` with a test for parsing bracketed query strings. ([#423](https://github.com/httpswift/swifter/pull/423)) by [@nejcvivod](https://github.com/nejcvivod) # [1.4.7] diff --git a/XCode/Tests/XCTestManifests.swift b/XCode/Tests/XCTestManifests.swift index 7e13780e..4147549b 100644 --- a/XCode/Tests/XCTestManifests.swift +++ b/XCode/Tests/XCTestManifests.swift @@ -97,7 +97,6 @@ extension SwifterTestsWebSocketSession { public func __allTests() -> [XCTestCaseEntry] { return [ - testCase(IOSafetyTests.__allTests__IOSafetyTests), testCase(MimeTypeTests.__allTests__MimeTypeTests), testCase(ServerThreadingTests.__allTests__ServerThreadingTests), testCase(SwifterTestsHttpParser.__allTests__SwifterTestsHttpParser),