Skip to content

Commit

Permalink
Add SourceAddr support (#32)
Browse files Browse the repository at this point in the history
* Add RemoteAddr support

* use SourceAddr
  • Loading branch information
zetaab authored Oct 31, 2023
1 parent 27f49b7 commit 487a6e2
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 0 deletions.
3 changes: 3 additions & 0 deletions handler/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions handler/internal/imports/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
3 changes: 3 additions & 0 deletions handler/internal/imports/imports.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
5 changes: 5 additions & 0 deletions handler/internal/imports/imports_stub.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
5 changes: 5 additions & 0 deletions handler/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

0 comments on commit 487a6e2

Please sign in to comment.