|
|
@@ -33,19 +33,46 @@ public class HttpParser {
|
|
|
}
|
|
|
|
|
|
private func extractQueryParams(_ url: String) -> [(String, String)] {
|
|
|
- let tokens = url.components(separatedBy: "?")
|
|
|
- guard let query = tokens.last, tokens.count >= 2 else {
|
|
|
+ guard let questionMark = url.characters.index(of: "?") else {
|
|
|
return []
|
|
|
}
|
|
|
- return query.components(separatedBy: "&").reduce([(String, String)]()) { (c, s) -> [(String, String)] in
|
|
|
- let tokens = s.components(separatedBy: "=")
|
|
|
- let name = tokens.first?.removingPercentEncoding
|
|
|
- let value = tokens.count > 1 ? (tokens.last?.removingPercentEncoding ?? "") : ""
|
|
|
- if let nameFound = name {
|
|
|
- return c + [(nameFound, value)]
|
|
|
- }
|
|
|
- return c
|
|
|
+ let queryStart = url.characters.index(after: questionMark)
|
|
|
+ guard url.endIndex > queryStart else {
|
|
|
+ return []
|
|
|
+ }
|
|
|
+ let query = String(url.characters[queryStart..<url.endIndex])
|
|
|
+ return query.components(separatedBy: "&")
|
|
|
+ .reduce([(String, String)]()) { (c, s) -> [(String, String)] in
|
|
|
+ guard let nameEndIndex = s.characters.index(of: "=") else {
|
|
|
+ return c
|
|
|
+ }
|
|
|
+ guard let name = String(s.characters[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.characters[valueStartIndex..<s.endIndex]).removingPercentEncoding else {
|
|
|
+ return c + [(name, "")]
|
|
|
+ }
|
|
|
+ return c + [(name, value)]
|
|
|
}
|
|
|
+
|
|
|
+
|
|
|
+// let tokens = url.components(separatedBy: "?")
|
|
|
+// guard let query = tokens.last, tokens.count >= 2 else {
|
|
|
+// return []
|
|
|
+// }
|
|
|
+// return query.components(separatedBy: "&").reduce([(String, String)]()) { (c, s) -> [(String, String)] in
|
|
|
+// let tokens = s.components(separatedBy: "=")
|
|
|
+// let name = tokens.first?.removingPercentEncoding
|
|
|
+// let value = tokens.count > 1 ? (tokens.last?.removingPercentEncoding ?? "") : ""
|
|
|
+// if let nameFound = name {
|
|
|
+// return c + [(nameFound, value)]
|
|
|
+// }
|
|
|
+// return c
|
|
|
+// }
|
|
|
}
|
|
|
|
|
|
private func readBody(_ socket: Socket, size: Int) throws -> [UInt8] {
|