From 8b1a251f6f92e99c49fdccba95fca3573f570c8c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felix=20Geisendo=CC=88rfer?= Date: Fri, 3 May 2013 13:02:24 +0200 Subject: [PATCH] Final-Length header --- src/http/data_store.go | 6 +++--- src/http/handler.go | 17 ++++++++++++++++- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/src/http/data_store.go b/src/http/data_store.go index 30a2a42..15a0c3d 100644 --- a/src/http/data_store.go +++ b/src/http/data_store.go @@ -24,7 +24,7 @@ func newDataStore(dir string, maxSize int64) *DataStore { return store } -func (s *DataStore) CreateFile(id string, size int64, contentType string, contentDisposition string) error { +func (s *DataStore) CreateFile(id string, size int64, meta map[string]string) error { file, err := os.OpenFile(s.filePath(id), os.O_CREATE|os.O_WRONLY, 0666) if err != nil { return err @@ -33,8 +33,8 @@ func (s *DataStore) CreateFile(id string, size int64, contentType string, conten entry := logEntry{Meta: &metaEntry{ Size: size, - ContentType: contentType, - ContentDisposition: contentDisposition, + ContentType: "", + ContentDisposition: "", }} return s.appendFileLog(id, entry) } diff --git a/src/http/handler.go b/src/http/handler.go index 99601ad..09b8e40 100644 --- a/src/http/handler.go +++ b/src/http/handler.go @@ -6,6 +6,7 @@ import ( "net/http" "os" "path" + "strconv" "strings" ) @@ -89,6 +90,19 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) { func (h *Handler) createFile(w http.ResponseWriter, r *http.Request) { id := uid() + + finalLength, err := strconv.ParseInt(r.Header.Get("Final-Length"), 10, 64) + if err != nil { + err = errors.New("invalid Final-Length header: "+err.Error()) + h.err(err, w, http.StatusBadRequest) + return + } + + if err := h.store.CreateFile(id, finalLength, nil); err != nil { + h.err(err, w, http.StatusInternalServerError) + return + } + w.Header().Set("Location", h.absUrl(r, "/"+id)) } @@ -97,7 +111,8 @@ func (h *Handler) createFile(w http.ResponseWriter, r *http.Request) { // // @TODO: Look at r.TLS to determine the url scheme. // @TODO: Make url prefix user configurable (optional) to deal with reverse -// proxies. +// proxies. This could be done by turning BasePath into BaseURL that +// that could be relative or absolute. func (h *Handler) absUrl(r *http.Request, relPath string) string { return "http://" + r.Host + path.Clean(h.config.BasePath+relPath) }