Skip to content

Commit

Permalink
all: close http response body (bluesky-social#44)
Browse files Browse the repository at this point in the history
Per official documentation, the client must close the response body when
finished with it.

Refs: https://pkg.go.dev/net/http#pkg-overview
  • Loading branch information
whyrusleeping authored Mar 6, 2023
2 parents 89b4778 + c4e2f86 commit 76afa11
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 0 deletions.
4 changes: 4 additions & 0 deletions api/plc.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ func (s *PLCServer) GetDocument(ctx context.Context, didstr string) (*did.Docume
return nil, err
}

defer resp.Body.Close()

if resp.StatusCode != 200 {
return nil, fmt.Errorf("get did request failed (code %d): %s", resp.StatusCode, resp.Status)
}
Expand Down Expand Up @@ -109,6 +111,8 @@ func (s *PLCServer) CreateDID(ctx context.Context, sigkey *did.PrivKey, recovery
return "", err
}

defer resp.Body.Close()

if resp.StatusCode != 200 {
b, _ := ioutil.ReadAll(resp.Body)
fmt.Println(string(b))
Expand Down
3 changes: 3 additions & 0 deletions cmd/beemo/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,9 @@ func sendSlackMsg(cctx *cli.Context, msg string) error {
if err != nil {
return err
}

defer resp.Body.Close()

buf := new(bytes.Buffer)
buf.ReadFrom(resp.Body)
if resp.StatusCode != 200 || buf.String() != "ok" {
Expand Down
2 changes: 2 additions & 0 deletions testing/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,8 @@ func (tp *testPDS) RequestScraping(t *testing.T, b *testBGS) {
t.Fatal(err)
}

defer resp.Body.Close()

if resp.StatusCode != 200 {
t.Fatal("invalid response from bgs", resp.StatusCode)
}
Expand Down
2 changes: 2 additions & 0 deletions xrpc/xrpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,8 @@ func (c *Client) Do(ctx context.Context, kind XRPCRequestType, inpenc string, me
return fmt.Errorf("request failed: %w", err)
}

defer resp.Body.Close()

if resp.StatusCode != 200 {
var i interface{}
_ = json.NewDecoder(resp.Body).Decode(&i)
Expand Down

0 comments on commit 76afa11

Please sign in to comment.