diff options
Diffstat (limited to 'src/rhimportd/uploadWeb.go')
-rw-r--r-- | src/rhimportd/uploadWeb.go | 34 |
1 files changed, 17 insertions, 17 deletions
diff --git a/src/rhimportd/uploadWeb.go b/src/rhimportd/uploadWeb.go index e014f71..7d1f460 100644 --- a/src/rhimportd/uploadWeb.go +++ b/src/rhimportd/uploadWeb.go @@ -60,22 +60,18 @@ const ( webUploadMaxRequestSize = (2 << 30) - 1 // 2GB, (2 << 30) overflows int on 32-bit systems therefore we use 2GB - 1 Byte ) -func webUploadParseForm(w http.ResponseWriter, r *http.Request) (username, sessionid, srcfile string, src *multipart.Part) { - mpr, err := r.MultipartReader() - if err != nil { - rhl.Printf("WebUploadHandler: error while parsing multipart-form: %v", err) - webUploadErrorResponse(w, http.StatusBadRequest, err.Error()) +func webUploadParseForm(r *http.Request) (username, sessionid, srcfile string, src *multipart.Part, err error) { + var mpr *multipart.Reader + if mpr, err = r.MultipartReader(); err != nil { return } for { - p, err := mpr.NextPart() - if err == io.EOF { - return - } - if err != nil { - rhl.Printf("WebUploadHandler: error while parsing multipart-form: %v", err) - webUploadErrorResponse(w, http.StatusBadRequest, err.Error()) + var p *multipart.Part + if p, err = mpr.NextPart(); err != nil { + if err == io.EOF { + err = nil + } return } var dstfield *string @@ -87,16 +83,14 @@ func webUploadParseForm(w http.ResponseWriter, r *http.Request) (username, sessi case "FILENAME": srcfile = p.FileName() src = p - return // don't read any fileds beyond this point because we would need to load the whole file in order to continue parsing + return // don't read any fields beyond this point because we would need to load the whole file in order to continue parsing default: rhdl.Printf("WebUploadHandler: ingoring unknown form field: '%s'", p.FormName()) continue } var buf bytes.Buffer - if _, err := io.CopyN(&buf, p, 1<<10); err != nil && err != io.EOF { // 1kB should be enough - rhl.Printf("WebUploadHandler: error while extracting form field: %v", err) - webUploadErrorResponse(w, http.StatusBadRequest, err.Error()) + if _, err = io.CopyN(&buf, p, 1<<10); err != nil && err != io.EOF { // 1kB should be enough return } *dstfield = buf.String() @@ -124,7 +118,12 @@ func webUploadHandler(conf *rhimport.Config, db *rddb.DBChan, sessions *rhimport } r.Body = http.MaxBytesReader(w, r.Body, webUploadMaxRequestSize) - username, sessionid, srcfile, src := webUploadParseForm(w, r) + username, sessionid, srcfile, src, err := webUploadParseForm(r) + if err != nil { + rhl.Printf("WebUploadHandler: error while parsing multipart-form: %v", err) + webUploadErrorResponse(w, http.StatusBadRequest, err.Error()) + return + } if username == "" { webUploadErrorResponse(w, http.StatusBadRequest, "missing field LOGIN_NAME") return @@ -166,6 +165,7 @@ func webUploadHandler(conf *rhimport.Config, db *rddb.DBChan, sessions *rhimport } chunk.Error = err if err == io.EOF { + rhl.Printf("WebUploadHandler: upload for file '%s' finished", srcfile) webUploadSuccessResponse(w) return } |