Skip to content

Commit

Permalink
Merge pull request #132 from d-exclaimation/reserved-paths
Browse files Browse the repository at this point in the history
Reserved paths for specific operation and protocol, Allow combination of upgrade Connection headers
  • Loading branch information
d-exclaimation authored Jun 14, 2023
2 parents d174002 + 74da633 commit 387c2a4
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ public extension Request {
guard let connection = headers.first(name: .connection), let upgrade = headers.first(name: .upgrade) else {
return false
}
return connection.lowercased() == "upgrade" && upgrade.lowercased() == "websocket"
return connection.lowercased().contains("upgrade") && upgrade.lowercased().contains("websocket")
}
}
19 changes: 17 additions & 2 deletions Sources/Pioneer/Vapor/Pioneer+Vapor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -82,18 +82,33 @@ public extension Pioneer {
self.websocketGuard = websocketGuard
}

private func matchingPath(to request: Request) -> Bool {
request.matching(path: path)
|| request.matching(path: path + ["websocket"])
|| request.matching(path: path + ["playground"])
|| request.matching(path: path + ["http"])
}

/// Check whether request should be served by Pioneer
/// - Parameter request: The incoming request
/// - Returns: True if should be served
private func shouldServe(to request: Request) -> Bool {
(request.method == .POST || request.method == .GET) && request.matching(path: path)
(request.method == .POST || request.method == .GET) && matchingPath(to: request)
}

/// What type of service should Pioneer serve for this request
/// - Parameter request: The incoming request
/// - Returns: A service to be done
private func serving(to request: Request) async throws -> Serve {
if request.method == .POST {
if request.pathComponents.last == "playground" {
return server.playground == .disable ? .ignore : .playground
}

if request.pathComponents.last == "websocket" && server.websocketProtocol.isAccepting {
return .upgrade
}

if request.method == .POST || request.pathComponents.last == "http" {
return .operation
}

Expand Down

0 comments on commit 387c2a4

Please sign in to comment.