Skip to content

Commit 3367b63

Browse files
authored
Merge pull request #404 from Mazyod/bugfix/parse-path-without-query
Parse HttpRequest Path without Query String
2 parents d3c2d21 + fb4ba22 commit 3367b63

File tree

4 files changed

+11
-54
lines changed

4 files changed

+11
-54
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,16 @@ All notable changes to this project will be documented in this file. Changes not
2727
- An issue in the `HttpRouter` causing issues to handle routes with overlapping in the tail. ([#379](https://github.com/httpswift/swifter/pull/359), [#382](https://github.com/httpswift/swifter/pull/382)) by [@Vkt0r](https://github.com/Vkt0r)
2828

2929
- Fixes build errors by excluding XC(UI)Test files from regular targets [#397](https://github.com/httpswift/swifter/pull/397)) by [@ChristianSteffens](https://github.com/ChristianSteffens)
30+
- Fixes `HttpRequest.path` value to be parsed without query parameters [#404](https://github.com/httpswift/swifter/pull/404)) by [@mazyod](https://github.com/mazyod)
3031

3132
## Changed
3233
- Performance: Batch reads of websocket payloads rather than reading byte-by-byte. ([#387](https://github.com/httpswift/swifter/pull/387)) by [@lynaghk](https://github.com/lynaghk)
3334
- Podspec source_files updated to match source file directory changes. ([#400](https://github.com/httpswift/swifter/pull/400)) by [@welsonpan](https://github.com/welsonpan)
3435
- Refactor: Use Foundation API for Base64 encoding. ([#403](https://github.com/httpswift/swifter/pull/403)) by [@mazyod](https://github.com/mazyod)
36+
- Refactor: Use `URLComponents` for `HttpRequest` path and query parameters parsing [#404](https://github.com/httpswift/swifter/pull/404)) by [@mazyod](https://github.com/mazyod)
37+
38+
## Removed
39+
- Dropped macOS 10.9 support [#404](https://github.com/httpswift/swifter/pull/404)) by [@mazyod](https://github.com/mazyod)
3540

3641
# [1.4.6]
3742
## Added

XCode/Sources/HttpParser.swift

Lines changed: 3 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -23,59 +23,15 @@ public class HttpParser {
2323
}
2424
let request = HttpRequest()
2525
request.method = statusLineTokens[0]
26-
request.path = statusLineTokens[1]
27-
request.queryParams = extractQueryParams(request.path)
26+
let urlComponents = URLComponents(string: statusLineTokens[1])
27+
request.path = urlComponents?.path ?? ""
28+
request.queryParams = urlComponents?.queryItems?.map { ($0.name, $0.value ?? "") } ?? []
2829
request.headers = try readHeaders(socket)
2930
if let contentLength = request.headers["content-length"], let contentLengthValue = Int(contentLength) {
3031
request.body = try readBody(socket, size: contentLengthValue)
3132
}
3233
return request
33-
}
34-
35-
private func extractQueryParams(_ url: String) -> [(String, String)] {
36-
#if compiler(>=5.0)
37-
guard let questionMarkIndex = url.firstIndex(of: "?") else {
38-
return []
39-
}
40-
#else
41-
guard let questionMarkIndex = url.index(of: "?") else {
42-
return []
43-
}
44-
#endif
45-
let queryStart = url.index(after: questionMarkIndex)
46-
47-
guard url.endIndex > queryStart else { return [] }
48-
49-
#if swift(>=4.0)
50-
let query = String(url[queryStart..<url.endIndex])
51-
#else
52-
guard let query = String(url[queryStart..<url.endIndex]) else { return [] }
53-
#endif
54-
55-
return query.components(separatedBy: "&")
56-
.reduce([(String, String)]()) { (result, stringValue) -> [(String, String)] in
57-
#if compiler(>=5.0)
58-
guard let nameEndIndex = stringValue.firstIndex(of: "=") else {
59-
return result
60-
}
61-
#else
62-
guard let nameEndIndex = stringValue.index(of: "=") else {
63-
return result
64-
}
65-
#endif
66-
guard let name = String(stringValue[stringValue.startIndex..<nameEndIndex]).removingPercentEncoding else {
67-
return result
68-
}
69-
let valueStartIndex = stringValue.index(nameEndIndex, offsetBy: 1)
70-
guard valueStartIndex < stringValue.endIndex else {
71-
return result + [(name, "")]
72-
}
73-
guard let value = String(stringValue[valueStartIndex..<stringValue.endIndex]).removingPercentEncoding else {
74-
return result + [(name, "")]
75-
}
76-
return result + [(name, value)]
7734
}
78-
}
7935

8036
private func readBody(_ socket: Socket, size: Int) throws -> [UInt8] {
8137
return try socket.read(length: size)

XCode/Swifter.xcodeproj/project.pbxproj

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1187,7 +1187,6 @@
11871187
INFOPLIST_FILE = SwifterMac/Info.plist;
11881188
INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks";
11891189
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks @loader_path/Frameworks";
1190-
MACOSX_DEPLOYMENT_TARGET = 10.9;
11911190
MTL_ENABLE_DEBUG_INFO = YES;
11921191
PRODUCT_BUNDLE_IDENTIFIER = pl.kolakowski.SwifterMac;
11931192
PRODUCT_NAME = Swifter;
@@ -1216,7 +1215,6 @@
12161215
INFOPLIST_FILE = SwifterMac/Info.plist;
12171216
INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks";
12181217
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks @loader_path/Frameworks";
1219-
MACOSX_DEPLOYMENT_TARGET = 10.9;
12201218
MTL_ENABLE_DEBUG_INFO = NO;
12211219
PRODUCT_BUNDLE_IDENTIFIER = pl.kolakowski.SwifterMac;
12221220
PRODUCT_NAME = Swifter;
@@ -1279,7 +1277,7 @@
12791277
GCC_WARN_UNUSED_FUNCTION = YES;
12801278
GCC_WARN_UNUSED_VARIABLE = YES;
12811279
IPHONEOS_DEPLOYMENT_TARGET = 8.0;
1282-
MACOSX_DEPLOYMENT_TARGET = 10.9;
1280+
MACOSX_DEPLOYMENT_TARGET = 10.10;
12831281
METAL_ENABLE_DEBUG_INFO = YES;
12841282
ONLY_ACTIVE_ARCH = YES;
12851283
OTHER_SWIFT_FLAGS = "-Xfrontend -warn-long-function-bodies=500";
@@ -1334,7 +1332,7 @@
13341332
GCC_WARN_UNUSED_FUNCTION = YES;
13351333
GCC_WARN_UNUSED_VARIABLE = YES;
13361334
IPHONEOS_DEPLOYMENT_TARGET = 8.0;
1337-
MACOSX_DEPLOYMENT_TARGET = 10.9;
1335+
MACOSX_DEPLOYMENT_TARGET = 10.10;
13381336
METAL_ENABLE_DEBUG_INFO = NO;
13391337
ONLY_ACTIVE_ARCH = NO;
13401338
OTHER_SWIFT_FLAGS = "-Xfrontend -warn-long-function-bodies=500";
@@ -1395,7 +1393,6 @@
13951393
"$(inherited)",
13961394
);
13971395
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks @loader_path/../Frameworks";
1398-
MACOSX_DEPLOYMENT_TARGET = 10.9;
13991396
MTL_ENABLE_DEBUG_INFO = YES;
14001397
ONLY_ACTIVE_ARCH = YES;
14011398
PRODUCT_NAME = "$(TARGET_NAME)";
@@ -1413,7 +1410,6 @@
14131410
CLANG_ENABLE_MODULES = YES;
14141411
DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym";
14151412
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks @loader_path/../Frameworks";
1416-
MACOSX_DEPLOYMENT_TARGET = 10.9;
14171413
MTL_ENABLE_DEBUG_INFO = NO;
14181414
PRODUCT_NAME = "$(TARGET_NAME)";
14191415
SDKROOT = macosx;

XCode/Tests/SwifterTestsHttpParser.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ class SwifterTestsHttpParser: XCTestCase {
153153

154154
XCTAssertEqual(resp?.queryParams.filter({ $0.0 == "link"}).first?.1, "https://www.youtube.com/watch?v=D2cUBG4PnOA")
155155
XCTAssertEqual(resp?.method, "GET", "Parser should extract HTTP method name from the status line.")
156-
XCTAssertEqual(resp?.path, "/open?link=https://www.youtube.com/watch?v=D2cUBG4PnOA", "Parser should extract HTTP path value from the status line.")
156+
XCTAssertEqual(resp?.path, "/open", "Parser should extract HTTP path value from the status line.")
157157
XCTAssertEqual(resp?.headers["content-length"], "10", "Parser should extract Content-Length header value.")
158158

159159
resp = try? parser.readHttpRequest(TestSocket("POST / HTTP/1.0\nContent-Length: 10\n\n1234567890"))

0 commit comments

Comments
 (0)