diff --git a/pkg/handler/post_test.go b/pkg/handler/post_test.go index 8d5c2025..64746e04 100644 --- a/pkg/handler/post_test.go +++ b/pkg/handler/post_test.go @@ -603,6 +603,7 @@ func TestPost(t *testing.T) { "Upload-Draft-Interop-Version": interopVersion, "Location": "http://tus.io/files/foo", "Upload-Offset": "11", + "Upload-Limit": "min-size=0,max-size=11", }, }).Run(handler, t) @@ -614,6 +615,7 @@ func TestPost(t *testing.T) { "Upload-Draft-Interop-Version": []string{interopVersion}, "Location": []string{"http://tus.io/files/foo"}, "X-Content-Type-Options": []string{"nosniff"}, + "Upload-Limit": []string{"min-size=0,max-size=11"}, }, }, }, res.InformationalResponses) @@ -650,6 +652,7 @@ func TestPost(t *testing.T) { StoreComposer: composer, BasePath: "/files/", EnableExperimentalProtocol: true, + MaxSize: 400, }) res := (&httpTest{ @@ -663,6 +666,7 @@ func TestPost(t *testing.T) { "Upload-Draft-Interop-Version": interopVersion, "Location": "http://tus.io/files/foo", "Upload-Offset": "11", + "Upload-Limit": "min-size=0,max-size=400", }, }).Run(handler, t) @@ -674,6 +678,7 @@ func TestPost(t *testing.T) { "Upload-Draft-Interop-Version": []string{interopVersion}, "Location": []string{"http://tus.io/files/foo"}, "X-Content-Type-Options": []string{"nosniff"}, + "Upload-Limit": []string{"min-size=0,max-size=400"}, }, }, }, res.InformationalResponses) @@ -728,6 +733,7 @@ func TestPost(t *testing.T) { "Upload-Draft-Interop-Version": interopVersion, "Location": "http://tus.io/files/foo", "Upload-Offset": "11", + "Upload-Limit": "min-size=0,max-size=11", }, }).Run(handler, t) }) @@ -802,6 +808,7 @@ func TestPost(t *testing.T) { "Upload-Draft-Interop-Version": interopVersion, "Location": "http://tus.io/files/foo", "Upload-Offset": "6", + "Upload-Limit": "min-size=0,max-size=11", }, }).Run(handler, t) }) diff --git a/pkg/handler/unrouted_handler.go b/pkg/handler/unrouted_handler.go index e0e82833..be8b7909 100644 --- a/pkg/handler/unrouted_handler.go +++ b/pkg/handler/unrouted_handler.go @@ -560,11 +560,14 @@ func (handler *UnroutedHandler) PostFileV2(w http.ResponseWriter, r *http.Reques id := info.ID url := handler.absFileURL(r, id) + limits := handler.getIETFDraftUploadLimits(info) resp.Header["Location"] = url + resp.Header["Upload-Limit"] = limits // Send 104 response w.Header().Set("Location", url) w.Header().Set("Upload-Draft-Interop-Version", string(currentUploadDraftInteropVersion)) + w.Header().Set("Upload-Limit", limits) w.WriteHeader(104) handler.Metrics.incUploadsCreated() @@ -1406,6 +1409,19 @@ func (handler UnroutedHandler) usesIETFDraft(r *http.Request) bool { return handler.config.EnableExperimentalProtocol && interopVersionHeader != "" } +// getIETFDraftUploadLimits returns the Upload-Limit header for a given upload +// according to the set resumable upload draft version from IETF. +func (handler UnroutedHandler) getIETFDraftUploadLimits(info FileInfo) string { + limits := "min-size=0" + if handler.config.MaxSize > 0 { + limits += ",max-size=" + strconv.FormatInt(handler.config.MaxSize, 10) + } else if !info.SizeIsDeferred { + limits += ",max-size=" + strconv.FormatInt(info.Size, 10) + } + + return limits +} + // getIETFDraftInteropVersion returns the resumable upload draft interop version from the headers. func getIETFDraftInteropVersion(r *http.Request) draftVersion { version := draftVersion(r.Header.Get("Upload-Draft-Interop-Version"))