-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhttp-codec.js
288 lines (262 loc) · 7.01 KB
/
http-codec.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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
"use strict";
var bodec = require('bodec');
var HTTP1_1 = bodec.fromUnicode("HTTP/1.1");
module.exports = {
server: {
encoder: serverEncoder,
decoder: serverDecoder,
},
client: {
encoder: clientEncoder,
decoder: clientDecoder,
},
};
function serverEncoder(write) {
return function (res) {
if (res === undefined) return write(undefined);
if (bodec.isBinary(res)) return write(res);
var head = "HTTP/1.1 " + res.code + " " + STATUS_CODES[res.code] + "\r\n";
res.headers.forEach(function (pair) {
head += pair[0] + ": " + pair[1] + "\r\n";
});
head += "\r\n";
write(bodec.fromUnicode(head));
};
}
function clientEncoder(write) {
return function (req) {
if (req === undefined) return write(undefined);
if (bodec.isBinary(req)) return write(req);
var head = req.method + " " + req.path + " HTTP/1.1\r\n";
req.headers.forEach(function (pair) {
head += pair[0] + ": " + pair[1] + "\r\n";
});
head += "\r\n";
write(bodec.fromUnicode(head));
};
}
function clientDecoder(emit) {
return parser(true, emit);
}
function serverDecoder(emit) {
return parser(false, emit);
}
function parser(client, emit) {
var position = 0, code = 0;
var key = "", value = "";
var chunked = false, length;
var headers = [];
var method = "", path = "";
var $start = client ? $version : $server;
var state = $start;
return function (chunk) {
if (chunk === undefined) return emit(undefined);
if (!state) return emit(chunk);
var i = 0, length = chunk.length;
while (i < length) {
state = state(chunk[i++]);
if (state) continue;
if (i < chunk.length) emit(bodec.slice(chunk, i));
break;
}
};
function $version(byte) {
if (byte === HTTP1_1[position++]) return $version;
if (position === 9) {
if (client && byte === 0x20) return $code;
if (!client && byte === 0x0d) return $newline;
}
throw new SyntaxError("Must be HTTP/1.1");
}
function $code(byte) {
if (byte === 0x20) return $message;
if (position++ < 3) {
code = (code * 10) + byte - 0x30;
position = 0;
return $code;
}
throw new SyntaxError("Invalid status code");
}
function $message(byte) {
if (byte === 0x0d) {
position = 0;
return $newline;
}
return $message;
}
function $server(byte) {
if (byte > 0x40 && byte <= 0x5a) { // Capital letter for method
method += String.fromCharCode(byte);
return $server;
}
if (!method) throw new SyntaxError("Missing HTTP method");
if (byte === 0x20) return $url;
throw new SyntaxError("Invalid HTTP method");
}
function $url(byte) {
if (byte !== 0x20) {
path += String.fromCharCode(byte);
return $url;
}
return $version;
}
function $newline(byte) {
if (byte === 0x0a) return $end;
throw new SyntaxError("Invalid line ending");
}
function $end(byte) {
if (byte === 0x0d) return $ending;
return $key(byte);
}
function $key(byte) {
if (byte === 0x3a) return $sep;
key += String.fromCharCode(byte);
return $key;
}
function $sep(byte) {
if (byte === 0x20) return $sep;
return $value(byte);
}
function $value(byte) {
if (byte === 0x0d) {
var lower = key.toLowerCase();
if (lower === "transfer-encoding" && value === "chunked") {
chunked = true;
}
else if (lower === "content-length") length = parseInt(value, 10);
headers.push([key, value]);
key = "";
value = "";
return $newline;
}
value += String.fromCharCode(byte);
return $value;
}
function $ending(byte) {
if (byte === 0x0a) {
if (client) {
emit({
code: code,
headers: headers
});
code = 0;
}
else {
emit({
method: method,
path: path,
headers: headers
});
method = "";
path = "";
}
headers = [];
if (chunked) return chunkMachine(emit, $start);
return null;
}
throw new SyntaxError("Invalid header ending");
}
}
function chunkMachine(emit, $start) {
var position = 0, size = 0;
var chunk = null;
return $len;
function $len(byte) {
if (byte === 0x0d) return $chunkStart;
size <<= 4;
if (byte >= 0x30 && byte < 0x40) size += byte - 0x30;
else if (byte > 0x60 && byte <= 0x66) size += byte - 0x57;
else if (byte > 0x40 && byte <= 0x46) size += byte - 0x37;
else throw new SyntaxError("Invalid chunked encoding length header");
return $len;
}
function $chunkStart(byte) {
if (byte === 0x0a) {
if (size) {
chunk = bodec.create(size);
return $chunk;
}
return $ending;
}
throw new SyntaxError("Invalid chunk ending");
}
function $chunk(byte) {
chunk[position++] = byte;
if (position < size) return $chunk;
return $ending;
}
function $ending(byte) {
if (byte !== 0x0d) throw new SyntaxError("Problem in chunked encoding");
return $end;
}
function $end(byte) {
if (byte !== 0x0a) throw new SyntaxError("Problem in chunked encoding");
var next;
if (size) {
emit(chunk);
next = $len;
}
else {
emit(undefined);
next = $start;
}
chunk = null;
size = 0;
position = 0;
return next;
}
}
var STATUS_CODES = {
'100': 'Continue',
'101': 'Switching Protocols',
'102': 'Processing', // RFC 2518, obsoleted by RFC 4918
'200': 'OK',
'201': 'Created',
'202': 'Accepted',
'203': 'Non-Authoritative Information',
'204': 'No Content',
'205': 'Reset Content',
'206': 'Partial Content',
'207': 'Multi-Status', // RFC 4918
'300': 'Multiple Choices',
'301': 'Moved Permanently',
'302': 'Moved Temporarily',
'303': 'See Other',
'304': 'Not Modified',
'305': 'Use Proxy',
'307': 'Temporary Redirect',
'400': 'Bad Request',
'401': 'Unauthorized',
'402': 'Payment Required',
'403': 'Forbidden',
'404': 'Not Found',
'405': 'Method Not Allowed',
'406': 'Not Acceptable',
'407': 'Proxy Authentication Required',
'408': 'Request Time-out',
'409': 'Conflict',
'410': 'Gone',
'411': 'Length Required',
'412': 'Precondition Failed',
'413': 'Request Entity Too Large',
'414': 'Request-URI Too Large',
'415': 'Unsupported Media Type',
'416': 'Requested Range Not Satisfiable',
'417': 'Expectation Failed',
'418': 'I\'m a teapot', // RFC 2324
'422': 'Unprocessable Entity', // RFC 4918
'423': 'Locked', // RFC 4918
'424': 'Failed Dependency', // RFC 4918
'425': 'Unordered Collection', // RFC 4918
'426': 'Upgrade Required', // RFC 2817
'500': 'Internal Server Error',
'501': 'Not Implemented',
'502': 'Bad Gateway',
'503': 'Service Unavailable',
'504': 'Gateway Time-out',
'505': 'HTTP Version not supported',
'506': 'Variant Also Negotiates', // RFC 2295
'507': 'Insufficient Storage', // RFC 4918
'509': 'Bandwidth Limit Exceeded',
'510': 'Not Extended' // RFC 2774
};