-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhttp.js
492 lines (408 loc) · 11.8 KB
/
http.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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
var http = function() {
var tcp = chrome.sockets.tcp,
tcpServer = chrome.sockets.tcpServer,
Event = chrome.Event || eventEmitter.Event;
if (!tcp || !tcpServer || !Event)
return {};
var STATUS_CODES = {
200: 'OK',
403: 'Forbidden',
404: 'Not Found',
405: 'Method Not Allowed',
418: 'I\'m a teapot', // RFC 2324
501: 'Not Implemented'
};
function ab2str(buf) {
return String.fromCharCode.apply(null, new Uint8Array(buf));
}
function str2ab(str) {
var i, len = str.length,
buf = new ArrayBuffer(len),
bufView = new Uint8Array(buf);
for (i = 0; i < len; i++)
bufView[i] = str.charCodeAt(i);
return buf;
}
function warn(state, socket, code) {
console.warn('[%d][%s] ' + chrome.runtime.lastError.message + ' (%d)', socket, state, code);
}
/**
* Users can register their callbacks on these events:
*
* Server.onRequest
*
* A callback funcion looks like this:
*
* function(IngressMessage req, EgressMessage response) { ... }
*
* The relationship among Server, Connection and Pipe classes:
*
* Server -- accept --> new Connection (client connection to be served)
* ------------> new Connection
* ----> new Pipe (proxy connection to the target)
* ------------> new Connection
* ----> new Pipe
* ------------> new Connection
* ------------> ...
*/
function Server(opt_requestListener) {
this.socketId_ = null;
this.connections_ = [];
this.acceptListener_ = undefined;
this.onRequest = new Event();
if (opt_requestListener)
this.onRequest.addListener(opt_requestListener);
}
Server.prototype.listen = function(port, opt_address) {
var t = this;
tcpServer.create({}, function(server) {
tcpServer.listen(server.socketId, opt_address || '0.0.0.0', port, function(result) {
if (result < 0) {
warn('listen on server', server.socketId, result);
return;
}
t.socketId_ = server.socketId;
t.acceptListener_ = establishConnectionForServer;
tcpServer.onAccept.addListener(t.acceptListener_);
});
});
function establishConnectionForServer(my) {
if (my.socketId != t.socketId_)
return;
var c = new Connection(t, my.clientSocketId);
t.connections_.push(c);
c.establish();
}
};
Server.prototype.close = function(opt_connection) {
if (!this.socketId_)
return;
// close the given connection
if (opt_connection) {
var i = this.connections_.indexOf(opt_connection);
if (i > -1)
this.connections_.splice(i, 1);
return;
}
// or close the server
tcpServer.onAccept.removeListener(this.acceptListener_);
tcpServer.disconnect(this.socketId_);
tcpServer.close(this.socketId_);
for (var i = this.connections_.length - 1; i >= 0; i--)
this.connections_[i].close();
};
/**
* Per-client logic, created when accepting a connection from Server,
* base of Pipe class.
*/
function Connection(server, socket) {
this.owner_ = server;
this.socketId_ = socket;
this.closed_ = false;
// instance of Pipe class, indicates a proxy connection's existed.
this.pipe_ = null;
// each client has its own listeners,
// for chrome.sockets.tcp.(onReceive|onReceiveError) events.
this.receiveListener_ = undefined;
this.receiveErrorListener_ = undefined;
}
Connection.prototype.establish = function() {
this.receiveListener_ = dataFromClient.bind(this);
this.receiveErrorListener_ = errorFromClient.bind(this);
tcp.onReceive.addListener(this.receiveListener_);
tcp.onReceiveError.addListener(this.receiveErrorListener_);
tcp.setPaused(this.socketId_, false);
function dataFromClient(my) {
if (my.socketId != this.socketId_)
return;
var req = new IngressMessage(this, my.data),
res = new EgressMessage(req);
this.owner_.onRequest.dispatch(req, res);
}
function errorFromClient(my) {
if (my.socketId != this.socketId_)
return;
console.log('[%d][receive error] Code: %d.', my.socketId, my.resultCode);
this.close();
}
};
Connection.prototype.close = function() {
if (this.closed_)
return;
this.closed_ = true;
console.log('[%d][Connection] Being closed.', this.socketId_);
// remove self from server's connection queues.
this.owner_.close(this);
tcp.onReceive.removeListener(this.receiveListener_);
tcp.onReceiveError.removeListener(this.receiveErrorListener_);
tcp.disconnect(this.socketId_);
tcp.close(this.socketId_);
if (this.pipe_)
this.pipe_.close();
};
/**
* Send the data out to the connection, where data is of type of ArrayBuffer.
*/
Connection.prototype.send = function(data) {
var t = this;
// FIXME: Socket happens to be closed while data is being sent.
tcp.send(this.socketId_, data, function(info) {
if (info.resultCode < 0) {
warn('be sent', t.socketId_, info.resultCode);
t.close();
return;
}
});
};
Connection.prototype.pipe = function(host, data, opt_mirrorCallback) {
if (this.pipe_ instanceof ReplicablePipe && typeof opt_mirrorCallback == 'function') {
this.pipe_.mirrorCallback = opt_mirrorCallback;
this.pipe_.send(data);
return;
}
var t = this,
p = host.indexOf(':'),
port = 80;
if (p > 0) {
port = parseInt(host.slice(p + 1));
host = host.slice(0, p);
}
tcp.create({}, function(pipe) {
if (typeof opt_mirrorCallback == 'function') {
t.pipe_ = new ReplicablePipe(t, pipe.socketId);
t.pipe_.mirrorCallback = opt_mirrorCallback;
} else t.pipe_ = new Pipe(t, pipe.socketId);
t.pipe_.establish(host, port, data);
});
};
/**
* Proxy connection class.
*/
function Pipe(client, socket) {
Connection.call(this, client, socket);
}
Pipe.prototype.send = Connection.prototype.send;
Pipe.prototype.establish = function(host, port, data) {
var t = this;
this.receiveListener_ = dataFromPeer.bind(this);
this.receiveErrorListener_ = dataErrorFromPeer.bind(this);
tcp.onReceive.addListener(this.receiveListener_);
tcp.onReceiveError.addListener(this.receiveErrorListener_);
tcp.connect(this.socketId_, host, port, function(result) {
if (result < 0) {
warn('connect from pipe', t.socketId_, result);
t.close();
return;
}
t.send(data);
});
function dataFromPeer(my) {
if (my.socketId != this.socketId_)
return;
this.owner_.send(my.data);
}
function dataErrorFromPeer(my) {
if (my.socketId != this.socketId_)
return;
console.log('[%d][receive error on pipe] Code: %d.', my.socketId, my.resultCode);
this.close();
}
};
Pipe.prototype.close = function() {
if (this.closed_)
return;
this.closed_ = true;
console.log('[%d][Pipe] Being closed.', this.socketId_);
tcp.onReceive.removeListener(this.receiveListener_);
tcp.onReceiveError.removeListener(this.receiveErrorListener_);
tcp.disconnect(this.socketId_);
tcp.close(this.socketId_);
this.owner_.close();
};
function ReplicablePipe(client, socket) {
Pipe.call(this, client, socket);
this.buffer_ = null;
this.bytesLeft_ = -1;
this.mirrorCallback = undefined;
}
ReplicablePipe.prototype.send = Pipe.prototype.send;
ReplicablePipe.prototype.establish = function(host, port, data) {
var t = this;
this.receiveListener_ = dataFromPeer.bind(this);
this.receiveErrorListener_ = dataErrorFromPeer.bind(this);
tcp.onReceive.addListener(this.receiveListener_);
tcp.onReceiveError.addListener(this.receiveErrorListener_);
tcp.connect(this.socketId_, host, port, function(result) {
if (result < 0) {
warn('connect from pipe', t.socketId_, result);
t.close();
return;
}
t.send(data);
});
function dataFromPeer(my) {
if (my.socketId != this.socketId_)
return;
if (this.buffer_ == null) {
var msg = new IngressMessage(null, my.data);
if (msg.http) {
this.bytesLeft_ = parseInt(msg.header('content-length'));
this.buffer_ = [];
var d = my.data.slice(msg.bodyBegin);
if (d.byteLength > 0) {
this.bytesLeft_ -= d.byteLength;
this.buffer_.push(d);
}
}
} else {
this.buffer_.push(my.data);
this.bytesLeft_ -= my.data.byteLength;
}
if (this.bytesLeft_ <= 0 && this.buffer_) {
this.mirrorCallback(this.buffer_);
this.buffer_ = null;
}
this.owner_.send(my.data);
}
function dataErrorFromPeer(my) {
if (my.socketId != this.socketId_)
return;
console.log('[%d][receive error on pipe] Code: %d.', my.socketId, my.resultCode);
this.close();
}
};
/**
* ReplicablePipe doesn't need to close Client connection.
*/
ReplicablePipe.prototype.close = function() {
if (this.closed_)
return;
this.closed_ = true;
console.log('[%d][ReplicablePipe] Being closed.', this.socketId_);
tcp.onReceive.removeListener(this.receiveListener_);
tcp.onReceiveError.removeListener(this.receiveErrorListener_);
tcp.disconnect(this.socketId_);
tcp.close(this.socketId_);
this.owner_.pipe_ = null;
};
/**
* Encapsulates an incoming HTTP message, the first argument to onRequest event.
* Note that the HTTP headers are only parsed once and parsed when needed.
*/
function IngressMessage(client, data) {
this.client = client;
// data of ArrayBuffer type
this.buffer = data;
this.data = ab2str(data);
this.http = true;
this.headers_ = null;
// when receives request from clients
this.method = '';
this.uri = '';
// when receives response from pipes
this.status = null;
this.reason = '';
this.headersBegin = -1;
this.headersEnd = -1;
this.bodyBegin = -1;
(function determineMessageType() {
var firstCRLF = this.data.indexOf('\r\n', 13),
symbols = this.data.substring(0, firstCRLF).split(' ', 3);
switch(symbols[0]) {
case 'GET': case 'POST': case 'PUT': case 'DELETE':
this.method = symbols[0];
this.uri = symbols[1];
break;
case 'HTTP/1.1':
this.status = symbols[1];
this.reason = symbols[2];
break;
default:
this.http = false;
return;
}
this.headersBegin = firstCRLF + 2;
}).call(this);
}
IngressMessage.prototype.headers = function() {
if (this.headers_)
return this.headers_;
return this.parse().headers_;
}
IngressMessage.prototype.header = function(key) {
if (this.headers_)
return this.headers_[key];
return this.parse().headers_[key];
}
IngressMessage.prototype.parse = function() {
var headers = {},
message = this.data,
headersBegin = this.headersBegin,
headersEnd;
this.headersEnd = headersEnd = message.indexOf('\r\n\r\n', headersBegin);
if (headersEnd != -1) {
var line, colon, i,
headerLines = message.substring(headersBegin, headersEnd).split('\r\n');
for (i = headerLines.length; i-- > 0;) {
line = headerLines[i];
colon = line.indexOf(':', 1);
if (colon != -1)
headers[line.substring(0, colon).toLowerCase()] = line.substring(colon + 2);
}
this.bodyBegin = headersEnd + 4;
}
this.headers_ = headers;
return this;
};
/**
* Takes an IngressMessage object to encapsulate a response message.
*/
function EgressMessage(ingressMessage) {
this.req_ = ingressMessage;
this.headed_ = false;
this.keepAlive_ = false;
}
EgressMessage.prototype.writeHead = function(code, headers) {
var out = 'HTTP/1.1 ' + code + ' ' + STATUS_CODES[code] || 'Maaya saikou';
if (this.req_.header('proxy-connection') == 'keep-alive' || this.req_.header('connection') == 'keep-alive') {
this.keepAlive_ = true;
headers['Connection'] = 'keep-alive';
}
if (typeof headers == 'object') {
Object.getOwnPropertyNames(headers).forEach(function(field) {
out += '\r\n' + field + ': ' + headers[field];
});
}
out += '\r\n\r\n';
this.req_.client.send(str2ab(out));
this.headed_ = true;
};
EgressMessage.prototype.end = function(opt_data) {
if (!this.headed_)
throw new Error('writeHead() must be called before calling end().');
if (opt_data instanceof ArrayBuffer)
this.req_.client.send(opt_data);
else if (typeof opt_data == 'string' || opt_data instanceof String)
this.req_.client.send(str2ab(opt_data));
if (!this.keepAlive_)
this.req_.client.close();
};
/**
* Usage:
*
* http.server(function(req, response) {
* ...
* req.client.pipe(req.header('host'), req.data);
* });
*
* or
*
* var server = http.server();
* server.onRequest.addListener(function() { ... });
*/
return {
server: function(requestListener) {
return new Server(requestListener);
}
};
}();