Skip to content

Commit 82ce2a5

Browse files
committed
fsthttp, internal: add get_addr_dest_ip/port hostcalls
Fixes #121
1 parent 6c328bd commit 82ce2a5

File tree

3 files changed

+96
-5
lines changed

3 files changed

+96
-5
lines changed

fsthttp/response.go

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package fsthttp
55
import (
66
"fmt"
77
"io"
8+
"net"
89
"sync"
910

1011
"github.com/fastly/compute-sdk-go/internal/abi/fastly"
@@ -32,6 +33,12 @@ type Response struct {
3233

3334
// Body of the response.
3435
Body io.ReadCloser
36+
37+
// BackendAddrIP is the ip address of the server that sent the response.
38+
BackendAddrIP net.IP
39+
40+
// BackendAddrPort is the port of the server that sent the response.
41+
BackendAddrPort uint16
3542
}
3643

3744
// Cookies parses and returns the cookies set in the Set-Cookie headers.
@@ -62,12 +69,24 @@ func newResponse(req *Request, backend string, abiResp *fastly.HTTPResponse, abi
6269
return nil, fmt.Errorf("read header keys: %w", err)
6370
}
6471

72+
addr, err := abiResp.GetAddrDestIP()
73+
if err != nil {
74+
return nil, fmt.Errorf("get addr dest ip: %w", err)
75+
}
76+
77+
port, err := abiResp.GetAddrDestPort()
78+
if err != nil {
79+
return nil, fmt.Errorf("get addr dest port: %w", err)
80+
}
81+
6582
return &Response{
66-
Request: req,
67-
Backend: backend,
68-
StatusCode: code,
69-
Header: header,
70-
Body: abiBody,
83+
Request: req,
84+
Backend: backend,
85+
StatusCode: code,
86+
Header: header,
87+
Body: abiBody,
88+
BackendAddrIP: addr,
89+
BackendAddrPort: port,
7190
}, nil
7291
}
7392

internal/abi/fastly/hostcalls_noguest.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,14 @@ func (r *HTTPRequest) SetFramingHeadersMode(manual bool) error {
177177
return fmt.Errorf("not implemented")
178178
}
179179

180+
func (r *HTTPResponse) GetAddrDestIP() (net.IP, error) {
181+
return nil, fmt.Errorf("not implemented")
182+
}
183+
184+
func (r *HTTPResponse) GetAddrDestPort() (uint16, error) {
185+
return 0, fmt.Errorf("not implemented")
186+
}
187+
180188
func HandoffWebsocket(backend string) error {
181189
return fmt.Errorf("not implemented")
182190
}

internal/abi/fastly/http_guest.go

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1869,6 +1869,70 @@ func (r *HTTPResponse) SetFramingHeadersMode(manual bool) error {
18691869
).toError()
18701870
}
18711871

1872+
// witx:
1873+
//
1874+
// ;;; Hostcall for getting the destination IP used for this request.
1875+
// ;;;
1876+
// ;;; The buffer for the IP address must be 16 bytes.
1877+
// ;;; syntax used in URLs as specified in RFC 3986 section 3.
1878+
// (@interface func (export "get_addr_dest_ip")
1879+
// (param $h $response_handle)
1880+
// ;; must be a 16-byte array
1881+
// (param $addr_octets_out (@witx pointer (@witx char8)))
1882+
// (result $err (expected $num_bytes (error $fastly_status)))
1883+
// )
1884+
//
1885+
//go:wasmimport fastly_http_resp get_addr_dest_ip
1886+
//go:noescape
1887+
func fastlyHTTPRespGetAddrDestIP(
1888+
h responseHandle,
1889+
addr prim.Pointer[prim.Char8],
1890+
nWritten prim.Pointer[prim.Usize],
1891+
) FastlyStatus
1892+
1893+
// GetAddrDestIP
1894+
func (r *HTTPResponse) GetAddrDestIP() (net.IP, error) {
1895+
buf := prim.NewWriteBuffer(ipBufLen)
1896+
1897+
if err := fastlyHTTPRespGetAddrDestIP(
1898+
r.h,
1899+
prim.ToPointer(buf.Char8Pointer()),
1900+
prim.ToPointer(buf.NPointer()),
1901+
).toError(); err != nil {
1902+
return nil, err
1903+
}
1904+
1905+
return net.IP(buf.AsBytes()), nil
1906+
}
1907+
1908+
// witx:
1909+
//
1910+
// ;;; Hostcall for getting the destination port used for this request.
1911+
// (@interface func (export "get_addr_dest_port")
1912+
// (param $h $response_handle)
1913+
// (result $err (expected $port (error $fastly_status)))
1914+
// )
1915+
//
1916+
//go:wasmimport fastly_http_resp get_addr_dest_port
1917+
//go:noescape
1918+
func fastlyHTTPRespGetAddrDestPort(
1919+
h responseHandle,
1920+
port prim.Pointer[prim.U16],
1921+
) FastlyStatus
1922+
1923+
// GetAddrDestPort
1924+
func (r *HTTPResponse) GetAddrDestPort() (uint16, error) {
1925+
var port prim.U16
1926+
1927+
if err := fastlyHTTPRespGetAddrDestPort(
1928+
r.h, prim.ToPointer(&port),
1929+
).toError(); err != nil {
1930+
return 0, err
1931+
}
1932+
1933+
return uint16(port), nil
1934+
}
1935+
18721936
// witx:
18731937
//
18741938
// (module $fastly_uap

0 commit comments

Comments
 (0)