3// todo: allow more invalid content-type values, we now stop parsing on: empty media type (eg "content-type: ; name=..."), empty value for property (eg "charset=", missing quotes for characters that should be quoted (eg boundary containing "=" but without quotes), duplicate properties (two charsets), empty pairs (eg "text/html;;").
4// todo: should we be forgiving when closing boundary in multipart message is missing? seems like spam messages do this...
5// todo: should we allow base64 messages where a line starts with a space? and possibly more whitespace. is happening in messages. coreutils base64 accepts it, encoding/base64 does not.
6// todo: handle comments in headers?
7// todo: should we just always store messages with \n instead of \r\n? \r\n seems easier for use with imap.
8// todo: can use a cleanup
19 "mime/quotedprintable"
26 "golang.org/x/text/encoding/ianaindex"
28 "github.com/mjl-/mox/mlog"
29 "github.com/mjl-/mox/smtp"
32// Pedantic enables stricter parsing.
36 ErrBadContentType = errors.New("bad content-type")
37 ErrHeader = errors.New("bad message header")
41 errNotMultipart = errors.New("not a multipart message")
42 errFirstBoundCloses = errors.New("first boundary cannot be finishing boundary")
43 errLineTooLong = errors.New("line too long")
44 errMissingBoundaryParam = errors.New("missing/empty boundary content-type parameter")
45 errMissingClosingBoundary = errors.New("eof without closing boundary")
46 errBareLF = errors.New("invalid bare line feed")
47 errBareCR = errors.New("invalid bare carriage return")
48 errUnexpectedEOF = errors.New("unexpected eof")
51// If set, during tests, attempts to reparse a part will cause an error, because sequentially reading parts should not lead to reparsing.
52var enforceSequential bool
54// Part represents a whole mail message, or a part of a multipart message. It
55// is designed to handle IMAP requirements efficiently.
57 BoundaryOffset int64 // Offset in message where bound starts. -1 for top-level message.
58 HeaderOffset int64 // Offset in message file where header starts.
59 BodyOffset int64 // Offset in message file where body starts.
60 EndOffset int64 // Where body of part ends. Set when part is fully read.
61 RawLineCount int64 // Number of lines in raw, undecoded, body of part. Set when part is fully read.
62 DecodedSize int64 // Number of octets when decoded. If this is a text mediatype, lines ending only in LF are changed end in CRLF and DecodedSize reflects that.
64 MediaType string // From Content-Type, upper case. E.g. "TEXT". Can be empty because content-type may be absent. In this case, the part may be treated as TEXT/PLAIN.
65 MediaSubType string // From Content-Type, upper case. E.g. "PLAIN".
66 ContentTypeParams map[string]string // E.g. holds "boundary" for multipart messages. Has lower-case keys, and original case values.
68 ContentDescription string
69 ContentTransferEncoding string // In upper case.
70 Envelope *Envelope // Email message headers. Not for non-message parts.
72 Parts []Part // Parts if this is a multipart.
74 // Only for message/rfc822 and message/global. This part may have a buffer as
75 // backing io.ReaderAt, because a message/global can have a non-identity
76 // content-transfer-encoding. This part has a nil parent.
80 header textproto.MIMEHeader // Parsed header.
81 nextBoundOffset int64 // If >= 0, the offset where the next part header starts. We can set this when a user fully reads each part.
82 lastBoundOffset int64 // Start of header of last/previous part. Used to skip a part if ParseNextPart is called and nextBoundOffset is -1.
83 parent *Part // Parent part, for getting bound from, and setting nextBoundOffset when a part has finished reading. Only for subparts, not top-level parts.
84 bound []byte // Only set if valid multipart with boundary, includes leading --, excludes \r\n.
85 strict bool // If set, valid crlf line endings are verified when reading body.
88// todo: have all Content* fields in Part?
89// todo: make Address contain a type Localpart and dns.Domain?
90// todo: if we ever make a major change and reparse all parts, switch to lower-case values if not too troublesome.
92// Envelope holds the basic/common message headers as used in IMAP4.
95 Subject string // Q/B-word-decoded.
102 InReplyTo string // From In-Reply-To header, includes <>.
103 MessageID string // From Message-Id header, includes <>.
106// Address as used in From and To headers.
108 Name string // Free-form name for display in mail applications.
109 User string // Localpart, encoded as string. Must be parsed before using as Localpart.
110 Host string // Domain in ASCII.
113// Parse reads the headers of the mail message and returns a part.
114// A part provides access to decoded and raw contents of a message and its multiple parts.
116// If strict is set, fewer attempts are made to continue parsing when errors are
117// encountered, such as with invalid content-type headers or bare carriage returns.
118func Parse(elog *slog.Logger, strict bool, r io.ReaderAt) (Part, error) {
119 log := mlog.New("message", elog)
120 return newPart(log, strict, r, 0, nil)
123// EnsurePart parses a part as with Parse, but ensures a usable part is always
124// returned, even if error is non-nil. If a parse error occurs, the message is
125// returned as application/octet-stream, and headers can still be read if they
128// If strict is set, fewer attempts are made to continue parsing when errors are
129// encountered, such as with invalid content-type headers or bare carriage returns.
130func EnsurePart(elog *slog.Logger, strict bool, r io.ReaderAt, size int64) (Part, error) {
131 log := mlog.New("message", elog)
132 p, err := Parse(log.Logger, strict, r)
134 err = p.Walk(log.Logger, nil)
137 np, err2 := fallbackPart(p, r, size)
146func fallbackPart(p Part, r io.ReaderAt, size int64) (Part, error) {
148 HeaderOffset: p.HeaderOffset,
149 BodyOffset: p.BodyOffset,
151 MediaType: "APPLICATION",
152 MediaSubType: "OCTET-STREAM",
153 ContentTypeParams: p.ContentTypeParams,
154 ContentID: p.ContentID,
155 ContentDescription: p.ContentDescription,
156 ContentTransferEncoding: p.ContentTransferEncoding,
157 Envelope: p.Envelope,
159 // - BoundaryOffset: irrelevant for top-level message.
160 // - RawLineCount and DecodedSize: set below.
161 // - Parts: we are not treating this as a multipart message.
164 // By reading body, the number of lines and decoded size will be set.
165 _, err := io.Copy(io.Discard, np.Reader())
169// SetReaderAt sets r as reader for this part and all its sub parts, recursively.
170// No reader is set for any Message subpart, see SetMessageReaderAt.
171func (p *Part) SetReaderAt(r io.ReaderAt) {
176 for i := range p.Parts {
182// SetMessageReaderAt sets a reader on p.Message, which must be non-nil.
183func (p *Part) SetMessageReaderAt() error {
184 // todo: if p.Message does not contain any non-identity content-transfer-encoding, we should set an offsetReader of p.Message, recursively.
185 buf, err := io.ReadAll(p.Reader())
189 p.Message.SetReaderAt(bytes.NewReader(buf))
193// Walk through message, decoding along the way, and collecting mime part offsets and sizes, and line counts.
194func (p *Part) Walk(elog *slog.Logger, parent *Part) error {
195 log := mlog.New("message", elog)
197 if len(p.bound) == 0 {
198 if p.MediaType == "MESSAGE" && (p.MediaSubType == "RFC822" || p.MediaSubType == "GLOBAL") {
199 // todo: don't read whole submessage in memory...
200 buf, err := io.ReadAll(p.Reader())
204 br := bytes.NewReader(buf)
205 mp, err := Parse(log.Logger, p.strict, br)
207 return fmt.Errorf("parsing embedded message: %w", err)
209 if err := mp.Walk(log.Logger, nil); err != nil {
210 // If this is a DSN and we are not in pedantic mode, accept unexpected end of
211 // message. This is quite common because MTA's sometimes just truncate the original
212 // message in a place that makes the message invalid.
213 if errors.Is(err, errUnexpectedEOF) && !Pedantic && parent != nil && len(parent.Parts) >= 3 && p == &parent.Parts[2] && parent.MediaType == "MULTIPART" && parent.MediaSubType == "REPORT" {
214 mp, err = fallbackPart(mp, br, int64(len(buf)))
216 return fmt.Errorf("parsing invalid embedded message: %w", err)
219 return fmt.Errorf("parsing parts of embedded message: %w", err)
222 // todo: if mp does not contain any non-identity content-transfer-encoding, we should set an offsetReader of p.r on mp, recursively.
226 _, err := io.Copy(io.Discard, p.Reader())
231 pp, err := p.ParseNextPart(log.Logger)
238 if err := pp.Walk(log.Logger, p); err != nil {
244// String returns a debugging representation of the part.
245func (p *Part) String() string {
246 return fmt.Sprintf("&Part{%s/%s offsets %d/%d/%d/%d lines %d decodedsize %d next %d last %d bound %q parts %v}", p.MediaType, p.MediaSubType, p.BoundaryOffset, p.HeaderOffset, p.BodyOffset, p.EndOffset, p.RawLineCount, p.DecodedSize, p.nextBoundOffset, p.lastBoundOffset, p.bound, p.Parts)
249// newPart parses a new part, which can be the top-level message.
250// offset is the bound offset for parts, and the start of message for top-level messages. parent indicates if this is a top-level message or sub-part.
251// If an error occurs, p's exported values can still be relevant. EnsurePart uses these values.
252func newPart(log mlog.Log, strict bool, r io.ReaderAt, offset int64, parent *Part) (p Part, rerr error) {
264 b := &bufAt{strict: strict, r: r, offset: offset}
267 p.BoundaryOffset = offset
268 if line, _, err := b.ReadLine(true); err != nil {
270 } else if match, finish := checkBound(line, parent.bound); !match {
271 return p, fmt.Errorf("missing bound")
273 return p, fmt.Errorf("new part for closing boundary")
278 p.HeaderOffset = b.offset
279 p.BodyOffset = b.offset
280 hb := &bytes.Buffer{}
282 line, _, err := b.ReadLine(true)
288 return p, fmt.Errorf("reading header line: %w", err)
295 p.BodyOffset = b.offset
297 // Don't attempt to parse empty header, mail.ReadMessage doesn't like it.
298 if p.HeaderOffset == p.BodyOffset {
299 p.header = textproto.MIMEHeader{}
301 h, err := parseHeader(hb)
303 return p, fmt.Errorf("parsing header: %w", err)
308 ct := p.header.Get("Content-Type")
309 mt, params, err := mime.ParseMediaType(ct)
310 if err != nil && ct != "" {
311 if Pedantic || strict {
312 return p, fmt.Errorf("%w: %s: %q", ErrBadContentType, err, ct)
315 // Try parsing just a content-type, ignoring parameters.
317 ct = strings.TrimSpace(strings.SplitN(ct, ";", 2)[0])
318 t := strings.SplitN(ct, "/", 2)
319 isToken := func(s string) bool {
321 for _, c := range s {
322 if c < 0x20 || c >= 0x80 || strings.ContainsRune(separators, c) {
328 // We cannot recover content-type of multipart, we won't have a boundary.
329 if len(t) == 2 && isToken(t[0]) && !strings.EqualFold(t[0], "multipart") && isToken(t[1]) {
330 p.MediaType = strings.ToUpper(t[0])
331 p.MediaSubType = strings.ToUpper(t[1])
333 p.MediaType = "APPLICATION"
334 p.MediaSubType = "OCTET-STREAM"
336 log.Debugx("malformed content-type, attempting to recover and continuing", err,
337 slog.String("contenttype", p.header.Get("Content-Type")),
338 slog.String("mediatype", p.MediaType),
339 slog.String("mediasubtype", p.MediaSubType))
341 t := strings.SplitN(strings.ToUpper(mt), "/", 2)
343 if Pedantic || strict {
344 return p, fmt.Errorf("bad content-type: %q (content-type %q)", mt, ct)
346 log.Debug("malformed media-type, ignoring and continuing", slog.String("type", mt))
347 p.MediaType = "APPLICATION"
348 p.MediaSubType = "OCTET-STREAM"
351 p.MediaSubType = t[1]
352 p.ContentTypeParams = params
356 p.ContentID = p.header.Get("Content-Id")
357 p.ContentDescription = p.header.Get("Content-Description")
358 p.ContentTransferEncoding = strings.ToUpper(p.header.Get("Content-Transfer-Encoding"))
361 p.Envelope, err = parseEnvelope(log, mail.Header(p.header))
367 if p.MediaType == "MULTIPART" {
368 s := params["boundary"]
370 return p, errMissingBoundaryParam
372 p.bound = append([]byte("--"), s...)
374 // Discard preamble, before first boundary.
376 line, _, err := b.PeekLine(true)
378 return p, fmt.Errorf("parsing line for part preamble: %w", err)
381 // Well, for compatibility, we require whitespace after the boundary. Because some
382 // software use the same boundary but with text appended for sub parts.
383 if match, finish := checkBound(line, p.bound); match {
385 return p, errFirstBoundCloses
391 p.nextBoundOffset = b.offset
392 p.lastBoundOffset = b.offset
398// Header returns the parsed header of this part.
400// Returns a ErrHeader for messages with invalid header syntax.
401func (p *Part) Header() (textproto.MIMEHeader, error) {
405 if p.HeaderOffset == p.BodyOffset {
406 p.header = textproto.MIMEHeader{}
409 h, err := parseHeader(p.HeaderReader())
414// HeaderReader returns a reader for the header section of this part, including ending bare CRLF.
415func (p *Part) HeaderReader() io.Reader {
416 return io.NewSectionReader(p.r, p.HeaderOffset, p.BodyOffset-p.HeaderOffset)
419// parse a header, only call this on non-empty input (even though that is a valid header).
420func parseHeader(r io.Reader) (textproto.MIMEHeader, error) {
421 // We read using mail.ReadMessage instead of textproto.ReadMIMEHeaders because the
422 // first handles email messages properly, while the second only works for HTTP
424 var zero textproto.MIMEHeader
426 // We read the header and add the optional \r\n header/body separator. If the \r\n
427 // is missing, parsing with Go <1.21 results in an EOF error.
428 // todo: directly parse from reader r when Go 1.20 is no longer supported.
429 buf, err := io.ReadAll(r)
433 if bytes.HasSuffix(buf, []byte("\r\n")) && !bytes.HasSuffix(buf, []byte("\r\n\r\n")) {
434 buf = append(buf, "\r\n"...)
436 msg, err := mail.ReadMessage(bytes.NewReader(buf))
438 // Recognize parsing errors from net/mail.ReadMessage.
439 // todo: replace with own message parsing code that returns proper error types.
440 errstr := err.Error()
441 if strings.HasPrefix(errstr, "malformed initial line:") || strings.HasPrefix(errstr, "malformed header line:") {
442 err = fmt.Errorf("%w: %v", ErrHeader, err)
446 return textproto.MIMEHeader(msg.Header), nil
449var wordDecoder = mime.WordDecoder{
450 CharsetReader: func(charset string, r io.Reader) (io.Reader, error) {
451 switch strings.ToLower(charset) {
452 case "", "us-ascii", "utf-8":
455 enc, _ := ianaindex.MIME.Encoding(charset)
457 enc, _ = ianaindex.IANA.Encoding(charset)
460 return r, fmt.Errorf("unknown charset %q", charset)
462 return enc.NewDecoder().Reader(r), nil
466func parseEnvelope(log mlog.Log, h mail.Header) (*Envelope, error) {
469 // We currently marshal this field to JSON. But JSON cannot represent all
470 // time.Time. Time zone of 24:00 was seen in the wild. We won't try for extreme
471 // years, but we can readjust timezones.
472 // todo: remove this once we no longer store using json.
473 _, offset := date.Zone()
474 if date.Year() > 9999 {
476 } else if offset <= -24*3600 || offset >= 24*3600 {
477 date = time.Unix(date.Unix(), 0).UTC()
480 subject := h.Get("Subject")
481 if s, err := wordDecoder.DecodeHeader(subject); err == nil {
488 parseAddressList(log, h, "from"),
489 parseAddressList(log, h, "sender"),
490 parseAddressList(log, h, "reply-to"),
491 parseAddressList(log, h, "to"),
492 parseAddressList(log, h, "cc"),
493 parseAddressList(log, h, "bcc"),
494 h.Get("In-Reply-To"),
500func parseAddressList(log mlog.Log, h mail.Header, k string) []Address {
501 // todo: possibly work around ios mail generating incorrect q-encoded "phrases" with unencoded double quotes?
../rfc/2047:382
506 parser := mail.AddressParser{WordDecoder: &wordDecoder}
507 l, err := parser.ParseList(v)
512 for _, a := range l {
514 var user, host string
515 addr, err := smtp.ParseNetMailAddress(a.Address)
517 log.Infox("parsing address (continuing)", err, slog.Any("netmailaddress", a.Address))
519 user = addr.Localpart.String()
520 host = addr.Domain.ASCII
522 r = append(r, Address{a.Name, user, host})
527// ParseNextPart parses the next (sub)part of this multipart message.
528// ParseNextPart returns io.EOF and a nil part when there are no more parts.
529// Only used for initial parsing of message. Once parsed, use p.Parts.
530func (p *Part) ParseNextPart(elog *slog.Logger) (*Part, error) {
531 log := mlog.New("message", elog)
533 if len(p.bound) == 0 {
534 return nil, errNotMultipart
536 if p.nextBoundOffset == -1 {
537 if enforceSequential {
538 panic("access not sequential")
540 // Set nextBoundOffset by fully reading the last part.
541 last, err := newPart(log, p.strict, p.r, p.lastBoundOffset, p)
545 if _, err := io.Copy(io.Discard, last.RawReader()); err != nil {
548 if p.nextBoundOffset == -1 {
549 return nil, fmt.Errorf("internal error: reading part did not set nextBoundOffset")
552 b := &bufAt{strict: p.strict, r: p.r, offset: p.nextBoundOffset}
553 // todo: should we require a crlf on final closing bound? we don't require it because some message/rfc822 don't have a crlf after their closing boundary, so those messages don't end in crlf.
554 line, crlf, err := b.ReadLine(false)
558 if match, finish := checkBound(line, p.bound); !match {
559 return nil, fmt.Errorf("expected bound, got %q", line)
561 // Read any trailing data.
564 line, _, err := b.PeekLine(false)
568 if match, _ := checkBound(line, p.parent.bound); match {
573 if p.parent.lastBoundOffset == p.BoundaryOffset {
574 p.parent.nextBoundOffset = b.offset
577 p.EndOffset = b.offset
580 return nil, fmt.Errorf("non-finishing bound without crlf: %w", errUnexpectedEOF)
582 boundOffset := p.nextBoundOffset
583 p.lastBoundOffset = boundOffset
584 p.nextBoundOffset = -1
585 np, err := newPart(log, p.strict, p.r, boundOffset, p)
589 p.Parts = append(p.Parts, np)
590 return &p.Parts[len(p.Parts)-1], nil
593// IsDSN returns whether the MIME structure of the part is a DSN.
594func (p *Part) IsDSN() bool {
595 return p.MediaType == "MULTIPART" &&
596 p.MediaSubType == "REPORT" &&
598 p.Parts[1].MediaType == "MESSAGE" &&
599 (p.Parts[1].MediaSubType == "DELIVERY-STATUS" || p.Parts[1].MediaSubType == "GLOBAL-DELIVERY-STATUS")
602func hasNonASCII(r io.Reader) (bool, error) {
603 br := bufio.NewReader(r)
605 b, err := br.ReadByte()
608 } else if err != nil {
611 if b > unicode.MaxASCII {
618// NeedsSMTPUTF8 returns whether the part needs the SMTPUTF8 extension to be
619// transported, due to non-ascii in message headers.
620func (p *Part) NeedsSMTPUTF8() (bool, error) {
621 if has, err := hasNonASCII(p.HeaderReader()); err != nil {
622 return false, fmt.Errorf("reading header: %w", err)
626 for _, pp := range p.Parts {
627 if has, err := pp.NeedsSMTPUTF8(); err != nil || has {
634var ErrParamEncoding = errors.New("bad header parameter encoding")
636// DispositionFilename tries to parse the disposition header and the "filename"
637// parameter. If the filename parameter is absent or can't be parsed, the "name"
638// parameter from the Content-Type header is used for the filename. The returned
639// filename is decoded according to RFC 2231 or RFC 2047. This is a best-effort
640// attempt to find a filename for a part. If no Content-Disposition header, or
641// filename was found, empty values without error are returned.
643// If the returned error is an ErrParamEncoding, it can be treated as a diagnostic
644// and a filename may still be returned.
645func (p *Part) DispositionFilename() (disposition string, filename string, err error) {
648 return "", "", fmt.Errorf("parsing header: %v", err)
651 var params map[string]string
652 cd := h.Get("Content-Disposition")
654 disp, params, err = mime.ParseMediaType(cd)
657 return "", "", fmt.Errorf("%w: parsing disposition header: %v", ErrParamEncoding, err)
659 filename, err = tryDecodeParam(params["filename"])
661 s, err2 := tryDecodeParam(p.ContentTypeParams["name"])
667 return disp, filename, err
670// Attempt q/b-word-decode name, coming from Content-Type "name" field or
671// Content-Disposition "filename" field.
673// RFC 2231 specifies an encoding for non-ascii values in mime header parameters. But
674// it appears common practice to instead just q/b-word encode the values.
675// Thunderbird and gmail.com do this for the Content-Type "name" parameter.
676// gmail.com also does that for the Content-Disposition "filename" parameter, where
677// Thunderbird uses the RFC 2231-defined encoding. Go's mime.ParseMediaType parses
678// the mechanism specified in RFC 2231 only. The value for "name" we get here would
679// already be decoded properly for standards-compliant headers, like
680// "filename*0*=UTF-8”%...; filename*1*=%.... We'll look for Q/B-word encoding
681// markers ("=?"-prefix or "?="-suffix) and try to decode if present. This would
682// only cause trouble for filenames having this prefix/suffix.
683func tryDecodeParam(name string) (string, error) {
684 if name == "" || !strings.HasPrefix(name, "=?") && !strings.HasSuffix(name, "?=") {
687 // todo: find where this is allowed. it seems quite common. perhaps we should remove the pedantic check?
689 return name, fmt.Errorf("%w: attachment contains rfc2047 q/b-word-encoded mime parameter instead of rfc2231-encoded", ErrParamEncoding)
691 s, err := wordDecoder.DecodeHeader(name)
693 return name, fmt.Errorf("%w: q/b-word decoding mime parameter: %v", ErrParamEncoding, err)
698// Reader returns a reader for the decoded body content.
699func (p *Part) Reader() io.Reader {
700 return p.bodyReader(p.RawReader())
703// ReaderUTF8OrBinary returns a reader for the decoded body content, transformed to
704// utf-8 for known mime/iana encodings (only if they aren't us-ascii or utf-8
705// already). For unknown or missing character sets/encodings, the original reader
707func (p *Part) ReaderUTF8OrBinary() io.Reader {
708 return DecodeReader(p.ContentTypeParams["charset"], p.Reader())
711func (p *Part) bodyReader(r io.Reader) io.Reader {
712 r = newDecoder(p.ContentTransferEncoding, r)
713 if p.MediaType == "TEXT" {
714 return &textReader{p, bufio.NewReader(r), 0, false}
716 return &countReader{p, r, 0}
719// countReader is an io.Reader that passes Reads to the underlying reader.
720// when eof is read, it sets p.DecodedSize to the number of bytes returned.
721type countReader struct {
727func (cr *countReader) Read(buf []byte) (int, error) {
728 n, err := cr.r.Read(buf)
733 cr.p.DecodedSize = cr.count
738// textReader is an io.Reader that ensures all lines return end in CRLF.
739// when eof is read from the underlying reader, it sets p.DecodedSize.
740type textReader struct {
744 prevcr bool // If previous byte returned was a CR.
747func (tr *textReader) Read(buf []byte) (int, error) {
750 c, err := tr.r.ReadByte()
753 tr.p.DecodedSize = tr.count
756 if c == '\n' && !tr.prevcr {
764 tr.prevcr = c == '\r'
771func newDecoder(cte string, r io.Reader) io.Reader {
775 return base64.NewDecoder(base64.StdEncoding, r)
776 case "QUOTED-PRINTABLE":
777 return quotedprintable.NewReader(r)
782// RawReader returns a reader for the raw, undecoded body content. E.g. with
783// quoted-printable or base64 content intact.
784// Fully reading a part helps its parent part find its next part efficiently.
785func (p *Part) RawReader() io.Reader {
787 panic("missing reader")
789 if p.EndOffset >= 0 {
790 return &crlfReader{strict: p.strict, r: io.NewSectionReader(p.r, p.BodyOffset, p.EndOffset-p.BodyOffset)}
794 return &offsetReader{p, p.BodyOffset, p.strict, true, false, 0}
796 return &boundReader{p: p, b: &bufAt{strict: p.strict, r: p.r, offset: p.BodyOffset}, prevlf: true}
799// crlfReader verifies there are no bare newlines and optionally no bare carriage returns.
800type crlfReader struct {
806func (r *crlfReader) Read(buf []byte) (int, error) {
807 n, err := r.r.Read(buf)
808 if err == nil || err == io.EOF {
809 for _, b := range buf[:n] {
810 if b == '\n' && !r.prevcr {
813 } else if b != '\n' && r.prevcr && (r.strict || Pedantic) {
823// bufAt is a buffered reader on an underlying ReaderAt.
824// bufAt verifies that lines end with crlf.
826 offset int64 // Offset in r currently consumed, i.e. not including any buffered data.
830 buf []byte // Buffered data.
831 nbuf int // Valid bytes in buf.
835// Messages should not have lines longer than 78+2 bytes, and must not have
836// lines longer than 998+2 bytes. But in practice they have longer lines. We
837// have a higher limit, but for when parsing with strict we check for the 1000
840const maxLineLength = 8 * 1024
842func (b *bufAt) maxLineLength() int {
843 if b.strict || Pedantic {
849// ensure makes sure b.nbuf is up to maxLineLength, unless eof is encountered.
850func (b *bufAt) ensure() error {
851 for _, c := range b.buf[:b.nbuf] {
856 if b.scratch == nil {
857 b.scratch = make([]byte, b.maxLineLength())
860 b.buf = make([]byte, b.maxLineLength())
862 for b.nbuf < b.maxLineLength() {
863 n, err := b.r.ReadAt(b.buf[b.nbuf:], b.offset+int64(b.nbuf))
867 if err != nil && err != io.EOF || err == io.EOF && b.nbuf+n == 0 {
870 if n == 0 || err == io.EOF {
877// ReadLine reads a line until \r\n is found, returning the line including \r\n.
878// If not found, or a bare \n is encountered, or a bare \r is enountered in pedantic mode, ReadLine returns an error.
879func (b *bufAt) ReadLine(requirecrlf bool) (buf []byte, crlf bool, err error) {
880 return b.line(true, requirecrlf)
883func (b *bufAt) PeekLine(requirecrlf bool) (buf []byte, crlf bool, err error) {
884 return b.line(false, requirecrlf)
887func (b *bufAt) line(consume, requirecrlf bool) (buf []byte, crlf bool, err error) {
888 if err := b.ensure(); err != nil {
889 return nil, false, err
891 for i, c := range b.buf[:b.nbuf] {
893 // Should have seen a \r, which should have been handled below.
894 return nil, false, errBareLF
900 if i >= b.nbuf || b.buf[i] != '\n' {
901 if b.strict || Pedantic {
902 return nil, false, errBareCR
906 b.scratch = b.scratch[:i+1]
907 copy(b.scratch, b.buf[:i+1])
909 copy(b.buf, b.buf[i+1:])
910 b.offset += int64(i + 1)
913 return b.scratch, true, nil
915 if b.nbuf >= b.maxLineLength() {
916 return nil, false, errLineTooLong
919 return nil, false, errUnexpectedEOF
921 b.scratch = b.scratch[:b.nbuf]
922 copy(b.scratch, b.buf[:b.nbuf])
924 b.offset += int64(b.nbuf)
927 return b.scratch, false, nil
930// PeekByte returns the next unread byte, or an error.
931func (b *bufAt) PeekByte() (byte, error) {
932 if err := b.ensure(); err != nil {
941// offsetReader reads from p.r starting from offset, and RawLineCount on p.
942// offsetReader validates lines end with \r\n.
943type offsetReader struct {
952func (r *offsetReader) Read(buf []byte) (int, error) {
953 n, err := r.p.r.ReadAt(buf, r.offset)
957 if r.strict || Pedantic {
961 for _, c := range buf[:n] {
965 if err == nil || err == io.EOF {
966 if c == '\n' && !r.prevcr {
968 } else if c != '\n' && r.prevcr && (r.strict || Pedantic) {
977 } else if r.linelength > max && err == nil {
983 r.p.EndOffset = r.offset
988var crlf = []byte("\r\n")
990// boundReader is a reader that stops at a closing multipart boundary.
991// boundReader ensures lines end with crlf through its use of bufAt.
992type boundReader struct {
995 buf []byte // Data from previous line, to be served first.
996 nbuf int // Number of valid bytes in buf.
997 crlf []byte // Possible crlf, to be returned if we do not yet encounter a boundary.
998 prevlf bool // If last char returned was a newline. For counting lines.
1001func (b *boundReader) Read(buf []byte) (count int, rerr error) {
1005 for _, c := range origBuf[:count] {
1009 b.prevlf = c == '\n'
1015 // Read data from earlier line.
1021 copy(buf, b.buf[:n])
1022 copy(b.buf, b.buf[n:])
1031 // Look at next line. If it is a boundary, we are done and won't serve the crlf from the last line.
1032 line, _, err := b.b.PeekLine(false)
1033 if match, _ := checkBound(line, b.p.parent.bound); match {
1034 b.p.EndOffset = b.b.offset - int64(len(b.crlf))
1035 if b.p.parent.lastBoundOffset == b.p.BoundaryOffset {
1036 b.p.parent.nextBoundOffset = b.b.offset
1037 } else if enforceSequential {
1038 panic("access not sequential")
1040 return count, io.EOF
1043 err = errMissingClosingBoundary
1045 if err != nil && err != io.EOF {
1048 if len(b.crlf) > 0 {
1053 copy(buf, b.crlf[:n])
1061 line, _, err = b.b.ReadLine(true)
1063 // Could be an unexpected end of the part.
1066 b.crlf = crlf // crlf will be read next time, but not if a boundary follows.
1078 b.buf = make([]byte, b.b.maxLineLength())
1087func checkBound(line, bound []byte) (bool, bool) {
1088 if !bytes.HasPrefix(line, bound) {
1091 line = line[len(bound):]
1092 if bytes.HasPrefix(line, []byte("--")) {
1100 case ' ', '\t', '\r', '\n':