Skip to content

Commit d1cf8d3

Browse files
committed
Ensure request body is closed
1 parent 60ca3d0 commit d1cf8d3

File tree

3 files changed

+13
-8
lines changed

3 files changed

+13
-8
lines changed

modules/lfs/http_client.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ func (c *HTTPClient) batch(ctx context.Context, operation string, objects []Poin
105105
defer res.Body.Close()
106106

107107
if res.StatusCode != http.StatusOK {
108-
return nil, fmt.Errorf("Unexpected server response: %s", res.Status)
108+
return nil, fmt.Errorf("unexpected server response: %s", res.Status)
109109
}
110110

111111
var response BatchResponse
@@ -177,7 +177,7 @@ func (c *HTTPClient) performOperation(ctx context.Context, objects []Pointer, dc
177177
link, ok := object.Actions["upload"]
178178
if !ok {
179179
log.Debug("%+v", object)
180-
return errors.New("Missing action 'upload'")
180+
return errors.New("missing action 'upload'")
181181
}
182182

183183
content, err := uc(object.Pointer, nil)
@@ -203,7 +203,7 @@ func (c *HTTPClient) performOperation(ctx context.Context, objects []Pointer, dc
203203
link, ok := object.Actions["download"]
204204
if !ok {
205205
log.Debug("%+v", object)
206-
return errors.New("Missing action 'download'")
206+
return errors.New("missing action 'download'")
207207
}
208208

209209
content, err := transferAdapter.Download(ctx, link)

modules/lfs/pointer.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@ const (
2929

3030
var (
3131
// ErrMissingPrefix occurs if the content lacks the LFS prefix
32-
ErrMissingPrefix = errors.New("Content lacks the LFS prefix")
32+
ErrMissingPrefix = errors.New("content lacks the LFS prefix")
3333

3434
// ErrInvalidStructure occurs if the content has an invalid structure
35-
ErrInvalidStructure = errors.New("Content has an invalid structure")
35+
ErrInvalidStructure = errors.New("content has an invalid structure")
3636

3737
// ErrInvalidOIDFormat occurs if the oid has an invalid format
3838
ErrInvalidOIDFormat = errors.New("OID has an invalid format")

modules/lfs/transferadapter.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ func (a *BasicTransferAdapter) Download(ctx context.Context, l *Link) (io.ReadCl
4444

4545
// Upload sends the content to the LFS server
4646
func (a *BasicTransferAdapter) Upload(ctx context.Context, l *Link, p Pointer, r io.Reader) error {
47-
_, err := a.performRequest(ctx, "PUT", l, r, func(req *http.Request) {
47+
res, err := a.performRequest(ctx, "PUT", l, r, func(req *http.Request) {
4848
if len(req.Header.Get("Content-Type")) == 0 {
4949
req.Header.Set("Content-Type", "application/octet-stream")
5050
}
@@ -58,6 +58,7 @@ func (a *BasicTransferAdapter) Upload(ctx context.Context, l *Link, p Pointer, r
5858
if err != nil {
5959
return err
6060
}
61+
defer res.Body.Close()
6162
return nil
6263
}
6364

@@ -69,15 +70,19 @@ func (a *BasicTransferAdapter) Verify(ctx context.Context, l *Link, p Pointer) e
6970
return err
7071
}
7172

72-
_, err = a.performRequest(ctx, "POST", l, bytes.NewReader(b), func(req *http.Request) {
73+
res, err := a.performRequest(ctx, "POST", l, bytes.NewReader(b), func(req *http.Request) {
7374
req.Header.Set("Content-Type", MediaType)
7475
})
7576
if err != nil {
7677
return err
7778
}
79+
defer res.Body.Close()
7880
return nil
7981
}
8082

83+
// performRequest sends a request, optionally performs a callback on the request and returns the response.
84+
// If the status code is 200, the response is returned, and it will contain a non-nil Body.
85+
// Otherwise, it will return an error, and the Body will be nil or closed.
8186
func (a *BasicTransferAdapter) performRequest(ctx context.Context, method string, l *Link, body io.Reader, callback func(*http.Request)) (*http.Response, error) {
8287
log.Trace("Calling: %s %s", method, l.Href)
8388

@@ -118,7 +123,7 @@ func handleErrorResponse(resp *http.Response) error {
118123

119124
er, err := decodeResponseError(resp.Body)
120125
if err != nil {
121-
return fmt.Errorf("Request failed with status %s", resp.Status)
126+
return fmt.Errorf("request failed with status %s", resp.Status)
122127
}
123128
log.Trace("ErrorRespone: %v", er)
124129
return errors.New(er.Message)

0 commit comments

Comments
 (0)