-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathstandalone-example.js
244 lines (201 loc) · 6.32 KB
/
standalone-example.js
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
/*
The following is an example of how to use the parser as a standalone module.
Both parseRequest and parseResponse can be used to parse a raw HTTP request/response from a Buffer.
*/
const { deepStrictEqual, throws } = require('assert');
// Replace the require when using the module from npm.
//const { HTTPParser } = require('http-parser-js');
const { HTTPParser } = require('./http-parser.js');
function parseRequest(input) {
const parser = new HTTPParser(HTTPParser.REQUEST);
let complete = false;
let shouldKeepAlive;
let upgrade;
let method;
let url;
let versionMajor;
let versionMinor;
let headers = [];
let trailers = [];
let bodyChunks = [];
parser[HTTPParser.kOnHeadersComplete] = function (req) {
shouldKeepAlive = req.shouldKeepAlive;
upgrade = req.upgrade;
method = HTTPParser.methods[req.method];
url = req.url;
versionMajor = req.versionMajor;
versionMinor = req.versionMinor;
headers = req.headers;
};
parser[HTTPParser.kOnBody] = function (chunk, offset, length) {
bodyChunks.push(chunk.slice(offset, offset + length));
};
// This is actually the event for trailers, go figure.
parser[HTTPParser.kOnHeaders] = function (t) {
trailers = t;
};
parser[HTTPParser.kOnMessageComplete] = function () {
complete = true;
};
// Since we are sending the entire Buffer at once here all callbacks above happen synchronously.
// The parser does not do _anything_ asynchronous.
// However, you can of course call execute() multiple times with multiple chunks, e.g. from a stream.
// But then you have to refactor the entire logic to be async (e.g. resolve a Promise in kOnMessageComplete and add timeout logic).
parser.execute(input);
parser.finish();
if (!complete) {
throw new Error('Could not parse request');
}
let body = Buffer.concat(bodyChunks);
return {
shouldKeepAlive,
upgrade,
method,
url,
versionMajor,
versionMinor,
headers,
body,
trailers,
};
}
function parseResponse(input) {
const parser = new HTTPParser(HTTPParser.RESPONSE);
let complete = false;
let shouldKeepAlive;
let upgrade;
let statusCode;
let statusMessage;
let versionMajor;
let versionMinor;
let headers = [];
let trailers = [];
let bodyChunks = [];
parser[HTTPParser.kOnHeadersComplete] = function (res) {
shouldKeepAlive = res.shouldKeepAlive;
upgrade = res.upgrade;
statusCode = res.statusCode;
statusMessage = res.statusMessage;
versionMajor = res.versionMajor;
versionMinor = res.versionMinor;
headers = res.headers;
};
parser[HTTPParser.kOnBody] = function (chunk, offset, length) {
bodyChunks.push(chunk.slice(offset, offset + length));
};
// This is actually the event for trailers, go figure.
parser[HTTPParser.kOnHeaders] = function (t) {
trailers = t;
};
parser[HTTPParser.kOnMessageComplete] = function () {
complete = true;
};
// Since we are sending the entire Buffer at once here all callbacks above happen synchronously.
// The parser does not do _anything_ asynchronous.
// However, you can of course call execute() multiple times with multiple chunks, e.g. from a stream.
// But then you have to refactor the entire logic to be async (e.g. resolve a Promise in kOnMessageComplete and add timeout logic).
parser.execute(input);
parser.finish();
if (!complete) {
throw new Error('Could not parse');
}
let body = Buffer.concat(bodyChunks);
return {
shouldKeepAlive,
upgrade,
statusCode,
statusMessage,
versionMajor,
versionMinor,
headers,
body,
trailers,
};
}
let parsed;
console.log('Example: basic GET request:');
parsed = parseRequest(
Buffer.from(`GET / HTTP/1.1
Host: www.example.com
`)
);
console.log(parsed);
deepStrictEqual(parsed.shouldKeepAlive, true);
deepStrictEqual(parsed.upgrade, false);
deepStrictEqual(parsed.method, 'GET');
deepStrictEqual(parsed.url, '/');
deepStrictEqual(parsed.versionMajor, 1);
deepStrictEqual(parsed.versionMinor, 1);
deepStrictEqual(parsed.headers, ['Host', 'www.example.com']);
deepStrictEqual(parsed.body.toString(), '');
deepStrictEqual(parsed.trailers, []);
console.log('Example: POST request with body:');
parsed = parseRequest(
Buffer.from(`POST /memes HTTP/1.1
Host: www.example.com
Content-Length: 7
Content-Type: text/plain
foo bar
`)
);
console.log(parsed);
deepStrictEqual(parsed.shouldKeepAlive, true);
deepStrictEqual(parsed.upgrade, false);
deepStrictEqual(parsed.method, 'POST');
deepStrictEqual(parsed.url, '/memes');
deepStrictEqual(parsed.versionMajor, 1);
deepStrictEqual(parsed.versionMinor, 1);
deepStrictEqual(parsed.headers, ['Host', 'www.example.com', 'Content-Length', '7', 'Content-Type', 'text/plain']);
deepStrictEqual(parsed.body.toString(), 'foo bar');
deepStrictEqual(parsed.trailers, []);
console.log('Example: basic HTML response');
parsed = parseResponse(
Buffer.from(`HTTP/1.1 404 Not Found
Content-Type: text/html
Content-Length: 33
<strong>Computer says no</strong>
`)
);
console.log(parsed);
deepStrictEqual(parsed.shouldKeepAlive, true);
deepStrictEqual(parsed.upgrade, false);
deepStrictEqual(parsed.statusCode, 404);
deepStrictEqual(parsed.statusMessage, 'Not Found');
deepStrictEqual(parsed.versionMajor, 1);
deepStrictEqual(parsed.versionMinor, 1);
deepStrictEqual(parsed.headers, ['Content-Type', 'text/html', 'Content-Length', '33']);
deepStrictEqual(parsed.body.toString(), '<strong>Computer says no</strong>');
deepStrictEqual(parsed.trailers, []);
console.log('Example: chunked response with trailers');
parsed = parseResponse(
Buffer.from(`HTTP/1.1 200 OK
Content-Type: text/plain
Transfer-Encoding: chunked
Trailer: Expires
7
Mozilla
9
Developer
7
Network
0
Expires: Wed, 21 Oct 2015 07:28:00 GMT
`)
);
console.log(parsed);
deepStrictEqual(parsed.shouldKeepAlive, true);
deepStrictEqual(parsed.upgrade, false);
deepStrictEqual(parsed.statusCode, 200);
deepStrictEqual(parsed.statusMessage, 'OK');
deepStrictEqual(parsed.versionMajor, 1);
deepStrictEqual(parsed.versionMinor, 1);
deepStrictEqual(parsed.headers, ['Content-Type', 'text/plain', 'Transfer-Encoding', 'chunked', 'Trailer', 'Expires']);
deepStrictEqual(parsed.body.toString(), 'MozillaDeveloperNetwork');
deepStrictEqual(parsed.trailers, ['Expires', 'Wed, 21 Oct 2015 07:28:00 GMT']);
throws(function () {
parseResponse(
Buffer.from(`HTTP/1.1 200 OK
Content-Length: 1
`)
);
});