update header names

This commit is contained in:
Acconut 2015-03-23 18:15:05 +01:00
parent c49f7bbf91
commit 46fabad314
7 changed files with 72 additions and 72 deletions

View File

@ -49,9 +49,9 @@ func TestConcatPartial(t *testing.T) {
Name: "Successful POST request", Name: "Successful POST request",
Method: "POST", Method: "POST",
ReqHeader: map[string]string{ ReqHeader: map[string]string{
"TUS-Resumable": "1.0.0", "Tus-Resumable": "1.0.0",
"Entity-Length": "300", "Upload-Length": "300",
"Concat": "partial", "Upload-Concat": "partial",
}, },
Code: http.StatusCreated, Code: http.StatusCreated,
}).Run(handler, t) }).Run(handler, t)
@ -61,11 +61,11 @@ func TestConcatPartial(t *testing.T) {
Method: "HEAD", Method: "HEAD",
URL: "foo", URL: "foo",
ReqHeader: map[string]string{ ReqHeader: map[string]string{
"TUS-Resumable": "1.0.0", "Tus-Resumable": "1.0.0",
}, },
Code: http.StatusNoContent, Code: http.StatusNoContent,
ResHeader: map[string]string{ ResHeader: map[string]string{
"Concat": "partial", "Upload-Concat": "partial",
}, },
}).Run(handler, t) }).Run(handler, t)
} }
@ -162,8 +162,8 @@ func TestConcatFinal(t *testing.T) {
Name: "Successful POST request", Name: "Successful POST request",
Method: "POST", Method: "POST",
ReqHeader: map[string]string{ ReqHeader: map[string]string{
"TUS-Resumable": "1.0.0", "Tus-Resumable": "1.0.0",
"Concat": "final; http://tus.io/files/a /files/b/", "Upload-Concat": "final; http://tus.io/files/a /files/b/",
}, },
Code: http.StatusCreated, Code: http.StatusCreated,
}).Run(handler, t) }).Run(handler, t)
@ -173,12 +173,12 @@ func TestConcatFinal(t *testing.T) {
Method: "HEAD", Method: "HEAD",
URL: "foo", URL: "foo",
ReqHeader: map[string]string{ ReqHeader: map[string]string{
"TUS-Resumable": "1.0.0", "Tus-Resumable": "1.0.0",
}, },
Code: http.StatusNoContent, Code: http.StatusNoContent,
ResHeader: map[string]string{ ResHeader: map[string]string{
"Concat": "final; http://tus.io/files/a http://tus.io/files/b", "Upload-Concat": "final; http://tus.io/files/a http://tus.io/files/b",
"Entity-Length": "10", "Upload-Length": "10",
}, },
}).Run(handler, t) }).Run(handler, t)
@ -186,8 +186,8 @@ func TestConcatFinal(t *testing.T) {
Name: "Concatenating non finished upload (id: c)", Name: "Concatenating non finished upload (id: c)",
Method: "POST", Method: "POST",
ReqHeader: map[string]string{ ReqHeader: map[string]string{
"TUS-Resumable": "1.0.0", "Tus-Resumable": "1.0.0",
"Concat": "final; http://tus.io/files/c", "Upload-Concat": "final; http://tus.io/files/c",
}, },
Code: http.StatusBadRequest, Code: http.StatusBadRequest,
}).Run(handler, t) }).Run(handler, t)
@ -204,8 +204,8 @@ func TestConcatFinal(t *testing.T) {
Name: "Exceeding MaxSize", Name: "Exceeding MaxSize",
Method: "POST", Method: "POST",
ReqHeader: map[string]string{ ReqHeader: map[string]string{
"TUS-Resumable": "1.0.0", "Tus-Resumable": "1.0.0",
"Concat": "final; http://tus.io/files/a /files/b/", "Upload-Concat": "final; http://tus.io/files/a /files/b/",
}, },
Code: http.StatusRequestEntityTooLarge, Code: http.StatusRequestEntityTooLarge,
}).Run(handler, t) }).Run(handler, t)

View File

