Skip to content

Commit

Permalink
Upload-Limit in POST responses
Browse files Browse the repository at this point in the history
  • Loading branch information
Acconut committed Nov 2, 2024
1 parent 7e370e1 commit a4287f3
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
7 changes: 7 additions & 0 deletions pkg/handler/post_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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)
Expand Down Expand Up @@ -650,6 +652,7 @@ func TestPost(t *testing.T) {
StoreComposer: composer,
BasePath: "/files/",
EnableExperimentalProtocol: true,
MaxSize: 400,
})

res := (&httpTest{
Expand All @@ -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)

Expand All @@ -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)
Expand Down Expand Up @@ -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)
})
Expand Down Expand Up @@ -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)
})
Expand Down
16 changes: 16 additions & 0 deletions pkg/handler/unrouted_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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"))
Expand Down

0 comments on commit a4287f3

Please sign in to comment.