-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmultipart.go
159 lines (140 loc) · 3.13 KB
/
multipart.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package pgpmail
import (
"bytes"
"crypto/rand"
"errors"
"fmt"
"io"
)
func (m *Message) IsMultipart() bool {
return m.ctPrimary == "multipart"
}
var malformed = errors.New("malformed mime body")
type multipartContent struct {
preamble []byte
boundary []byte
parts []*MessagePart
}
type extractState struct {
messageBody []byte
seenFinal bool
delim []byte
crlf []byte
dashes []byte
}
var dashes = []byte("--")
func newMultipartContent(boundary, preamble string) *multipartContent {
mp := new(multipartContent)
mp.preamble = []byte(preamble)
mp.boundary = []byte(boundary)
mp.parts = []*MessagePart{}
return mp
}
func (mp *multipartContent) addPart(p *MessagePart) {
mp.parts = append(mp.parts, p)
}
func (m *Message) extractMultiparts() error {
body := []byte(m.Body)
boundary, ok := m.ctParams["boundary"]
if !ok {
return errors.New("cannot extract multiparts, no boundary parameter")
}
delim := []byte("\r\n--" + boundary)
preamble, _, err := extractPart(&body, delim[2:])
if err != nil {
return err
}
mp := newMultipartContent(boundary, string(preamble))
for {
p, last, err := extractPart(&body, delim)
if err != nil {
return err
}
r := NewReader(string(p))
part, err := r.ReadMessagePart()
if err != nil {
return errors.New("failed to extract multiparts: " + err.Error())
}
mp.parts = append(mp.parts, part)
if last {
m.mpContent = mp
return nil
}
}
}
func (m *Message) AddMultipart(p *MessagePart) {
if m.mpContent == nil {
boundary := randomBoundary()
m.mpContent = newMultipartContent(boundary, "")
}
m.mpContent.addPart(p)
}
func (m *Message) ClearMultiparts() {
if m.mpContent != nil {
m.mpContent.parts = nil
m.mpContent.preamble = nil
}
}
func (m *Message) PackMultiparts() error {
if m.mpContent == nil {
return errors.New("not a multipart message")
}
m.Body = renderMultiparts(m.mpContent)
return nil
}
func renderMultiparts(mp *multipartContent) string {
b := new(bytes.Buffer)
b.Write(mp.preamble)
for _, part := range mp.parts {
writeBoundary(b, mp.boundary, false)
b.WriteString(part.String())
}
writeBoundary(b, mp.boundary, true)
return b.String()
}
func writeBoundary(buffer *bytes.Buffer, boundary []byte, final bool) {
if buffer.Len() > 0 {
buffer.Write(crlf)
}
buffer.Write(dashes)
buffer.Write(boundary)
if final {
buffer.Write(dashes)
}
buffer.Write(crlf)
}
func extractPart(messageBody *[]byte, delim []byte) ([]byte, bool, error) {
body := *messageBody
idx := bytes.Index(body, delim)
if idx == -1 {
return nil, false, malformed
}
data := body[:idx]
idx += len(delim)
if len(body[idx:]) < 2 {
return nil, false, malformed
}
suffix := body[idx : idx+2]
body = body[idx+2:]
if bytes.Equal(suffix, crlf) {
*messageBody = body
return data, false, nil
} else if bytes.Equal(suffix, dashes) {
*messageBody = body
return data, true, nil
} else {
return nil, false, malformed
}
}
func randomBoundary() string {
var buf [30]byte
rnd := rand.Reader
if testingRandHook != nil {
rnd = testingRandHook
}
_, err := io.ReadFull(rnd, buf[:])
if err != nil {
panic(err)
}
return fmt.Sprintf("%x", buf[:])
}