66 "fmt"
77 "io"
88 "net"
9+ "strconv"
910 "sync"
1011
1112 "github.com/fastly/compute-sdk-go/internal/abi/fastly"
@@ -34,18 +35,50 @@ type Response struct {
3435 // Body of the response.
3536 Body io.ReadCloser
3637
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
38+ abi struct {
39+ resp * fastly.HTTPResponse
40+ }
4241}
4342
4443// Cookies parses and returns the cookies set in the Set-Cookie headers.
4544func (resp * Response ) Cookies () []* Cookie {
4645 return readSetCookies (resp .Header )
4746}
4847
48+ // RemoteAddr returns the address of the server that provided the response.
49+ func (resp * Response ) RemoteAddr () (net.Addr , error ) {
50+
51+ var addr netaddr
52+ var err error
53+
54+ addr .ip , err = resp .abi .resp .GetAddrDestIP ()
55+ if err != nil {
56+ return nil , fmt .Errorf ("get addr dest ip: %w" , err )
57+ }
58+
59+ addr .port , err = resp .abi .resp .GetAddrDestPort ()
60+ if err != nil {
61+ return nil , fmt .Errorf ("get addr dest port: %w" , err )
62+ }
63+
64+ return & addr , nil
65+ }
66+
67+ type netaddr struct {
68+ ip net.IP
69+ port uint16
70+ }
71+
72+ var _ net.Addr = (* netaddr )(nil )
73+
74+ func (n * netaddr ) Network () string {
75+ return "tcp"
76+ }
77+
78+ func (n * netaddr ) String () string {
79+ return net .JoinHostPort (n .ip .String (), strconv .Itoa (int (n .port )))
80+ }
81+
4982func newResponse (req * Request , backend string , abiResp * fastly.HTTPResponse , abiBody * fastly.HTTPBody ) (* Response , error ) {
5083 code , err := abiResp .GetStatusCode ()
5184 if err != nil {
@@ -69,24 +102,12 @@ func newResponse(req *Request, backend string, abiResp *fastly.HTTPResponse, abi
69102 return nil , fmt .Errorf ("read header keys: %w" , err )
70103 }
71104
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-
82105 return & Response {
83- Request : req ,
84- Backend : backend ,
85- StatusCode : code ,
86- Header : header ,
87- Body : abiBody ,
88- BackendAddrIP : addr ,
89- BackendAddrPort : port ,
106+ Request : req ,
107+ Backend : backend ,
108+ StatusCode : code ,
109+ Header : header ,
110+ Body : abiBody ,
90111 }, nil
91112}
92113
0 commit comments