Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ linters:
- depguard
- err113 # disabled temporarily: there are just too many issues to address
- exhaustruct
- exhaustruct_v5
- funlen
- gochecknoglobals
- gochecknoinits
Expand Down
13 changes: 12 additions & 1 deletion multipart_stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,10 @@ func (s *MultipartFormStream) Drain() error {
// particular, a net/http server request body may discard a limited amount of
// unread data to allow connection reuse, so Close is not guaranteed to return
// immediately. Call Drain when trailing form fields must be collected.
//
// Close returns the error reported by the request body, except io.EOF: a
// net/http server body reports io.EOF once it discards that unread remainder,
// which tells us the body ended, not that closing it failed.
func (s *MultipartFormStream) Close() error {
if s == nil || s.closed {
return nil
Expand All @@ -357,7 +361,14 @@ func (s *MultipartFormStream) Close() error {
return nil
}

return s.request.Body.Close()
// Since go1.27.0, net/http (*body).Close returns io.EOF when it discards
// the unread remainder of the request itself. Reaching the end of the body
// is not a close failure.
if err := s.request.Body.Close(); err != nil && !stderrors.Is(err, io.EOF) {
return err
}

return nil
}

func supportsMultipartFormStream(method string) bool {
Expand Down
42 changes: 41 additions & 1 deletion multipart_stream_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,44 @@ func TestMultipartFormStreamDrainCollectsTrailingFieldsAndClosesBody(t *testing.
require.ErrorIs(t, err, io.EOF)
}

func TestMultipartFormStreamCloseIgnoresBodyCloseEOF(t *testing.T) {
// Since go1.27.0, a net/http server request body reports io.EOF from Close
// when it discards the unread remainder of the request. The stream must not
// pass that on as a failure.
body, contentType := orderedMultipartBody(t,
orderedFile{field: testFieldFile, filename: streamedFilename, content: "payload"},
orderedField{name: "after", value: "value"},
)
trackedBody := &observableReadCloser{Reader: body, closeErr: io.EOF}
request := httptest.NewRequestWithContext(t.Context(), http.MethodPost, testUploadPath, nil)
request.Body = trackedBody
request.Header.Set(HeaderContentType, contentType)
stream, err := NewMultipartFormStream(request)
require.NoError(t, err)

_, err = stream.NextFile()
require.NoError(t, err)
require.NoError(t, stream.Drain())
assert.EqualT(t, "value", request.Form.Get("after"))
assert.TrueT(t, trackedBody.Closed())
}

func TestMultipartFormStreamCloseReportsBodyCloseError(t *testing.T) {
body, contentType := orderedMultipartBody(t,
orderedFile{field: testFieldFile, filename: streamedFilename, content: "payload"},
)
closeErr := stderrors.New("close failed")
trackedBody := &observableReadCloser{Reader: body, closeErr: closeErr}
request := httptest.NewRequestWithContext(t.Context(), http.MethodPost, testUploadPath, nil)
request.Body = trackedBody
request.Header.Set(HeaderContentType, contentType)
stream, err := NewMultipartFormStream(request)
require.NoError(t, err)

require.ErrorIs(t, stream.Close(), closeErr)
assert.TrueT(t, trackedBody.Closed())
}

func TestMultipartFormStreamCloseAbortsWithoutDraining(t *testing.T) {
body, contentType := orderedMultipartBody(t,
orderedFile{field: testFieldFile, filename: streamedFilename, content: "payload"},
Expand Down Expand Up @@ -626,6 +664,8 @@ func orderedMultipartBody(t *testing.T, parts ...orderedMultipartPart) (*bytes.B
type observableReadCloser struct {
io.Reader

closeErr error

mu sync.Mutex
closed bool
}
Expand All @@ -635,7 +675,7 @@ func (r *observableReadCloser) Close() error {
defer r.mu.Unlock()
r.closed = true

return nil
return r.closeErr
}

func (r *observableReadCloser) Closed() bool {
Expand Down
Loading