-
Notifications
You must be signed in to change notification settings - Fork 195
/
connection.js
648 lines (548 loc) · 15.8 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
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
var util = require('util');
var assert = require('assert');
var spdy = require('../spdy');
var constants = spdy.protocol.constants;
var Stream = spdy.Stream;
//
// ### function Connection (socket, state, server)
// #### @socket {net.Socket} server's connection
// #### @options {Object} Connection options
// #### @server {net.Server} server
// Abstract connection @constructor
//
function Connection(socket, options, server) {
process.EventEmitter.call(this);
var state = {};
this._spdyState = state;
// NOTE: There's a big trick here. Connection is used as a `this` argument
// to the wrapped `connection` event listener.
// socket end doesn't necessarly mean connection drop
this.httpAllowHalfOpen = true;
// Socket timeout
this.timeout = server && server.timeout || 0;
// Defaults
state.maxStreams = options.maxStreams || Infinity;
state.initialSinkSize = constants.DEFAULT_WINDOW;
state.initialWindowSize = options.windowSize || 1 << 20;
state.autoSpdy31 = options.autoSpdy31;
// Connection-level flow control
state.sinkSize = state.initialSinkSize;
state.windowSize = constants.DEFAULT_WINDOW;
// Interleaving configuration
state.maxChunk = options.maxChunk === undefined ? 8 * 1024 : options.maxChunk;
// Various state info
state.closed = false;
state.pool = spdy.zlibpool.create(options.headerCompression);
state.counters = {
pushCount: 0,
streamCount: 0
};
state.socketBuffering = false;
state.version = null;
state.deflate = null;
state.inflate = null;
// Init streams list
state.isServer = options.isServer;
state.streams = {};
state.streamCount = 0;
state.lastId = 0;
state.pushId = 0;
state.pingId = state.isServer ? 0 : 1;
state.pings = {};
state.goaway = false;
// X-Forwarded feature
state.xForward = null;
// Initialize scheduler
state.scheduler = spdy.scheduler.create(this);
// Create parser and hole for framer
state.parser = spdy.protocol.parser.create(this);
state.framer = spdy.protocol.framer.create();
// Lock data
state.locked = false;
state.lockQueue = [];
this.socket = socket;
this.encrypted = socket.encrypted;
this.readable = true;
this.writable = true;
this._init();
}
util.inherits(Connection, process.EventEmitter);
exports.Connection = Connection;
Connection.prototype._init = function init() {
var self = this;
var state = this._spdyState;
var pool = state.pool;
var pair = null;
var sentSettings = false;
// Initialize parser
this._spdyState.parser.on('frame', this._handleFrame.bind(this));
this._spdyState.parser.on('version', function onversion(version) {
var prev = state.version;
var framer = state.framer;
state.version = version;
// Ignore transition to 3.1
if (!prev) {
pair = pool.get('spdy/' + version);
state.deflate = pair.deflate;
state.inflate = pair.inflate;
framer.setCompression(pair.deflate, pair.inflate);
}
framer.setVersion(version);
// Send settings frame (once)
if (!sentSettings) {
sentSettings = true;
framer.settingsFrame({
maxStreams: state.maxStreams,
windowSize: state.initialWindowSize
}, function(err, frame) {
if (err)
return self.emit('error', err);
self.write(frame);
});
// Send WINDOW_UPDATE for 3.1
self._sendWindowUpdate(true);
}
});
// Propagate parser errors
state.parser.on('error', function onParserError(err) {
self.emit('error', err);
});
this.socket.pipe(state.parser);
// 2 minutes socket timeout
this.socket.setTimeout(this.timeout);
this.socket.once('timeout', function ontimeout() {
self.socket.destroy();
});
// Allow high-level api to catch socket errors
this.socket.on('error', function onSocketError(e) {
self.emit('error', e);
});
this.socket.once('close', function onclose() {
var err = new Error('socket hang up');
err.code = 'ECONNRESET';
self._destroyStreams(err);
self.emit('close');
state.closed = true;
if (pair)
pool.put(pair);
});
// Do not allow half-open connections
this.socket.allowHalfOpen = false;
if (spdy.utils.isLegacy) {
this.socket.on('drain', function ondrain() {
self.emit('drain');
});
}
// for both legacy and non-legacy, when our socket is ready for writes again,
// drain the sinks in case they were queuing due to socketBuffering.
this.socket.on('drain', function () {
if (!state.socketBuffering)
return;
state.socketBuffering = false;
Object.keys(state.streams).forEach(function(id) {
state.streams[id]._drainSink(0);
});
});
};
//
// ### function handleFrame (frame)
// #### @frame {Object} SPDY frame
//
Connection.prototype._handleFrame = function handleFrame(frame) {
var state = this._spdyState;
if (state.closed)
return;
var stream;
// Create new stream
if (frame.type === 'SYN_STREAM') {
stream = this._handleSynStream(frame);
} else {
if (frame.id !== undefined) {
// Load created one
stream = state.streams[frame.id];
// Fail if not found
if (stream === undefined &&
!(frame.type === 'WINDOW_UPDATE' && frame.id === 0)) {
if (frame.type === 'RST_STREAM')
return;
this._rst(frame.id, constants.rst.INVALID_STREAM);
return;
}
}
// Emit 'data' event
if (frame.type === 'DATA') {
this._handleData(stream, frame);
// Reply for client stream
} else if (frame.type === 'SYN_REPLY') {
// If stream not client - send RST
if (!stream._spdyState.isClient) {
this._rst(frame.id, constants.rst.PROTOCOL_ERROR);
return;
}
stream._handleResponse(frame);
// Destroy stream if we were asked to do this
} else if (frame.type === 'RST_STREAM') {
stream._spdyState.rstCode = 0;
stream._spdyState.closedBy.us = true;
stream._spdyState.closedBy.them = true;
// Emit error on destroy
var err = new Error('Received rst: ' + frame.status);
err.code = 'RST_STREAM';
err.status = frame.status;
stream.destroy(err);
// Respond with same PING
} else if (frame.type === 'PING') {
this._handlePing(frame.pingId);
} else if (frame.type === 'SETTINGS') {
this._setDefaultWindow(frame.settings);
} else if (frame.type === 'GOAWAY') {
state.goaway = frame.lastId;
this.writable = false;
} else if (frame.type === 'WINDOW_UPDATE') {
if (stream)
stream._drainSink(frame.delta);
else
this._drainSink(frame.delta);
} else if (frame.type === 'HEADERS') {
stream.emit('headers', frame.headers);
} else if (frame.type === 'X_FORWARDED') {
state.xForward = frame.host;
} else {
console.error('Unknown type: ', frame.type);
}
}
// Handle half-closed
if (frame.fin) {
// Don't allow to close stream twice
if (stream._spdyState.closedBy.them) {
stream._spdyState.rstCode = constants.rst.PROTOCOL_ERROR;
stream.emit('error', 'Already half-closed');
} else {
stream._spdyState.closedBy.them = true;
// Receive FIN
stream._recvEnd();
stream._handleClose();
}
}
};
//
// ### function handleSynStream (frame)
// #### @frame {Object} SPDY frame
//
Connection.prototype._handleSynStream = function handleSynStream(frame) {
var state = this._spdyState;
var associated;
if (state.goaway && state.goaway < frame.id)
return this._rst(frame.id, constants.rst.REFUSED_STREAM);
// PUSH stream
if (!state.isServer) {
// Incorrect frame id
if (frame.id % 2 === 1 || frame.associated % 2 === 0)
return this._rst(frame.id, constants.rst.PROTOCOL_ERROR);
associated = state.streams[frame.associated];
// Fail if not found
if (associated === undefined) {
if (frame.type === 'RST_STREAM')
return;
this._rst(frame.id, constants.rst.INVALID_STREAM);
return;
}
}
var stream = new Stream(this, frame);
// Associate streams
if (associated) {
stream.associated = associated;
}
// TODO(indutny) handle stream limit
this.emit('stream', stream);
stream._start(frame.url, frame.headers);
return stream;
};
//
// ### function _handleData (stream, frame)
// #### @stream {Stream} SPDY Stream
// #### @frame {Object} SPDY frame
//
Connection.prototype._handleData = function handleData(stream, frame) {
var state = this._spdyState;
if (frame.data.length > 0) {
if (stream._spdyState.closedBy.them) {
stream._spdyState.rstCode = constants.rst.PROTOCOL_ERROR;
stream.emit('error', 'Writing to half-closed stream');
} else {
// Connection-level flow control
state.windowSize -= frame.data.length;
this._sendWindowUpdate();
stream._recv(frame.data);
}
}
};
//
// ### function sendWindowUpdate (force)
// #### @force {Boolean} send even if windowSize is positive
// Send WINDOW_UPDATE if needed
//
Connection.prototype._sendWindowUpdate = function sendWindowUpdate(force) {
var state = this._spdyState;
if (state.version < 3.1 && (!state.isServer || !state.autoSpdy31) ||
state.windowSize > state.initialWindowSize / 2 && !force) {
return;
}
var self = this;
var delta = state.initialWindowSize - state.windowSize;
if (delta === 0)
return;
state.windowSize += delta;
state.framer.windowUpdateFrame(0, delta, function(err, frame) {
if (err)
return self.emit('error', err);
self.write(frame);
});
};
//
// ### function _drainSink (delta)
// #### @delta {Number} WINDOW_UPDATE delta value
//
Connection.prototype._drainSink = function drainSink(delta) {
var state = this._spdyState;
// Switch to 3.1
if (state.version !== 3.1) {
this._setVersion(3.1);
this._sendWindowUpdate();
}
state.sinkSize += delta;
if (state.sinkSize <= 0)
return;
this.emit('drain');
// Try to write all pending data to the socket
Object.keys(state.streams).forEach(function(id) {
state.streams[id]._drainSink(0);
});
};
//
// ### function setVersion (version)
// #### @version {Number} Protocol version
// Set protocol version to use
//
Connection.prototype._setVersion = function setVersion(version) {
this._spdyState.parser.setVersion(version);
};
//
// ### function addStream (stream)
// #### @stream {Stream}
//
Connection.prototype._addStream = function addStream(stream) {
var state = this._spdyState;
var id = stream._spdyState.id;
if (state.streams[id])
return;
state.streams[id] = stream;
// Update lastId
state.lastId = Math.max(state.lastId, id);
var isClient = id % 2 == 1;
if (isClient && state.isServer || !isClient && !state.isServer)
state.streamCount++;
};
//
// ### function removeStream (stream)
// #### @stream {Stream}
//
Connection.prototype._removeStream = function removeStream(stream) {
var state = this._spdyState;
var id = stream._spdyState.id;
if (!state.streams[id])
return;
delete state.streams[id];
var isClient = id % 2 == 1;
if (isClient && state.isServer || !isClient && !state.isServer)
state.streamCount--;
if (!state.isServer &&
state.goaway &&
state.streamCount === 0 &&
this.socket) {
this.socket.destroySoon();
}
};
//
// ### function destroyStreams (err)
// #### @err {Error} *optional*
// Destroys all active streams
//
Connection.prototype._destroyStreams = function destroyStreams(err) {
var state = this._spdyState;
var streams = state.streams;
state.streams = {};
state.streamCount = 0;
Object.keys(streams).forEach(function(id) {
streams[id].destroy();
});
};
//
// ### function _rst (streamId, code, extra)
// #### @streamId {Number}
// #### @code {Number}
// #### @extra {String}
// Send RST frame
//
Connection.prototype._rst = function rst(streamId, code, extra) {
var self = this;
this._spdyState.framer.rstFrame(streamId, code, extra, function(err, frame) {
if (err)
return self.emit('error', err);
self.write(frame);
});
};
//
// ### function lock (callback)
// #### @callback {Function} continuation callback
// Acquire lock
//
Connection.prototype._lock = function lock(callback) {
if (!callback)
return;
var state = this._spdyState;
if (state.locked) {
state.lockQueue.push(callback);
} else {
state.locked = true;
callback(null);
}
};
//
// ### function unlock ()
// Release lock and call all buffered callbacks
//
Connection.prototype._unlock = function unlock() {
var state = this._spdyState;
if (state.locked) {
if (state.lockQueue.length) {
var cb = state.lockQueue.shift();
cb(null);
} else {
state.locked = false;
}
}
};
//
// ### function write (data, encoding)
// #### @data {String|Buffer} data
// #### @encoding {String} (optional) encoding
// Writes data to socket
//
Connection.prototype.write = function write(data, encoding) {
if (this.socket.writable) {
var wroteThrough = this.socket.write(data, encoding);
// if write returns false, the socket layer is buffering the data, so let's
// set a flag that we can use to create some backpressure
if (!wroteThrough)
this._spdyState.socketBuffering = true;
return wroteThrough;
}
};
//
// ### function _setDefaultWindow (settings)
// #### @settings {Object}
// Update the default transfer window -- in the connection and in the
// active streams
//
Connection.prototype._setDefaultWindow = function _setDefaultWindow(settings) {
if (!settings)
return;
if (!settings.initial_window_size ||
settings.initial_window_size.persisted) {
return;
}
var state = this._spdyState;
state.initialSinkSize = settings.initial_window_size.value;
Object.keys(state.streams).forEach(function(id) {
state.streams[id]._updateSinkSize(settings.initial_window_size.value);
});
};
//
// ### function handlePing (id)
// #### @id {Number} PING id
//
Connection.prototype._handlePing = function handlePing(id) {
var self = this;
var state = this._spdyState;
var ours = state.isServer && (id % 2 === 0) ||
!state.isServer && (id % 2 === 1);
// Handle incoming PING
if (!ours) {
state.framer.pingFrame(id, function(err, frame) {
if (err)
return self.emit('error', err);
self.emit('ping', id);
self.write(frame);
});
return;
}
// Handle reply PING
if (!state.pings[id])
return;
var ping = state.pings[id];
delete state.pings[id];
if (ping.cb)
ping.cb(null);
};
//
// ### function ping (callback)
// #### @callback {Function}
// Send PING frame and invoke callback once received it back
//
Connection.prototype.ping = function ping(callback) {
var self = this;
var state = this._spdyState;
var id = state.pingId;
state.pingId += 2;
state.framer.pingFrame(id, function(err, frame) {
if (err)
return self.emit('error', err);
state.pings[id] = { cb: callback };
self.write(frame);
});
};
//
// ### function getCounter (name)
// #### @name {String} Counter name
// Get counter value
//
Connection.prototype.getCounter = function getCounter(name) {
return this._spdyState.counters[name];
};
//
// ### function cork ()
// Accumulate data before writing out
//
Connection.prototype.cork = function cork() {
if (this.socket && this.socket.cork)
this.socket.cork();
};
//
// ### function uncork ()
// Write out accumulated data
//
Connection.prototype.uncork = function uncork() {
if (this.socket && this.socket.uncork)
this.socket.uncork();
};
Connection.prototype.end = function end() {
var self = this;
var state = this._spdyState;
state.framer.goawayFrame(state.lastId,
constants.goaway.OK,
function(err, frame) {
if (err)
return self.emit('error', err);
self.write(frame, function() {
state.goaway = state.lastId;
// Destroy socket if there are no streams
if (!state.isServer &&
state.goaway &&
state.streamCount === 0 &&
self.socket) {
self.socket.destroySoon();
}
});
});
};