-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
185 lines (141 loc) · 4.03 KB
/
server.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
'use strict';
var EventEmitter = require('events').EventEmitter;
var inherits = require('inherits');
var spdy = require('spdy');
var async = require('async');
var channels = require('./channels');
var ReadChannel = channels.ReadChannel;
var WriteChannel = channels.WriteChannel;
var ByteStream = channels.ByteStream;
var encoder = require('jschan/lib/encoder');
var generate = require('self-signed');
function ServerSession() {
this.encoder = encoder(this, channels);
this._nextId = 0;
this._channels = {};
this._awayting = {};
var that = this;
this.encoder.on('channel', function (chan) {
var id = chan.id;
if (that._awayting[id]) {
chan.handle(that._awayting[id].req, that._awayting[id].res);
delete that._awayting[id];
}
chan.on('close', function() {
delete that._channels[id];
});
that._channels[id] = chan;
return chan;
});
}
inherits(ServerSession, EventEmitter);
function pushStream(parent, chan) {
// parent._outStream is our response
var pusher = parent._outStream;
pusher.push('/', {
'libchan-parent-ref': parent.id,
'libchan-ref': chan.id
}, function(err, stream) {
if (err) {
return chan.emit('error', err);
}
// the first chunk contains the HTTP headers
// let's just skip it, but we should have them
// parsed
// FIXME submit issue to node-spdy
chan.skipFirstChunk = true;
chan.handle(stream, stream);
});
}
function createChannel(session, Klass, parent) {
var id = (session._nextId += 2);
var chan = new Klass(session, id);
session._channels[id] = chan;
chan.on('close', function() {
delete session._channels[id];
});
chan.on('error', session.emit.bind(session, 'error'));
if (parent) {
pushStream(parent, chan);
}
return chan;
}
ServerSession.prototype._createReadChannel = function(parent) {
return createChannel(this, ReadChannel, parent);
};
ServerSession.prototype._createWriteChannel = function(parent) {
return createChannel(this, WriteChannel, parent);
};
ServerSession.prototype._createByteStream = function(parent) {
return createChannel(this, ByteStream, parent);
};
ServerSession.prototype._receive = function receive(req, res) {
var chan;
var id = req.headers['libchan-ref'];
var parent = req.headers['libchan-parent-ref'];
if (parent === '0') {
chan = this._createReadChannel();
this._channels[id] = chan;
chan.handle(req, res);
this.emit('channel', chan);
} else if (this._channels[id]) {
chan.parent = this._channels[parent];
chan = this._channels[id];
chan.handle(req, res);
} else {
this._awayting[id] = {
req: req,
res: res
};
}
res._implicitHeader();
res._send('');
};
ServerSession.prototype.destroy = function close(cb) {
var that = this;
if (cb) {
this.on('close', cb);
}
async.each(Object.keys(this._channels), function(id, cb) {
that._channels[id].destroy(cb);
}, function(err) {
if (cb) {
return cb(err);
}
Object.keys(that._awayting).forEach(function(id) {
that._awayting[id].res.end();
});
if (err) {
that.emit('error', err);
}
that.emit('close');
});
return this;
};
function spdyServer(opts) {
var server;
var generated;
opts = opts || {};
if (!opts.key) {
generated = generate();
opts.key = generated.private;
opts.cert = generated.cert;
}
opts.maxChunk = Math.pow(2, 53);
server = spdy.createServer(opts);
server.on('connection', function(socket) {
// creates a new ServerSession per new connection
var session = new ServerSession();
socket.session = session;
server.emit('session', session);
});
server.on('request', function(req, res) {
// this is the crappiest hack ever
// I'm not sure if this is ever going to change in node-spdy
var session = req.connection.socket.socket.session;
session._receive(req, res);
});
return server;
}
ServerSession.prototype.close = ServerSession.prototype.destroy;
module.exports = spdyServer;