Send empty response Body for HEAD requests

Fixes #26
This commit is contained in:
Marius 2015-11-02 15:30:57 +01:00
parent 52eb00b2f8
commit e13c87d966
1 changed files with 29 additions and 21 deletions

View File

@ -168,7 +168,7 @@ func (handler *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// 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, r, ErrUnsupportedVersion)
return return
} }
@ -182,7 +182,7 @@ func (handler *Handler) postFile(w http.ResponseWriter, r *http.Request) {
// Parse Upload-Concat header // Parse Upload-Concat header
isPartial, isFinal, partialUploads, err := parseConcat(r.Header.Get("Upload-Concat")) isPartial, isFinal, partialUploads, err := parseConcat(r.Header.Get("Upload-Concat"))
if err != nil { if err != nil {
handler.sendError(w, err) handler.sendError(w, r, err)
return return
} }
@ -193,20 +193,20 @@ func (handler *Handler) postFile(w http.ResponseWriter, r *http.Request) {
if isFinal { if isFinal {
size, err = handler.sizeOfUploads(partialUploads) size, err = handler.sizeOfUploads(partialUploads)
if err != nil { if err != nil {
handler.sendError(w, err) handler.sendError(w, r, err)
return return
} }
} else { } else {
size, err = strconv.ParseInt(r.Header.Get("Upload-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, ErrInvalidUploadLength) handler.sendError(w, r, ErrInvalidUploadLength)
return return
} }
} }
// Test whether the size is still allowed // Test whether the size is still allowed
if handler.config.MaxSize > 0 && size > handler.config.MaxSize { if handler.config.MaxSize > 0 && size > handler.config.MaxSize {
handler.sendError(w, ErrMaxSizeExceeded) handler.sendError(w, r, ErrMaxSizeExceeded)
return return
} }
@ -223,13 +223,13 @@ func (handler *Handler) postFile(w http.ResponseWriter, r *http.Request) {
id, err := handler.dataStore.NewUpload(info) id, err := handler.dataStore.NewUpload(info)
if err != nil { if err != nil {
handler.sendError(w, err) handler.sendError(w, r, err)
return return
} }
if isFinal { if isFinal {
if err := handler.fillFinalUpload(id, partialUploads); err != nil { if err := handler.fillFinalUpload(id, partialUploads); err != nil {
handler.sendError(w, err) handler.sendError(w, r, err)
return return
} }
} }
@ -241,10 +241,11 @@ func (handler *Handler) postFile(w http.ResponseWriter, r *http.Request) {
// Returns the length and offset for the HEAD request // Returns the length and offset for the HEAD request
func (handler *Handler) headFile(w http.ResponseWriter, r *http.Request) { func (handler *Handler) headFile(w http.ResponseWriter, r *http.Request) {
id := r.URL.Query().Get(":id") id := r.URL.Query().Get(":id")
info, err := handler.dataStore.GetInfo(id) info, err := handler.dataStore.GetInfo(id)
if err != nil { if err != nil {
handler.sendError(w, err) handler.sendError(w, r, err)
return return
} }
@ -278,7 +279,7 @@ func (handler *Handler) patchFile(w http.ResponseWriter, r *http.Request) {
// Ensure file is not locked // Ensure file is not locked
if _, ok := handler.locks[id]; ok { if _, ok := handler.locks[id]; ok {
handler.sendError(w, ErrFileLocked) handler.sendError(w, r, ErrFileLocked)
return return
} }
@ -292,25 +293,25 @@ func (handler *Handler) patchFile(w http.ResponseWriter, r *http.Request) {
info, err := handler.dataStore.GetInfo(id) info, err := handler.dataStore.GetInfo(id)
if err != nil { if err != nil {
handler.sendError(w, err) handler.sendError(w, r, err)
return return
} }
// Modifying a final upload is not allowed // Modifying a final upload is not allowed
if info.IsFinal { if info.IsFinal {
handler.sendError(w, ErrModifyFinal) handler.sendError(w, r, ErrModifyFinal)
return return
} }
// Ensure the offsets match // Ensure the offsets match
offset, err := strconv.ParseInt(r.Header.Get("Upload-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, r, ErrInvalidOffset)
return return
} }
if offset != info.Offset { if offset != info.Offset {
handler.sendError(w, ErrIllegalOffset) handler.sendError(w, r, ErrIllegalOffset)
return return
} }
@ -319,7 +320,7 @@ func (handler *Handler) patchFile(w http.ResponseWriter, r *http.Request) {
// Test if this upload fits into the file's size // Test if this upload fits into the file's size
if offset+length > info.Size { if offset+length > info.Size {
handler.sendError(w, ErrSizeExceeded) handler.sendError(w, r, ErrSizeExceeded)
return return
} }
@ -333,7 +334,7 @@ func (handler *Handler) patchFile(w http.ResponseWriter, r *http.Request) {
bytesWritten, err := handler.dataStore.WriteChunk(id, offset, reader) bytesWritten, err := handler.dataStore.WriteChunk(id, offset, reader)
if err != nil { if err != nil {
handler.sendError(w, err) handler.sendError(w, r, err)
return return
} }
@ -356,7 +357,7 @@ func (handler *Handler) getFile(w http.ResponseWriter, r *http.Request) {
// Ensure file is not locked // Ensure file is not locked
if _, ok := handler.locks[id]; ok { if _, ok := handler.locks[id]; ok {
handler.sendError(w, ErrFileLocked) handler.sendError(w, r, ErrFileLocked)
return return
} }
@ -370,7 +371,7 @@ func (handler *Handler) getFile(w http.ResponseWriter, r *http.Request) {
info, err := handler.dataStore.GetInfo(id) info, err := handler.dataStore.GetInfo(id)
if err != nil { if err != nil {
handler.sendError(w, err) handler.sendError(w, r, err)
return return
} }
@ -383,7 +384,7 @@ func (handler *Handler) getFile(w http.ResponseWriter, r *http.Request) {
// Get reader // Get reader
src, err := handler.dataStore.GetReader(id) src, err := handler.dataStore.GetReader(id)
if err != nil { if err != nil {
handler.sendError(w, err) handler.sendError(w, r, err)
return return
} }
@ -403,7 +404,7 @@ func (handler *Handler) delFile(w http.ResponseWriter, r *http.Request) {
// Ensure file is not locked // Ensure file is not locked
if _, ok := handler.locks[id]; ok { if _, ok := handler.locks[id]; ok {
handler.sendError(w, ErrFileLocked) handler.sendError(w, r, ErrFileLocked)
return return
} }
@ -417,7 +418,7 @@ func (handler *Handler) delFile(w http.ResponseWriter, r *http.Request) {
err := handler.dataStore.Terminate(id) err := handler.dataStore.Terminate(id)
if err != nil { if err != nil {
handler.sendError(w, err) handler.sendError(w, r, err)
return return
} }
@ -426,7 +427,7 @@ func (handler *Handler) delFile(w http.ResponseWriter, r *http.Request) {
// Send the error in the response body. The status code will be looked up in // Send the error in the response body. The status code will be looked up in
// ErrStatusCodes. If none is found 500 Internal Error will be used. // ErrStatusCodes. If none is found 500 Internal Error will be used.
func (handler *Handler) sendError(w http.ResponseWriter, err error) { func (handler *Handler) sendError(w http.ResponseWriter, r *http.Request, err error) {
// Interpret os.ErrNotExist as 404 Not Found // Interpret os.ErrNotExist as 404 Not Found
if os.IsNotExist(err) { if os.IsNotExist(err) {
err = ErrNotFound err = ErrNotFound
@ -436,7 +437,14 @@ func (handler *Handler) sendError(w http.ResponseWriter, err error) {
if !ok { if !ok {
status = 500 status = 500
} }
reason := err.Error()
if r.Method == "HEAD" {
reason = ""
}
w.Header().Set("Content-Type", "text/plain") w.Header().Set("Content-Type", "text/plain")
w.Header().Set("Content-Length", strconv.Itoa(len(reason)))
w.WriteHeader(status) w.WriteHeader(status)
w.Write([]byte(err.Error() + "\n")) w.Write([]byte(err.Error() + "\n"))
} }