2015-02-11 01:25:40 +00:00
|
|
|
package handlers
|
2014-11-11 02:57:38 +00:00
|
|
|
|
|
|
|
import (
|
2017-08-11 22:31:16 +00:00
|
|
|
"context"
|
2015-07-30 01:18:50 +00:00
|
|
|
"errors"
|
2020-08-19 09:07:38 +00:00
|
|
|
"fmt"
|
2014-11-21 03:57:01 +00:00
|
|
|
"io"
|
2014-11-11 02:57:38 +00:00
|
|
|
"net/http"
|
2020-08-19 09:07:38 +00:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
2015-07-30 01:18:50 +00:00
|
|
|
|
2020-08-24 11:18:39 +00:00
|
|
|
dcontext "github.com/distribution/distribution/v3/context"
|
2014-11-11 02:57:38 +00:00
|
|
|
)
|
|
|
|
|
2014-11-21 03:57:01 +00:00
|
|
|
// 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)
|
|
|
|
})
|
|
|
|
}
|
2015-07-30 01:18:50 +00:00
|
|
|
|
2016-06-02 05:31:13 +00:00
|
|
|
// copyFullPayload copies the payload of an HTTP request to destWriter. If it
|
2015-07-30 01:18:50 +00:00
|
|
|
// receives less content than expected, and the client disconnected during the
|
|
|
|
// upload, it avoids sending a 400 error to keep the logs cleaner.
|
2017-07-06 23:01:26 +00:00
|
|
|
//
|
|
|
|
// The copy will be limited to `limit` bytes, if limit is greater than zero.
|
2017-08-11 22:31:16 +00:00
|
|
|
func copyFullPayload(ctx context.Context, responseWriter http.ResponseWriter, r *http.Request, destWriter io.Writer, limit int64, action string) error {
|
2015-07-30 01:18:50 +00:00
|
|
|
// Get a channel that tells us if the client disconnects
|
2018-09-11 21:19:08 +00:00
|
|
|
clientClosed := r.Context().Done()
|
2022-11-02 21:05:45 +00:00
|
|
|
body := r.Body
|
2017-07-06 23:01:26 +00:00
|
|
|
if limit > 0 {
|
|
|
|
body = http.MaxBytesReader(responseWriter, body, limit)
|
|
|
|
}
|
|
|
|
|
2015-07-30 01:18:50 +00:00
|
|
|
// Read in the data, if any.
|
2017-07-06 23:01:26 +00:00
|
|
|
copied, err := io.Copy(destWriter, body)
|
2015-07-30 01:18:50 +00:00
|
|
|
if clientClosed != nil && (err != nil || (r.ContentLength > 0 && copied < r.ContentLength)) {
|
2016-02-11 00:26:29 +00:00
|
|
|
// Didn't receive as much content as expected. Did the client
|
2015-07-30 01:18:50 +00:00
|
|
|
// disconnect during the request? If so, avoid returning a 400
|
|
|
|
// error to keep the logs cleaner.
|
|
|
|
select {
|
|
|
|
case <-clientClosed:
|
2015-08-01 00:39:30 +00:00
|
|
|
// 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)
|
|
|
|
|
2017-08-11 22:31:16 +00:00
|
|
|
dcontext.GetLoggerWithFields(ctx, map[interface{}]interface{}{
|
2016-04-05 00:18:09 +00:00
|
|
|
"error": err,
|
|
|
|
"copied": copied,
|
|
|
|
"contentLength": r.ContentLength,
|
|
|
|
}, "error", "copied", "contentLength").Error("client disconnected during " + action)
|
2015-07-30 01:18:50 +00:00
|
|
|
return errors.New("client disconnected")
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
2017-08-11 22:31:16 +00:00
|
|
|
dcontext.GetLogger(ctx).Errorf("unknown error reading request payload: %v", err)
|
2015-07-30 01:18:50 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2020-08-19 09:07:38 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|