-
-
Notifications
You must be signed in to change notification settings - Fork 100
/
header.go
179 lines (143 loc) · 4.23 KB
/
header.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
// headers.go implements "Q" encoding as specified by RFC 2047.
//Modified from https://github.com/joegrasse/mime to use with Go Simple Mail
package mail
import (
"bufio"
"bytes"
"fmt"
"io"
"strings"
"unicode/utf8"
)
type encoder struct {
w *bufio.Writer
charset string
encoding headerEncoding
usedChars int
}
// newEncoder returns a new mime header encoder that writes to w. The c
// parameter specifies the name of the character set of the text that will be
// encoded. The u parameter indicates how many characters have been used
// already.
func newEncoder(w io.Writer, c string, encoding headerEncoding, u int) *encoder {
return &encoder{bufio.NewWriter(w), strings.ToUpper(c), encoding, u}
}
// encode encodes p using the encoding scheme specified in e
// If all chars are printable ascii chars, no encoding is performed.
// Limits line length to 75 characters and folds lines as necessary.
func (e *encoder) encode(p []byte) (n int, err error) {
var output bytes.Buffer
allPrintable := true
// some lines we encode end in "
//maxLineLength := 75 - 1
maxLineLength := 76
// prevent header injection
p = secureHeader(p)
// check to see if we have all printable characters
for _, c := range p {
if !isVchar(c) && !isWSP(c) {
allPrintable = false
break
}
}
// all characters are printable. just do line folding
if allPrintable || e.encoding == HeaderEncodingNone {
text := string(p)
words := strings.Split(text, " ")
lineBuffer := ""
firstWord := true
// split the line where necessary
for _, word := range words {
newWord := ""
if !firstWord {
newWord += " "
}
newWord += word
// check line length
if (e.usedChars+len(lineBuffer)+len(newWord)) > maxLineLength && (lineBuffer != "" || e.usedChars != 0) {
output.WriteString(lineBuffer + "\r\n")
// first word on newline needs a space in front
if !firstWord {
lineBuffer = ""
} else {
lineBuffer = " "
}
// reset since not on the first line anymore
e.usedChars = 0
}
lineBuffer += newWord /*word*/
firstWord = false
}
output.WriteString(lineBuffer)
} else {
// else block can only be HeaderEncodingQ as of now
firstLine := true
// A single encoded word can not be longer than 75 characters
if e.usedChars == 0 {
maxLineLength = 75
}
wordBegin := "=?" + e.charset + "?Q?"
wordEnd := "?="
lineBuffer := wordBegin
for i := 0; i < len(p); {
// encode the character
encodedChar, runeLength := encode(p, i)
// Check line length
if len(lineBuffer)+e.usedChars+len(encodedChar) > (maxLineLength - len(wordEnd)) {
output.WriteString(lineBuffer + wordEnd + "\r\n")
lineBuffer = " " + wordBegin
firstLine = false
}
lineBuffer += encodedChar
i = i + runeLength
// reset since not on the first line anymore
if !firstLine {
e.usedChars = 0
maxLineLength = 76
}
}
output.WriteString(lineBuffer + wordEnd)
}
e.w.Write(output.Bytes())
e.w.Flush()
n = output.Len()
return n, nil
}
// encode takes a string and position in that string and encodes one utf-8
// character. It then returns the encoded string and number of runes in the
// character.
func encode(text []byte, i int) (encodedString string, runeLength int) {
started := false
for ; i < len(text) && (!utf8.RuneStart(text[i]) || !started); i++ {
switch c := text[i]; {
case c == ' ':
encodedString += "_"
case isVchar(c) && c != '=' && c != '?' && c != '_':
encodedString += string(c)
default:
encodedString += fmt.Sprintf("=%02X", c)
}
runeLength++
started = true
}
return
}
// secureHeader removes all unnecessary values to prevent
// header injection
func secureHeader(text []byte) []byte {
secureValue := strings.TrimSpace(string(text))
secureValue = strings.Replace(secureValue, "\r", "", -1)
secureValue = strings.Replace(secureValue, "\n", "", -1)
secureValue = strings.Replace(secureValue, "\t", "", -1)
return []byte(secureValue)
}
// isVchar returns true if c is an RFC 5322 VCHAR character.
func isVchar(c byte) bool {
// Visible (printing) characters.
return '!' <= c && c <= '~'
}
// isWSP returns true if c is a WSP (white space).
// WSP is a space or horizontal tab (RFC5234 Appendix B).
func isWSP(c byte) bool {
return c == ' ' || c == '\t'
}