forked from emersion/go-smtp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
data.go
58 lines (46 loc) · 879 Bytes
/
data.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package smtp
import (
"io"
)
// SMTPError specifies the error code and message that needs to be returned to the client
type SMTPError struct {
Code int
Message string
}
func (err *SMTPError) Error() string {
return err.Message
}
var ErrDataTooLarge = &SMTPError{
Code: 552,
Message: "Maximum message size exceeded",
}
type dataReader struct {
r io.Reader
limited bool
n int64 // Maximum bytes remaining
}
func newDataReader(c *Conn) io.Reader {
dr := &dataReader{
r: c.text.DotReader(),
}
if c.server.MaxMessageBytes > 0 {
dr.limited = true
dr.n = int64(c.server.MaxMessageBytes)
}
return dr
}
func (r *dataReader) Read(b []byte) (n int, err error) {
if r.limited {
if r.n <= 0 {
return 0, ErrDataTooLarge
}
if int64(len(b)) > r.n {
b = b[0:r.n]
}
}
n, err = r.r.Read(b)
if r.limited {
r.n -= int64(n)
}
return
}