From 62a471dba463fbf7cfb2a2c0dec5b0aad27147a0 Mon Sep 17 00:00:00 2001 From: Roman Volosatovs Date: Tue, 24 Sep 2024 16:33:02 +0200 Subject: [PATCH] fix(go): ensure HTTP bodies are closed Signed-off-by: Roman Volosatovs --- examples/go/http/http_test.go | 3 +++ go.mod | 2 +- go.work | 2 +- tests/go/wasi/wasi_test.go | 3 +++ westhttp/http.go | 9 ++++----- 5 files changed, 12 insertions(+), 7 deletions(-) diff --git a/examples/go/http/http_test.go b/examples/go/http/http_test.go index ab22641..c2787ef 100644 --- a/examples/go/http/http_test.go +++ b/examples/go/http/http_test.go @@ -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) }) } diff --git a/go.mod b/go.mod index f0c661e..dbc58e6 100644 --- a/go.mod +++ b/go.mod @@ -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 => ./. diff --git a/go.work b/go.work index 6637829..2fd3057 100644 --- a/go.work +++ b/go.work @@ -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 => ./. diff --git a/tests/go/wasi/wasi_test.go b/tests/go/wasi/wasi_test.go index df5a941..472c4f8 100644 --- a/tests/go/wasi/wasi_test.go +++ b/tests/go/wasi/wasi_test.go @@ -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) }) } diff --git a/westhttp/http.go b/westhttp/http.go index e68ded1..5ed3d2c 100644 --- a/westhttp/http.go +++ b/westhttp/http.go @@ -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() @@ -151,7 +150,6 @@ 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()) } } @@ -159,8 +157,10 @@ func NewOutgoingRequest(req *http.Request) (types.OutgoingRequest, func(func(pol 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 { @@ -168,7 +168,6 @@ func NewOutgoingRequest(req *http.Request) (types.OutgoingRequest, func(func(pol 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()