Skip to content

Commit

Permalink
net/mail: propagate unsupported charset error
Browse files Browse the repository at this point in the history
Fixes #6807.

LGTM=iant
R=golang-codereviews, iant
CC=golang-codereviews
https://golang.org/cl/95060043
  • Loading branch information
crawshaw committed May 7, 2014
1 parent 5d25189 commit 8bc1bfb
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/pkg/net/mail/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ func (p *addrParser) consumePhrase() (phrase string, err error) {
// Ignore any error if we got at least one word.
if err != nil && len(words) == 0 {
debug.Printf("consumePhrase: hit err: %v", err)
return "", errors.New("mail: missing word in phrase")
return "", fmt.Errorf("mail: missing word in phrase: %v", err)
}
phrase = strings.Join(words, " ")
return phrase, nil
Expand Down Expand Up @@ -442,11 +442,11 @@ func (p *addrParser) len() int {
func decodeRFC2047Word(s string) (string, error) {
fields := strings.Split(s, "?")
if len(fields) != 5 || fields[0] != "=" || fields[4] != "=" {
return "", errors.New("mail: address not RFC 2047 encoded")
return "", errors.New("address not RFC 2047 encoded")
}
charset, enc := strings.ToLower(fields[1]), strings.ToLower(fields[2])
if charset != "iso-8859-1" && charset != "utf-8" {
return "", fmt.Errorf("mail: charset not supported: %q", charset)
return "", fmt.Errorf("charset not supported: %q", charset)
}

in := bytes.NewBufferString(fields[3])
Expand All @@ -457,7 +457,7 @@ func decodeRFC2047Word(s string) (string, error) {
case "q":
r = qDecoder{r: in}
default:
return "", fmt.Errorf("mail: RFC 2047 encoding not supported: %q", enc)
return "", fmt.Errorf("RFC 2047 encoding not supported: %q", enc)
}

dec, err := ioutil.ReadAll(r)
Expand Down
9 changes: 9 additions & 0 deletions src/pkg/net/mail/message_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"bytes"
"io/ioutil"
"reflect"
"strings"
"testing"
"time"
)
Expand Down Expand Up @@ -116,6 +117,14 @@ func TestDateParsing(t *testing.T) {
}
}

func TestAddressParsingError(t *testing.T) {
const txt = "=?iso-8859-2?Q?Bogl=E1rka_Tak=E1cs?= <unknown@gmail.com>"
_, err := ParseAddress(txt)
if err == nil || !strings.Contains(err.Error(), "charset not supported") {
t.Errorf(`mail.ParseAddress(%q) err: %q, want ".*charset not supported.*"`, txt, err)
}
}

func TestAddressParsing(t *testing.T) {
tests := []struct {
addrsStr string
Expand Down

0 comments on commit 8bc1bfb

Please sign in to comment.