mirror of
https://github.com/apple/swift-http-types
synced 2026-11-22 04:30:40 +00:00
Convert WebSocket URLRequest to RFC8441 HTTPRequest (#92)
WebSocket uses the extended-CONNECT form in HTTP/2 and HTTP/3. When creating an HTTPRequest from a URLRequest, we should translate it into the new form. `:method`: `GET` -> `CONNECT` `:scheme`: `wss`/`ws` -> `https`/`http` `:protocol`: `websocket` HTTPTypes defaults to modern HTTP versions, and the expectation is that the HTTP/1 serializer has to translate the extended-CONNECT request back to the old request form. --------- Co-authored-by: Cory Benfield <lukasa@apple.com>
This commit is contained in:
1 parent
4366cd1e64
commit
9c9d5ea7e3
2 files changed
+54
No files matched your search
@@ -25,6 +25,23 @@ extension URLRequest {
|
||||
/// Create a `URLRequest` from an `HTTPRequest`.
|
||||
/// - Parameter httpRequest: The HTTP request to convert from.
|
||||
public init?(httpRequest: HTTPRequest) {
|
||||
// Translate an extended-CONNECT WebSocket request to the legacy form
|
||||
if httpRequest.method == .connect && httpRequest.extendedConnectProtocol == "websocket" {
|
||||
var legacyRequest = httpRequest
|
||||
legacyRequest.method = .get
|
||||
switch httpRequest.scheme?.lowercased() {
|
||||
case "https":
|
||||
legacyRequest.scheme = "wss"
|
||||
case "http":
|
||||
legacyRequest.scheme = "ws"
|
||||
default:
|
||||
break
|
||||
}
|
||||
legacyRequest.extendedConnectProtocol = nil
|
||||
self.init(httpRequest: legacyRequest)
|
||||
return
|
||||
}
|
||||
|
||||
guard let url = httpRequest.url else {
|
||||
return nil
|
||||
}
|
||||
@@ -63,6 +80,27 @@ extension URLRequest {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Translate a legacy WebSocket request to the extended-CONNECT form
|
||||
if method == .get, let scheme = request.scheme {
|
||||
switch scheme.utf8.count {
|
||||
case 3:
|
||||
if scheme.lowercased() == "wss" {
|
||||
request.method = .connect
|
||||
request.scheme = "https"
|
||||
request.extendedConnectProtocol = "websocket"
|
||||
}
|
||||
case 2:
|
||||
if scheme.lowercased() == "ws" {
|
||||
request.method = .connect
|
||||
request.scheme = "http"
|
||||
request.extendedConnectProtocol = "websocket"
|
||||
}
|
||||
default:
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
return request
|
||||
}
|
||||
}
|
||||
|
||||
@@ -143,6 +143,22 @@ final class HTTPTypesFoundationTests: XCTestCase {
|
||||
XCTAssertEqual(request.headerFields[.init("x-foo")!], "Bar")
|
||||
}
|
||||
|
||||
func testWebSocketRequest() throws {
|
||||
let urlRequest = URLRequest(url: URL(string: "wss://www.example.com/")!)
|
||||
|
||||
let request = try XCTUnwrap(urlRequest.httpRequest)
|
||||
XCTAssertEqual(request.method, .connect)
|
||||
XCTAssertEqual(request.scheme, "https")
|
||||
XCTAssertEqual(request.authority, "www.example.com")
|
||||
XCTAssertEqual(request.path, "/")
|
||||
XCTAssertEqual(request.extendedConnectProtocol, "websocket")
|
||||
|
||||
let urlRequestConverted = try XCTUnwrap(URLRequest(httpRequest: request))
|
||||
XCTAssertEqual(urlRequestConverted.httpMethod, "GET")
|
||||
XCTAssertEqual(urlRequestConverted.url, URL(string: "wss://www.example.com/"))
|
||||
XCTAssertEqual(urlRequest, urlRequestConverted)
|
||||
}
|
||||
|
||||
func testResponseToFoundation() throws {
|
||||
let response = HTTPResponse(
|
||||
status: .ok,
|
||||
|
||||
Reference in new issue
Block a user