From 515c1ca33cbc0b959a87d1a0d0f535db75acc475 Mon Sep 17 00:00:00 2001 From: Stephan Wienczny Date: Tue, 19 May 2015 14:32:48 +0200 Subject: [PATCH] 'Set' headers instead of 'Add' to prevent duplicate header fields --- api/v1.go | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/api/v1.go b/api/v1.go index 9974c38..41370d8 100644 --- a/api/v1.go +++ b/api/v1.go @@ -152,8 +152,8 @@ func (apiv1 *APIv1) download(w http.ResponseWriter, req *http.Request) { apiv1.defaultOptions(w, req) - w.Header().Add("Content-Type", "message/rfc822") - w.Header().Add("Content-Disposition", "attachment; filename=\""+id+".eml\"") + w.Header().Set("Content-Type", "message/rfc822") + w.Header().Set("Content-Disposition", "attachment; filename=\""+id+".eml\"") switch apiv1.config.Storage.(type) { case *storage.MongoDB: @@ -185,17 +185,25 @@ func (apiv1 *APIv1) download_part(w http.ResponseWriter, req *http.Request) { // TODO extension from content-type? apiv1.defaultOptions(w, req) - w.Header().Add("Content-Disposition", "attachment; filename=\""+id+"-part-"+part+"\"") + w.Header().Set("Content-Disposition", "attachment; filename=\""+id+"-part-"+part+"\"") message, _ := apiv1.config.Storage.Load(id) contentTransferEncoding := "" pid, _ := strconv.Atoi(part) for h, l := range message.MIME.Parts[pid].Headers { for _, v := range l { - if strings.ToLower(h) == "content-transfer-encoding" && contentTransferEncoding == "" { - contentTransferEncoding = v + switch strings.ToLower(h) { + case "content-disposition": + // Prevent duplicate "content-disposition" + w.Header().Set(h, v) + case "content-transfer-encoding": + if contentTransferEncoding == "" { + contentTransferEncoding = v + } + fallthrough + default: + w.Header().Add(h, v) } - w.Header().Add(h, v) } } body := []byte(message.MIME.Parts[pid].Body)