1
0
mirror of https://github.com/golang/go synced 2024-11-20 07:04:40 -07:00

net/mail: allow us-ascii encoding

Fixes #6611.

LGTM=bradfitz
R=bradfitz
CC=golang-codereviews
https://golang.org/cl/14990045
This commit is contained in:
Russ Cox 2014-09-16 17:40:33 -04:00
parent a325f4f2b3
commit 06e4b06893
2 changed files with 22 additions and 1 deletions

View File

@ -28,6 +28,7 @@ import (
"strconv" "strconv"
"strings" "strings"
"time" "time"
"unicode"
) )
var debug = debugT(false) var debug = debugT(false)
@ -445,7 +446,7 @@ func decodeRFC2047Word(s string) (string, error) {
return "", errors.New("address not RFC 2047 encoded") return "", errors.New("address not RFC 2047 encoded")
} }
charset, enc := strings.ToLower(fields[1]), strings.ToLower(fields[2]) charset, enc := strings.ToLower(fields[1]), strings.ToLower(fields[2])
if charset != "iso-8859-1" && charset != "utf-8" { if charset != "us-ascii" && charset != "iso-8859-1" && charset != "utf-8" {
return "", fmt.Errorf("charset not supported: %q", charset) return "", fmt.Errorf("charset not supported: %q", charset)
} }
@ -466,6 +467,16 @@ func decodeRFC2047Word(s string) (string, error) {
} }
switch charset { switch charset {
case "us-ascii":
b := new(bytes.Buffer)
for _, c := range dec {
if c >= 0x80 {
b.WriteRune(unicode.ReplacementChar)
} else {
b.WriteRune(rune(c))
}
}
return b.String(), nil
case "iso-8859-1": case "iso-8859-1":
b := new(bytes.Buffer) b := new(bytes.Buffer)
for _, c := range dec { for _, c := range dec {

View File

@ -194,6 +194,16 @@ func TestAddressParsing(t *testing.T) {
}, },
}, },
}, },
// RFC 2047 "Q"-encoded US-ASCII address. Dumb but legal.
{
`=?us-ascii?q?J=6Frg_Doe?= <joerg@example.com>`,
[]*Address{
{
Name: `Jorg Doe`,
Address: "joerg@example.com",
},
},
},
// RFC 2047 "Q"-encoded UTF-8 address. // RFC 2047 "Q"-encoded UTF-8 address.
{ {
`=?utf-8?q?J=C3=B6rg_Doe?= <joerg@example.com>`, `=?utf-8?q?J=C3=B6rg_Doe?= <joerg@example.com>`,