From 64266cfe37494b0737e5516ce9fc321ef8058721 Mon Sep 17 00:00:00 2001 From: Maz Jaleel Date: Tue, 16 Apr 2019 16:05:02 +0400 Subject: [PATCH 1/2] Fix path to resolve only path part # Conflicts: # XCode/Sources/HttpParser.swift # XCode/Swifter.xcodeproj/project.pbxproj # XCode/Tests/SwifterTestsHttpParser.swift --- XCode/Sources/HttpParser.swift | 50 ++---------------------- XCode/Swifter.xcodeproj/project.pbxproj | 8 +--- XCode/Tests/SwifterTestsHttpParser.swift | 2 +- 3 files changed, 6 insertions(+), 54 deletions(-) diff --git a/XCode/Sources/HttpParser.swift b/XCode/Sources/HttpParser.swift index 63507778..6403ea9d 100644 --- a/XCode/Sources/HttpParser.swift +++ b/XCode/Sources/HttpParser.swift @@ -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.. [(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.. [UInt8] { return try socket.read(length: size) diff --git a/XCode/Swifter.xcodeproj/project.pbxproj b/XCode/Swifter.xcodeproj/project.pbxproj index 53e4fe02..43d23adc 100644 --- a/XCode/Swifter.xcodeproj/project.pbxproj +++ b/XCode/Swifter.xcodeproj/project.pbxproj @@ -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; @@ -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; @@ -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"; @@ -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"; @@ -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)"; @@ -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; diff --git a/XCode/Tests/SwifterTestsHttpParser.swift b/XCode/Tests/SwifterTestsHttpParser.swift index 212f7754..497df434 100644 --- a/XCode/Tests/SwifterTestsHttpParser.swift +++ b/XCode/Tests/SwifterTestsHttpParser.swift @@ -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")) From fb4ba22b6aa5dc399fd12e1ec87364ab75a9719c Mon Sep 17 00:00:00 2001 From: Maz Jaleel Date: Wed, 1 May 2019 08:59:22 +0400 Subject: [PATCH 2/2] Added changelog entry --- CHANGELOG.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0e7d55af..fbff8963 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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