2015-02-11 01:25:40 +00:00
|
|
|
package handlers
|
2015-01-06 18:37:27 +00:00
|
|
|
|
|
|
|
import (
|
2015-07-30 01:18:50 +00:00
|
|
|
"errors"
|
2015-01-06 18:37:27 +00:00
|
|
|
"io"
|
|
|
|
"net/http"
|
2015-07-30 01:18:50 +00:00
|
|
|
|
|
|
|
ctxu "github.com/docker/distribution/context"
|
|
|
|
"github.com/docker/distribution/registry/api/errcode"
|
2015-01-06 18:37:27 +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
|
|
|
|
|
|
|
// copyFullPayload copies the payload of a 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.
|
|
|
|
func copyFullPayload(responseWriter http.ResponseWriter, r *http.Request, destWriter io.Writer, context ctxu.Context, action string, errSlice *errcode.Errors) error {
|
|
|
|
// Get a channel that tells us if the client disconnects
|
|
|
|
var clientClosed <-chan bool
|
|
|
|
if notifier, ok := responseWriter.(http.CloseNotifier); ok {
|
|
|
|
clientClosed = notifier.CloseNotify()
|
|
|
|
} else {
|
2015-08-18 22:40:14 +00:00
|
|
|
ctxu.GetLogger(context).Warnf("the ResponseWriter does not implement CloseNotifier (type: %T)", responseWriter)
|
2015-07-30 01:18:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Read in the data, if any.
|
|
|
|
copied, err := io.Copy(destWriter, r.Body)
|
|
|
|
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)
|
|
|
|
|
2016-04-05 00:18:09 +00:00
|
|
|
ctxu.GetLoggerWithFields(context, map[interface{}]interface{}{
|
|
|
|
"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 {
|
|
|
|
ctxu.GetLogger(context).Errorf("unknown error reading request payload: %v", err)
|
|
|
|
*errSlice = append(*errSlice, errcode.ErrorCodeUnknown.WithDetail(err))
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|