fix: unblock stalled and frozen uploads
A TUS PATCH rejected before its body was read left the client still streaming a chunk. net/http gives up draining an unread request body after 256KiB and then closes the connection, so the status we replied with was lost and the browser saw a bare transport error instead. The client could not tell a conflict from a network fault, retried blindly, and the upload stalled after the first chunk — while curl, which reads the response as it writes, still reported the status and appeared to work. Drain what is left of a rejected chunk so the answer arrives on a connection that stays usable. The explicit r.Body.Close() had to go with it: it ran before the drain, and the server closes the request body itself once the handler returns. Pressing Upload awaited a recursive listing of the whole destination subtree before a single byte was sent, with no sign that anything was happening. On a large destination that reads as a dead button, and the walk kept running after the client gave up, then reported the failed write as a 500. A flat upload can only collide with a direct child of the destination, so walk recursively only for folder uploads, stop the walk once the request is done, and show the action as busy while the destination is checked. Along the way: a failed upload was credited as fully sent, so the progress bar reported 100% when nothing had been written; the progress timer was recreated without clearing the previous one; and the chunk size field threw on a value with no unit and raced its own debounce on submit, so it appeared to refuse every change. Fixes #6006 Refs #5987
This commit is contained in:
12 files changed
+715
-156
No files matched your search
@@ -412,6 +412,13 @@ var resourceGetRecursiveHandler = withUser(func(w http.ResponseWriter, r *http.R
|
||||
entries := make([]RecursiveEntry, 0)
|
||||
|
||||
err = afero.Walk(d.user.Fs, rootPath, func(fPath string, info os.FileInfo, err error) error {
|
||||
// Walking a large tree is expensive, and every entry is stat'ed through
|
||||
// the scoped filesystem. Stop as soon as nobody is waiting for the
|
||||
// answer instead of finishing the walk for a client that has gone.
|
||||
if ctxErr := r.Context().Err(); ctxErr != nil {
|
||||
return ctxErr
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
return nil // skip entries we cannot read
|
||||
}
|
||||
@@ -442,6 +449,12 @@ var resourceGetRecursiveHandler = withUser(func(w http.ResponseWriter, r *http.R
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
// Once the request is done there is nobody left to answer, whatever the
|
||||
// walk reported. Asking for a status here only produces a second header
|
||||
// write on a connection that is already gone.
|
||||
if r.Context().Err() != nil {
|
||||
return 0, err
|
||||
}
|
||||
return http.StatusInternalServerError, err
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,81 @@
|
||||
package fbhttp
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"github.com/filebrowser/filebrowser/v2/settings"
|
||||
"github.com/filebrowser/filebrowser/v2/users"
|
||||
)
|
||||
|
||||
func recursiveTestHandler(t *testing.T, userScope string) (http.Handler, string) {
|
||||
t.Helper()
|
||||
|
||||
key := []byte("test-signing-key")
|
||||
perm := users.Permissions{Create: true, Modify: true, Download: true}
|
||||
st := scopedUserStorage(t, userScope, perm, key)
|
||||
|
||||
return handle(resourceGetRecursiveHandler, "/api/resources/recursive", st, &settings.Server{}), signToken(t, perm, key)
|
||||
}
|
||||
|
||||
func TestResourceRecursiveListsTree(t *testing.T) {
|
||||
userScope := t.TempDir()
|
||||
if err := os.MkdirAll(filepath.Join(userScope, "target", "nested"), 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.WriteFile(filepath.Join(userScope, "target", "nested", "file.txt"), []byte("hi"), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
handler, token := recursiveTestHandler(t, userScope)
|
||||
|
||||
req, _ := http.NewRequest(http.MethodGet, "/api/resources/recursive/target", http.NoBody)
|
||||
req.Header.Set("X-Auth", token)
|
||||
rec := httptest.NewRecorder()
|
||||
handler.ServeHTTP(rec, req)
|
||||
|
||||
if rec.Code != http.StatusOK {
|
||||
t.Fatalf("status = %d body = %q", rec.Code, rec.Body.String())
|
||||
}
|
||||
|
||||
var entries []RecursiveEntry
|
||||
if err := json.Unmarshal(rec.Body.Bytes(), &entries); err != nil {
|
||||
t.Fatalf("decode response: %v", err)
|
||||
}
|
||||
if len(entries) != 2 {
|
||||
t.Fatalf("got %d entries, want 2: %+v", len(entries), entries)
|
||||
}
|
||||
}
|
||||
|
||||
// Walking the tree stats every entry through the scoped filesystem, which is
|
||||
// expensive on a large destination. When the client has already hung up there
|
||||
// is nothing to answer, so the walk must stop instead of running to completion
|
||||
// and then reporting a write failure as a 500.
|
||||
func TestResourceRecursiveStopsWhenClientDisconnects(t *testing.T) {
|
||||
userScope := t.TempDir()
|
||||
if err := os.MkdirAll(filepath.Join(userScope, "target"), 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.WriteFile(filepath.Join(userScope, "target", "file.txt"), []byte("hi"), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
handler, token := recursiveTestHandler(t, userScope)
|
||||
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
cancel()
|
||||
|
||||
req, _ := http.NewRequestWithContext(ctx, http.MethodGet, "/api/resources/recursive/target", http.NoBody)
|
||||
req.Header.Set("X-Auth", token)
|
||||
rec := httptest.NewRecorder()
|
||||
handler.ServeHTTP(rec, req)
|
||||
|
||||
if body := rec.Body.String(); body != "" {
|
||||
t.Fatalf("wrote a response for a disconnected client: %q", body)
|
||||
}
|
||||
}
|
||||
+129
-97
@@ -15,6 +15,24 @@ import (
|
||||
"github.com/spf13/afero"
|
||||
)
|
||||
|
||||
// maxPatchDrainBytes bounds how much of a rejected PATCH body is discarded to
|
||||
// keep the connection reusable. It comfortably covers a default-sized chunk;
|
||||
// beyond that the body is not worth reading just to throw away.
|
||||
const maxPatchDrainBytes = 32 << 20 // 32MB
|
||||
|
||||
// drainRequestBody discards what the client already put on the wire for a
|
||||
// request the handler answered without reading. net/http only drains 256KiB on
|
||||
// its own before giving up and closing the connection, and a connection closed
|
||||
// while the client is still streaming a chunk reaches the browser as a transport
|
||||
// error instead of the status we replied with. The client then cannot tell a
|
||||
// conflict from a network fault, retries blindly, and the upload stalls.
|
||||
func drainRequestBody(r *http.Request) {
|
||||
if r.Body == nil {
|
||||
return
|
||||
}
|
||||
_, _ = io.Copy(io.Discard, io.LimitReader(r.Body, maxPatchDrainBytes))
|
||||
}
|
||||
|
||||
// keepUploadActive periodically touches the cache entry to prevent eviction during transfer
|
||||
func keepUploadActive(cache UploadCache, filePath string) func() {
|
||||
stop := make(chan bool)
|
||||
@@ -160,107 +178,121 @@ func tusHeadHandler(cache UploadCache) handleFunc {
|
||||
|
||||
func tusPatchHandler(cache UploadCache) handleFunc {
|
||||
return withUser(func(w http.ResponseWriter, r *http.Request, d *data) (int, error) {
|
||||
if !d.user.Perm.Create || !d.Check(r.URL.Path) {
|
||||
return http.StatusForbidden, nil
|
||||
status, err := tusPatchUpload(w, r, d, cache)
|
||||
// A rejected chunk is still a chunk the client is streaming: read what is
|
||||
// left of it so the answer reaches the client on a connection that stays
|
||||
// usable, instead of being lost to a reset.
|
||||
if status >= 400 {
|
||||
drainRequestBody(r)
|
||||
}
|
||||
if r.Header.Get("Content-Type") != "application/offset+octet-stream" {
|
||||
return http.StatusUnsupportedMediaType, nil
|
||||
}
|
||||
|
||||
uploadOffset, err := getUploadOffset(r)
|
||||
if err != nil {
|
||||
return http.StatusBadRequest, fmt.Errorf("invalid upload offset")
|
||||
}
|
||||
|
||||
file, err := files.NewFileInfo(&files.FileOptions{
|
||||
Fs: d.user.Fs,
|
||||
Path: r.URL.Path,
|
||||
Modify: d.user.Perm.Modify,
|
||||
Expand: false,
|
||||
ReadHeader: d.server.TypeDetectionByHeader,
|
||||
Checker: d,
|
||||
})
|
||||
|
||||
switch {
|
||||
case errors.Is(err, afero.ErrFileNotFound):
|
||||
return http.StatusNotFound, nil
|
||||
case err != nil:
|
||||
return errToStatus(err), err
|
||||
}
|
||||
|
||||
uploadLength, err := cache.GetLength(file.RealPath())
|
||||
if err != nil {
|
||||
return http.StatusNotFound, err
|
||||
}
|
||||
|
||||
if uploadOffset > uploadLength {
|
||||
return http.StatusBadRequest, fmt.Errorf("upload offset %d exceeds declared length %d", uploadOffset, uploadLength)
|
||||
}
|
||||
|
||||
// Prevent the upload from being evicted during the transfer
|
||||
stop := keepUploadActive(cache, file.RealPath())
|
||||
defer stop()
|
||||
|
||||
switch {
|
||||
case file.IsDir:
|
||||
return http.StatusBadRequest, fmt.Errorf("cannot upload to a directory %s", file.RealPath())
|
||||
case file.Size != uploadOffset:
|
||||
return http.StatusConflict, fmt.Errorf(
|
||||
"%s file size doesn't match the provided offset: %d",
|
||||
file.RealPath(),
|
||||
uploadOffset,
|
||||
)
|
||||
}
|
||||
|
||||
openFile, err := d.user.Fs.OpenFile(r.URL.Path, os.O_WRONLY|os.O_APPEND, d.settings.FileMode)
|
||||
if err != nil {
|
||||
return http.StatusInternalServerError, fmt.Errorf("could not open file: %w", err)
|
||||
}
|
||||
defer openFile.Close()
|
||||
|
||||
_, err = openFile.Seek(uploadOffset, 0)
|
||||
if err != nil {
|
||||
return http.StatusInternalServerError, fmt.Errorf("could not seek file: %w", err)
|
||||
}
|
||||
|
||||
defer r.Body.Close()
|
||||
// Enforce the declared Upload-Length: never write more than the bytes
|
||||
// still expected for this upload. Reading one byte past the remainder
|
||||
// lets an over-length body be detected and rejected. Without this bound a
|
||||
// PATCH could stream arbitrary data to disk regardless of the length the
|
||||
// client declared when the upload was created.
|
||||
remaining := uploadLength - uploadOffset
|
||||
bytesWritten, err := io.Copy(openFile, io.LimitReader(r.Body, remaining+1))
|
||||
if err != nil {
|
||||
return http.StatusInternalServerError, fmt.Errorf("could not write to file: %w", err)
|
||||
}
|
||||
if bytesWritten > remaining {
|
||||
// The client sent more than it declared; roll this chunk back so the
|
||||
// file stays consistent with the tracked offset, and reject it.
|
||||
if truncErr := openFile.Truncate(uploadOffset); truncErr != nil {
|
||||
return http.StatusInternalServerError, fmt.Errorf("could not truncate file: %w", truncErr)
|
||||
}
|
||||
return http.StatusRequestEntityTooLarge, fmt.Errorf("upload exceeds declared length of %d bytes", uploadLength)
|
||||
}
|
||||
|
||||
// Sync the file to ensure all data is written to storage
|
||||
// to prevent file corruption.
|
||||
if err := openFile.Sync(); err != nil {
|
||||
return http.StatusInternalServerError, fmt.Errorf("could not sync file: %w", err)
|
||||
}
|
||||
|
||||
newOffset := uploadOffset + bytesWritten
|
||||
w.Header().Set("Upload-Offset", strconv.FormatInt(newOffset, 10))
|
||||
|
||||
if newOffset >= uploadLength {
|
||||
cache.Complete(file.RealPath())
|
||||
_ = d.RunHook(func() error { return nil }, "upload", r.URL.Path, "", d.user)
|
||||
}
|
||||
|
||||
return http.StatusNoContent, nil
|
||||
return status, err
|
||||
})
|
||||
}
|
||||
|
||||
func tusPatchUpload(w http.ResponseWriter, r *http.Request, d *data, cache UploadCache) (int, error) {
|
||||
if !d.user.Perm.Create || !d.Check(r.URL.Path) {
|
||||
return http.StatusForbidden, nil
|
||||
}
|
||||
if r.Header.Get("Content-Type") != "application/offset+octet-stream" {
|
||||
return http.StatusUnsupportedMediaType, nil
|
||||
}
|
||||
|
||||
uploadOffset, err := getUploadOffset(r)
|
||||
if err != nil {
|
||||
return http.StatusBadRequest, fmt.Errorf("invalid upload offset")
|
||||
}
|
||||
|
||||
file, err := files.NewFileInfo(&files.FileOptions{
|
||||
Fs: d.user.Fs,
|
||||
Path: r.URL.Path,
|
||||
Modify: d.user.Perm.Modify,
|
||||
Expand: false,
|
||||
ReadHeader: d.server.TypeDetectionByHeader,
|
||||
Checker: d,
|
||||
})
|
||||
|
||||
switch {
|
||||
case errors.Is(err, afero.ErrFileNotFound):
|
||||
return http.StatusNotFound, nil
|
||||
case err != nil:
|
||||
return errToStatus(err), err
|
||||
}
|
||||
|
||||
uploadLength, err := cache.GetLength(file.RealPath())
|
||||
if err != nil {
|
||||
return http.StatusNotFound, err
|
||||
}
|
||||
|
||||
if uploadOffset > uploadLength {
|
||||
return http.StatusBadRequest, fmt.Errorf("upload offset %d exceeds declared length %d", uploadOffset, uploadLength)
|
||||
}
|
||||
|
||||
// Prevent the upload from being evicted during the transfer
|
||||
stop := keepUploadActive(cache, file.RealPath())
|
||||
defer stop()
|
||||
|
||||
switch {
|
||||
case file.IsDir:
|
||||
return http.StatusBadRequest, fmt.Errorf("cannot upload to a directory %s", file.RealPath())
|
||||
case file.Size != uploadOffset:
|
||||
return http.StatusConflict, fmt.Errorf(
|
||||
"%s file size doesn't match the provided offset: %d",
|
||||
file.RealPath(),
|
||||
uploadOffset,
|
||||
)
|
||||
}
|
||||
|
||||
openFile, err := d.user.Fs.OpenFile(r.URL.Path, os.O_WRONLY|os.O_APPEND, d.settings.FileMode)
|
||||
if err != nil {
|
||||
return http.StatusInternalServerError, fmt.Errorf("could not open file: %w", err)
|
||||
}
|
||||
defer openFile.Close()
|
||||
|
||||
_, err = openFile.Seek(uploadOffset, 0)
|
||||
if err != nil {
|
||||
return http.StatusInternalServerError, fmt.Errorf("could not seek file: %w", err)
|
||||
}
|
||||
|
||||
// The server closes the request body once the handler returns; closing it
|
||||
// here would run before the caller gets to drain a rejected chunk, so every
|
||||
// status raised below this point would still cost the connection.
|
||||
//
|
||||
// Enforce the declared Upload-Length: never write more than the bytes
|
||||
// still expected for this upload. Reading one byte past the remainder
|
||||
// lets an over-length body be detected and rejected. Without this bound a
|
||||
// PATCH could stream arbitrary data to disk regardless of the length the
|
||||
// client declared when the upload was created.
|
||||
remaining := uploadLength - uploadOffset
|
||||
bytesWritten, err := io.Copy(openFile, io.LimitReader(r.Body, remaining+1))
|
||||
if err != nil {
|
||||
return http.StatusInternalServerError, fmt.Errorf("could not write to file: %w", err)
|
||||
}
|
||||
if bytesWritten > remaining {
|
||||
// The client sent more than it declared; roll this chunk back so the
|
||||
// file stays consistent with the tracked offset, and reject it.
|
||||
if truncErr := openFile.Truncate(uploadOffset); truncErr != nil {
|
||||
return http.StatusInternalServerError, fmt.Errorf("could not truncate file: %w", truncErr)
|
||||
}
|
||||
return http.StatusRequestEntityTooLarge, fmt.Errorf("upload exceeds declared length of %d bytes", uploadLength)
|
||||
}
|
||||
|
||||
// Sync the file to ensure all data is written to storage
|
||||
// to prevent file corruption.
|
||||
if err := openFile.Sync(); err != nil {
|
||||
return http.StatusInternalServerError, fmt.Errorf("could not sync file: %w", err)
|
||||
}
|
||||
|
||||
newOffset := uploadOffset + bytesWritten
|
||||
w.Header().Set("Upload-Offset", strconv.FormatInt(newOffset, 10))
|
||||
|
||||
if newOffset >= uploadLength {
|
||||
cache.Complete(file.RealPath())
|
||||
_ = d.RunHook(func() error { return nil }, "upload", r.URL.Path, "", d.user)
|
||||
}
|
||||
|
||||
return http.StatusNoContent, nil
|
||||
}
|
||||
|
||||
func tusDeleteHandler(cache UploadCache) handleFunc {
|
||||
return withUser(func(_ http.ResponseWriter, r *http.Request, d *data) (int, error) {
|
||||
if r.URL.Path == "/" || !d.user.Perm.Delete {
|
||||
|
||||
@@ -0,0 +1,297 @@
|
||||
package fbhttp
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"net/http/httptrace"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"testing"
|
||||
|
||||
"github.com/filebrowser/filebrowser/v2/settings"
|
||||
"github.com/filebrowser/filebrowser/v2/storage"
|
||||
"github.com/filebrowser/filebrowser/v2/users"
|
||||
)
|
||||
|
||||
// newTusTestServer mounts the TUS handlers behind a real HTTP server, under the
|
||||
// same "/api/tus" prefix used in production, so tests exercise connection
|
||||
// handling and header round-tripping instead of a bare ResponseRecorder.
|
||||
func newTusTestServer(t *testing.T, st *storage.Storage, cache UploadCache) *httptest.Server {
|
||||
t.Helper()
|
||||
|
||||
server := &settings.Server{}
|
||||
post := handle(tusPostHandler(cache), "/api/tus", st, server)
|
||||
head := handle(tusHeadHandler(cache), "/api/tus", st, server)
|
||||
patch := handle(tusPatchHandler(cache), "/api/tus", st, server)
|
||||
|
||||
mux := http.NewServeMux()
|
||||
mux.HandleFunc("/api/tus/", func(w http.ResponseWriter, r *http.Request) {
|
||||
switch r.Method {
|
||||
case http.MethodPost:
|
||||
post.ServeHTTP(w, r)
|
||||
case http.MethodHead:
|
||||
head.ServeHTTP(w, r)
|
||||
case http.MethodPatch:
|
||||
patch.ServeHTTP(w, r)
|
||||
default:
|
||||
w.WriteHeader(http.StatusMethodNotAllowed)
|
||||
}
|
||||
})
|
||||
|
||||
srv := httptest.NewServer(mux)
|
||||
t.Cleanup(srv.Close)
|
||||
return srv
|
||||
}
|
||||
|
||||
// tusTestFixture wires a scoped user, an upload cache and a live server.
|
||||
type tusTestFixture struct {
|
||||
srv *httptest.Server
|
||||
client *http.Client
|
||||
token string
|
||||
scope string
|
||||
}
|
||||
|
||||
func newTusTestFixture(t *testing.T) *tusTestFixture {
|
||||
t.Helper()
|
||||
|
||||
root := t.TempDir()
|
||||
userScope := filepath.Join(root, "user")
|
||||
if err := os.MkdirAll(userScope, 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
key := []byte("test-signing-key")
|
||||
perm := users.Permissions{Create: true, Modify: true}
|
||||
st := scopedUserStorage(t, userScope, perm, key)
|
||||
|
||||
cache := newMemoryUploadCache()
|
||||
t.Cleanup(cache.Close)
|
||||
|
||||
srv := newTusTestServer(t, st, cache)
|
||||
return &tusTestFixture{
|
||||
srv: srv,
|
||||
client: srv.Client(),
|
||||
token: signToken(t, perm, key),
|
||||
scope: userScope,
|
||||
}
|
||||
}
|
||||
|
||||
// create sends the TUS creation request and returns the absolute upload URL.
|
||||
func (f *tusTestFixture) create(t *testing.T, name string, length int) string {
|
||||
t.Helper()
|
||||
|
||||
req, err := http.NewRequest(http.MethodPost, f.srv.URL+"/api/tus/"+name+"?override=false", http.NoBody)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
req.Header.Set("X-Auth", f.token)
|
||||
req.Header.Set("Upload-Length", strconv.Itoa(length))
|
||||
|
||||
res, err := f.client.Do(req)
|
||||
if err != nil {
|
||||
t.Fatalf("POST create: %v", err)
|
||||
}
|
||||
defer res.Body.Close()
|
||||
body, _ := io.ReadAll(res.Body)
|
||||
|
||||
if res.StatusCode != http.StatusCreated {
|
||||
t.Fatalf("POST create: status %d body %q", res.StatusCode, body)
|
||||
}
|
||||
location := res.Header.Get("Location")
|
||||
if location == "" {
|
||||
t.Fatal("POST create: missing Location header")
|
||||
}
|
||||
return f.srv.URL + location
|
||||
}
|
||||
|
||||
// patch sends one chunk and reports the response plus whether the underlying
|
||||
// connection was reused from the pool.
|
||||
func (f *tusTestFixture) patch(t *testing.T, url string, offset int, chunk []byte) (*http.Response, bool) {
|
||||
t.Helper()
|
||||
|
||||
req, err := http.NewRequest(http.MethodPatch, url, bytes.NewReader(chunk))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
req.Header.Set("X-Auth", f.token)
|
||||
req.Header.Set("Content-Type", "application/offset+octet-stream")
|
||||
req.Header.Set("Upload-Offset", strconv.Itoa(offset))
|
||||
|
||||
var reused bool
|
||||
trace := &httptrace.ClientTrace{
|
||||
GotConn: func(info httptrace.GotConnInfo) { reused = info.Reused },
|
||||
}
|
||||
req = req.WithContext(httptrace.WithClientTrace(req.Context(), trace))
|
||||
|
||||
res, err := f.client.Do(req)
|
||||
if err != nil {
|
||||
t.Fatalf("PATCH at offset %d: %v", offset, err)
|
||||
}
|
||||
return res, reused
|
||||
}
|
||||
|
||||
func testPayload(size int) []byte {
|
||||
payload := make([]byte, size)
|
||||
for i := range payload {
|
||||
payload[i] = byte(i % 251)
|
||||
}
|
||||
return payload
|
||||
}
|
||||
|
||||
// A file larger than the chunk size must upload across several PATCH requests
|
||||
// over a single reused connection, exactly as the web UI drives it. Each chunk
|
||||
// has to be acknowledged with 204 and the new Upload-Offset, and the resulting
|
||||
// file must be byte-identical to the source.
|
||||
func TestTusMultiChunkUpload(t *testing.T) {
|
||||
const chunkSize = 64 * 1024
|
||||
const fileSize = chunkSize*3 + 1234
|
||||
|
||||
f := newTusTestFixture(t)
|
||||
payload := testPayload(fileSize)
|
||||
uploadURL := f.create(t, "movie.bin", fileSize)
|
||||
|
||||
for offset := 0; offset < fileSize; {
|
||||
end := min(offset+chunkSize, fileSize)
|
||||
|
||||
res, _ := f.patch(t, uploadURL, offset, payload[offset:end])
|
||||
body, _ := io.ReadAll(res.Body)
|
||||
res.Body.Close()
|
||||
|
||||
if res.StatusCode != http.StatusNoContent {
|
||||
t.Fatalf("PATCH at offset %d: status %d body %q", offset, res.StatusCode, body)
|
||||
}
|
||||
if got, want := res.Header.Get("Upload-Offset"), strconv.Itoa(end); got != want {
|
||||
t.Fatalf("PATCH at offset %d: Upload-Offset = %q, want %q", offset, got, want)
|
||||
}
|
||||
|
||||
offset = end
|
||||
}
|
||||
|
||||
got, err := os.ReadFile(filepath.Join(f.scope, "movie.bin"))
|
||||
if err != nil {
|
||||
t.Fatalf("read uploaded file: %v", err)
|
||||
}
|
||||
if !bytes.Equal(got, payload) {
|
||||
t.Fatalf("uploaded file differs from source (got %d bytes, want %d)", len(got), len(payload))
|
||||
}
|
||||
}
|
||||
|
||||
// Between chunks a client may re-synchronise with a HEAD request. It must
|
||||
// report the bytes already on disk plus the declared total, so the client can
|
||||
// resume from the right offset.
|
||||
func TestTusHeadReportsOffsetBetweenChunks(t *testing.T) {
|
||||
const chunkSize = 32 * 1024
|
||||
const fileSize = chunkSize * 2
|
||||
|
||||
f := newTusTestFixture(t)
|
||||
payload := testPayload(fileSize)
|
||||
uploadURL := f.create(t, "resume.bin", fileSize)
|
||||
|
||||
res, _ := f.patch(t, uploadURL, 0, payload[:chunkSize])
|
||||
res.Body.Close()
|
||||
if res.StatusCode != http.StatusNoContent {
|
||||
t.Fatalf("first PATCH: status %d", res.StatusCode)
|
||||
}
|
||||
|
||||
req, err := http.NewRequest(http.MethodHead, uploadURL, http.NoBody)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
req.Header.Set("X-Auth", f.token)
|
||||
|
||||
headRes, err := f.client.Do(req)
|
||||
if err != nil {
|
||||
t.Fatalf("HEAD: %v", err)
|
||||
}
|
||||
defer headRes.Body.Close()
|
||||
|
||||
if headRes.StatusCode != http.StatusOK {
|
||||
t.Fatalf("HEAD: status %d", headRes.StatusCode)
|
||||
}
|
||||
if got, want := headRes.Header.Get("Upload-Offset"), strconv.Itoa(chunkSize); got != want {
|
||||
t.Fatalf("HEAD Upload-Offset = %q, want %q", got, want)
|
||||
}
|
||||
if got, want := headRes.Header.Get("Upload-Length"), strconv.Itoa(fileSize); got != want {
|
||||
t.Fatalf("HEAD Upload-Length = %q, want %q", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
// A PATCH that the handler rejects before reading the body must still leave the
|
||||
// connection usable. Go abandons draining an unread request body after 256 KiB
|
||||
// and then closes the socket, which turns a plain 409 into an opaque transport
|
||||
// error for the client and stalls the upload. Rejecting a chunk-sized body must
|
||||
// not cost the connection.
|
||||
func TestTusPatchRejectionKeepsConnectionUsable(t *testing.T) {
|
||||
const chunkSize = 1024 * 1024
|
||||
const fileSize = chunkSize * 2
|
||||
|
||||
f := newTusTestFixture(t)
|
||||
payload := testPayload(fileSize)
|
||||
uploadURL := f.create(t, "conflict.bin", fileSize)
|
||||
|
||||
// Land the first chunk so the file size no longer matches offset 0.
|
||||
res, _ := f.patch(t, uploadURL, 0, payload[:chunkSize])
|
||||
res.Body.Close()
|
||||
if res.StatusCode != http.StatusNoContent {
|
||||
t.Fatalf("first PATCH: status %d", res.StatusCode)
|
||||
}
|
||||
|
||||
// Replaying offset 0 with a full chunk must be answered with a real 409.
|
||||
res, _ = f.patch(t, uploadURL, 0, payload[:chunkSize])
|
||||
body, _ := io.ReadAll(res.Body)
|
||||
res.Body.Close()
|
||||
if res.StatusCode != http.StatusConflict {
|
||||
t.Fatalf("stale-offset PATCH: status %d body %q, want 409", res.StatusCode, body)
|
||||
}
|
||||
|
||||
// And the connection must survive it, so the client's next attempt is not
|
||||
// reported as a network failure.
|
||||
res, reused := f.patch(t, uploadURL, chunkSize, payload[chunkSize:])
|
||||
body, _ = io.ReadAll(res.Body)
|
||||
res.Body.Close()
|
||||
if res.StatusCode != http.StatusNoContent {
|
||||
t.Fatalf("PATCH after rejection: status %d body %q", res.StatusCode, body)
|
||||
}
|
||||
if !reused {
|
||||
t.Fatal("connection was dropped by the rejected PATCH instead of being reused")
|
||||
}
|
||||
|
||||
got, err := os.ReadFile(filepath.Join(f.scope, "conflict.bin"))
|
||||
if err != nil {
|
||||
t.Fatalf("read uploaded file: %v", err)
|
||||
}
|
||||
if !bytes.Equal(got, payload) {
|
||||
t.Fatalf("uploaded file differs from source (got %d bytes, want %d)", len(got), len(payload))
|
||||
}
|
||||
}
|
||||
|
||||
// Same requirement for a rejection raised once the write is already underway:
|
||||
// an over-length body is refused after part of it has been read, and the rest
|
||||
// still has to be drained for the answer to survive.
|
||||
func TestTusOverLengthPatchKeepsConnectionUsable(t *testing.T) {
|
||||
const declared = 1024
|
||||
|
||||
f := newTusTestFixture(t)
|
||||
payload := testPayload(4 << 20)
|
||||
uploadURL := f.create(t, "toolong.bin", declared)
|
||||
|
||||
res, _ := f.patch(t, uploadURL, 0, payload)
|
||||
body, _ := io.ReadAll(res.Body)
|
||||
res.Body.Close()
|
||||
if res.StatusCode != http.StatusRequestEntityTooLarge {
|
||||
t.Fatalf("over-length PATCH: status %d body %q, want 413", res.StatusCode, body)
|
||||
}
|
||||
|
||||
res, reused := f.patch(t, uploadURL, 0, payload[:declared])
|
||||
body, _ = io.ReadAll(res.Body)
|
||||
res.Body.Close()
|
||||
if res.StatusCode != http.StatusNoContent {
|
||||
t.Fatalf("PATCH after rejection: status %d body %q", res.StatusCode, body)
|
||||
}
|
||||
if !reused {
|
||||
t.Fatal("connection was dropped by the over-length PATCH instead of being reused")
|
||||
}
|
||||
}
|
||||
+4
-1
@@ -65,7 +65,10 @@ func renderJSON(w http.ResponseWriter, _ *http.Request, data interface{}) (int,
|
||||
|
||||
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
||||
if _, err := w.Write(marsh); err != nil {
|
||||
return http.StatusInternalServerError, err
|
||||
// The status line is already on the wire, so there is no error status
|
||||
// left to send: a failed write here means the client hung up. Report it
|
||||
// for the log without asking the caller to write a second header.
|
||||
return 0, err
|
||||
}
|
||||
|
||||
return 0, nil
|
||||
|
||||
Reference in new issue
Block a user