Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix another crash around URLComponents/URLQueryItem #80

Merged
merged 3 commits into from Nov 30, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 10 additions & 6 deletions Sources/ApplicativeRouter/SyntaxRouter.swift
Expand Up @@ -137,14 +137,18 @@ extension Router {
private func requestData(from request: URLRequest) -> RequestData {
let method = request.httpMethod.flatMap(Method.init(string:)) ?? .get

guard let components = request.url.flatMap({ URLComponents(url: $0, resolvingAgainstBaseURL: false) })
else { return .init(method: method, path: [], query: [:], body: request.httpBody) }

let path = components.path.components(separatedBy: "/")
|> mapOptional { $0.isEmpty ? nil : $0 }
guard let url = request.url else {
return .init(method: method, path: [], query: [:], body: request.httpBody)
}

var query: [String: String] = [:]
components.queryItems?.forEach { query[$0.name] = $0.value ?? "" }
url.query?.split(separator: "&").forEach {
let pair = $0.split(separator: "=", maxSplits: 1, omittingEmptySubsequences: false)
query[String(pair[0]).removingPercentEncoding ?? ""] = String(pair[1]).removingPercentEncoding ?? ""
}

let path = url.path.components(separatedBy: "/")
|> mapOptional { $0.isEmpty ? nil : $0 }

return .init(method: method, path: path, query: query, body: request.httpBody)
}
Expand Down