Skip to content

Commit

Permalink
Fix path to resolve only path part
Browse files Browse the repository at this point in the history
  • Loading branch information
Mazyod committed Apr 16, 2019
1 parent fd2c8e0 commit 8a474d4
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 54 deletions.
50 changes: 3 additions & 47 deletions Sources/HttpParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,60 +23,16 @@ 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)]()) { (c, s) -> [(String, String)] in
#if compiler(>=5.0)
guard let nameEndIndex = s.firstIndex(of: "=") else {
return c
}
#else
guard let nameEndIndex = s.index(of: "=") else {
return c
}
#endif
guard let name = String(s[s.startIndex..<nameEndIndex]).removingPercentEncoding else {
return c
}
let valueStartIndex = s.index(nameEndIndex, offsetBy: 1)
guard valueStartIndex < s.endIndex else {
return c + [(name, "")]
}
guard let value = String(s[valueStartIndex..<s.endIndex]).removingPercentEncoding else {
return c + [(name, "")]
}
return c + [(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 @@ -1159,7 +1159,6 @@
INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks";
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks @loader_path/Frameworks";
MACH_O_TYPE = staticlib;
MACOSX_DEPLOYMENT_TARGET = 10.9;
MTL_ENABLE_DEBUG_INFO = YES;
PRODUCT_BUNDLE_IDENTIFIER = pl.kolakowski.SwifterMac;
PRODUCT_NAME = Swifter;
Expand Down Expand Up @@ -1188,7 +1187,6 @@
INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks";
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks @loader_path/Frameworks";
MACH_O_TYPE = staticlib;
MACOSX_DEPLOYMENT_TARGET = 10.9;
MTL_ENABLE_DEBUG_INFO = NO;
PRODUCT_BUNDLE_IDENTIFIER = pl.kolakowski.SwifterMac;
PRODUCT_NAME = Swifter;
Expand Down Expand Up @@ -1250,7 +1248,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 @@ -1305,7 +1303,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 @@ -1364,7 +1362,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 @@ -1381,7 +1378,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 @@ -152,7 +152,7 @@ class SwifterTestsHttpParser: XCTestCase {

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

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

0 comments on commit 8a474d4

Please sign in to comment.