Ver código fonte

Merge pull request #404 from Mazyod/bugfix/parse-path-without-query

Parse HttpRequest Path without Query String
Victor Sigler 7 anos atrás
pai
commit
3367b63158

+ 5 - 0
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

+ 3 - 47
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..<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)

+ 2 - 6
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;

+ 1 - 1
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"))