-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathparser.cfc
262 lines (227 loc) · 7.73 KB
/
parser.cfc
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
component {
/*
Sources:
https://github.com/apiaryio/curl-trace-parser/blob/4a94b80c7acb19242728e7b876a800d4f90d7617/src/parser.coffee
https://github.com/apiaryio/http-string-parser/blob/ebb15b92d040aae1f79abd4bbe371ef68b040d68/src/parser.coffee
*/
public function init() {
variables.Pattern = createObject('java', 'java.util.regex.Pattern');
variables.HttpParser = createObject('java', 'org.apache.commons.httpclient.HttpParser');
variables.IOUtils = createObject('java', 'org.apache.commons.io.IOUtils');
variables.URLDecoder = createObject('java', 'java.net.URLDecoder');
variables.encoding = 'utf-8';
}
public function parse(array traceLines) {
// find ASCI bytes in raw lines
// will contain array of arrays with direction and data
// e.g [['<=', "47 45 54 20 2f 73 68 6f 70 70 69 6e 67 2d 63 61"]]
var lastDir = '';
var lastDirName = '';
var lastSection = '';
var calls = [];
var i_req = 0;
var dataPattern = '^(?:[a-z0-9]{4}:) ((?:[a-z0-9]{2} ){1,16})';
var dirPattern = '^(=>|<=) (Recv|Send) ([a-z-A-Z-0-9]+)';
var line = '';
for(line in traceLines) {
var dirMatch = _match(dirPattern, line);
if(arrayLen(dirMatch)) {
lastDir = trim(dirMatch[1]);
lastSection = lCase(trim(dirMatch[2]));
lastDirName = trim(dirMatch[3]);
if( trim(dirMatch[2]) == 'Send' && lastDirName == 'header') {
i_req++;
calls[i_req] = [];
}
}
var dataMatch = _match(dataPattern, line);
if(arrayLen(dataMatch)) {
data = trim(dataMatch[1]);
var shouldIgnore = false;
// Ignore SSL related lines
if (lastDirName == 'ssl') shouldIgnore = true;
// Ignore "send data", we don't care about it and it may cause out-of-memory!
if (lastSection == 'Send' && lastDirName == 'data') shouldIgnore = true;
if(!shouldIgnore && i_req > 0) {
calls[i_req].add([lastDir, lastDirName, data]);
}
}
}
var converted = [];
var asciiHexSets = [];
for(asciiHexSets in calls) {
converted.add(_convertASCIIsets(asciiHexSets));
}
var parsed = [];
for(var i = 1; i <= arrayLen(converted); i++) {
var r = {};
var dirs = ['request', 'response'];
var dir = '';
for(dir in dirs) {
if(structKeyExists(converted[i], dir)) {
r[dir] = {};
var section = '';
for(section in converted[i][dir]) {
var value = converted[i][dir][section];
if(dir == 'response' && section == 'header') {
// Remove "Continue" in case multipart
value = reReplace(value, 'HTTP\/\d\.\d\s100[^(HTTP)]*', '');
r[dir]['status'] = _parseStatusLine(value);
r[dir][section] = _parseHeaders(value);
r[dir]['cookies'] = _parseCookies(r[dir][section]);
} else {
r[dir][section] = value;
}
}
}
}
parsed.add(r);
}
return parsed;
}
// ...
private string function _dir(required string ident) {
return ident == '=>' ? 'request' : 'response';
}
private array function _match(required string r, required string str, flags) {
if(!isNull(flags)) {
if(isSimpleValue(flags)) {
flags = listToArray(flags);
}
var flagInt = 0;
var flagName = '';
for (flagName in flags)
flagInt += Pattern[flagName];
var p = Pattern.compile(r, javacast("int", flagInt));
} else {
var p = Pattern.compile(r);
}
var m = p.matcher(str);
var i = 1;
var res = [];
if(m.find()) {
var i = 1;
while(i <= m.groupCount()) {
res.add(m.group(i));
i++;
}
}
return res;
}
private struct function _parseStatusLine(required string headerLines) {
var statusRE = '(HTTP/\d\.\d\s+(\d+)\s+[^\n]+)';
return {
'line' = reReplace(headerLines, statusRE & '.*', '\1'),
'code' = reReplace(headerLines, statusRE & '.*', '\2')
};
}
private array function _parseCookies(required struct headers) {
var cookies = [];
var aCookies = structKeyExists(arguments.headers,"set-cookie") ? arguments.headers["set-cookie"] : [];
if (!isArray(aCookies)) {
aCookies = [ aCookies ];
}
for(var c in aCookies) {
cookies.add(_parseSingleCookie(c));
}
return cookies;
}
private struct function _parseSingleCookie( required string c) {
var aData = listToArray(arguments.c,";");
var stReturn = {};
for (var i = 1; i <= arrayLen(aData); i++ ) {
if(i EQ 1) {
stReturn["name"] = listFirst(aData[i],"=");
stReturn["value"] = listLast(aData[i],"=");
} else {
stReturn[listFirst(aData[i],"=")] = listLast(aData[i],"=");
}
}
return stReturn;
}
private struct function _parseHeaders(required string headerLines) {
var statusRE = '(HTTP/\d\.\d\s+(\d+)\s+[^\n]+)';
var content = IOUtils.toInputStream(trim(reReplace(headerLines, statusRE, '')), variables.encoding);
var headersArr = HttpParser.parseHeaders(content);
var headers = {};
var tmpName = "";
var tmp = "";
for (var i = 1; i <= arrayLen(headersArr); i++) {
tmpName = headersArr[i].getName();
if (structKeyExists(headers,tmpName) AND isArray(headers[tmpName])) {
arrayAppend(headers[tmpName],headersArr[i].getValue());
} else if (structKeyExists(headers,tmpName) AND isSimpleValue(headers[tmpName])) {
tmp = headers[tmpName];
headers[tmpName] = [ tmp, headersArr[i].getValue() ];
} else {
headers[tmpName] = headersArr[i].getValue();
}
}
return headers;
}
private struct function _convertASCIIsets(required array asciiHexSets) {
// split lines by spaces and make array of ASCII hex bytes
var asciiHexBuffer = {
request = {},
response = {}
};
var set = '';
for(set in asciiHexSets) {
var d = _dir(set[1]);
var section = lCase(set[2]);
var data = set[3];
var byte = '';
if(!structKeyExists(asciiHexBuffer[d], section)) {
asciiHexBuffer[d][section] = [];
}
asciiHexBuffer[d][section].add(arrayToList(listToArray(uCase(data), ' '), '%'));
}
// convert ASCII hex to ASCII integers codes
var stringBuffer = {
request = {},
response = {}
};
var dir = '';
for(dir in asciiHexBuffer) {
var section = '';
for(section in asciiHexBuffer[dir]) {
if(!structKeyExists(stringBuffer[dir], section)) {
stringBuffer[dir][section] = [];
}
var codes = asciiHexBuffer[dir][section];
var str = URLDecoder.decode('%' & arrayToList(codes, '%'));
stringBuffer[dir][section].add(str);
}
}
// convert stringBuffer to output
var output = {
'request' = {},
'response' = {}
};
for(dir in stringBuffer) {
var isChunked = false;
var sections = structKeyArray(stringBuffer[dir]);
// headers first
if(arrayFindNoCase(sections, 'header')) {
var headers = arrayToList(stringBuffer[dir].header, '');
if(arrayLen(_match('(transfer-encoding:\s*?chunked)', headers, 'CASE_INSENSITIVE'))) {
isChunked = true;
}
output[dir]['header'] = headers;
arrayDelete(sections,'header');
}
var section = '';
for(section in stringBuffer[dir]) {
var str = arrayToList(stringBuffer[dir][section], '');
if(section == 'data' && isChunked) {
var hexPattern = '(\r\n[0-9a-f]+\r\n)';
str = reReplace(str, hexPattern, '', 'all');
// Remove first value of data.
str = reReplace(str, '^([0-9a-f]+\r\n)', '');
}
output[dir][section] = str;
}
}
return output;
}
}