Skip to content

Commit 73ac810

Browse files
authored
Merge pull request #423 from nejcvivod/fix/encoded_url_string
fix parsing url strings with brackets
2 parents 9994415 + 83bcc7a commit 73ac810

File tree

3 files changed

+16
-1
lines changed

3 files changed

+16
-1
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ All notable changes to this project will be documented in this file. Changes not
2525
## Changed
2626

2727
- 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)
28+
- 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)
29+
- Update `SwifterTestsHttpParser` with a test for parsing bracketed query strings. ([#423](https://github.com/httpswift/swifter/pull/423)) by [@nejcvivod](https://github.com/nejcvivod)
2830

2931
# [1.4.7]
3032

XCode/Sources/HttpParser.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ public class HttpParser {
2323
}
2424
let request = HttpRequest()
2525
request.method = statusLineTokens[0]
26-
let urlComponents = URLComponents(string: statusLineTokens[1])
26+
let encodedPath = statusLineTokens[1].addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) ?? statusLineTokens[1]
27+
let urlComponents = URLComponents(string: encodedPath)
2728
request.path = urlComponents?.path ?? ""
2829
request.queryParams = urlComponents?.queryItems?.map { ($0.name, $0.value ?? "") } ?? []
2930
request.headers = try readHeaders(socket)

XCode/Tests/SwifterTestsHttpParser.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,5 +165,17 @@ class SwifterTestsHttpParser: XCTestCase {
165165
resp = try? parser.readHttpRequest(TestSocket("GET / HTTP/1.0\nHeader1: 1\nHeader2: 2\nContent-Length: 0\n\n"))
166166
XCTAssertEqual(resp?.headers["header1"], "1", "Parser should extract multiple headers from the request.")
167167
XCTAssertEqual(resp?.headers["header2"], "2", "Parser should extract multiple headers from the request.")
168+
169+
resp = try? parser.readHttpRequest(TestSocket("GET /some/path?subscript_query[]=1&subscript_query[]=2 HTTP/1.0\nContent-Length: 10\n\n1234567890"))
170+
let queryPairs = resp?.queryParams ?? []
171+
XCTAssertEqual(queryPairs.count, 2)
172+
XCTAssertEqual(queryPairs.first?.0, "subscript_query[]")
173+
XCTAssertEqual(queryPairs.first?.1, "1")
174+
XCTAssertEqual(queryPairs.last?.0, "subscript_query[]")
175+
XCTAssertEqual(queryPairs.last?.1, "2")
176+
XCTAssertEqual(resp?.method, "GET", "Parser should extract HTTP method name from the status line.")
177+
XCTAssertEqual(resp?.path, "/some/path", "Parser should extract HTTP path value from the status line.")
178+
XCTAssertEqual(resp?.headers["content-length"], "10", "Parser should extract Content-Length header value.")
179+
168180
}
169181
}

0 commit comments

Comments
 (0)