forked from nasss/spdy-client
/
connection.js
590 lines (519 loc) · 21.3 KB
/
connection.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
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
var spdy = require('spdy'),
stream = require('stream'),
events = require('events'),
util = require('util'),
net = require('net'),
tls = require('tls'),
utils = require('./utils'),
fs = require('fs'),
client = require('./client'),
requestMod = require('./request'),
log4js = require('log4js'),
Buffer = require('buffer').Buffer;
/* -----INIT----- */
var logger = log4js.getLogger('CONNECTION');
logger.setLevel('ERROR');
var connection = exports;
connection.logger = logger;
//
// ### function ClientSpdyResponse (frame)
// #### @frame {Object} frame generated
// SPDY Response @constructor
//
function ClientSpdyResponse(frame) {
var self = this;
self.frame = frame;
self.headers = frame.headers;
self.httpVersion = frame.headers.version;
self.statusCode = parseInt(frame.headers.status.slice(0, 3));
return self;
}
util.inherits(ClientSpdyResponse, stream);
//
// ### function ClientSpdyConnection (host, port, plain)
// #### @host {String} server host
// #### @port {Integer} server port
// #### @plain {Boolean} (optional) plain or tls connection
// #### @version {Integer} (optional) SPDY version
// TCP connection @constructor
//
function ClientSpdyConnection(host, port, plain, version) {
process.EventEmitter.call(this);
var self = this;
/* init connection fields */
this.opened = false;
this.version = version || 3;
/* code mostly copied from server.js:Connection(socket, pool, options) */
this.deflate = this._deflate = spdy.utils.createDeflate(this.version);
this.inflate = this._inflate = spdy.utils.createInflate(this.version);
/* deflate and inflate fields may be only visible at framer level? */
this.framer = new spdy.protocol['' + this.version].Framer(spdy.utils
.zwrap(this.deflate), spdy.utils.zwrap(this.inflate));
this.streams = {};
this.streamsCount = 0;
this.streamId = -1;
this.pushId = 0;
this.pingId = -1;
this.goAway = 0;
this.pendingRequests = [];
this._rstCode = 1;
this.maxStreams = 100;
/* Last good Stream ID for a goaway frame */
this.lastGoodID = 0;
/* Lock data */
this._locked = false;
this._lockBuffer = [];
this.on('close', function() {
this.closeConnection();
});
this.on('ping', function(id) {
logger.info('PING RECEIVED ID=', id, '-- for connection: (', host,
port, (plain ? "plain)" : "tls)"));
});
this.on('error', function(err) {
this.socket.emit('error', err);
});
/* create socket */
if (!plain) {
logger.info("/* trying tls connection */");
this.socket = tls.connect(port, host,
/* can be parameterized correctly */
{
NPNProtocols : [ 'spdy/' + this.version ],
rejectUnauthorized : false
}, function() {
/* ready to start interacting with server */
self.startConnection();
self.socket.on('close', function() {
logger.info('Socket closed');
});
});
this.socket.on('error', function(err) {
logger.error("connection socket error : ", err.message);
});
} else {
logger.info("trying plain connection");
this.socket = net.connect(port, host, function() {
/* ready to start interacting with server */
self.startConnection();
self.socket.on('close', function() {
logger.info('Socket closed');
});
});
this.socket.on('error', function(err) {
logger.error("connection socket error : ", err.message);
});
}
/*
* create parser that will be responsible of receiving the data from the
* server
*/
this.parser = spdy.parser.create(this, this.deflate, this.inflate);
this.parser
.on(
'frame',
function(frame) {
if (!this.connection.opened)
return;
logger.trace("client parser, on frame : ", frame);
/*
* process the frame, typical case will be a SYN_REPLY
* or a DATA frame
*/
if (frame.type === 'SYN_REPLY') {
var request = self.streams[frame.id];
if (request == null) {
/* send rst-stream with error INVALID_STREAM */
self.write(self.framer.rstFrame(frame.id, 2));
/* close connection */
self.opened = false;
self.emit('close');
return;
} else {
/* If we reached stream limit */
if (self.streamsCount > self.maxStreams) {
request.once('error', function onerror() {
});
/* REFUSED_STREAM */
self._rstCode = 3;
self.closeRequest(request.id);
return;
}
/* set headers, status and so on within response */
request.response = new ClientSpdyResponse(frame);
request.response.method = request.options.method;
request.emit("method_"
+ request.response.method);
if (request.response.statusCode == 100) {
request.emit("continue");
}
request.response.url = request.options.path
|| request.options.url;
request.callback(request.response);
/*
* send 'data' event if there is some data
* available
*/
if (frame.data != null) {
request.response.emit('data', frame.data,
frame.priority, frame.data.length);
}
}
} else if (frame.type == 'DATA') {
var request = self.streams[frame.id];
if (request == null) {
/* send rst-stream with error INVALID_STREAM */
self.write(this.connection.framer.rstFrame(
frame.id, 2));
/* close connection */
self.opened = false;
self.emit('close');
return;
} else {
/*
* response should send 'data' event if there is
* some data available
*/
if (frame.data.length > 0) {
if (request.halfclosed.server) {
/*
* should not reveive data from half
* closed server
*/
self.write(self.framer.rstFrame(
frame.id, 9));
return;
}
request.response.emit('data', frame.data,
0, frame.data.length);
}
}
} else if (frame.type === "SYN_STREAM") {
this.lastGoodID = frame.id;
/* Get original stream */
var originalReq = self.streams[frame.associated];
/* Verify headers */
if (!(frame.headers.path && frame.headers.scheme && frame.headers.host)
|| originalReq == null) {
/* send rst */
if (frame.assoc == 0) {
/* protocol error = 1 */
self.write(this.connection.framer.rstFrame(
frame.id, 1));
} else {
/* http_protocol_error id = ??? */
self.write(this.connection.framer.rstFrame(
frame.id, 1));
}
return;
} else if (frame.headers.host != originalReq.options.host) {
/* different hosts */
return;
}
/*
* pushed document, send push event and call a
* specific push callback
*/
originalReq.emit('push', frame); // just for log
var opt = {
method : 'GET',
url : frame.headers.path,
host : frame.headers.host,
// Needed
scheme : frame.headers.scheme,
headers : originalReq.options.headers
}
if (originalReq.port)
opt.port = originalReq.port;
var pushReqObj = new requestMod.ClientSpdyRequest(
opt, null, this);
pushReqObj.id = frame.id;
self.streams[frame.id]= pushReqObj;
self.streamsCount++;
/*
* unidirectionnal : the client will not send this
* request
*/
pushReqObj.halfclosed.client = true;
originalReq.pushedStreams.push(pushReqObj);
/* callback for pushed streams */
var cb = originalReq.options.pushcb;
if (cb) {
/* call push callback */
var accept = cb(opt, originalReq);
if (accept.error) {
/* cancel the original stream */
self.write(this.connection.framer.rstFrame(
frame.associated, 5));
accept.error();
} else {
/*
* Create a response and call the received
* callback
*/
pushReqObj.response = new ClientSpdyResponse(
frame);
pushReqObj.response.method = pushReqObj.options.method;
pushReqObj.response.url = pushReqObj.options.path
|| pushReqObj.options.url;
accept.success(pushReqObj.response);
}
} else {
/* cancel the original stream */
self.write(this.connection.framer.rstFrame(
frame.associated, 5));
}
}
else if (frame.type === 'RST_STREAM') {
logger.info("GET RST_STREAM from server status= "
+ frame.status);
self._rstCode = 0;
self.closeRequest(frame.id);
} else if (frame.type === 'PING') {
if (frame.pingId % 2 == 0) {
/* OK : valid even id from the server */
self.write(self.framer.pingFrame(frame.pingId));
}
/* else it's ping response or bad id */
logger.debug("ping response : ", frame);
self.emit('ping', frame.pingId
.readUInt32BE(0, true) & 0x7fffffff);
} else if (frame.type === 'SETTINGS') {
/* TODO later */
} else if (frame.type === 'GOAWAY') {
self.goAway = frame.lastId;
/* TODO later */
} else if (frame.type === 'WINDOW_UPDATE') {
/* TODO later */
} else {
logger.error('Unknown type: ', frame.type);
}
/* TODO headers frame? */
/* Handle half-closed */
if (frame.fin) {
var request = self.streams[frame.id];
/*
* response should send 'end' event Emitted exactly
* once for each response. After that, no more
* 'data' events will be emitted on the response.
*/
request.response.emit('end');
/* half close state */
request.halfclosed.server = true;
}
});
/* pipe socket with parser */
this.socket.pipe(this.parser);
}
util.inherits(ClientSpdyConnection, process.EventEmitter);
//
// ### function _lock (callback)
// #### @callback {Function} continuation callback
// Acquire lock
//
ClientSpdyConnection.prototype._lock = function lock(callback) {
if (!callback)
return;
if (this._locked) {
this._lockBuffer.push(callback);
} else {
this._locked = true;
callback.call(this, null);
}
};
//
// ### function _unlock ()
// Release lock and call all buffered callbacks
//
ClientSpdyConnection.prototype._unlock = function unlock() {
if (this._locked) {
this._locked = false;
this._lock(this._lockBuffer.shift());
}
};
//
// ### function write (data, encoding)
// #### @data {String|Buffer} data
// #### @encoding {String} (optional) encoding
// Writes data to socket
//
ClientSpdyConnection.prototype.write = function write(data, encoding) {
if (this.socket.writable) {
return this.socket.write(data, encoding);
}
};
//
// ### function startConnection ()
// Callback for the TCP connection
//
ClientSpdyConnection.prototype.startConnection = function startConnection() {
logger.info("/* started connection */");
this.opened = true;
this.write(this.windowSizeFrame(10485760));
this.socket.setTimeout(2 * 60 * 1000);
this.socket.once('timeout', function ontimeout() {
logger.error('connection timeout');
this.destroy();
});
for ( var cptr = 0; cptr < this.pendingRequests.length; cptr++) {
this.doRequest(this.pendingRequests[cptr]);
}
this.pendingRequests = [];
}
//
// ### function startRequest (options, callback)
// #### @options {Object} request options
// #### @callback {Function} (optional) request callback
// Sends request or store it for later
//
ClientSpdyConnection.prototype.startRequest = function startRequest(options,
callback) {
var req = new requestMod.ClientSpdyRequest(options, callback, this);
if (this.opened && req.isReady()) {
/* do the request immediatly */
this.doRequest(req);
} else {
/* check error cases as well */
this.pendingRequests.push(req);
}
return req;
}
//
// ### function doRequest (ClientSpdyRequest)
// #### @request {ClientSpdyRequest} Client request
// Do the request immediatly
//
ClientSpdyConnection.prototype.doRequest = function doRequest(request) {
if (!request.isReady())
return;
request.isPendingRequest = false;
var self = this;
var dict = utils.headersToDict(request.options.headers, function(headers) {
headers[':version'] = 'HTTP/1.1';
headers[':method'] = request.options.method;
headers[':url'] = request.options.url;
headers[':path'] = request.options.path;
headers[':port'] = request.options.port;
headers[':host'] = request.options.host;
});
var callback = request.callback;
/* Create a new stream, reuse framer for that */
this._lock(function() {
this.streamId += 2;
this.framer._synFrame('SYN_STREAM', this.streamId, null, 0, dict,
function(err, frame) {
if (err) {
request.emit('error', err);
return;
}
/* store the request with the frame id */
self.streams[self.streamId] = request;
self.streamsCount++;
request.id = self.streamId;
/* write frame within socket */
self.write(frame);
request.emit('socket', self.socket);
self._unlock();
request.flushPendingData(function() {
logger.debug("DATA FLUNSHED");
});
});
});
}
//
// ### function doPendingRequest (ClientSpdyRequest)
// #### @req {ClientSpdyRequest} Client request
// Do the pending request
//
ClientSpdyConnection.prototype.doPendingRequest = function doPendingRequest(req) {
if (this.opened && req.isReady()) {
/* do the request immediatly */
this.doRequest(req);
/* remove from pending */
for ( var i = 0; i < this.pendingRequests.length; i++) {
if (this.pendingRequests[i].id == req.id) {
this.pendingRequests.splice(i, 1);
}
}
}
return req;
}
connection.ClientSpdyConnection = ClientSpdyConnection;
//
// ### function ping ()
// Writes ping frame
//
ClientSpdyConnection.prototype.ping = function() {
/* It is a priority */
var id = new Buffer(4);
id.writeUInt32BE((this.pingId += 2) & 0x7fffffff, 0, true); // -ID
return this.write(this.framer.pingFrame(id));
}
//
// ### function closeConnection()
// Close connection
ClientSpdyConnection.prototype.closeConnection = function closeConnection() {
// close proprely
this.goaway(0);
if (this.socket)
this.socket.destroy();
}
//
// ### function closeRequest (id,error)
// #### @id {Integer} stream ID
// #### @error {Error} (optional) error
// Destroys stream
//
ClientSpdyConnection.prototype.closeRequest = function closeRequest(id, error) {
var _stream = this.streams[id];
if (error) {
if (this._rstCode) {
this.write(this.framer.rstFrame(id, this._rstCode));
}
}
if (error) {
_stream.emit('error', error);
this.emit('error', error);
}
if (id % 2 === 1) {
this.streamsCount--;
}
delete this.streams[id];
};
//
// ### function goaway ()
// Stop accepting/sending streams on this connection
//
ClientSpdyConnection.prototype.goaway = function goaway(status) {
var header = new Buffer(16);
header.writeUInt32BE(0x80030007, 0, true); // Version and type
header.writeUInt32BE(0x00000008, 4, true); // Length
var id = new Buffer(4);
// Server last good StreamID
id.writeUInt32BE((this.lastGoodID) & 0x7fffffff, 0, true);
id.copy(header, 8, 0, 4);
var statusBuffer = new Buffer(4);
statusBuffer.writeUInt32BE((status || 0) & 0x7fffffff, 0, true);
statusBuffer.copy(header, 12, 0, 4);
this.goAway = this.streamId;
this.write(header);
}
//
// ### function windowSizeFrame (size)
// #### @size {Number} data transfer window size
// Sends SETTINGS frame with window size
//
ClientSpdyConnection.prototype.windowSizeFrame = function windowSizeFrame(size) {
var settings;
if (!(settings = ClientSpdyConnection.windowSizeCache[size])) {
settings = new Buffer(20);
settings.writeUInt32BE(0x80030004, 0, true); // Version and type
settings.writeUInt32BE((4 + 8) & 0x00FFFFFF, 4, true); // length
settings.writeUInt32BE(0x00000001, 8, true); // Count of entries
settings.writeUInt32BE(0x01000007, 12, true); // Entry ID and Persist flag
settings.writeUInt32BE(size & 0x7fffffff, 16, true); // Window Size (KB)
ClientSpdyConnection.windowSizeCache[size] = settings;
}
return settings;
};
ClientSpdyConnection.windowSizeCache = {};