diff options
author | Koni Marti <koni.marti@gmail.com> | 2022-06-02 14:43:34 +0200 |
---|---|---|
committer | Robin Jarry <robin@jarry.cc> | 2022-06-07 16:19:27 +0200 |
commit | 955f7683af104b98ec67ef259f0cbe4c8f2a52ed (patch) | |
tree | 61e08f711914fd25cb65ceccb5636cf74692f00c /worker/lib/parse.go | |
parent | 8b6f9719a84fa0ee31d84b9e864495af4f166d92 (diff) | |
download | aerc-955f7683af104b98ec67ef259f0cbe4c8f2a52ed.tar.gz |
parse: fix content-type parsing error
If an error occurs when parsing the content-type, check if the
content-type is quoted; if so, remove quotes. If this is not the case,
then return a text/plain content-type as a sane fallback option, so that
the message can be at least viewed in plaintext.
Fixes: https://todo.sr.ht/~rjarry/aerc/44
Signed-off-by: Koni Marti <koni.marti@gmail.com>
Acked-by: Robin Jarry <robin@jarry.cc>
Diffstat (limited to 'worker/lib/parse.go')
-rw-r--r-- | worker/lib/parse.go | 27 |
1 files changed, 26 insertions, 1 deletions
diff --git a/worker/lib/parse.go b/worker/lib/parse.go index 5d95046c..bdd927f9 100644 --- a/worker/lib/parse.go +++ b/worker/lib/parse.go @@ -64,12 +64,37 @@ func splitMIME(m string) (string, string) { return parts[0], parts[1] } +func fixContentType(h message.Header) (string, map[string]string) { + ct, rest := h.Get("Content-Type"), "" + if i := strings.Index(ct, ";"); i > 0 { + ct, rest = ct[:i], ct[i:] + } + + // check if there are quotes around the content type + if strings.Contains(ct, "\"") { + header := strings.ReplaceAll(ct, "\"", "") + if rest != "" { + header += rest + } + h.Set("Content-Type", header) + if contenttype, params, err := h.ContentType(); err == nil { + return contenttype, params + } + } + + // if all else fails, return text/plain + return "text/plain", nil +} + func ParseEntityStructure(e *message.Entity) (*models.BodyStructure, error) { var body models.BodyStructure contentType, ctParams, err := e.Header.ContentType() if err != nil { - return nil, fmt.Errorf("could not parse content type: %v", err) + // try to fix the error; if all measures fail, then return a + // text/plain content type to display at least plaintext + contentType, ctParams = fixContentType(e.Header) } + mimeType, mimeSubType := splitMIME(contentType) body.MIMEType = mimeType body.MIMESubType = mimeSubType |