Skip to content
This repository was archived by the owner on Dec 2, 2022. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions SKServer/Sources/Conformers/SwifterServer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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) }))
)
Expand Down
13 changes: 9 additions & 4 deletions SKServer/Sources/Titan/TitanCore/TitanRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
}
Expand All @@ -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
}
Expand All @@ -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)
}
}

Expand Down
37 changes: 0 additions & 37 deletions SKServer/Sources/Titan/TitanQueryString/TitanQueryString.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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" = <space>"
// 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] {
Expand Down
4 changes: 2 additions & 2 deletions SKServer/Sources/Titan/TitanRouter/InitializerOverloads.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down
2 changes: 1 addition & 1 deletion SKWebAPI/Sources/Endpoint.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down