Skip to content

Commit

Permalink
fix: made content-length check non mandatory as per RFC
Browse files Browse the repository at this point in the history
  • Loading branch information
notanatol committed Mar 5, 2024
1 parent 826ac1a commit efcf285
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 42 deletions.
24 changes: 2 additions & 22 deletions pkg/api/dirs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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 {
Expand All @@ -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,
Expand Down
20 changes: 0 additions & 20 deletions pkg/api/dirs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit efcf285

Please sign in to comment.