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

View File

@ -22,15 +22,15 @@ var reExtractFileID = regexp.MustCompile(`([^/]+)\/?$`)
var (
ErrUnsupportedVersion = errors.New("unsupported version")
ErrMaxSizeExceeded = errors.New("maximum size exceeded")
ErrInvalidEntityLength = errors.New("missing or invalid Entity-Length header")
ErrInvalidOffset = errors.New("missing or invalid Offset header")
ErrInvalidUploadLength = errors.New("missing or invalid Upload-Length header")
ErrInvalidOffset = errors.New("missing or invalid Upload-Offset header")
ErrNotFound = errors.New("upload not found")
ErrFileLocked = errors.New("file currently locked")
ErrIllegalOffset = errors.New("illegal offset")
ErrSizeExceeded = errors.New("resource's size exceeded")
ErrNotImplemented = errors.New("feature not implemented")
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")
)
@ -38,7 +38,7 @@ var (
var ErrStatusCodes = map[error]int{
ErrUnsupportedVersion: http.StatusPreconditionFailed,
ErrMaxSizeExceeded: http.StatusRequestEntityTooLarge,
ErrInvalidEntityLength: http.StatusBadRequest,
ErrInvalidUploadLength: http.StatusBadRequest,
ErrInvalidOffset: http.StatusBadRequest,
ErrNotFound: http.StatusNotFound,
ErrFileLocked: 423, // Locked (WebDAV) (RFC 4918)
@ -122,27 +122,27 @@ func (handler *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if r.Method == "OPTIONS" {
// Preflight request
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")
} else {
// 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
header.Set("TUS-Resumable", "1.0.0")
header.Set("Tus-Resumable", "1.0.0")
// Set appropriated headers in case of OPTIONS method allowing protocol
// discovery and end with an 204 No Content
if r.Method == "OPTIONS" {
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-Extension", "file-creation,concatenation,termination")
header.Set("Tus-Version", "1.0.0")
header.Set("Tus-Extension", "file-creation,concatenation,termination")
w.WriteHeader(http.StatusNoContent)
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
// 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.
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)
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
// and parsing the metadata.
func (handler *Handler) postFile(w http.ResponseWriter, r *http.Request) {
// Parse Concat header
isPartial, isFinal, partialUploads, err := parseConcat(r.Header.Get("Concat"))
// Parse Upload-Concat header
isPartial, isFinal, partialUploads, err := parseConcat(r.Header.Get("Upload-Concat"))
if err != nil {
handler.sendError(w, err)
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
// uploads the size is sum of all sizes of these files (no need for
// Entity-Length header)
// Upload-Length header)
var size int64
if isFinal {
size, err = handler.sizeOfUploads(partialUploads)
@ -181,9 +181,9 @@ func (handler *Handler) postFile(w http.ResponseWriter, r *http.Request) {
return
}
} 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 {
handler.sendError(w, ErrInvalidEntityLength)
handler.sendError(w, ErrInvalidUploadLength)
return
}
}
@ -195,7 +195,7 @@ func (handler *Handler) postFile(w http.ResponseWriter, r *http.Request) {
}
// Parse metadata
meta := parseMeta(r.Header.Get("Metadata"))
meta := parseMeta(r.Header.Get("Upload-Metadata"))
info := FileInfo{
Size: size,
@ -232,9 +232,9 @@ func (handler *Handler) headFile(w http.ResponseWriter, r *http.Request) {
return
}
// Add Concat header if possible
// Add Upload-Concat header if possible
if info.IsPartial {
w.Header().Set("Concat", "partial")
w.Header().Set("Upload-Concat", "partial")
}
if info.IsFinal {
@ -242,15 +242,15 @@ func (handler *Handler) headFile(w http.ResponseWriter, r *http.Request) {
for _, uploadID := range info.PartialUploads {
v += " " + handler.absFileURL(r, uploadID)
}
w.Header().Set("Concat", v)
w.Header().Set("Upload-Concat", v)
}
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("Offset", strconv.FormatInt(info.Offset, 10))
w.Header().Set("Upload-Length", strconv.FormatInt(info.Size, 10))
w.Header().Set("Upload-Offset", strconv.FormatInt(info.Offset, 10))
w.WriteHeader(http.StatusNoContent)
}
@ -286,7 +286,7 @@ func (handler *Handler) patchFile(w http.ResponseWriter, r *http.Request) {
}
// 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 {
handler.sendError(w, ErrInvalidOffset)
return
@ -466,8 +466,8 @@ func (handler *Handler) fillFinalUpload(id string, uploads []string) error {
return handler.dataStore.WriteChunk(id, 0, reader)
}
// Parse the Metadata header as defined in the File Creation extension.
// e.g. Metadata: name bHVucmpzLnBuZw==,type aW1hZ2UvcG5n
// Parse the Upload-Metadata header as defined in the File Creation extension.
// e.g. Upload-Metadata: name bHVucmpzLnBuZw==,type aW1hZ2UvcG5n
func parseMeta(header string) map[string]string {
meta := make(map[string]string)
@ -494,9 +494,9 @@ func parseMeta(header string) map[string]string {
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.
// e.g. Metadata: name bHVucmpzLnBuZw==,type aW1hZ2UvcG5n
// e.g. Upload-Metadata: name bHVucmpzLnBuZw==,type aW1hZ2UvcG5n
func serializeMeta(meta map[string]string) string {
header := ""
for key, value := range meta {
@ -512,9 +512,9 @@ func serializeMeta(meta map[string]string) string {
return header
}
// Parse the Concat header, e.g.
// Concat: partial
// Concat: final; http://tus.io/files/a /files/b/
// Parse the Upload-Concat header, e.g.
// Upload-Concat: partial
// Upload-Concat: final; http://tus.io/files/a /files/b/
func parseConcat(header string) (isPartial bool, isFinal bool, partialUploads []string, err error) {
if len(header) == 0 {
return

View File

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

View File

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

View File

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

View File

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

View File

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