From 35eac5b9ad014c3f413b020d97d9586fc9315e5a Mon Sep 17 00:00:00 2001 From: binwiederhier Date: Fri, 21 Apr 2023 21:07:07 -0400 Subject: [PATCH] Simplify --- server/errors.go | 1 - server/server.go | 15 ++------------- server/util.go | 8 ++++++++ 3 files changed, 10 insertions(+), 14 deletions(-) diff --git a/server/errors.go b/server/errors.go index 862fa9d7..8e565197 100644 --- a/server/errors.go +++ b/server/errors.go @@ -106,7 +106,6 @@ var ( errHTTPBadRequestNotAPaidUser = &errHTTP{40027, http.StatusBadRequest, "invalid request: not a paid user", "", nil} errHTTPBadRequestBillingRequestInvalid = &errHTTP{40028, http.StatusBadRequest, "invalid request: not a valid billing request", "", nil} errHTTPBadRequestBillingSubscriptionExists = &errHTTP{40029, http.StatusBadRequest, "invalid request: billing subscription already exists", "", nil} - errHTTPBadRequestInvalidMimeHeader = &errHTTP{40030, http.StatusBadRequest, "invalid request: invalid MIME encoding of header", "", nil} errHTTPNotFound = &errHTTP{40401, http.StatusNotFound, "page not found", "", nil} errHTTPUnauthorized = &errHTTP{40101, http.StatusUnauthorized, "unauthorized", "https://ntfy.sh/docs/publish/#authentication", nil} errHTTPForbidden = &errHTTP{40301, http.StatusForbidden, "forbidden", "https://ntfy.sh/docs/publish/#authentication", nil} diff --git a/server/server.go b/server/server.go index cae74e55..5c4f2a9e 100644 --- a/server/server.go +++ b/server/server.go @@ -843,17 +843,10 @@ func (s *Server) forwardPollRequest(v *visitor, m *message) { func (s *Server) parsePublishParams(r *http.Request, m *message) (cache bool, firebase bool, email string, unifiedpush bool, err *errHTTP) { cache = readBoolParam(r, true, "x-cache", "cache") firebase = readBoolParam(r, true, "x-firebase", "firebase") + m.Title = maybeDecodeHeader(readParam(r, "x-title", "title", "t")) m.Click = readParam(r, "x-click", "click") icon := readParam(r, "x-icon", "icon") filename := readParam(r, "x-filename", "filename", "file", "f") - title := readParam(r, "x-title", "title", "t") - if title != "" { - title, err := mimeDecoder.DecodeHeader(title) - if err != nil { - return false, false, "", false, errHTTPBadRequestInvalidMimeHeader.Wrap("invalid X-Title header: %s", err.Error()) - } - m.Title = title - } attach := readParam(r, "x-attach", "attach", "a") if attach != "" || filename != "" { m.Attachment = &attachment{} @@ -891,11 +884,7 @@ func (s *Server) parsePublishParams(r *http.Request, m *message) (cache bool, fi } messageStr := strings.ReplaceAll(readParam(r, "x-message", "message", "m"), "\\n", "\n") if messageStr != "" { - messageStr, err := mimeDecoder.DecodeHeader(messageStr) - if err != nil { - return false, false, "", false, errHTTPBadRequestInvalidMimeHeader.Wrap("invalid X-Message header: %s", err.Error()) - } - m.Message = messageStr + m.Message = maybeDecodeHeader(messageStr) } var e error m.Priority, e = util.ParsePriority(readParam(r, "x-priority", "priority", "prio", "p")) diff --git a/server/util.go b/server/util.go index 98a76630..f0b49d28 100644 --- a/server/util.go +++ b/server/util.go @@ -117,3 +117,11 @@ func fromContext[T any](r *http.Request, key contextKey) (T, error) { } return t, nil } + +func maybeDecodeHeader(header string) string { + decoded, err := mimeDecoder.DecodeHeader(header) + if err != nil { + return header + } + return decoded +}