diff options
Diffstat (limited to 'vendor/github.com/emersion/go-smtp/lengthlimit_reader.go')
-rw-r--r-- | vendor/github.com/emersion/go-smtp/lengthlimit_reader.go | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/vendor/github.com/emersion/go-smtp/lengthlimit_reader.go b/vendor/github.com/emersion/go-smtp/lengthlimit_reader.go new file mode 100644 index 0000000..1513e56 --- /dev/null +++ b/vendor/github.com/emersion/go-smtp/lengthlimit_reader.go @@ -0,0 +1,47 @@ +package smtp + +import ( + "errors" + "io" +) + +var ErrTooLongLine = errors.New("smtp: too long a line in input stream") + +// lineLimitReader reads from the underlying Reader but restricts +// line length of lines in input stream to a certain length. +// +// If line length exceeds the limit - Read returns ErrTooLongLine +type lineLimitReader struct { + R io.Reader + LineLimit int + + curLineLength int +} + +func (r *lineLimitReader) Read(b []byte) (int, error) { + if r.curLineLength > r.LineLimit && r.LineLimit > 0 { + return 0, ErrTooLongLine + } + + n, err := r.R.Read(b) + if err != nil { + return n, err + } + + if r.LineLimit == 0 { + return n, nil + } + + for _, chr := range b[:n] { + if chr == '\n' { + r.curLineLength = 0 + } + r.curLineLength++ + + if r.curLineLength > r.LineLimit { + return 0, ErrTooLongLine + } + } + + return n, nil +} |