Skip to content

Commit

Permalink
fix(go): ensure HTTP bodies are closed
Browse files Browse the repository at this point in the history
Signed-off-by: Roman Volosatovs <[email protected]>
  • Loading branch information
rvolosatovs committed Sep 24, 2024
1 parent b85e5cc commit 62a471d
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 7 deletions.
3 changes: 3 additions & 0 deletions examples/go/http/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ func TestIncomingHandler(t *testing.T) {
if err != nil {
t.Fatalf("failed to read HTTP response body: %s", err)
}
if err := resp.Body.Close(); err != nil {
t.Fatalf("failed to close response body: %s", err)
}
assert.Equal(t, []byte("hello world"), buf)
})
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ require (
gopkg.in/yaml.v3 v3.0.1 // indirect
)

replace github.com/wasmCloud/west => ./.
replace github.com/wasmCloud/west v0.2.0 => ./.
2 changes: 1 addition & 1 deletion go.work
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ go 1.23.0

use ./.

replace github.com/wasmCloud/west v0.0.1 => ./.
replace github.com/wasmCloud/west v0.2.0 => ./.
3 changes: 3 additions & 0 deletions tests/go/wasi/wasi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ func TestIncomingHandler(t *testing.T) {
if err != nil {
t.Fatalf("failed to read HTTP response body: %s", err)
}
if err := resp.Body.Close(); err != nil {
t.Fatalf("failed to close response body: %s", err)
}
assert.Equal(t, []byte("🧭🧭🧭🧭🧭foo bar baz"), buf)
})
}
9 changes: 4 additions & 5 deletions westhttp/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,6 @@ func NewOutgoingRequest(req *http.Request) (types.OutgoingRequest, func(func(pol
slog.Debug("write stream closed")
return io.EOF
}
slog.Debug("failed to check write buffer capacity")
return fmt.Errorf("failed to check write buffer capacity: %s", err.LastOperationFailed().ToDebugString())
}
wn := *checkWriteRes.OK()
Expand All @@ -151,24 +150,24 @@ func NewOutgoingRequest(req *http.Request) (types.OutgoingRequest, func(func(pol
slog.Debug("write stream closed")
return io.EOF
}
slog.Debug("failed to write to buffer to stream")
return fmt.Errorf("failed to write buffer: %s", err.LastOperationFailed().ToDebugString())
}
}
if err == nil {
continue
}
if err != io.EOF {
slog.Debug("failed to read buffer from body stream")
return err
return fmt.Errorf("failed read buffer from body stream: %w", err)
}
if err := req.Body.Close(); err != nil {
return fmt.Errorf("failed to close request body: %w", err)
}
flushRes := resStream.Flush()
if err := flushRes.Err(); err != nil {
if err.Closed() {
slog.Debug("write stream closed")
return io.EOF
}
slog.Debug("failed to flush body stream")
return fmt.Errorf("failed to flush body stream: %s", err.LastOperationFailed().ToDebugString())
}
resStream.ResourceDrop()
Expand Down

0 comments on commit 62a471d

Please sign in to comment.