"rsync for cloud storage" - Google Drive, S3, Dropbox, Backblaze B2, One Drive, Swift, Hubic, Wasabi, Google Cloud Storage, Azure Blob, Azure Files, Yandex Files
Go azure-blobazure-blob-storageazure-filesbackblaze-b2cloud-storagedropboxencryptionftpfuse-filesystemgogolanggoogle-cloud-storagegoogle-driveonedriveopenstack-swiftrclones3sftpsyncwebdav
lib/http: return 416 for ranges --rc-serve and serve restic can't satisfy - fixes #10140
Rohit Beheraf17b89b0c23d
diff --git a/lib/http/serve/serve.go b/lib/http/serve/serve.goindex 244509b47..62bfa4632 100644--- a/lib/http/serve/serve.go+++ b/lib/http/serve/serve.go@@ -67,7 +67,9 @@ func Object(w http.ResponseWriter, r *http.Request, o fs.Object) {code := http.StatusOKsize := o.Size()var options []fs.OpenOption- if rangeRequest := r.Header.Get("Range"); rangeRequest != "" {+ // Like http.ServeContent, ignore ranges on empty objects as some+ // clients send a Range header with every request+ if rangeRequest := r.Header.Get("Range"); rangeRequest != "" && o.Size() != 0 {//fs.Debugf(nil, "Range: request %q", rangeRequest)option, err := fs.ParseRangeOption(rangeRequest)if err != nil {@@ -77,6 +79,15 @@ func Object(w http.ResponseWriter, r *http.Request, o fs.Object) {}options = append(options, option)offset, limit := option.Decode(o.Size())+ // RFC 9110 section 15.5.17: a range starting at or beyond the+ // end, or ending before it starts, can't be satisfied+ if (option.End >= 0 && option.Start > option.End) || (o.Size() > 0 && offset >= o.Size()) {+ if o.Size() > 0 {+ w.Header().Set("Content-Range", fmt.Sprintf("bytes */%d", o.Size()))+ }+ http.Error(w, http.StatusText(http.StatusRequestedRangeNotSatisfiable), http.StatusRequestedRangeNotSatisfiable)+ return+ }end := o.Size() // exclusiveif limit >= 0 {end = offset + limitdiff --git a/lib/http/serve/serve_test.go b/lib/http/serve/serve_test.goindex b945fa28b..cfdf50224 100644--- a/lib/http/serve/serve_test.go+++ b/lib/http/serve/serve_test.go@@ -88,6 +88,47 @@ func TestObjectRangeSuffixLongerThanObject(t *testing.T) {assert.Equal(t, "hello", string(body))}+func TestObjectRangeNotSatisfiable(t *testing.T) {+ for _, rangeHeader := range []string{"bytes=10-", "bytes=10-12", "bytes=-0", "bytes=5-2", "bytes=20-"} {+ t.Run(rangeHeader, func(t *testing.T) {+ w := httptest.NewRecorder()+ r := httptest.NewRequest("GET", "http://example.com/aFile", nil)+ r.Header.Add("Range", rangeHeader)+ o := object.NewMemoryObject("aFile", time.Now(), []byte("0123456789"))+ Object(w, r, o)+ resp := w.Result()+ assert.Equal(t, http.StatusRequestedRangeNotSatisfiable, resp.StatusCode)+ assert.Equal(t, "bytes */10", resp.Header.Get("Content-Range"))+ })+ }+}++func TestObjectRangeEndPastObject(t *testing.T) {+ w := httptest.NewRecorder()+ r := httptest.NewRequest("GET", "http://example.com/aFile", nil)+ r.Header.Add("Range", "bytes=8-20")+ o := object.NewMemoryObject("aFile", time.Now(), []byte("0123456789"))+ Object(w, r, o)+ resp := w.Result()+ assert.Equal(t, http.StatusPartialContent, resp.StatusCode)+ assert.Equal(t, "2", resp.Header.Get("Content-Length"))+ assert.Equal(t, "bytes 8-9/10", resp.Header.Get("Content-Range"))+ body, _ := io.ReadAll(resp.Body)+ assert.Equal(t, "89", string(body))+}++func TestObjectRangeEmptyObject(t *testing.T) {+ w := httptest.NewRecorder()+ r := httptest.NewRequest("GET", "http://example.com/aFile", nil)+ r.Header.Add("Range", "bytes=0-")+ o := object.NewMemoryObject("aFile", time.Now(), []byte{})+ Object(w, r, o)+ resp := w.Result()+ assert.Equal(t, http.StatusOK, resp.StatusCode)+ assert.Equal(t, "0", resp.Header.Get("Content-Length"))+ assert.Equal(t, "", resp.Header.Get("Content-Range"))+}+func TestObjectBadRange(t *testing.T) {w := httptest.NewRecorder()r := httptest.NewRequest("GET", "http://example.com/aFile", nil)