@ -22,15 +22,15 @@ var reExtractFileID = regexp.MustCompile(`([^/]+)\/?$`)
var ( var (
ErrUnsupportedVersion = errors.New("unsupported version") ErrUnsupportedVersion = errors.New("unsupported version")
ErrMaxSizeExceeded = errors.New("maximum size exceeded") ErrMaxSizeExceeded = errors.New("maximum size exceeded")
ErrInvalidEntityLength = errors.New("missing or invalid Entity-Length header") ErrInvalidUploadLength = errors.New("missing or invalid Upload-Length header")
ErrInvalidOffset = errors.New("missing or invalid Offset header") ErrInvalidOffset = errors.New("missing or invalid Upload-Offset header")
ErrNotFound = errors.New("upload not found") ErrNotFound = errors.New("upload not found")
ErrFileLocked = errors.New("file currently locked") ErrFileLocked = errors.New("file currently locked")
ErrIllegalOffset = errors.New("illegal offset") ErrIllegalOffset = errors.New("illegal offset")
ErrSizeExceeded = errors.New("resource's size exceeded") ErrSizeExceeded = errors.New("resource's size exceeded")
ErrNotImplemented = errors.New("feature not implemented") ErrNotImplemented = errors.New("feature not implemented")
ErrUploadNotFinished = errors.New("one of the partial uploads is not finished") ErrUploadNotFinished = errors.New("one of the partial uploads is not finished")
ErrInvalidConcat = errors.New("invalid Concat header") ErrInvalidConcat = errors.New("invalid Upload-Concat header")
ErrModifyFinal = errors.New("modifying a final upload is not allowed") ErrModifyFinal = errors.New("modifying a final upload is not allowed")
) )
@ -38,7 +38,7 @@ var (
var ErrStatusCodes = map[error]int{ var ErrStatusCodes = map[error]int{
ErrUnsupportedVersion: http.StatusPreconditionFailed, ErrUnsupportedVersion: http.StatusPreconditionFailed,
ErrMaxSizeExceeded: http.StatusRequestEntityTooLarge, ErrMaxSizeExceeded: http.StatusRequestEntityTooLarge,
ErrInvalidEntityLength: http.StatusBadRequest, ErrInvalidUploadLength: http.StatusBadRequest,
ErrInvalidOffset: http.StatusBadRequest, ErrInvalidOffset: http.StatusBadRequest,
ErrNotFound: http.StatusNotFound, ErrNotFound: http.StatusNotFound,
ErrFileLocked: 423, // Locked (WebDAV) (RFC 4918) ErrFileLocked: 423, // Locked (WebDAV) (RFC 4918)
@ -122,27 +122,27 @@ func (handler *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if r.Method == "OPTIONS" { if r.Method == "OPTIONS" {
// Preflight request // Preflight request
header.Set("Access-Control-Allow-Methods", "POST, HEAD, PATCH, OPTIONS") header.Set("Access-Control-Allow-Methods", "POST, HEAD, PATCH, OPTIONS")
header.Set("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Entity-Length, Offset, TUS-Resumable, Metadata") header.Set("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Upload-Length, Upload-Offset, Tus-Resumable, Upload-Metadata")
header.Set("Access-Control-Max-Age", "86400") header.Set("Access-Control-Max-Age", "86400")
} else { } else {
// Actual request // Actual request
header.Set("Access-Control-Expose-Headers", "Offset, Location, Entity-Length, TUS-Version, TUS-Resumable, TUS-Max-Size, TUS-Extension, Metadata") header.Set("Access-Control-Expose-Headers", "Upload-Offset, Location, Upload-Length, Tus-Version, TUS-Resumable, TUS-Max-Size, TUS-Extension, Upload-Metadata")
} }
} }
// Set current version used by the server // Set current version used by the server
header.Set("TUS-Resumable", "1.0.0") header.Set("Tus-Resumable", "1.0.0")
// Set appropriated headers in case of OPTIONS method allowing protocol // Set appropriated headers in case of OPTIONS method allowing protocol
// discovery and end with an 204 No Content // discovery and end with an 204 No Content
if r.Method == "OPTIONS" { if r.Method == "OPTIONS" {
if handler.config.MaxSize > 0 { if handler.config.MaxSize > 0 {
header.Set("TUS-Max-Size", strconv.FormatInt(handler.config.MaxSize, 10)) header.Set("Tus-Max-Size", strconv.FormatInt(handler.config.MaxSize, 10))
} }
header.Set("TUS-Version", "1.0.0") header.Set("Tus-Version", "1.0.0")
header.Set("TUS-Extension", "file-creation,concatenation,termination") header.Set("Tus-Extension", "file-creation,concatenation,termination")
w.WriteHeader(http.StatusNoContent) w.WriteHeader(http.StatusNoContent)
return return
@ -151,7 +151,7 @@ func (handler *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// Test if the version sent by the client is supported // Test if the version sent by the client is supported
// GET methods are not checked since a browser may visit this URL and does // GET methods are not checked since a browser may visit this URL and does
// not include this header. This request is not part of the specification. // not include this header. This request is not part of the specification.
if r.Method != "GET" && r.Header.Get("TUS-Resumable") != "1.0.0" { if r.Method != "GET" && r.Header.Get("Tus-Resumable") != "1.0.0" {
handler.sendError(w, ErrUnsupportedVersion) handler.sendError(w, ErrUnsupportedVersion)
return return
} }
@ -163,8 +163,8 @@ func (handler *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// Create a new file upload using the datastore after validating the length // Create a new file upload using the datastore after validating the length
// and parsing the metadata. // and parsing the metadata.
func (handler *Handler) postFile(w http.ResponseWriter, r *http.Request) { func (handler *Handler) postFile(w http.ResponseWriter, r *http.Request) {
// Parse Concat header // Parse Upload-Concat header
isPartial, isFinal, partialUploads, err := parseConcat(r.Header.Get("Concat")) isPartial, isFinal, partialUploads, err := parseConcat(r.Header.Get("Upload-Concat"))
if err != nil { if err != nil {
handler.sendError(w, err) handler.sendError(w, err)
return return
@ -172,7 +172,7 @@ func (handler *Handler) postFile(w http.ResponseWriter, r *http.Request) {
// If the upload is a final upload created by concatenation multiple partial // If the upload is a final upload created by concatenation multiple partial
// uploads the size is sum of all sizes of these files (no need for // uploads the size is sum of all sizes of these files (no need for
// Entity-Length header) // Upload-Length header)
var size int64 var size int64
if isFinal { if isFinal {
size, err = handler.sizeOfUploads(partialUploads) size, err = handler.sizeOfUploads(partialUploads)
@ -181,9 +181,9 @@ func (handler *Handler) postFile(w http.ResponseWriter, r *http.Request) {
return return
} }
} else { } else {
size, err = strconv.ParseInt(r.Header.Get("Entity-Length"), 10, 64) size, err = strconv.ParseInt(r.Header.Get("Upload-Length"), 10, 64)
if err != nil || size < 0 { if err != nil || size < 0 {
handler.sendError(w, ErrInvalidEntityLength) handler.sendError(w, ErrInvalidUploadLength)
return return
} }
} }
@ -195,7 +195,7 @@ func (handler *Handler) postFile(w http.ResponseWriter, r *http.Request) {
} }
// Parse metadata // Parse metadata
meta := parseMeta(r.Header.Get("Metadata")) meta := parseMeta(r.Header.Get("Upload-Metadata"))
info := FileInfo{ info := FileInfo{
Size: size, Size: size,
@ -232,9 +232,9 @@ func (handler *Handler) headFile(w http.ResponseWriter, r *http.Request) {
return return
} }
// Add Concat header if possible // Add Upload-Concat header if possible
if info.IsPartial { if info.IsPartial {
w.Header().Set("Concat", "partial") w.Header().Set("Upload-Concat", "partial")
} }
if info.IsFinal { if info.IsFinal {
@ -242,15 +242,15 @@ func (handler *Handler) headFile(w http.ResponseWriter, r *http.Request) {
for _, uploadID := range info.PartialUploads { for _, uploadID := range info.PartialUploads {
v += " " + handler.absFileURL(r, uploadID) v += " " + handler.absFileURL(r, uploadID)
} }
w.Header().Set("Concat", v) w.Header().Set("Upload-Concat", v)
} }
if len(info.MetaData) != 0 { if len(info.MetaData) != 0 {
w.Header().Set("Metadata", serializeMeta(info.MetaData)) w.Header().Set("Upload-Metadata", serializeMeta(info.MetaData))
} }
w.Header().Set("Entity-Length", strconv.FormatInt(info.Size, 10)) w.Header().Set("Upload-Length", strconv.FormatInt(info.Size, 10))
w.Header().Set("Offset", strconv.FormatInt(info.Offset, 10)) w.Header().Set("Upload-Offset", strconv.FormatInt(info.Offset, 10))
w.WriteHeader(http.StatusNoContent) w.WriteHeader(http.StatusNoContent)
} }
@ -286,7 +286,7 @@ func (handler *Handler) patchFile(w http.ResponseWriter, r *http.Request) {
} }
// Ensure the offsets match // Ensure the offsets match
offset, err := strconv.ParseInt(r.Header.Get("Offset"), 10, 64) offset, err := strconv.ParseInt(r.Header.Get("Upload-Offset"), 10, 64)
if err != nil { if err != nil {
handler.sendError(w, ErrInvalidOffset) handler.sendError(w, ErrInvalidOffset)
return return
@ -466,8 +466,8 @@ func (handler *Handler) fillFinalUpload(id string, uploads []string) error {
return handler.dataStore.WriteChunk(id, 0, reader) return handler.dataStore.WriteChunk(id, 0, reader)
} }
// Parse the Metadata header as defined in the File Creation extension. // Parse the Upload-Metadata header as defined in the File Creation extension.
// e.g. Metadata: name bHVucmpzLnBuZw==,type aW1hZ2UvcG5n // e.g. Upload-Metadata: name bHVucmpzLnBuZw==,type aW1hZ2UvcG5n
func parseMeta(header string) map[string]string { func parseMeta(header string) map[string]string {
meta := make(map[string]string) meta := make(map[string]string)
@ -494,9 +494,9 @@ func parseMeta(header string) map[string]string {
return meta return meta
} }
// Serialize a map of strings into the Metadata header format used in the // Serialize a map of strings into the Upload-Metadata header format used in the
// response for HEAD requests. // response for HEAD requests.
// e.g. Metadata: name bHVucmpzLnBuZw==,type aW1hZ2UvcG5n // e.g. Upload-Metadata: name bHVucmpzLnBuZw==,type aW1hZ2UvcG5n
func serializeMeta(meta map[string]string) string { func serializeMeta(meta map[string]string) string {
header := "" header := ""
for key, value := range meta { for key, value := range meta {
@ -512,9 +512,9 @@ func serializeMeta(meta map[string]string) string {
return header return header
} }
// Parse the Concat header, e.g. // Parse the Upload-Concat header, e.g.
// Concat: partial // Upload-Concat: partial
// Concat: final; http://tus.io/files/a /files/b/ // Upload-Concat: final; http://tus.io/files/a /files/b/
func parseConcat(header string) (isPartial bool, isFinal bool, partialUploads []string, err error) { func parseConcat(header string) (isPartial bool, isFinal bool, partialUploads []string, err error) {
if len(header) == 0 { if len(header) == 0 {
return return

View File

@ -36,13 +36,13 @@ func TestHead(t *testing.T) {
Method: "HEAD", Method: "HEAD",
URL: "yes", URL: "yes",
ReqHeader: map[string]string{ ReqHeader: map[string]string{
"TUS-Resumable": "1.0.0", "Tus-Resumable": "1.0.0",
}, },
Code: http.StatusNoContent, Code: http.StatusNoContent,
ResHeader: map[string]string{ ResHeader: map[string]string{
"Offset": "11", "Upload-Offset": "11",
"Entity-Length": "44", "Upload-Length": "44",
"Metadata": "name bHVucmpzLnBuZw==,type aW1hZ2UvcG5n", "Upload-Metadata": "name bHVucmpzLnBuZw==,type aW1hZ2UvcG5n",
}, },
}).Run(handler, t) }).Run(handler, t)
@ -51,7 +51,7 @@ func TestHead(t *testing.T) {
Method: "HEAD", Method: "HEAD",
URL: "no", URL: "no",
ReqHeader: map[string]string{ ReqHeader: map[string]string{
"TUS-Resumable": "1.0.0", "Tus-Resumable": "1.0.0",
}, },
Code: http.StatusNotFound, Code: http.StatusNotFound,
}).Run(handler, t) }).Run(handler, t)

View File

@ -15,10 +15,10 @@ func TestOptions(t *testing.T) {
Method: "OPTIONS", Method: "OPTIONS",
Code: http.StatusNoContent, Code: http.StatusNoContent,
ResHeader: map[string]string{ ResHeader: map[string]string{
"TUS-Extension": "file-creation,concatenation,termination", "Tus-Extension": "file-creation,concatenation,termination",
"TUS-Version": "1.0.0", "Tus-Version": "1.0.0",
"TUS-Resumable": "1.0.0", "Tus-Resumable": "1.0.0",
"TUS-Max-Size": "400", "Tus-Max-Size": "400",
}, },
}).Run(handler, t) }).Run(handler, t)
@ -26,7 +26,7 @@ func TestOptions(t *testing.T) {
Name: "Invalid or unsupported version", Name: "Invalid or unsupported version",
Method: "POST", Method: "POST",
ReqHeader: map[string]string{ ReqHeader: map[string]string{
"TUS-Resumable": "foo", "Tus-Resumable": "foo",
}, },
Code: http.StatusPreconditionFailed, Code: http.StatusPreconditionFailed,
}).Run(handler, t) }).Run(handler, t)

View File

@ -61,8 +61,8 @@ func TestPatch(t *testing.T) {
Method: "PATCH", Method: "PATCH",
URL: "yes", URL: "yes",
ReqHeader: map[string]string{ ReqHeader: map[string]string{
"TUS-Resumable": "1.0.0", "Tus-Resumable": "1.0.0",
"Offset": "5", "Upload-Offset": "5",
}, },
ReqBody: strings.NewReader("hello"), ReqBody: strings.NewReader("hello"),
Code: http.StatusNoContent, Code: http.StatusNoContent,
@ -73,8 +73,8 @@ func TestPatch(t *testing.T) {
Method: "PATCH", Method: "PATCH",
URL: "no", URL: "no",
ReqHeader: map[string]string{ ReqHeader: map[string]string{
"TUS-Resumable": "1.0.0", "Tus-Resumable": "1.0.0",
"Offset": "5", "Upload-Offset": "5",
}, },
Code: http.StatusNotFound, Code: http.StatusNotFound,
}).Run(handler, t) }).Run(handler, t)
@ -84,8 +84,8 @@ func TestPatch(t *testing.T) {
Method: "PATCH", Method: "PATCH",
URL: "yes", URL: "yes",
ReqHeader: map[string]string{ ReqHeader: map[string]string{
"TUS-Resumable": "1.0.0", "Tus-Resumable": "1.0.0",
"Offset": "4", "Upload-Offset": "4",
}, },
Code: http.StatusConflict, Code: http.StatusConflict,
}).Run(handler, t) }).Run(handler, t)
@ -95,8 +95,8 @@ func TestPatch(t *testing.T) {
Method: "PATCH", Method: "PATCH",
URL: "yes", URL: "yes",
ReqHeader: map[string]string{ ReqHeader: map[string]string{
"TUS-Resumable": "1.0.0", "Tus-Resumable": "1.0.0",
"Offset": "5", "Upload-Offset": "5",
}, },
ReqBody: strings.NewReader("hellothisismorethan15bytes"), ReqBody: strings.NewReader("hellothisismorethan15bytes"),
Code: http.StatusRequestEntityTooLarge, Code: http.StatusRequestEntityTooLarge,
@ -190,8 +190,8 @@ func TestPatchOverflow(t *testing.T) {
Method: "PATCH", Method: "PATCH",
URL: "yes", URL: "yes",
ReqHeader: map[string]string{ ReqHeader: map[string]string{
"TUS-Resumable": "1.0.0", "Tus-Resumable": "1.0.0",
"Offset": "5", "Upload-Offset": "5",
"Content-Length": "3", "Content-Length": "3",
}, },
ReqBody: body, ReqBody: body,

View File

@ -44,9 +44,9 @@ func TestPost(t *testing.T) {
Name: "Successful request", Name: "Successful request",
Method: "POST", Method: "POST",
ReqHeader: map[string]string{ ReqHeader: map[string]string{
"TUS-Resumable": "1.0.0", "Tus-Resumable": "1.0.0",
"Entity-Length": "300", "Upload-Length": "300",
"Metadata": "foo aGVsbG8=, bar d29ybGQ=", "Upload-Metadata": "foo aGVsbG8=, bar d29ybGQ=",
}, },
Code: http.StatusCreated, Code: http.StatusCreated,
ResHeader: map[string]string{ ResHeader: map[string]string{
@ -58,9 +58,9 @@ func TestPost(t *testing.T) {
Name: "Exceeding MaxSize", Name: "Exceeding MaxSize",
Method: "POST", Method: "POST",
ReqHeader: map[string]string{ ReqHeader: map[string]string{
"TUS-Resumable": "1.0.0", "Tus-Resumable": "1.0.0",
"Entity-Length": "500", "Upload-Length": "500",
"Metadata": "foo aGVsbG8=, bar d29ybGQ=", "Upload-Metadata": "foo aGVsbG8=, bar d29ybGQ=",
}, },
Code: http.StatusRequestEntityTooLarge, Code: http.StatusRequestEntityTooLarge,
}).Run(handler, t) }).Run(handler, t)

View File

@ -29,7 +29,7 @@ func TestTerminate(t *testing.T) {
Method: "DELETE", Method: "DELETE",
URL: "foo", URL: "foo",
ReqHeader: map[string]string{ ReqHeader: map[string]string{
"TUS-Resumable": "1.0.0", "Tus-Resumable": "1.0.0",
}, },
Code: http.StatusNoContent, Code: http.StatusNoContent,
}).Run(handler, t) }).Run(handler, t)