Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add SourceAddr support #82

Merged
merged 4 commits into from
Oct 31, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions api/handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,9 @@ type Host interface {
// FuncRemoveHeader with HeaderKindResponseTrailers. This panics if
// FeatureTrailers is not supported.
RemoveResponseTrailer(ctx context.Context, name string)

// GetSourceAddr supports the WebAssembly function export FuncGetSourceAddr.
GetSourceAddr(ctx context.Context) string
}

// eofReader is safer than reading from os.DevNull as it can never overrun
Expand Down Expand Up @@ -206,3 +209,4 @@ func (UnimplementedHost) GetResponseTrailerValues(context.Context, string) (valu
func (UnimplementedHost) SetResponseTrailerValue(context.Context, string, string) {}
func (UnimplementedHost) AddResponseTrailerValue(context.Context, string, string) {}
func (UnimplementedHost) RemoveResponseTrailer(context.Context, string) {}
func (UnimplementedHost) GetSourceAddr(context.Context) string { return "1.1.1.1:12345" }
6 changes: 6 additions & 0 deletions api/handler/wasm.go
Original file line number Diff line number Diff line change
Expand Up @@ -316,4 +316,10 @@ const (
//
// TODO: document on http-wasm-abi
FuncSetStatusCode = "set_status_code"

// FuncGetSourceAddr writes the SourceAddr to memory if it isn't larger than BufLimit.
// The result is its length in bytes. Ex. "1.1.1.1:12345" or "[fe80::101e:2bdf:8bfb:b97e]:12345"
//
// TODO: document on http-wasm-abi
FuncGetSourceAddr = "get_source_addr"
)
14 changes: 14 additions & 0 deletions handler/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -565,6 +565,17 @@ func (m *middleware) writeBody(ctx context.Context, mod wazeroapi.Module, params
writeBody(mod, buf, bufLen, w)
}

// getSourceAddr implements the WebAssembly host function handler.FuncGetSourceAddr.
func (m *middleware) getSourceAddr(ctx context.Context, mod wazeroapi.Module, stack []uint64) {
buf := uint32(stack[0])
bufLimit := handler.BufLimit(stack[1])

method := m.host.GetSourceAddr(ctx)
methodLen := writeStringIfUnderLimit(mod.Memory(), buf, bufLimit, method)

stack[0] = uint64(methodLen)
}

func writeBody(mod wazeroapi.Module, buf, bufLen uint32, w io.Writer) {
// buf_len 0 means to overwrite with nothing
var b []byte
Expand Down Expand Up @@ -694,6 +705,9 @@ func (m *middleware) instantiateHost(ctx context.Context) (wazeroapi.Module, err
WithGoModuleFunction(wazeroapi.GoModuleFunc(m.writeBody), []wazeroapi.ValueType{i32, i32, i32}, []wazeroapi.ValueType{}).
WithParameterNames("kind", "body", "body_len").Export(handler.FuncWriteBody).
NewFunctionBuilder().
WithGoModuleFunction(wazeroapi.GoModuleFunc(m.getSourceAddr), []wazeroapi.ValueType{i32, i32}, []wazeroapi.ValueType{i32}).
WithParameterNames("buf", "buf_limit").Export(handler.FuncGetSourceAddr).
NewFunctionBuilder().
WithGoFunction(wazeroapi.GoFunc(m.getStatusCode), []wazeroapi.ValueType{}, []wazeroapi.ValueType{i32}).
WithParameterNames().Export(handler.FuncGetStatusCode).
NewFunctionBuilder().
Expand Down
6 changes: 6 additions & 0 deletions handler/nethttp/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -346,3 +346,9 @@ func addTrailer(header http.Header, name string, value string) {
func removeTrailer(header http.Header, name string) {
header.Del(http.TrailerPrefix + name)
}

// GetSourceAddr implements the same method as documented on handler.Host.
func (host) GetSourceAddr(ctx context.Context) string {
r := requestStateFromContext(ctx).r
return r.RemoteAddr
}