1 | 'use strict'
|
2 |
|
3 | var assert = require('assert')
|
4 | var http = require('http')
|
5 | var https = require('https')
|
6 | var net = require('net')
|
7 | var util = require('util')
|
8 | var transport = require('spdy-transport')
|
9 | var debug = require('debug')('spdy:client')
|
10 |
|
11 |
|
12 | Object.assign = process.versions.modules >= 46
|
13 | ? Object.assign
|
14 | : util._extend
|
15 |
|
16 | var EventEmitter = require('events').EventEmitter
|
17 |
|
18 | var spdy = require('../spdy')
|
19 |
|
20 | var mode = /^v0\.8\./.test(process.version)
|
21 | ? 'rusty'
|
22 | : /^v0\.(9|10)\./.test(process.version)
|
23 | ? 'old'
|
24 | : /^v0\.12\./.test(process.version)
|
25 | ? 'normal'
|
26 | : 'modern'
|
27 |
|
28 | var proto = {}
|
29 |
|
30 | function instantiate (base) {
|
31 | function Agent (options) {
|
32 | this._init(base, options)
|
33 | }
|
34 | util.inherits(Agent, base)
|
35 |
|
36 | Agent.create = function create (options) {
|
37 | return new Agent(options)
|
38 | }
|
39 |
|
40 | Object.keys(proto).forEach(function (key) {
|
41 | Agent.prototype[key] = proto[key]
|
42 | })
|
43 |
|
44 | return Agent
|
45 | }
|
46 |
|
47 | proto._init = function _init (base, options) {
|
48 | base.call(this, options)
|
49 |
|
50 | var state = {}
|
51 | this._spdyState = state
|
52 |
|
53 | state.host = options.host
|
54 | state.options = options.spdy || {}
|
55 | state.secure = this instanceof https.Agent
|
56 | state.fallback = false
|
57 | state.createSocket = this._getCreateSocket()
|
58 | state.socket = null
|
59 | state.connection = null
|
60 |
|
61 |
|
62 | this.keepAlive = false
|
63 |
|
64 | var self = this
|
65 | this._connect(options, function (err, connection) {
|
66 | if (err) {
|
67 | return self.emit('error', err)
|
68 | }
|
69 |
|
70 | state.connection = connection
|
71 | self.emit('_connect')
|
72 | })
|
73 | }
|
74 |
|
75 | proto._getCreateSocket = function _getCreateSocket () {
|
76 |
|
77 | var createSocket
|
78 | var cons = this.constructor.super_
|
79 | do {
|
80 | createSocket = cons.prototype.createSocket
|
81 |
|
82 | if (cons.super_ === EventEmitter || !cons.super_) {
|
83 | break
|
84 | }
|
85 | cons = cons.super_
|
86 | } while (!createSocket)
|
87 | if (!createSocket) {
|
88 | createSocket = http.Agent.prototype.createSocket
|
89 | }
|
90 |
|
91 | assert(createSocket, '.createSocket() method not found')
|
92 |
|
93 | return createSocket
|
94 | }
|
95 |
|
96 | proto._connect = function _connect (options, callback) {
|
97 | var self = this
|
98 | var state = this._spdyState
|
99 |
|
100 | var protocols = state.options.protocols || [
|
101 | 'h2',
|
102 | 'spdy/3.1', 'spdy/3', 'spdy/2',
|
103 | 'http/1.1', 'http/1.0'
|
104 | ]
|
105 |
|
106 |
|
107 | var socket = this.createConnection(Object.assign({
|
108 | NPNProtocols: protocols,
|
109 | ALPNProtocols: protocols,
|
110 | servername: options.servername || options.host
|
111 | }, options))
|
112 | state.socket = socket
|
113 |
|
114 | socket.setNoDelay(true)
|
115 |
|
116 | function onError (err) {
|
117 | return callback(err)
|
118 | }
|
119 | socket.on('error', onError)
|
120 |
|
121 | socket.on(state.secure ? 'secureConnect' : 'connect', function () {
|
122 | socket.removeListener('error', onError)
|
123 |
|
124 | var protocol
|
125 | if (state.secure) {
|
126 | protocol = socket.npnProtocol ||
|
127 | socket.alpnProtocol ||
|
128 | state.options.protocol
|
129 | } else {
|
130 | protocol = state.options.protocol
|
131 | }
|
132 |
|
133 |
|
134 | if (!protocol || protocol === 'http/1.1' || protocol === 'http/1.0') {
|
135 | debug('activating fallback')
|
136 | socket.destroy()
|
137 | state.fallback = true
|
138 | return
|
139 | }
|
140 |
|
141 | debug('connected protocol=%j', protocol)
|
142 | var connection = transport.connection.create(socket, Object.assign({
|
143 | protocol: /spdy/.test(protocol) ? 'spdy' : 'http2',
|
144 | isServer: false
|
145 | }, state.options.connection || {}))
|
146 |
|
147 |
|
148 | connection.on('error', function (err) {
|
149 | self.emit('error', err)
|
150 | })
|
151 |
|
152 |
|
153 | if (protocol === 'h2') {
|
154 | connection.start(4)
|
155 | } else if (protocol === 'spdy/3.1') {
|
156 | connection.start(3.1)
|
157 | } else if (protocol === 'spdy/3') {
|
158 | connection.start(3)
|
159 | } else if (protocol === 'spdy/2') {
|
160 | connection.start(2)
|
161 | } else {
|
162 | socket.destroy()
|
163 | callback(new Error('Unexpected protocol: ' + protocol))
|
164 | return
|
165 | }
|
166 |
|
167 | if (state.options['x-forwarded-for'] !== undefined) {
|
168 | connection.sendXForwardedFor(state.options['x-forwarded-for'])
|
169 | }
|
170 |
|
171 | callback(null, connection)
|
172 | })
|
173 | }
|
174 |
|
175 | proto._createSocket = function _createSocket (req, options, callback) {
|
176 | var state = this._spdyState
|
177 | if (state.fallback) { return state.createSocket(req, options) }
|
178 |
|
179 | var handle = spdy.handle.create(null, null, state.socket)
|
180 |
|
181 | var socketOptions = {
|
182 | handle: handle,
|
183 | allowHalfOpen: true
|
184 | }
|
185 |
|
186 | var socket
|
187 | if (state.secure) {
|
188 | socket = new spdy.Socket(state.socket, socketOptions)
|
189 | } else {
|
190 | socket = new net.Socket(socketOptions)
|
191 | }
|
192 |
|
193 | handle.assignSocket(socket)
|
194 | handle.assignClientRequest(req)
|
195 |
|
196 |
|
197 | var self = this
|
198 | handle.once('needStream', function () {
|
199 | if (state.connection === null) {
|
200 | self.once('_connect', function () {
|
201 | handle.setStream(self._createStream(req, handle))
|
202 | })
|
203 | } else {
|
204 | handle.setStream(self._createStream(req, handle))
|
205 | }
|
206 | })
|
207 |
|
208 |
|
209 | req.on('response', function (res) {
|
210 | handle.assignRequest(res)
|
211 | })
|
212 | handle.assignResponse(req)
|
213 |
|
214 |
|
215 | req.addListener('newListener', spdy.request.onNewListener)
|
216 |
|
217 |
|
218 | socket.readable = true
|
219 | socket.writable = true
|
220 |
|
221 | if (callback) {
|
222 | return callback(null, socket)
|
223 | }
|
224 |
|
225 | return socket
|
226 | }
|
227 |
|
228 | if (mode === 'modern' || mode === 'normal') {
|
229 | proto.createSocket = proto._createSocket
|
230 | } else {
|
231 | proto.createSocket = function createSocket (name, host, port, addr, req) {
|
232 | var state = this._spdyState
|
233 | if (state.fallback) {
|
234 | return state.createSocket(name, host, port, addr, req)
|
235 | }
|
236 |
|
237 | return this._createSocket(req, {
|
238 | host: host,
|
239 | port: port
|
240 | })
|
241 | }
|
242 | }
|
243 |
|
244 | proto._createStream = function _createStream (req, handle) {
|
245 | var state = this._spdyState
|
246 |
|
247 | var self = this
|
248 | return state.connection.reserveStream({
|
249 | method: req.method,
|
250 | path: req.path,
|
251 | headers: req._headers,
|
252 | host: state.host
|
253 | }, function (err, stream) {
|
254 | if (err) {
|
255 | return self.emit('error', err)
|
256 | }
|
257 |
|
258 | stream.on('response', function (status, headers) {
|
259 | handle.emitResponse(status, headers)
|
260 | })
|
261 | })
|
262 | }
|
263 |
|
264 |
|
265 |
|
266 | proto.close = function close (callback) {
|
267 | var state = this._spdyState
|
268 |
|
269 | if (state.connection === null) {
|
270 | this.once('_connect', function () {
|
271 | this.close(callback)
|
272 | })
|
273 | return
|
274 | }
|
275 |
|
276 | state.connection.end(callback)
|
277 | }
|
278 |
|
279 | exports.Agent = instantiate(https.Agent)
|
280 | exports.PlainAgent = instantiate(http.Agent)
|
281 |
|
282 | exports.create = function create (base, options) {
|
283 | if (typeof base === 'object') {
|
284 | options = base
|
285 | base = null
|
286 | }
|
287 |
|
288 | if (base) {
|
289 | return instantiate(base).create(options)
|
290 | }
|
291 |
|
292 | if (options.spdy && options.spdy.plain) {
|
293 | return exports.PlainAgent.create(options)
|
294 | } else { return exports.Agent.create(options) }
|
295 | }
|