Commit 045f9df4 authored by Alexandre Cesaro's avatar Alexandre Cesaro Committed by Brad Fitzpatrick

mime/quotedprintable: create the package

This commit creates the mime/quotedprintable package. It moves and
exports the QP reader of mime/internal/quotedprintable.

The code is almost unchanged to preserve the commit history.

Updates #4943

Change-Id: I4b7b5a2a40a4c84346d42e4cdd2c11a91b28f9e3
Reviewed-on: https://go-review.googlesource.com/5940
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: default avatarBrad Fitzpatrick <bradfitz@golang.org>
parent 26438d4d
...@@ -311,7 +311,7 @@ var pkgDeps = map[string][]string{ ...@@ -311,7 +311,7 @@ var pkgDeps = map[string][]string{
"crypto/x509/pkix": {"L4", "CRYPTO-MATH"}, "crypto/x509/pkix": {"L4", "CRYPTO-MATH"},
// Simple net+crypto-aware packages. // Simple net+crypto-aware packages.
"mime/multipart": {"L4", "OS", "mime", "crypto/rand", "net/textproto", "mime/internal/quotedprintable"}, "mime/multipart": {"L4", "OS", "mime", "crypto/rand", "net/textproto", "mime/quotedprintable"},
"net/smtp": {"L4", "CRYPTO", "NET", "crypto/tls"}, "net/smtp": {"L4", "CRYPTO", "NET", "crypto/tls"},
// HTTP, kingpin of dependencies. // HTTP, kingpin of dependencies.
......
...@@ -19,7 +19,7 @@ import ( ...@@ -19,7 +19,7 @@ import (
"io" "io"
"io/ioutil" "io/ioutil"
"mime" "mime"
"mime/internal/quotedprintable" "mime/quotedprintable"
"net/textproto" "net/textproto"
) )
......
...@@ -2,12 +2,8 @@ ...@@ -2,12 +2,8 @@
// Use of this source code is governed by a BSD-style // Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file. // license that can be found in the LICENSE file.
// The file define a quoted-printable decoder, as specified in RFC 2045. // Package quotedprintable implements quoted-printable encoding as specified by
// Deviations: // RFC 2045.
// 1. in addition to "=\r\n", "=\n" is also treated as soft line break.
// 2. it will pass through a '\r' or '\n' not preceded by '=', consistent
// with other broken QP encoders & decoders.
package quotedprintable package quotedprintable
import ( import (
...@@ -17,14 +13,19 @@ import ( ...@@ -17,14 +13,19 @@ import (
"io" "io"
) )
type qpReader struct { // Deviations from RFC 2045:
// 1. in addition to "=\r\n", "=\n" is also treated as soft line break.
// 2. it will pass through a '\r' or '\n' not preceded by '=', consistent
// with other broken QP encoders & decoders.
type reader struct {
br *bufio.Reader br *bufio.Reader
rerr error // last read error rerr error // last read error
line []byte // to be consumed before more of br line []byte // to be consumed before more of br
} }
// NewReader returns a quoted-printable reader, decoding from r.
func NewReader(r io.Reader) io.Reader { func NewReader(r io.Reader) io.Reader {
return &qpReader{ return &reader{
br: bufio.NewReader(r), br: bufio.NewReader(r),
} }
} }
...@@ -36,10 +37,10 @@ func fromHex(b byte) (byte, error) { ...@@ -36,10 +37,10 @@ func fromHex(b byte) (byte, error) {
case b >= 'A' && b <= 'F': case b >= 'A' && b <= 'F':
return b - 'A' + 10, nil return b - 'A' + 10, nil
} }
return 0, fmt.Errorf("multipart: invalid quoted-printable hex byte 0x%02x", b) return 0, fmt.Errorf("quotedprintable: invalid hex byte 0x%02x", b)
} }
func (q *qpReader) readHexByte(v []byte) (b byte, err error) { func (q *reader) readHexByte(v []byte) (b byte, err error) {
if len(v) < 2 { if len(v) < 2 {
return 0, io.ErrUnexpectedEOF return 0, io.ErrUnexpectedEOF
} }
...@@ -67,7 +68,7 @@ var ( ...@@ -67,7 +68,7 @@ var (
softSuffix = []byte("=") softSuffix = []byte("=")
) )
func (q *qpReader) Read(p []byte) (n int, err error) { func (q *reader) Read(p []byte) (n int, err error) {
for len(p) > 0 { for len(p) > 0 {
if len(q.line) == 0 { if len(q.line) == 0 {
if q.rerr != nil { if q.rerr != nil {
...@@ -84,7 +85,7 @@ func (q *qpReader) Read(p []byte) (n int, err error) { ...@@ -84,7 +85,7 @@ func (q *qpReader) Read(p []byte) (n int, err error) {
rightStripped := wholeLine[len(q.line):] rightStripped := wholeLine[len(q.line):]
q.line = q.line[:len(q.line)-1] q.line = q.line[:len(q.line)-1]
if !bytes.HasPrefix(rightStripped, lf) && !bytes.HasPrefix(rightStripped, crlf) { if !bytes.HasPrefix(rightStripped, lf) && !bytes.HasPrefix(rightStripped, crlf) {
q.rerr = fmt.Errorf("multipart: invalid bytes after =: %q", rightStripped) q.rerr = fmt.Errorf("quotedprintable: invalid bytes after =: %q", rightStripped)
} }
} else if hasLF { } else if hasLF {
if hasCR { if hasCR {
...@@ -107,7 +108,7 @@ func (q *qpReader) Read(p []byte) (n int, err error) { ...@@ -107,7 +108,7 @@ func (q *qpReader) Read(p []byte) (n int, err error) {
case b == '\t' || b == '\r' || b == '\n': case b == '\t' || b == '\r' || b == '\n':
break break
case b < ' ' || b > '~': case b < ' ' || b > '~':
return n, fmt.Errorf("multipart: invalid unescaped byte 0x%02x in quoted-printable body", b) return n, fmt.Errorf("quotedprintable: invalid unescaped byte 0x%02x in body", b)
} }
p[0] = b p[0] = b
p = p[1:] p = p[1:]
......
...@@ -19,7 +19,7 @@ import ( ...@@ -19,7 +19,7 @@ import (
"time" "time"
) )
func TestQuotedPrintable(t *testing.T) { func TestReader(t *testing.T) {
tests := []struct { tests := []struct {
in, want string in, want string
err interface{} err interface{}
...@@ -30,14 +30,14 @@ func TestQuotedPrintable(t *testing.T) { ...@@ -30,14 +30,14 @@ func TestQuotedPrintable(t *testing.T) {
{in: "foo bar=\n", want: "foo bar"}, {in: "foo bar=\n", want: "foo bar"},
{in: "foo bar\n", want: "foo bar\n"}, // somewhat lax. {in: "foo bar\n", want: "foo bar\n"}, // somewhat lax.
{in: "foo bar=0", want: "foo bar", err: io.ErrUnexpectedEOF}, {in: "foo bar=0", want: "foo bar", err: io.ErrUnexpectedEOF},
{in: "foo bar=ab", want: "foo bar", err: "multipart: invalid quoted-printable hex byte 0x61"}, {in: "foo bar=ab", want: "foo bar", err: "quotedprintable: invalid hex byte 0x61"},
{in: "foo bar=0D=0A", want: "foo bar\r\n"}, {in: "foo bar=0D=0A", want: "foo bar\r\n"},
{in: " A B \r\n C ", want: " A B\r\n C"}, {in: " A B \r\n C ", want: " A B\r\n C"},
{in: " A B =\r\n C ", want: " A B C"}, {in: " A B =\r\n C ", want: " A B C"},
{in: " A B =\n C ", want: " A B C"}, // lax. treating LF as CRLF {in: " A B =\n C ", want: " A B C"}, // lax. treating LF as CRLF
{in: "foo=\nbar", want: "foobar"}, {in: "foo=\nbar", want: "foobar"},
{in: "foo\x00bar", want: "foo", err: "multipart: invalid unescaped byte 0x00 in quoted-printable body"}, {in: "foo\x00bar", want: "foo", err: "quotedprintable: invalid unescaped byte 0x00 in body"},
{in: "foo bar\xff", want: "foo bar", err: "multipart: invalid unescaped byte 0xff in quoted-printable body"}, {in: "foo bar\xff", want: "foo bar", err: "quotedprintable: invalid unescaped byte 0xff in body"},
// Equal sign. // Equal sign.
{in: "=3D30\n", want: "=30\n"}, {in: "=3D30\n", want: "=30\n"},
...@@ -56,8 +56,8 @@ func TestQuotedPrintable(t *testing.T) { ...@@ -56,8 +56,8 @@ func TestQuotedPrintable(t *testing.T) {
// Different types of soft line-breaks. // Different types of soft line-breaks.
{in: "foo=\r\nbar", want: "foobar"}, {in: "foo=\r\nbar", want: "foobar"},
{in: "foo=\nbar", want: "foobar"}, {in: "foo=\nbar", want: "foobar"},
{in: "foo=\rbar", want: "foo", err: "multipart: invalid quoted-printable hex byte 0x0d"}, {in: "foo=\rbar", want: "foo", err: "quotedprintable: invalid hex byte 0x0d"},
{in: "foo=\r\r\r \nbar", want: "foo", err: `multipart: invalid bytes after =: "\r\r\r \n"`}, {in: "foo=\r\r\r \nbar", want: "foo", err: `quotedprintable: invalid bytes after =: "\r\r\r \n"`},
// Example from RFC 2045: // Example from RFC 2045:
{in: "Now's the time =\n" + "for all folk to come=\n" + " to the aid of their country.", {in: "Now's the time =\n" + "for all folk to come=\n" + " to the aid of their country.",
...@@ -101,7 +101,7 @@ var useQprint = flag.Bool("qprint", false, "Compare against the 'qprint' program ...@@ -101,7 +101,7 @@ var useQprint = flag.Bool("qprint", false, "Compare against the 'qprint' program
var badSoftRx = regexp.MustCompile(`=([^\r\n]+?\n)|([^\r\n]+$)|(\r$)|(\r[^\n]+\n)|( \r\n)`) var badSoftRx = regexp.MustCompile(`=([^\r\n]+?\n)|([^\r\n]+$)|(\r$)|(\r[^\n]+\n)|( \r\n)`)
func TestQPExhaustive(t *testing.T) { func TestExhaustive(t *testing.T) {
if *useQprint { if *useQprint {
_, err := exec.LookPath("qprint") _, err := exec.LookPath("qprint")
if err != nil { if err != nil {
...@@ -123,7 +123,7 @@ func TestQPExhaustive(t *testing.T) { ...@@ -123,7 +123,7 @@ func TestQPExhaustive(t *testing.T) {
errStr = "invalid bytes after =" errStr = "invalid bytes after ="
} }
res[errStr]++ res[errStr]++
if strings.Contains(errStr, "invalid quoted-printable hex byte ") { if strings.Contains(errStr, "invalid hex byte ") {
if strings.HasSuffix(errStr, "0x20") && (strings.Contains(s, "=0 ") || strings.Contains(s, "=A ") || strings.Contains(s, "= ")) { if strings.HasSuffix(errStr, "0x20") && (strings.Contains(s, "=0 ") || strings.Contains(s, "=A ") || strings.Contains(s, "= ")) {
return return
} }
...@@ -193,10 +193,10 @@ func TestQPExhaustive(t *testing.T) { ...@@ -193,10 +193,10 @@ func TestQPExhaustive(t *testing.T) {
got := strings.Join(outcomes, "\n") got := strings.Join(outcomes, "\n")
want := `OK: 21576 want := `OK: 21576
invalid bytes after =: 3397 invalid bytes after =: 3397
multipart: invalid quoted-printable hex byte 0x0a: 1400 quotedprintable: invalid hex byte 0x0a: 1400
multipart: invalid quoted-printable hex byte 0x0d: 2700 quotedprintable: invalid hex byte 0x0d: 2700
multipart: invalid quoted-printable hex byte 0x20: 2490 quotedprintable: invalid hex byte 0x20: 2490
multipart: invalid quoted-printable hex byte 0x3d: 440 quotedprintable: invalid hex byte 0x3d: 440
unexpected EOF: 3122` unexpected EOF: 3122`
if got != want { if got != want {
t.Errorf("Got:\n%s\nWant:\n%s", got, want) t.Errorf("Got:\n%s\nWant:\n%s", got, want)
......
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