From 487a6e2eec5ea8315c183663c7b47086a4f37299 Mon Sep 17 00:00:00 2001 From: Jesse Haka Date: Tue, 31 Oct 2023 15:41:25 +0200 Subject: [PATCH] Add SourceAddr support (#32) * Add RemoteAddr support * use SourceAddr --- handler/api/api.go | 3 +++ handler/internal/imports/host.go | 4 ++++ handler/internal/imports/imports.go | 3 +++ handler/internal/imports/imports_stub.go | 5 +++++ handler/request.go | 5 +++++ 5 files changed, 20 insertions(+) diff --git a/handler/api/api.go b/handler/api/api.go index 2e62579..1500a31 100644 --- a/handler/api/api.go +++ b/handler/api/api.go @@ -121,6 +121,9 @@ type Request interface { // Headers allows access to any incoming request headers. Headers() Header + // GetSourceAddr returns client SourceAddr. + GetSourceAddr() string + // Body allows access to any incoming request body. To read this without // preventing the Next from reading it, enable FeatureBufferRequest. Body() Body diff --git a/handler/internal/imports/host.go b/handler/internal/imports/host.go index febea15..d0daa9c 100644 --- a/handler/internal/imports/host.go +++ b/handler/internal/imports/host.go @@ -178,3 +178,7 @@ func GetStatusCode() uint32 { func SetStatusCode(statusCode uint32) { setStatusCode(statusCode) } + +func GetSourceAddr(ptr uint32, limit BufLimit) (len uint32) { + return getSourceAddr(ptr, limit) +} diff --git a/handler/internal/imports/imports.go b/handler/internal/imports/imports.go index d99a20a..9321891 100644 --- a/handler/internal/imports/imports.go +++ b/handler/internal/imports/imports.go @@ -57,3 +57,6 @@ func getStatusCode() uint32 //go:wasmimport http_handler set_status_code func setStatusCode(statusCode uint32) + +//go:wasmimport http_handler get_source_addr +func getSourceAddr(ptr uint32, limit BufLimit) (len uint32) diff --git a/handler/internal/imports/imports_stub.go b/handler/internal/imports/imports_stub.go index 0909d87..8e66af1 100644 --- a/handler/internal/imports/imports_stub.go +++ b/handler/internal/imports/imports_stub.go @@ -77,3 +77,8 @@ func getStatusCode() uint32 { // setStatusCode is stubbed for compilation outside TinyGo. func setStatusCode(statusCode uint32) {} + +// getSourceAddr is stubbed for compilation outside TinyGo. +func getSourceAddr(ptr uint32, limit BufLimit) (len uint32) { + return 0 +} diff --git a/handler/request.go b/handler/request.go index 414416e..1dca7fc 100644 --- a/handler/request.go +++ b/handler/request.go @@ -57,3 +57,8 @@ func (wasmRequest) Body() api.Body { func (wasmRequest) Trailers() api.Header { return wasmRequestTrailers } + +// GetSourceAddr implements the same method as documented on api.Request. +func (wasmRequest) GetSourceAddr() string { + return mem.GetString(imports.GetSourceAddr) +}