From e34a1df3aba0a5f03eca0242682da9c9cd4b2396 Mon Sep 17 00:00:00 2001 From: David Jennes Date: Wed, 29 Dec 2021 19:29:36 +0100 Subject: [PATCH 1/2] Fix extracting query parameters from request --- .../Sources/Conformers/SwifterServer.swift | 1 + .../Titan/TitanCore/TitanRequest.swift | 13 +++++-- .../TitanQueryString/TitanQueryString.swift | 37 ------------------- .../TitanRouter/InitializerOverloads.swift | 4 +- 4 files changed, 12 insertions(+), 43 deletions(-) diff --git a/SKServer/Sources/Conformers/SwifterServer.swift b/SKServer/Sources/Conformers/SwifterServer.swift index c70127e8..714a8e70 100755 --- a/SKServer/Sources/Conformers/SwifterServer.swift +++ b/SKServer/Sources/Conformers/SwifterServer.swift @@ -58,6 +58,7 @@ extension HttpRequest { return try! Request( method: HTTPMethod.custom(named: method), path: path, + queryPairs: queryParams, body: String(bytes: body, encoding: .utf8) ?? "", headers: HTTPHeaders(headers: headers.map ({ Header(name: $0.key, value: $0.value) })) ) diff --git a/SKServer/Sources/Titan/TitanCore/TitanRequest.swift b/SKServer/Sources/Titan/TitanCore/TitanRequest.swift index b4969fb9..3968efef 100755 --- a/SKServer/Sources/Titan/TitanCore/TitanRequest.swift +++ b/SKServer/Sources/Titan/TitanCore/TitanRequest.swift @@ -17,8 +17,10 @@ import Foundation public protocol RequestType { /// The HTTP request body. var body: Data { get } - /// The HTTP request path, including query string and any fragments. + /// The HTTP request path var path: String { get } + /// The HTTP request query pairs + var queryPairs: [(String, String)] { get } /// The HTTP request method. var method: HTTPMethod { get } /// The HTTP request headers. @@ -30,14 +32,16 @@ public struct Request: RequestType { public var method: HTTPMethod public var path: String + public var queryPairs: [(String, String)] public var body: Data public var headers: HTTPHeaders /// Create a Request /// Throws an error if the body parameter cannot be converted to Data - public init(method: HTTPMethod, path: String, body: String, headers: HTTPHeaders) throws { + public init(method: HTTPMethod, path: String, queryPairs: [(String, String)], body: String, headers: HTTPHeaders) throws { self.method = method self.path = path + self.queryPairs = queryPairs guard let data = body.data(using: .utf8) else { throw TitanError.dataConversion } @@ -46,9 +50,10 @@ public struct Request: RequestType { } /// Create a Request - public init(method: HTTPMethod, path: String, body: Data, headers: HTTPHeaders) { + public init(method: HTTPMethod, path: String, queryPairs: [(String, String)], body: Data, headers: HTTPHeaders) { self.method = method self.path = path + self.queryPairs = queryPairs self.body = body self.headers = headers } @@ -57,7 +62,7 @@ public struct Request: RequestType { extension Request { /// Create a Request from a RequestType public init(request: RequestType) { - self.init(method: request.method, path: request.path, body: request.body, headers: request.headers) + self.init(method: request.method, path: request.path, queryPairs: request.queryPairs, body: request.body, headers: request.headers) } } diff --git a/SKServer/Sources/Titan/TitanQueryString/TitanQueryString.swift b/SKServer/Sources/Titan/TitanQueryString/TitanQueryString.swift index a0782461..50f0fc6a 100755 --- a/SKServer/Sources/Titan/TitanQueryString/TitanQueryString.swift +++ b/SKServer/Sources/Titan/TitanQueryString/TitanQueryString.swift @@ -14,43 +14,6 @@ import Foundation public extension RequestType { - /// The pairs of keys and values in the query string of the `RequestType`s path. - /// Complexity: 0(n) on all invocations. - var queryPairs: [(key: String, value: String)] { - // Ensure there is a query string, otherwise return - guard let indexOfQuery = self.path.firstIndex(of: "?") else { - return [] - } - - let query = self.path.suffix(from: indexOfQuery).dropFirst() - // Create an array of the individual query pairs, e.g. ["foo=bar", "baz=qux"] - let pairs = query.split(separator: "&") - - // Decode `foo=bar` -> `(key: "foo", value: "bar")`, percent decoding any values along the way - return pairs.map { pair -> (key: String, value: String) in - // Separate the query pair into an array, e.g. "foo=bar" -> ["foo", "bar"] - let comps = pair.split(separator: "=") - // Split returns an array of subsequences which should conform to StringProtocol, however on Linux StringProtocol is out of date. - // Workaround for https://bugs.swift.org/browse/SR-5727 by converting to a String directly - .map(String.init) - .map { - // Percent encoding mandates that "%20" = " - // however, many applications use "+" to mean space as well, so decode those (before we decode any percent-encoded plus signs!) - return $0.replacingOccurrences(of: "+", with: " ") - }.map { - return $0.removingPercentEncoding ?? "" - } - switch comps.count { - case 1: // "?foo=" - return (key: String(comps[0]), value: "") - case 2: // "?foo=bar" - return (key: String(comps[0]), value: String(comps[1])) - default: // "?" - return (key: "", value: "") - } - } - } - /// Access the query string as a dictionary, with case sensitive keys. /// Complexity: 0(n) on all invocations. var query: [String: String] { diff --git a/SKServer/Sources/Titan/TitanRouter/InitializerOverloads.swift b/SKServer/Sources/Titan/TitanRouter/InitializerOverloads.swift index 5e4ba7a6..60c702d1 100755 --- a/SKServer/Sources/Titan/TitanRouter/InitializerOverloads.swift +++ b/SKServer/Sources/Titan/TitanRouter/InitializerOverloads.swift @@ -14,8 +14,8 @@ import Foundation extension Request { - public init(_ method: HTTPMethod = .get, _ path: String = "/", _ body: String = "", _ headers: HTTPHeaders = HTTPHeaders()) { - self.init(method: method, path: path, body: body.data(using: .utf8) ?? Data(), headers: headers) + public init(_ method: HTTPMethod = .get, _ path: String = "/", _ queryPairs: [(String, String)] = [], _ body: String = "", _ headers: HTTPHeaders = HTTPHeaders()) { + self.init(method: method, path: path, queryPairs: queryPairs, body: body.data(using: .utf8) ?? Data(), headers: headers) } } From 076e87c03b21d0fba87eb521810802de786cab88 Mon Sep 17 00:00:00 2001 From: David Jennes Date: Thu, 30 Dec 2021 17:29:42 +0100 Subject: [PATCH 2/2] downgrade oauth access path --- SKWebAPI/Sources/Endpoint.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/SKWebAPI/Sources/Endpoint.swift b/SKWebAPI/Sources/Endpoint.swift index 1f3fbb50..00a22bce 100755 --- a/SKWebAPI/Sources/Endpoint.swift +++ b/SKWebAPI/Sources/Endpoint.swift @@ -70,7 +70,7 @@ public enum Endpoint: String { case mpimList = "mpim.list" case mpimMark = "mpim.mark" case mpimOpen = "mpim.open" - case oauthAccess = "oauth.v2.access" + case oauthAccess = "oauth.access" case pinsAdd = "pins.add" case pinsList = "pins.list" case pinsRemove = "pins.remove"