]> Cypherpunks repositories - gostls13.git/commitdiff
net/mail: correctly handle whitespaces when formatting an email address
authorJakub Ryszard Czarnowicz <j.czarnowicz@gmail.com>
Thu, 6 Feb 2014 23:49:10 +0000 (10:49 +1100)
committerDavid Symonds <dsymonds@golang.org>
Thu, 6 Feb 2014 23:49:10 +0000 (10:49 +1100)
Whitespace characters are allowed in quoted-string according to RFC 5322 without
being "Q"-encoding. Address.String() already always formats the name portion in
quoted string, so whitespace characters should be allowed in there.

Fixes #6641.

LGTM=dave, dsymonds
R=golang-codereviews, gobot, dsymonds, dave
CC=golang-codereviews
https://golang.org/cl/55770043

src/pkg/net/mail/message.go
src/pkg/net/mail/message_test.go

index dc2ab44dab247c0e5eb1b2329a99fe70130da9d0..4b332c1b5be6e8ffd2cf95e1eaf1246f341f9ee8 100644 (file)
@@ -159,7 +159,9 @@ func (a *Address) String() string {
        // If every character is printable ASCII, quoting is simple.
        allPrintable := true
        for i := 0; i < len(a.Name); i++ {
-               if !isVchar(a.Name[i]) {
+               // isWSP here should actually be isFWS,
+               // but we don't support folding yet.
+               if !isVchar(a.Name[i]) && !isWSP(a.Name[i]) {
                        allPrintable = false
                        break
                }
@@ -167,7 +169,7 @@ func (a *Address) String() string {
        if allPrintable {
                b := bytes.NewBufferString(`"`)
                for i := 0; i < len(a.Name); i++ {
-                       if !isQtext(a.Name[i]) {
+                       if !isQtext(a.Name[i]) && !isWSP(a.Name[i]) {
                                b.WriteByte('\\')
                        }
                        b.WriteByte(a.Name[i])
@@ -535,3 +537,9 @@ func isVchar(c byte) bool {
        // Visible (printing) characters.
        return '!' <= c && c <= '~'
 }
+
+// isWSP returns true if c is a WSP (white space).
+// WSP is a space or horizontal tab (RFC5234 Appendix B).
+func isWSP(c byte) bool {
+       return c == ' ' || c == '\t'
+}
index 3c037f38385209ec3b23555d7d5c0a9475359370..1bb4e8bc40137e6c72d9049b03d7b55c1014ea04 100644 (file)
@@ -277,6 +277,14 @@ func TestAddressFormatting(t *testing.T) {
                        &Address{Name: "Böb", Address: "bob@example.com"},
                        `=?utf-8?q?B=C3=B6b?= <bob@example.com>`,
                },
+               {
+                       &Address{Name: "Bob Jane", Address: "bob@example.com"},
+                       `"Bob Jane" <bob@example.com>`,
+               },
+               {
+                       &Address{Name: "Böb Jacöb", Address: "bob@example.com"},
+                       `=?utf-8?q?B=C3=B6b_Jac=C3=B6b?= <bob@example.com>`,
+               },
        }
        for _, test := range tests {
                s := test.addr.String()