forked from TrueCloudLab/distribution
e0281dc609
gofumpt (https://github.com/mvdan/gofumpt) provides a supserset of `gofmt` / `go fmt`, and addresses various formatting issues that linters may be checking for. We can consider enabling the `gofumpt` linter to verify the formatting in CI, although not every developer may have it installed, so for now this runs it once to get formatting in shape. Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
86 lines
2.5 KiB
Go
86 lines
2.5 KiB
Go
package handlers
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
"strconv"
|
|
"strings"
|
|
|
|
dcontext "github.com/distribution/distribution/v3/context"
|
|
)
|
|
|
|
// closeResources closes all the provided resources after running the target
|
|
// handler.
|
|
func closeResources(handler http.Handler, closers ...io.Closer) http.Handler {
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
for _, closer := range closers {
|
|
defer closer.Close()
|
|
}
|
|
handler.ServeHTTP(w, r)
|
|
})
|
|
}
|
|
|
|
// copyFullPayload copies the payload of an HTTP request to destWriter. If it
|
|
// receives less content than expected, and the client disconnected during the
|
|
// upload, it avoids sending a 400 error to keep the logs cleaner.
|
|
//
|
|
// The copy will be limited to `limit` bytes, if limit is greater than zero.
|
|
func copyFullPayload(ctx context.Context, responseWriter http.ResponseWriter, r *http.Request, destWriter io.Writer, limit int64, action string) error {
|
|
// Get a channel that tells us if the client disconnects
|
|
clientClosed := r.Context().Done()
|
|
body := r.Body
|
|
if limit > 0 {
|
|
body = http.MaxBytesReader(responseWriter, body, limit)
|
|
}
|
|
|
|
// Read in the data, if any.
|
|
copied, err := io.Copy(destWriter, body)
|
|
if clientClosed != nil && (err != nil || (r.ContentLength > 0 && copied < r.ContentLength)) {
|
|
// Didn't receive as much content as expected. Did the client
|
|
// disconnect during the request? If so, avoid returning a 400
|
|
// error to keep the logs cleaner.
|
|
select {
|
|
case <-clientClosed:
|
|
// Set the response code to "499 Client Closed Request"
|
|
// Even though the connection has already been closed,
|
|
// this causes the logger to pick up a 499 error
|
|
// instead of showing 0 for the HTTP status.
|
|
responseWriter.WriteHeader(499)
|
|
|
|
dcontext.GetLoggerWithFields(ctx, map[interface{}]interface{}{
|
|
"error": err,
|
|
"copied": copied,
|
|
"contentLength": r.ContentLength,
|
|
}, "error", "copied", "contentLength").Error("client disconnected during " + action)
|
|
return errors.New("client disconnected")
|
|
default:
|
|
}
|
|
}
|
|
|
|
if err != nil {
|
|
dcontext.GetLogger(ctx).Errorf("unknown error reading request payload: %v", err)
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func parseContentRange(cr string) (int64, int64, error) {
|
|
ranges := strings.Split(cr, "-")
|
|
if len(ranges) != 2 {
|
|
return -1, -1, fmt.Errorf("invalid content range format, %s", cr)
|
|
}
|
|
start, err := strconv.ParseInt(ranges[0], 10, 64)
|
|
if err != nil {
|
|
return -1, -1, err
|
|
}
|
|
end, err := strconv.ParseInt(ranges[1], 10, 64)
|
|
if err != nil {
|
|
return -1, -1, err
|
|
}
|
|
|
|
return start, end, nil
|
|
}
|