Skip to content

Commit

Permalink
Do not add a Content-Type header to the HTTP upgrade request (#127)
Browse files Browse the repository at this point in the history
Do not add a `Content-Type` header to the HTTP upgrade request.

Fixes #126.

[Some](https://alpaca.markets/docs/api-references/trading-api/streaming/) WebSocket servers use the `Content-Type` header to determine which serialization format to use for its WebSocket messages. However, `HTTPInitialRequestHandler` [adds](https://github.com/vapor/websocket-kit/blob/62f568aa9d338aa33790534fa7b5800976f93af8/Sources/WebSocketKit/HTTPInitialRequestHandler.swift#L24) a hard-coded `Content-Type: text/plain; charset=utf-8` header to the WebSocket client’s HTTP upgrade request, so the client cannot customize the header value.

Given that the HTTP upgrade request body is empty, there is no need for `HTTPInitialRequestHandler` to add the `Content-Type` header. This will allow the client to add their own, if desired.

Co-authored-by: fumoboy007 <[email protected]>
Co-authored-by: Tim Condon <[email protected]>
  • Loading branch information
3 people authored Mar 7, 2023
1 parent e8e6bbe commit 2b88859
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
1 change: 0 additions & 1 deletion Sources/WebSocketKit/HTTPInitialRequestHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ final class HTTPInitialRequestHandler: ChannelInboundHandler, RemovableChannelHa

func channelActive(context: ChannelHandlerContext) {
var headers = self.headers
headers.add(name: "Content-Type", value: "text/plain; charset=utf-8")
headers.add(name: "Host", value: self.host)

var uri = self.path.hasPrefix("/") ? self.path : "/" + self.path
Expand Down
18 changes: 13 additions & 5 deletions Tests/WebSocketKitTests/WebSocketKitTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -179,12 +179,20 @@ final class WebSocketKitTests: XCTestCase {
func testHeadersAreSent() throws {
let promiseAuth = self.elg.next().makePromise(of: String.self)

// make sure there is no content-length header
let promiseNoContentLength = self.elg.next().makePromise(of: Bool.self)
// make sure there are no unwanted headers such as `Content-Length` or `Content-Type`
let promiseHasUnwantedHeaders = self.elg.next().makePromise(of: Bool.self)

let server = try ServerBootstrap.webSocket(on: self.elg) { req, ws in
promiseAuth.succeed(req.headers.first(name: "Auth")!)
promiseNoContentLength.succeed(req.headers.contains(name: "content-length"))
let headers = req.headers

promiseAuth.succeed(headers.first(name: "Auth")!)

let hasUnwantedHeaders = (
headers.contains(name: "Content-Length") ||
headers.contains(name: "Content-Type")
)
promiseHasUnwantedHeaders.succeed(hasUnwantedHeaders)

ws.close(promise: nil)
}.bind(host: "localhost", port: 0).wait()

Expand All @@ -201,7 +209,7 @@ final class WebSocketKitTests: XCTestCase {
}.cascadeFailure(to: promiseAuth)

try XCTAssertEqual(promiseAuth.futureResult.wait(), "supersecretsauce")
try XCTAssertFalse(promiseNoContentLength.futureResult.wait())
try XCTAssertFalse(promiseHasUnwantedHeaders.futureResult.wait())
try server.close(mode: .all).wait()
}

Expand Down

0 comments on commit 2b88859

Please sign in to comment.