-
Notifications
You must be signed in to change notification settings - Fork 195
/
agent.js
295 lines (238 loc) · 6.76 KB
/
agent.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
'use strict'
var assert = require('assert')
var http = require('http')
var https = require('https')
var net = require('net')
var util = require('util')
var transport = require('spdy-transport')
var debug = require('debug')('spdy:client')
// Node.js 0.10 and 0.12 support
Object.assign = process.versions.modules >= 46
? Object.assign // eslint-disable-next-line
: util._extend
var EventEmitter = require('events').EventEmitter
var spdy = require('../spdy')
var mode = /^v0\.8\./.test(process.version)
? 'rusty'
: /^v0\.(9|10)\./.test(process.version)
? 'old'
: /^v0\.12\./.test(process.version)
? 'normal'
: 'modern'
var proto = {}
function instantiate (base) {
function Agent (options) {
this._init(base, options)
}
util.inherits(Agent, base)
Agent.create = function create (options) {
return new Agent(options)
}
Object.keys(proto).forEach(function (key) {
Agent.prototype[key] = proto[key]
})
return Agent
}
proto._init = function _init (base, options) {
base.call(this, options)
var state = {}
this._spdyState = state
state.host = options.host
state.options = options.spdy || {}
state.secure = this instanceof https.Agent
state.fallback = false
state.createSocket = this._getCreateSocket()
state.socket = null
state.connection = null
// No chunked encoding
this.keepAlive = false
var self = this
this._connect(options, function (err, connection) {
if (err) {
return self.emit('error', err)
}
state.connection = connection
self.emit('_connect')
})
}
proto._getCreateSocket = function _getCreateSocket () {
// Find super's `createSocket` method
var createSocket
var cons = this.constructor.super_
do {
createSocket = cons.prototype.createSocket
if (cons.super_ === EventEmitter || !cons.super_) {
break
}
cons = cons.super_
} while (!createSocket)
if (!createSocket) {
createSocket = http.Agent.prototype.createSocket
}
assert(createSocket, '.createSocket() method not found')
return createSocket
}
proto._connect = function _connect (options, callback) {
var self = this
var state = this._spdyState
var protocols = state.options.protocols || [
'h2',
'spdy/3.1', 'spdy/3', 'spdy/2',
'http/1.1', 'http/1.0'
]
// TODO(indutny): reconnect automatically?
var socket = this.createConnection(Object.assign({
NPNProtocols: protocols,
ALPNProtocols: protocols,
servername: options.servername || options.host
}, options))
state.socket = socket
socket.setNoDelay(true)
function onError (err) {
return callback(err)
}
socket.on('error', onError)
socket.on(state.secure ? 'secureConnect' : 'connect', function () {
socket.removeListener('error', onError)
var protocol
if (state.secure) {
protocol = socket.npnProtocol ||
socket.alpnProtocol ||
state.options.protocol
} else {
protocol = state.options.protocol
}
// HTTP server - kill socket and switch to the fallback mode
if (!protocol || protocol === 'http/1.1' || protocol === 'http/1.0') {
debug('activating fallback')
socket.destroy()
state.fallback = true
return
}
debug('connected protocol=%j', protocol)
var connection = transport.connection.create(socket, Object.assign({
protocol: /spdy/.test(protocol) ? 'spdy' : 'http2',
isServer: false
}, state.options.connection || {}))
// Pass connection level errors are passed to the agent.
connection.on('error', function (err) {
self.emit('error', err)
})
// Set version when we are certain
if (protocol === 'h2') {
connection.start(4)
} else if (protocol === 'spdy/3.1') {
connection.start(3.1)
} else if (protocol === 'spdy/3') {
connection.start(3)
} else if (protocol === 'spdy/2') {
connection.start(2)
} else {
socket.destroy()
callback(new Error('Unexpected protocol: ' + protocol))
return
}
if (state.options['x-forwarded-for'] !== undefined) {
connection.sendXForwardedFor(state.options['x-forwarded-for'])
}
callback(null, connection)
})
}
proto._createSocket = function _createSocket (req, options, callback) {
var state = this._spdyState
if (state.fallback) { return state.createSocket(req, options) }
var handle = spdy.handle.create(null, null, state.socket)
var socketOptions = {
handle: handle,
allowHalfOpen: true
}
var socket
if (state.secure) {
socket = new spdy.Socket(state.socket, socketOptions)
} else {
socket = new net.Socket(socketOptions)
}
handle.assignSocket(socket)
handle.assignClientRequest(req)
// Create stream only once `req.end()` is called
var self = this
handle.once('needStream', function () {
if (state.connection === null) {
self.once('_connect', function () {
handle.setStream(self._createStream(req, handle))
})
} else {
handle.setStream(self._createStream(req, handle))
}
})
// Yes, it is in reverse
req.on('response', function (res) {
handle.assignRequest(res)
})
handle.assignResponse(req)
// Handle PUSH
req.addListener('newListener', spdy.request.onNewListener)
// For v0.8
socket.readable = true
socket.writable = true
if (callback) {
return callback(null, socket)
}
return socket
}
if (mode === 'modern' || mode === 'normal') {
proto.createSocket = proto._createSocket
} else {
proto.createSocket = function createSocket (name, host, port, addr, req) {
var state = this._spdyState
if (state.fallback) {
return state.createSocket(name, host, port, addr, req)
}
return this._createSocket(req, {
host: host,
port: port
})
}
}
proto._createStream = function _createStream (req, handle) {
var state = this._spdyState
var self = this
return state.connection.reserveStream({
method: req.method,
path: req.path,
headers: req._headers,
host: state.host
}, function (err, stream) {
if (err) {
return self.emit('error', err)
}
stream.on('response', function (status, headers) {
handle.emitResponse(status, headers)
})
})
}
// Public APIs
proto.close = function close (callback) {
var state = this._spdyState
if (state.connection === null) {
this.once('_connect', function () {
this.close(callback)
})
return
}
state.connection.end(callback)
}
exports.Agent = instantiate(https.Agent)
exports.PlainAgent = instantiate(http.Agent)
exports.create = function create (base, options) {
if (typeof base === 'object') {
options = base
base = null
}
if (base) {
return instantiate(base).create(options)
}
if (options.spdy && options.spdy.plain) {
return exports.PlainAgent.create(options)
} else { return exports.Agent.create(options) }
}