Skip to content

Commit

Permalink
Merge pull request #404 from Mazyod/bugfix/parse-path-without-query
Browse files Browse the repository at this point in the history
Parse HttpRequest Path without Query String
  • Loading branch information
Vkt0r authored May 2, 2019
2 parents d3c2d21 + fb4ba22 commit 3367b63
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 54 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,16 @@ All notable changes to this project will be documented in this file. Changes not
- 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)

- 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)
- Fixes `HttpRequest.path` value to be parsed without query parameters [#404](https://github.com/httpswift/swifter/pull/404)) by [@mazyod](https://github.com/mazyod)

## Changed
- 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)
- Podspec source_files updated to match source file directory changes. ([#400](https://github.com/httpswift/swifter/pull/400)) by [@welsonpan](https://github.com/welsonpan)
- Refactor: Use Foundation API for Base64 encoding. ([#403](https://github.com/httpswift/swifter/pull/403)) by [@mazyod](https://github.com/mazyod)
- Refactor: Use `URLComponents` for `HttpRequest` path and query parameters parsing [#404](https://github.com/httpswift/swifter/pull/404)) by [@mazyod](https://github.com/mazyod)

## Removed
- Dropped macOS 10.9 support [#404](https://github.com/httpswift/swifter/pull/404)) by [@mazyod](https://github.com/mazyod)

# [1.4.6]
## Added
Expand Down
50 changes: 3 additions & 47 deletions XCode/Sources/HttpParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,59 +23,15 @@ public class HttpParser {
}
let request = HttpRequest()
request.method = statusLineTokens[0]
request.path = statusLineTokens[1]
request.queryParams = extractQueryParams(request.path)
let urlComponents = URLComponents(string: statusLineTokens[1])
request.path = urlComponents?.path ?? ""
request.queryParams = urlComponents?.queryItems?.map { ($0.name, $0.value ?? "") } ?? []
request.headers = try readHeaders(socket)
if let contentLength = request.headers["content-length"], let contentLengthValue = Int(contentLength) {
request.body = try readBody(socket, size: contentLengthValue)
}
return request
}

private func extractQueryParams(_ url: String) -> [(String, String)] {
#if compiler(>=5.0)
guard let questionMarkIndex = url.firstIndex(of: "?") else {
return []
}
#else
guard let questionMarkIndex = url.index(of: "?") else {
return []
}
#endif
let queryStart = url.index(after: questionMarkIndex)

guard url.endIndex > queryStart else { return [] }

#if swift(>=4.0)
let query = String(url[queryStart..<url.endIndex])
#else
guard let query = String(url[queryStart..<url.endIndex]) else { return [] }
#endif

return query.components(separatedBy: "&")
.reduce([(String, String)]()) { (result, stringValue) -> [(String, String)] in
#if compiler(>=5.0)
guard let nameEndIndex = stringValue.firstIndex(of: "=") else {
return result
}
#else
guard let nameEndIndex = stringValue.index(of: "=") else {
return result
}
#endif
guard let name = String(stringValue[stringValue.startIndex..<nameEndIndex]).removingPercentEncoding else {
return result
}
let valueStartIndex = stringValue.index(nameEndIndex, offsetBy: 1)
guard valueStartIndex < stringValue.endIndex else {
return result + [(name, "")]
}
guard let value = String(stringValue[valueStartIndex..<stringValue.endIndex]).removingPercentEncoding else {
return result + [(name, "")]
}
return result + [(name, value)]
}
}

private func readBody(_ socket: Socket, size: Int) throws -> [UInt8] {
return try socket.read(length: size)
Expand Down
8 changes: 2 additions & 6 deletions XCode/Swifter.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -1187,7 +1187,6 @@
INFOPLIST_FILE = SwifterMac/Info.plist;
INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks";
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks @loader_path/Frameworks";
MACOSX_DEPLOYMENT_TARGET = 10.9;
MTL_ENABLE_DEBUG_INFO = YES;
PRODUCT_BUNDLE_IDENTIFIER = pl.kolakowski.SwifterMac;
PRODUCT_NAME = Swifter;
Expand Down Expand Up @@ -1216,7 +1215,6 @@
INFOPLIST_FILE = SwifterMac/Info.plist;
INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks";
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks @loader_path/Frameworks";
MACOSX_DEPLOYMENT_TARGET = 10.9;
MTL_ENABLE_DEBUG_INFO = NO;
PRODUCT_BUNDLE_IDENTIFIER = pl.kolakowski.SwifterMac;
PRODUCT_NAME = Swifter;
Expand Down Expand Up @@ -1279,7 +1277,7 @@
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 8.0;
MACOSX_DEPLOYMENT_TARGET = 10.9;
MACOSX_DEPLOYMENT_TARGET = 10.10;
METAL_ENABLE_DEBUG_INFO = YES;
ONLY_ACTIVE_ARCH = YES;
OTHER_SWIFT_FLAGS = "-Xfrontend -warn-long-function-bodies=500";
Expand Down Expand Up @@ -1334,7 +1332,7 @@
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 8.0;
MACOSX_DEPLOYMENT_TARGET = 10.9;
MACOSX_DEPLOYMENT_TARGET = 10.10;
METAL_ENABLE_DEBUG_INFO = NO;
ONLY_ACTIVE_ARCH = NO;
OTHER_SWIFT_FLAGS = "-Xfrontend -warn-long-function-bodies=500";
Expand Down Expand Up @@ -1395,7 +1393,6 @@
"$(inherited)",
);
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks @loader_path/../Frameworks";
MACOSX_DEPLOYMENT_TARGET = 10.9;
MTL_ENABLE_DEBUG_INFO = YES;
ONLY_ACTIVE_ARCH = YES;
PRODUCT_NAME = "$(TARGET_NAME)";
Expand All @@ -1413,7 +1410,6 @@
CLANG_ENABLE_MODULES = YES;
DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym";
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks @loader_path/../Frameworks";
MACOSX_DEPLOYMENT_TARGET = 10.9;
MTL_ENABLE_DEBUG_INFO = NO;
PRODUCT_NAME = "$(TARGET_NAME)";
SDKROOT = macosx;
Expand Down
2 changes: 1 addition & 1 deletion XCode/Tests/SwifterTestsHttpParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ class SwifterTestsHttpParser: XCTestCase {

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

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

0 comments on commit 3367b63

Please sign in to comment.