Skip to content

Commit

Permalink
Content-Type: application/partial-upload
Browse files Browse the repository at this point in the history
  • Loading branch information
Acconut committed Nov 2, 2024
1 parent 496c5e6 commit df25c35
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 9 deletions.
38 changes: 30 additions & 8 deletions pkg/handler/patch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -843,10 +843,10 @@ func TestPatch(t *testing.T) {
(&httpTest{
Method: "PATCH",
URL: "yes",
ReqHeader: addIETFUploadCompleteHeader(map[string]string{
ReqHeader: addIETFContentTypeHeader(addIETFUploadCompleteHeader(map[string]string{
"Upload-Draft-Interop-Version": interopVersion,
"Upload-Offset": "5",
}, true, interopVersion),
}, true, interopVersion), interopVersion),
ReqBody: strings.NewReader("hello"),
Code: http.StatusNoContent,
ResHeader: map[string]string{
Expand Down Expand Up @@ -887,10 +887,10 @@ func TestPatch(t *testing.T) {
(&httpTest{
Method: "PATCH",
URL: "yes",
ReqHeader: addIETFUploadCompleteHeader(map[string]string{
ReqHeader: addIETFContentTypeHeader(addIETFUploadCompleteHeader(map[string]string{
"Upload-Draft-Interop-Version": interopVersion,
"Upload-Offset": "5",
}, true, interopVersion),
}, true, interopVersion), interopVersion),
ReqBody: strings.NewReader("hello"),
Code: http.StatusNoContent,
ResHeader: map[string]string{
Expand Down Expand Up @@ -922,10 +922,10 @@ func TestPatch(t *testing.T) {
(&httpTest{
Method: "PATCH",
URL: "yes",
ReqHeader: addIETFUploadCompleteHeader(map[string]string{
ReqHeader: addIETFContentTypeHeader(addIETFUploadCompleteHeader(map[string]string{
"Upload-Draft-Interop-Version": interopVersion,
"Upload-Offset": "5",
}, false, interopVersion),
}, false, interopVersion), interopVersion),
ReqBody: strings.NewReader("hel"),
Code: http.StatusNoContent,
ResHeader: map[string]string{
Expand Down Expand Up @@ -957,17 +957,39 @@ func TestPatch(t *testing.T) {
(&httpTest{
Method: "PATCH",
URL: "yes",
ReqHeader: addIETFUploadCompleteHeader(map[string]string{
ReqHeader: addIETFContentTypeHeader(addIETFUploadCompleteHeader(map[string]string{
"Upload-Draft-Interop-Version": interopVersion,
"Upload-Offset": "5",
}, false, interopVersion),
}, false, interopVersion), interopVersion),
ReqBody: strings.NewReader("hel"),
Code: http.StatusNoContent,
ResHeader: map[string]string{
"Upload-Offset": "8",
},
}).Run(handler, t)
})

if interopVersion != "3" && interopVersion != "4" && interopVersion != "5" {
SubTest(t, "InvalidContentType", func(t *testing.T, store *MockFullDataStore, composer *StoreComposer) {
handler, _ := NewHandler(Config{
StoreComposer: composer,
EnableExperimentalProtocol: true,
})

(&httpTest{
Method: "PATCH",
URL: "yes",
ReqHeader: map[string]string{
"Upload-Draft-Interop-Version": interopVersion,
"Content-Type": "application/not-partial-upload",
"Upload-Offset": "0",
},
ReqBody: strings.NewReader("test"),
Code: http.StatusBadRequest,
ResBody: "ERR_INVALID_CONTENT_TYPE: missing or invalid Content-Type header\n",
}).Run(handler, t)
})
}
})
}
})
Expand Down
10 changes: 9 additions & 1 deletion pkg/handler/unrouted_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -698,12 +698,20 @@ func (handler *UnroutedHandler) PatchFile(w http.ResponseWriter, r *http.Request

isTusV1 := !handler.usesIETFDraft(r)

// Check for presence of application/offset+octet-stream
// Check for presence of application/offset+octet-stream (tus v1) or application/partial-upload (IETF draft since -04)
if isTusV1 && r.Header.Get("Content-Type") != "application/offset+octet-stream" {
handler.sendError(c, ErrInvalidContentType)
return
}

if !isTusV1 {
currentInteropVersion := getIETFDraftInteropVersion(r)
if currentInteropVersion != interopVersion3 && currentInteropVersion != interopVersion4 && currentInteropVersion != interopVersion5 && r.Header.Get("Content-Type") != "application/partial-upload" {
handler.sendError(c, ErrInvalidContentType)
return
}
}

// Check for presence of a valid Upload-Offset Header
offset, err := strconv.ParseInt(r.Header.Get("Upload-Offset"), 10, 64)
if err != nil || offset < 0 {
Expand Down
9 changes: 9 additions & 0 deletions pkg/handler/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,3 +152,12 @@ func addIETFUploadCompleteHeader(header map[string]string, isComplete bool, inte

return header
}

// addIETFContentTypeHeader writes the Content-Type header depending on the interop version.
func addIETFContentTypeHeader(header map[string]string, interopVersion string) map[string]string {
switch interopVersion {
case "6":
header["Content-Type"] = "application/partial-upload"
}
return header
}

0 comments on commit df25c35

Please sign in to comment.