2015-02-10 17:25:40 -08:00
|
|
|
package handlers
|
2014-11-10 18:57:38 -08:00
|
|
|
|
|
|
|
import (
|
2017-08-11 15:31:16 -07:00
|
|
|
"context"
|
2015-07-29 18:18:50 -07:00
|
|
|
"errors"
|
2020-08-19 17:07:38 +08:00
|
|
|
"fmt"
|
2014-11-20 19:57:01 -08:00
|
|
|
"io"
|
2014-11-10 18:57:38 -08:00
|
|
|
"net/http"
|
2020-08-19 17:07:38 +08:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
2015-07-29 18:18:50 -07:00
|
|
|
|
2023-10-24 13:16:58 -04:00
|
|
|
"github.com/distribution/distribution/v3/internal/dcontext"
|
2014-11-10 18:57:38 -08:00
|
|
|
)
|
|
|
|
|
2014-11-20 19:57:01 -08: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-29 18:18:50 -07:00
|
|
|
|
2016-06-02 13:31:13 +08:00
|
|
|
// copyFullPayload copies the payload of an HTTP request to destWriter. If it
|
2015-07-29 18:18:50 -07: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 16:01:26 -07:00
|
|
|
//
|
|
|
|
// The copy will be limited to `limit` bytes, if limit is greater than zero.
|
2017-08-11 15:31:16 -07:00
|
|
|
func copyFullPayload(ctx context.Context, responseWriter http.ResponseWriter, r *http.Request, destWriter io.Writer, limit int64, action string) error {
|
2015-07-29 18:18:50 -07:00
|
|
|
// Get a channel that tells us if the client disconnects
|
2018-09-11 14:19:08 -07:00
|
|
|
clientClosed := r.Context().Done()
|
2022-11-02 22:05:45 +01:00
|
|
|
body := r.Body
|
2017-07-06 16:01:26 -07:00
|
|
|
if limit > 0 {
|
|
|
|
body = http.MaxBytesReader(responseWriter, body, limit)
|
|
|
|
}
|
|
|
|
|
2015-07-29 18:18:50 -07:00
|
|
|
// Read in the data, if any.
|
2017-07-06 16:01:26 -07:00
|
|
|
copied, err := io.Copy(destWriter, body)
|
2015-07-29 18:18:50 -07:00
|
|
|
if clientClosed != nil && (err != nil || (r.ContentLength > 0 && copied < r.ContentLength)) {
|
2016-02-10 16:26:29 -08:00
|
|
|
// Didn't receive as much content as expected. Did the client
|
2015-07-29 18:18:50 -07:00
|
|
|
// disconnect during the request? If so, avoid returning a 400
|
|
|
|
// error to keep the logs cleaner.
|
|
|
|
select {
|
|
|
|
case <-clientClosed:
|
2015-07-31 17:39:30 -07: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 15:31:16 -07:00
|
|
|
dcontext.GetLoggerWithFields(ctx, map[interface{}]interface{}{
|
2016-04-04 17:18:09 -07:00
|
|
|
"error": err,
|
|
|
|
"copied": copied,
|
|
|
|
"contentLength": r.ContentLength,
|
|
|
|
}, "error", "copied", "contentLength").Error("client disconnected during " + action)
|
2015-07-29 18:18:50 -07:00
|
|
|
return errors.New("client disconnected")
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
2017-08-11 15:31:16 -07:00
|
|
|
dcontext.GetLogger(ctx).Errorf("unknown error reading request payload: %v", err)
|
2015-07-29 18:18:50 -07:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2020-08-19 17:07:38 +08:00
|
|
|
|
replace strings.Split(N) for strings.Cut() or alternatives
Go 1.18 and up now provides a strings.Cut() which is better suited for
splitting key/value pairs (and similar constructs), and performs better:
```go
func BenchmarkSplit(b *testing.B) {
b.ReportAllocs()
data := []string{"12hello=world", "12hello=", "12=hello", "12hello"}
for i := 0; i < b.N; i++ {
for _, s := range data {
_ = strings.SplitN(s, "=", 2)[0]
}
}
}
func BenchmarkCut(b *testing.B) {
b.ReportAllocs()
data := []string{"12hello=world", "12hello=", "12=hello", "12hello"}
for i := 0; i < b.N; i++ {
for _, s := range data {
_, _, _ = strings.Cut(s, "=")
}
}
}
```
BenchmarkSplit
BenchmarkSplit-10 8244206 128.0 ns/op 128 B/op 4 allocs/op
BenchmarkCut
BenchmarkCut-10 54411998 21.80 ns/op 0 B/op 0 allocs/op
While looking at occurrences of `strings.Split()`, I also updated some for alternatives,
or added some constraints;
- for cases where an specific number of items is expected, I used `strings.SplitN()`
with a suitable limit. This prevents (theoretical) unlimited splits.
- in some cases it we were using `strings.Split()`, but _actually_ were trying to match
a prefix; for those I replaced the code to just match (and/or strip) the prefix.
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-11-02 20:32:03 +01:00
|
|
|
func parseContentRange(cr string) (start int64, end int64, err error) {
|
|
|
|
rStart, rEnd, ok := strings.Cut(cr, "-")
|
|
|
|
if !ok {
|
2020-08-19 17:07:38 +08:00
|
|
|
return -1, -1, fmt.Errorf("invalid content range format, %s", cr)
|
|
|
|
}
|
replace strings.Split(N) for strings.Cut() or alternatives
Go 1.18 and up now provides a strings.Cut() which is better suited for
splitting key/value pairs (and similar constructs), and performs better:
```go
func BenchmarkSplit(b *testing.B) {
b.ReportAllocs()
data := []string{"12hello=world", "12hello=", "12=hello", "12hello"}
for i := 0; i < b.N; i++ {
for _, s := range data {
_ = strings.SplitN(s, "=", 2)[0]
}
}
}
func BenchmarkCut(b *testing.B) {
b.ReportAllocs()
data := []string{"12hello=world", "12hello=", "12=hello", "12hello"}
for i := 0; i < b.N; i++ {
for _, s := range data {
_, _, _ = strings.Cut(s, "=")
}
}
}
```
BenchmarkSplit
BenchmarkSplit-10 8244206 128.0 ns/op 128 B/op 4 allocs/op
BenchmarkCut
BenchmarkCut-10 54411998 21.80 ns/op 0 B/op 0 allocs/op
While looking at occurrences of `strings.Split()`, I also updated some for alternatives,
or added some constraints;
- for cases where an specific number of items is expected, I used `strings.SplitN()`
with a suitable limit. This prevents (theoretical) unlimited splits.
- in some cases it we were using `strings.Split()`, but _actually_ were trying to match
a prefix; for those I replaced the code to just match (and/or strip) the prefix.
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-11-02 20:32:03 +01:00
|
|
|
start, err = strconv.ParseInt(rStart, 10, 64)
|
2020-08-19 17:07:38 +08:00
|
|
|
if err != nil {
|
|
|
|
return -1, -1, err
|
|
|
|
}
|
replace strings.Split(N) for strings.Cut() or alternatives
Go 1.18 and up now provides a strings.Cut() which is better suited for
splitting key/value pairs (and similar constructs), and performs better:
```go
func BenchmarkSplit(b *testing.B) {
b.ReportAllocs()
data := []string{"12hello=world", "12hello=", "12=hello", "12hello"}
for i := 0; i < b.N; i++ {
for _, s := range data {
_ = strings.SplitN(s, "=", 2)[0]
}
}
}
func BenchmarkCut(b *testing.B) {
b.ReportAllocs()
data := []string{"12hello=world", "12hello=", "12=hello", "12hello"}
for i := 0; i < b.N; i++ {
for _, s := range data {
_, _, _ = strings.Cut(s, "=")
}
}
}
```
BenchmarkSplit
BenchmarkSplit-10 8244206 128.0 ns/op 128 B/op 4 allocs/op
BenchmarkCut
BenchmarkCut-10 54411998 21.80 ns/op 0 B/op 0 allocs/op
While looking at occurrences of `strings.Split()`, I also updated some for alternatives,
or added some constraints;
- for cases where an specific number of items is expected, I used `strings.SplitN()`
with a suitable limit. This prevents (theoretical) unlimited splits.
- in some cases it we were using `strings.Split()`, but _actually_ were trying to match
a prefix; for those I replaced the code to just match (and/or strip) the prefix.
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-11-02 20:32:03 +01:00
|
|
|
end, err = strconv.ParseInt(rEnd, 10, 64)
|
2020-08-19 17:07:38 +08:00
|
|
|
if err != nil {
|
|
|
|
return -1, -1, err
|
|
|
|
}
|
|
|
|
return start, end, nil
|
|
|
|
}
|