@@ -35,7 +35,16 @@ var DefaultClient = &http.Client{Transport: DefaultTransport}
3535
3636func (r * Transport ) requestOptions () types.RequestOptions {
3737 options := types .NewRequestOptions ()
38- options .SetConnectTimeout (cm .Some (monotonicclock .Duration (r .ConnectTimeout )))
38+ if r .ConnectTimeout > 0 {
39+ // Go’s time.Duration is a nanosecond count, and WASI’s monotonicclock.Duration is also a u64 of nanoseconds
40+ options .SetConnectTimeout (
41+ cm .Some (monotonicclock .Duration (r .ConnectTimeout )),
42+ )
43+ } else {
44+ options .SetConnectTimeout (
45+ cm .None [monotonicclock.Duration ](),
46+ )
47+ }
3948 return options
4049}
4150
@@ -68,49 +77,47 @@ func (r *Transport) RoundTrip(incomingRequest *http.Request) (*http.Response, er
6877 outRequest .SetScheme (cm .Some (types .SchemeOther (incomingRequest .URL .Scheme )))
6978 }
7079
71- var adaptedBody io.WriteCloser
72- var body * types.OutgoingBody
73- if incomingRequest .Body != nil {
74- bodyRes := outRequest .Body ()
75- if bodyRes .IsErr () {
76- return nil , fmt .Errorf ("failed to acquire resource handle to request body: %s" , bodyRes .Err ())
77- }
78- body = bodyRes .OK ()
79- adaptedBody , err = NewOutgoingBody (body )
80- if err != nil {
81- return nil , fmt .Errorf ("failed to adapt body: %s" , err )
82- }
80+ bodyRes := outRequest .Body ()
81+ if bodyRes .IsErr () {
82+ return nil , fmt .Errorf ("failed to acquire resource handle to request body: %s" , bodyRes .Err ())
8383 }
84+ body := bodyRes .OK ()
8485
8586 handleResp := outgoinghandler .Handle (outRequest , cm .Some (r .requestOptions ()))
8687 if handleResp .Err () != nil {
8788 return nil , fmt .Errorf ("%v" , handleResp .Err ())
8889 }
8990
91+ maybeTrailers := cm .None [types.Fields ]()
92+ if len (incomingRequest .Trailer ) > 0 {
93+ outTrailers := types .NewFields ()
94+ if err := HTTPtoWASIHeader (incomingRequest .Trailer , outTrailers ); err != nil {
95+ return nil , err
96+ }
97+ maybeTrailers = cm .Some (outTrailers )
98+ }
99+
90100 // NOTE(lxf): If request includes a body, copy it to the adapted wasi body
91- if body != nil {
101+ if incomingRequest .Body != nil {
102+ adaptedBody , err := NewOutgoingBody (body )
103+ if err != nil {
104+ return nil , fmt .Errorf ("failed to adapt body: %s" , err )
105+ }
92106 if _ , err := io .Copy (adaptedBody , incomingRequest .Body ); err != nil {
93107 return nil , fmt .Errorf ("failed to copy body: %v" , err )
94108 }
95109
96110 if err := adaptedBody .Close (); err != nil {
97111 return nil , fmt .Errorf ("failed to close body: %v" , err )
98112 }
113+ }
99114
100- outTrailers := types .NewFields ()
101- if err := HTTPtoWASIHeader (incomingRequest .Trailer , outTrailers ); err != nil {
102- return nil , err
103- }
104-
105- maybeTrailers := cm .None [types.Fields ]()
106- if len (incomingRequest .Trailer ) > 0 {
107- maybeTrailers = cm .Some (outTrailers )
108- }
109-
110- outFinish := types .OutgoingBodyFinish (* body , maybeTrailers )
111- if outFinish .IsErr () {
112- return nil , fmt .Errorf ("failed to finish body: %v" , outFinish .Err ())
113- }
115+ // From `outgoing-body` documentation:
116+ // Finalize an outgoing body, optionally providing trailers. This must be
117+ // called to signal that the response is complete.
118+ outFinish := types .OutgoingBodyFinish (* body , maybeTrailers )
119+ if outFinish .IsErr () {
120+ return nil , fmt .Errorf ("failed to finish body: %v" , outFinish .Err ())
114121 }
115122
116123 // NOTE(lxf): Request is fully sent. Processing response.
0 commit comments