Skip to content

Commit

Permalink
Merge pull request #201 from IBM-Swift/ws-url-query-params
Browse files Browse the repository at this point in the history
Support WebSocket URIs with query parameters
  • Loading branch information
Pushkar N Kulkarni authored May 4, 2019
2 parents e1576b4 + cac959e commit 4b2a926
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Sources/KituraNet/HTTP/HTTPRequestHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ internal class HTTPRequestHandler: ChannelInboundHandler, RemovableChannelHandle
default:
// Handle only NIOWebSocketUpgradeError here, nothing else
if let upgradeError = error as? NIOWebSocketUpgradeError, upgradeError == .unsupportedWebSocketTarget {
let target = server.latestWebSocketURI ?? "/<unknown>"
let target = server.latestWebSocketURI
message = "No service has been registered for the path \(target)"
} else {
context.close(promise: nil)
Expand Down
7 changes: 3 additions & 4 deletions Sources/KituraNet/HTTP/HTTPServer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -165,12 +165,12 @@ public class HTTPServer: Server {
private var sslContext: NIOSSLContext?

/// URI for which the latest WebSocket upgrade was requested by a client
var latestWebSocketURI: String?
var latestWebSocketURI: String = "/<unknown>"

/// Determines if the request should be upgraded and adds additional upgrade headers to the request
private func shouldUpgradeToWebSocket(channel: Channel, webSocketHandlerFactory: ProtocolHandlerFactory, head: HTTPRequestHead) -> EventLoopFuture<HTTPHeaders?> {
self.latestWebSocketURI = head.uri
guard webSocketHandlerFactory.isServiceRegistered(at: head.uri) else { return channel.eventLoop.makeSucceededFuture(nil) }
self.latestWebSocketURI = String(head.uri.split(separator: "?")[0])
guard webSocketHandlerFactory.isServiceRegistered(at: self.latestWebSocketURI) else { return channel.eventLoop.makeSucceededFuture(nil) }
var headers = HTTPHeaders()
if let wsProtocol = head.headers["Sec-WebSocket-Protocol"].first {
headers.add(name: "Sec-WebSocket-Protocol", value: wsProtocol)
Expand All @@ -187,7 +187,6 @@ public class HTTPServer: Server {
/// Creates upgrade request and adds WebSocket handler to pipeline
private func upgradeHandler(webSocketHandlerFactory: ProtocolHandlerFactory, request: HTTPRequestHead) -> EventLoopFuture<Void> {
guard let ctx = self.ctx else { fatalError("Cannot create ServerRequest") }
///TODO: Handle secure upgrade request ("wss://")
let serverRequest = HTTPServerRequest(ctx: ctx, requestHead: request, enableSSL: false)
let websocketConnectionHandler = webSocketHandlerFactory.handler(for: serverRequest)
let future = ctx.channel.pipeline.addHandler(websocketConnectionHandler)
Expand Down

0 comments on commit 4b2a926

Please sign in to comment.