Commit 8bc1bfb6 authored by David Crawshaw's avatar David Crawshaw

net/mail: propagate unsupported charset error

Fixes #6807.

LGTM=iant
R=golang-codereviews, iant
CC=golang-codereviews
https://golang.org/cl/95060043
parent 5d25189d
...@@ -363,7 +363,7 @@ func (p *addrParser) consumePhrase() (phrase string, err error) { ...@@ -363,7 +363,7 @@ func (p *addrParser) consumePhrase() (phrase string, err error) {
// Ignore any error if we got at least one word. // Ignore any error if we got at least one word.
if err != nil && len(words) == 0 { if err != nil && len(words) == 0 {
debug.Printf("consumePhrase: hit err: %v", err) 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, " ") phrase = strings.Join(words, " ")
return phrase, nil return phrase, nil
...@@ -442,11 +442,11 @@ func (p *addrParser) len() int { ...@@ -442,11 +442,11 @@ func (p *addrParser) len() int {
func decodeRFC2047Word(s string) (string, error) { func decodeRFC2047Word(s string) (string, error) {
fields := strings.Split(s, "?") fields := strings.Split(s, "?")
if len(fields) != 5 || fields[0] != "=" || fields[4] != "=" { 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]) charset, enc := strings.ToLower(fields[1]), strings.ToLower(fields[2])
if charset != "iso-8859-1" && charset != "utf-8" { 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]) in := bytes.NewBufferString(fields[3])
...@@ -457,7 +457,7 @@ func decodeRFC2047Word(s string) (string, error) { ...@@ -457,7 +457,7 @@ func decodeRFC2047Word(s string) (string, error) {
case "q": case "q":
r = qDecoder{r: in} r = qDecoder{r: in}
default: 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) dec, err := ioutil.ReadAll(r)
......
...@@ -8,6 +8,7 @@ import ( ...@@ -8,6 +8,7 @@ import (
"bytes" "bytes"
"io/ioutil" "io/ioutil"
"reflect" "reflect"
"strings"
"testing" "testing"
"time" "time"
) )
...@@ -116,6 +117,14 @@ func TestDateParsing(t *testing.T) { ...@@ -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) { func TestAddressParsing(t *testing.T) {
tests := []struct { tests := []struct {
addrsStr string addrsStr string
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment