Skip to content

Commit

Permalink
drain body when put failed
Browse files Browse the repository at this point in the history
Signed-off-by: jkoberg <jkoberg@owncloud.com>
  • Loading branch information
kobergj committed Jan 24, 2023
1 parent a6fe69c commit fa4fd8a
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 29 deletions.
5 changes: 5 additions & 0 deletions changelog/unreleased/drain-body-on-put.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Bugfix: Drain body on failed put

When a put request fails the server would not drain the body. This will lead to `connection closed` errors on clients when using http 1

https://github.com/cs3org/reva/pull/3618
66 changes: 37 additions & 29 deletions pkg/rhttp/datatx/manager/spaces/spaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package spaces

import (
"io"
"net/http"
"path"
"strings"
Expand Down Expand Up @@ -113,38 +114,45 @@ func (m *manager) Handler(fs storage.FS) (http.Handler, error) {
sublog.Error().Err(err).Msg("failed to publish FileUploaded event")
}
})
switch v := err.(type) {
case nil:
// set etag, mtime and file id
w.Header().Set(net.HeaderETag, info.Etag)
w.Header().Set(net.HeaderOCETag, info.Etag)
if info.Id != nil {
w.Header().Set(net.HeaderOCFileID, storagespace.FormatResourceID(*info.Id))
if err != nil {
if r.ProtoMajor == 1 {
// drain body to avoid `connection closed` errors
_, _ = io.Copy(io.Discard, r.Body)
}
if info.Mtime != nil {
t := utils.TSToTime(info.Mtime).UTC()
lastModifiedString := t.Format(time.RFC1123Z)
w.Header().Set(net.HeaderLastModified, lastModifiedString)

switch v := err.(type) {
case errtypes.PartialContent:
w.WriteHeader(http.StatusPartialContent)
case errtypes.ChecksumMismatch:
w.WriteHeader(errtypes.StatusChecksumMismatch)
case errtypes.NotFound:
w.WriteHeader(http.StatusNotFound)
case errtypes.PermissionDenied:
w.WriteHeader(http.StatusForbidden)
case errtypes.InvalidCredentials:
w.WriteHeader(http.StatusUnauthorized)
case errtypes.InsufficientStorage:
w.WriteHeader(http.StatusInsufficientStorage)
case errtypes.PreconditionFailed, errtypes.Aborted:
w.WriteHeader(http.StatusPreconditionFailed)
default:
sublog.Error().Err(v).Msg("error uploading file")
w.WriteHeader(http.StatusInternalServerError)
}
w.WriteHeader(http.StatusOK)
case errtypes.PartialContent:
w.WriteHeader(http.StatusPartialContent)
case errtypes.ChecksumMismatch:
w.WriteHeader(errtypes.StatusChecksumMismatch)
case errtypes.NotFound:
w.WriteHeader(http.StatusNotFound)
case errtypes.PermissionDenied:
w.WriteHeader(http.StatusForbidden)
case errtypes.InvalidCredentials:
w.WriteHeader(http.StatusUnauthorized)
case errtypes.InsufficientStorage:
w.WriteHeader(http.StatusInsufficientStorage)
case errtypes.PreconditionFailed, errtypes.Aborted:
w.WriteHeader(http.StatusPreconditionFailed)
default:
sublog.Error().Err(v).Msg("error uploading file")
w.WriteHeader(http.StatusInternalServerError)
return
}
// set etag, mtime and file id
w.Header().Set(net.HeaderETag, info.Etag)
w.Header().Set(net.HeaderOCETag, info.Etag)
if info.Id != nil {
w.Header().Set(net.HeaderOCFileID, storagespace.FormatResourceID(*info.Id))
}
if info.Mtime != nil {
t := utils.TSToTime(info.Mtime).UTC()
lastModifiedString := t.Format(time.RFC1123Z)
w.Header().Set(net.HeaderLastModified, lastModifiedString)
}
w.WriteHeader(http.StatusOK)
return
default:
w.WriteHeader(http.StatusNotImplemented)
Expand Down

0 comments on commit fa4fd8a

Please sign in to comment.