diff --git a/Sources/Compute/IncomingRequest.swift b/Sources/Compute/IncomingRequest.swift index 0fb7c571..1e4957c7 100644 --- a/Sources/Compute/IncomingRequest.swift +++ b/Sources/Compute/IncomingRequest.swift @@ -66,6 +66,14 @@ 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() @@ -73,7 +81,12 @@ public struct IncomingRequest: Sendable { 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) + } } } diff --git a/Sources/Compute/Runtime/Request.swift b/Sources/Compute/Runtime/Request.swift index 8238cf7b..dfcd612f 100644 --- a/Sources/Compute/Runtime/Request.swift +++ b/Sources/Compute/Runtime/Request.swift @@ -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)) } } diff --git a/Sources/Compute/Runtime/Stubs.swift b/Sources/Compute/Runtime/Stubs.swift index 18e326b1..9ca0dade 100644 --- a/Sources/Compute/Runtime/Stubs.swift +++ b/Sources/Compute/Runtime/Stubs.swift @@ -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!, _ backend_len: Int) -> Int32 { fatalError() } +func fastly_http_req__redirect_to_websocket_proxy(_ backend: UnsafePointer!, _ backend_len: Int) -> Int32 { fatalError() } + +func fastly_http_req__redirect_to_grip_proxy(_ backend: UnsafePointer!, _ backend_len: Int) -> Int32 { fatalError() } func fastly_http_req__register_dynamic_backend(_ name: UnsafePointer!, _ name_len: Int, _ target: UnsafePointer!, _ target_len: Int, _ backend_config_mask: UInt32, _ backend_configuration: UnsafeMutablePointer!) -> Int32 { fatalError() } diff --git a/Sources/ComputeRuntime/include/ComputeRuntime.h b/Sources/ComputeRuntime/include/ComputeRuntime.h index ad91c98f..eea521f3 100644 --- a/Sources/ComputeRuntime/include/ComputeRuntime.h +++ b/Sources/ComputeRuntime/include/ComputeRuntime.h @@ -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,