From efcf28507f88dd1a85b2c98cc507634da96a30a7 Mon Sep 17 00:00:00 2001 From: notanatol Date: Tue, 5 Mar 2024 09:39:13 -0600 Subject: [PATCH] fix: made content-length check non mandatory as per RFC --- pkg/api/dirs.go | 24 ++---------------------- pkg/api/dirs_test.go | 20 -------------------- 2 files changed, 2 insertions(+), 42 deletions(-) diff --git a/pkg/api/dirs.go b/pkg/api/dirs.go index 5a3ad1a0c4a..05431c61f01 100644 --- a/pkg/api/dirs.go +++ b/pkg/api/dirs.go @@ -91,10 +91,6 @@ func (s *Service) dirUploadHandler( jsonhttp.BadRequest(w, errEmptyDir) case errors.Is(err, tar.ErrHeader): jsonhttp.BadRequest(w, "invalid filename in tar archive") - case errors.Is(err, ErrMissingContentLenHeader): - jsonhttp.BadRequest(w, "missing Content-Length header") - case errors.Is(err, ErrMissingContentTypeHeader): - jsonhttp.BadRequest(w, "missing Content-Type header") default: jsonhttp.InternalServerError(w, errDirectoryStore) } @@ -270,11 +266,6 @@ type multipartReader struct { r *multipart.Reader } -var ( - ErrMissingContentLenHeader = errors.New("content-length missing") - ErrMissingContentTypeHeader = errors.New("content-type missing") -) - func (m *multipartReader) Next() (*FileInfo, error) { part, err := m.r.NextPart() if err != nil { @@ -285,25 +276,14 @@ func (m *multipartReader) Next() (*FileInfo, error) { if filePath == "" { filePath = part.FormName() } - if filePath == "" { - return nil, errors.New("filepath missing") - } fileName := filepath.Base(filePath) contentType := part.Header.Get(ContentTypeHeader) - if contentType == "" { - return nil, ErrMissingContentTypeHeader - } contentLength := part.Header.Get(ContentLengthHeader) - if contentLength == "" { - return nil, ErrMissingContentLenHeader - } - fileSize, err := strconv.ParseInt(contentLength, 10, 64) - if err != nil { - return nil, errors.New("invalid file size") - } + + fileSize, _ := strconv.ParseInt(contentLength, 10, 64) return &FileInfo{ Path: filePath, diff --git a/pkg/api/dirs_test.go b/pkg/api/dirs_test.go index e06f9f8e56d..078dc89dc00 100644 --- a/pkg/api/dirs_test.go +++ b/pkg/api/dirs_test.go @@ -93,26 +93,6 @@ func TestDirs(t *testing.T) { ) }) - t.Run("missing content type", func(t *testing.T) { - tarReader, mwBoundary := multipartFiles(t, []f{{ - data: []byte("robots text"), - name: "robots.txt", - // header left empty on purpose - }}) - - jsonhttptest.Request(t, client, http.MethodPost, dirUploadResource, http.StatusBadRequest, - jsonhttptest.WithRequestHeader(api.SwarmPostageBatchIdHeader, batchOkStr), - jsonhttptest.WithRequestBody(tarReader), - jsonhttptest.WithRequestHeader(api.SwarmCollectionHeader, "True"), - jsonhttptest.WithRequestHeader(api.ContentTypeHeader, fmt.Sprintf("multipart/form-data; boundary=%q", mwBoundary)), - jsonhttptest.WithExpectedJSONResponse(jsonhttp.StatusResponse{ - Message: "missing Content-Type header", - Code: http.StatusBadRequest, - }), - jsonhttptest.WithRequestHeader(api.ContentTypeHeader, "multipart/form-data"), - ) - }) - // valid tars for _, tc := range []struct { name string