forked from hsiafan/httpdump
-
Notifications
You must be signed in to change notification settings - Fork 0
/
text_utils.go
229 lines (215 loc) · 6.13 KB
/
text_utils.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
package main
import (
"bytes"
"io"
"io/ioutil"
"strings"
//"github.com/saintfish/chardet" // not work, realy stupid...
"golang.org/x/text/encoding"
"golang.org/x/text/encoding/htmlindex"
"golang.org/x/text/transform"
)
// mime type struct
type mimeType struct {
Type string
subType string
scope string
}
// parse mime type
func parseMimeType(contentTypeStr string) mimeType {
var idx = strings.Index(contentTypeStr, "/")
if idx == -1 {
// should not happen
return mimeType{contentTypeStr, "", ""}
}
var scope = ""
var subType = contentTypeStr[idx+1:]
if strings.HasPrefix(subType, "x-") {
subType = subType[2:]
scope = "x"
} else if strings.HasPrefix(subType, "vnd.") {
subType = subType[4:]
scope = "vnd"
}
var i = strings.Index(subType, ".")
if i > 0 {
subType = subType[:i]
}
return mimeType{contentTypeStr[:idx], subType, scope}
}
//TODO: multipart/form-data
var textTypes = map[string]bool{"text": true}
var textSubTypes = map[string]bool{
"html": true,
"xml": true,
"json": true,
"www-form-urlencoded": true,
"javascript": true,
"postscript": true,
"atomcat+xml": true,
"atomsvc+xml": true,
"atom+xml": true,
"xml-dtd": true,
"ecmascript": true,
"java-jnlp-file": true,
"latex": true,
"mpegurl": true,
"rdf+xml": true,
"rtf": true,
"rss+xml": true,
"svg+xml": true,
"uri-list": true,
"wsdl+xml": true,
"xhtml+xml": true,
"xslt+xml": true,
"ns-proxy-autoconfig": true,
"javascript-config": true,
}
// if is text type mime
func (ct mimeType) isTextContent() bool {
return textTypes[ct.Type] || textSubTypes[ct.subType]
}
var binaryTypes = map[string]bool{"image": true, "audio": true, "video": true}
var binarySubtypes = map[string]bool{
"7z-compressed": true,
"abiword": true,
"ace-compressed": true,
"shockwave-flash": true,
"pdf": true,
"director": true,
"bzip": true,
"bzip2": true,
"debian-package": true,
"epub+zip": true,
"font-ghostscript": true,
"font-bdf": true,
"java-archive": true,
"java-vm": true,
"java-serialized-object": true,
"msaccess": true,
"msdownload": true,
"ms-application": true,
"ms-fontobject": true,
"ms-excel": true,
"openxmlformats-officedocument": true,
"msbinder": true,
"ms-officetheme": true,
"onenote": true,
"ms-powerpoint": true,
"ms-project": true,
"mspublisher": true,
"msschedule": true,
"silverlight-app": true,
"visio": true,
"ms-wmd": true,
"ms-htmlhelp": true,
"msword": true,
"ms-works": true,
"oda": true,
"ogg": true,
"oasis": true,
"sun": true,
"font-otf": true,
"x-font-ttf": true,
"unity": true,
"zip": true,
"x509-ca-cert": true,
"octet-stream": true,
"png": true,
"ppt": true,
"xls": true,
}
// if is binary type mime
func (ct mimeType) isBinaryContent() bool {
return binaryTypes[ct.Type] || binarySubtypes[ct.subType]
}
// read reader content to string, using charset specified
func readToStringWithCharset(reader io.Reader, charset string) (string, error) {
charset = strings.ToUpper(charset)
var data []byte
var err error
if charset == "UTF-8" || charset == "UTF8" {
data, err = ioutil.ReadAll(reader)
} else {
if charset == "GBK" || charset == "GB2312" {
charset = "GB18030"
}
var encoder encoding.Encoding
encoder, err = htmlindex.Get(charset)
if err != nil {
return "", err
}
data, err = ioutil.ReadAll(transform.NewReader(reader, encoder.NewDecoder()))
}
if err != nil {
return "", err
}
return string(data), err
}
// convert byte array to string, using charset specified
func byteToStringWithCharset(data []byte, charset string) (string, error) {
charset = strings.ToUpper(charset)
if charset == "UTF-8" || charset == "UTF8" {
return string(data), nil
}
var reader = bytes.NewBuffer(data)
return readToStringWithCharset(reader, charset)
}
// parse content type to mimeType and charset
func parseContentType(contentType string) (string, string) {
var mimeTypeStr, charset string
idx := strings.Index(contentType, ";")
if idx < 0 {
mimeTypeStr = strings.TrimSpace(contentType)
charset = ""
} else {
mimeTypeStr = strings.TrimSpace(contentType[:idx])
charsetSeg := strings.TrimSpace(contentType[idx+1:])
eidx := strings.Index(charsetSeg, "=")
if eidx < 0 {
charset = ""
} else {
charset = strings.TrimSpace(charsetSeg[eidx+1:])
}
}
return mimeTypeStr, charset
}
// if sting 'looks like' a json string
func likeJSON(value string) bool {
if len(value) < 2 {
return false
}
value = strings.TrimSpace(value)
if value[0] == '[' && value[len(value)-1] == ']' || value[0] == '{' && value[len(value)-1] == '}' {
return true
}
return false
}
// if str wildcard match pattern
func wildcardMatch(str string, pattern string) bool {
var n = len(pattern)
var i = 0
var j = 0
var asterick = -1
var match = 0
for i < len(str) {
if j < n && pattern[j] == '*' {
match = i
asterick = j
j++
} else if j < n && (str[i] == pattern[j] || pattern[j] == '?') {
i++
j++
} else if asterick >= 0 {
match++
i = match
j = asterick + 1
} else {
return false
}
}
for j < n && pattern[j] == '*' {
j++
}
return j == n
}