Skip to content

Commit

Permalink
Websocket support
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrewBarba committed Nov 10, 2022
1 parent b11b440 commit 9050c49
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 7 deletions.
17 changes: 15 additions & 2 deletions Sources/Compute/IncomingRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,27 @@ public struct IncomingRequest: Sendable {
return .localhost
}
}
}

extension IncomingRequest {

public enum UpgradeWebsocketBehavior {
case proxy
case fanout
}

public func isUpgradeWebsocketRequest() -> Bool {
let connection = headers[.connection, default: ""].lowercased()
let upgrade = headers[.upgrade, default: ""].lowercased()
return connection.contains("upgrade") && upgrade.contains("websocket")
}

public func upgradeWebsocket(backend: String) throws {
try request.upgradeWebsocket(backend: backend)
public func upgradeWebsocket(backend: String, behavior: UpgradeWebsocketBehavior) throws {
switch behavior {
case .proxy:
try request.redirectToWebsocketProxy(backend: backend)
case .fanout:
try request.redirectToGripProxy(backend: backend)
}
}
}
8 changes: 6 additions & 2 deletions Sources/Compute/Runtime/Request.swift
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,12 @@ public struct Request: Sendable {
try wasi(fastly_http_req__framing_headers_mode_set(handle, newValue.rawValue))
}

public func upgradeWebsocket(backend: String) throws {
try wasi(fastly_http_req__upgrade_websocket(backend, backend.utf8.count))
public func redirectToWebsocketProxy(backend: String) throws {
try wasi(fastly_http_req__redirect_to_websocket_proxy(backend, backend.utf8.count))
}

public func redirectToGripProxy(backend: String) throws {
try wasi(fastly_http_req__redirect_to_grip_proxy(backend, backend.utf8.count))
}
}

Expand Down
4 changes: 3 additions & 1 deletion Sources/Compute/Runtime/Stubs.swift
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,9 @@ func fastly_http_req__auto_decompress_response_set(_ req_handle: WasiHandle, _ e

func fastly_http_req__framing_headers_mode_set(_ req_handle: WasiHandle, _ mode: UInt32) -> Int32 { fatalError() }

func fastly_http_req__upgrade_websocket(_ backend: UnsafePointer<CChar>!, _ backend_len: Int) -> Int32 { fatalError() }
func fastly_http_req__redirect_to_websocket_proxy(_ backend: UnsafePointer<CChar>!, _ backend_len: Int) -> Int32 { fatalError() }

func fastly_http_req__redirect_to_grip_proxy(_ backend: UnsafePointer<CChar>!, _ backend_len: Int) -> Int32 { fatalError() }

func fastly_http_req__register_dynamic_backend(_ name: UnsafePointer<CChar>!, _ name_len: Int, _ target: UnsafePointer<CChar>!, _ target_len: Int, _ backend_config_mask: UInt32, _ backend_configuration: UnsafeMutablePointer<DynamicBackendConfig>!) -> Int32 { fatalError() }

Expand Down
7 changes: 5 additions & 2 deletions Sources/ComputeRuntime/include/ComputeRuntime.h
Original file line number Diff line number Diff line change
Expand Up @@ -191,8 +191,11 @@ int fastly_http_req__pending_req_select(WasiHandle req_handles[], size_t req_han
WASM_IMPORT("fastly_http_req", "framing_headers_mode_set")
int fastly_http_req__framing_headers_mode_set(WasiHandle req_handle, uint32_t mode);

WASM_IMPORT("fastly_http_req", "upgrade_websocket")
int fastly_http_req__upgrade_websocket(const char *backend, size_t backend_len);
WASM_IMPORT("fastly_http_req", "redirect_to_websocket_proxy")
int fastly_http_req__redirect_to_websocket_proxy(const char *backend, size_t backend_len);

WASM_IMPORT("fastly_http_req", "redirect_to_grip_proxy")
int fastly_http_req__redirect_to_grip_proxy(const char *backend, size_t backend_len);

WASM_IMPORT("fastly_http_req", "register_dynamic_backend")
int fastly_http_req__register_dynamic_backend(const char *name,
Expand Down

0 comments on commit 9050c49

Please sign in to comment.