Skip to content

Commit d4b1e64

Browse files
Fixed a bug with parsing query keys/values containing question marks.
1 parent 768dfe8 commit d4b1e64

File tree

1 file changed

+15
-13
lines changed

1 file changed

+15
-13
lines changed

Sources/Http.swift

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -232,20 +232,22 @@ public class HttpIncomingDataPorcessor: Hashable, IncomingDataProcessor {
232232
throw SwifterError.httpError("Invalid 'path' value \(requestLineTokens[1]).")
233233
}
234234

235-
let queryComponents = path.components(separatedBy: "?")
236-
237-
if queryComponents.count > 1, let first = queryComponents.first, let last = queryComponents.last {
238-
request.path = first
239-
request.query = last
240-
.components(separatedBy: "&")
241-
.reduce([(String, String)]()) { (c, s) -> [(String, String)] in
242-
let tokens = s.components(separatedBy: "=")
243-
if let name = tokens.first, let value = tokens.last {
244-
if let nameDecoded = name.removingPercentEncoding, let valueDecoded = value.removingPercentEncoding {
245-
return c + [(nameDecoded, tokens.count > 1 ? valueDecoded : "")]
235+
if let firstQuestionMark = path.characters.index(of: "?") {
236+
request.path = String(path.characters[path.startIndex..<firstQuestionMark])
237+
let queryStartIndex = path.characters.index(after: firstQuestionMark)
238+
if path.endIndex > queryStartIndex {
239+
let query = String(path.characters[queryStartIndex..<path.endIndex])
240+
request.query = query
241+
.components(separatedBy: "&")
242+
.reduce([(String, String)]()) { (c, s) -> [(String, String)] in
243+
let tokens = s.components(separatedBy: "=")
244+
if let name = tokens.first, let value = tokens.last {
245+
if let nameDecoded = name.removingPercentEncoding, let valueDecoded = value.removingPercentEncoding {
246+
return c + [(nameDecoded, tokens.count > 1 ? valueDecoded : "")]
247+
}
246248
}
247-
}
248-
return c
249+
return c
250+
}
249251
}
250252
} else {
251253
request.path = path

0 commit comments

Comments
 (0)