1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 | 'use strict'
|
24 |
|
25 | const http = require('http')
|
26 | const https = require('https')
|
27 | const url = require('url')
|
28 |
|
29 | const httpLib = require('../lib/http')
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 | var RequestOptions
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 | function getRequestOptions(aUrl) {
|
48 |
|
49 | let options = url.parse(aUrl)
|
50 | if (!options.hostname) {
|
51 | throw new Error('Invalid URL: ' + aUrl)
|
52 | }
|
53 |
|
54 | options.search = null
|
55 | options.hash = null
|
56 | options.path = options.pathname
|
57 | return options
|
58 | }
|
59 |
|
60 |
|
61 | const USER_AGENT = (function () {
|
62 | const version = require('../package.json').version
|
63 | const platform =
|
64 | { darwin: 'mac', win32: 'windows' }[process.platform] || 'linux'
|
65 | return `selenium/${version} (js ${platform})`
|
66 | })()
|
67 |
|
68 |
|
69 |
|
70 |
|
71 |
|
72 |
|
73 | class HttpClient {
|
74 | |
75 |
|
76 |
|
77 |
|
78 |
|
79 |
|
80 |
|
81 |
|
82 | constructor(serverUrl, opt_agent, opt_proxy, client_options = {}) {
|
83 |
|
84 | this.agent_ = opt_agent || null
|
85 |
|
86 | |
87 |
|
88 |
|
89 |
|
90 | this.options_ = getRequestOptions(serverUrl)
|
91 |
|
92 | |
93 |
|
94 |
|
95 | this.client_options = client_options
|
96 |
|
97 | |
98 |
|
99 |
|
100 |
|
101 | this.keepAlive = this.client_options['keep-alive']
|
102 |
|
103 | |
104 |
|
105 |
|
106 | this.proxyOptions_ = opt_proxy ? getRequestOptions(opt_proxy) : null
|
107 | }
|
108 |
|
109 | get keepAlive() {
|
110 | return this.agent_.keepAlive
|
111 | }
|
112 |
|
113 | set keepAlive(value) {
|
114 | if (value === 'true' || value === true) {
|
115 | this.agent_.keepAlive = true
|
116 | }
|
117 | }
|
118 |
|
119 |
|
120 | send(httpRequest) {
|
121 | let data
|
122 |
|
123 | let headers = {}
|
124 |
|
125 | if (httpRequest.headers) {
|
126 | httpRequest.headers.forEach(function (value, name) {
|
127 | headers[name] = value
|
128 | })
|
129 | }
|
130 |
|
131 | headers['User-Agent'] = this.client_options['user-agent'] || USER_AGENT
|
132 | headers['Content-Length'] = 0
|
133 | if (httpRequest.method == 'POST' || httpRequest.method == 'PUT') {
|
134 | data = JSON.stringify(httpRequest.data)
|
135 | headers['Content-Length'] = Buffer.byteLength(data, 'utf8')
|
136 | headers['Content-Type'] = 'application/json;charset=UTF-8'
|
137 | }
|
138 |
|
139 | let path = this.options_.path
|
140 | if (path.endsWith('/') && httpRequest.path.startsWith('/')) {
|
141 | path += httpRequest.path.substring(1)
|
142 | } else {
|
143 | path += httpRequest.path
|
144 | }
|
145 |
|
146 | let parsedPath = url.parse(path)
|
147 |
|
148 | let options = {
|
149 | agent: this.agent_ || null,
|
150 | method: httpRequest.method,
|
151 |
|
152 | auth: this.options_.auth,
|
153 | hostname: this.options_.hostname,
|
154 | port: this.options_.port,
|
155 | protocol: this.options_.protocol,
|
156 |
|
157 | path: parsedPath.path,
|
158 | pathname: parsedPath.pathname,
|
159 | search: parsedPath.search,
|
160 | hash: parsedPath.hash,
|
161 |
|
162 | headers,
|
163 | }
|
164 |
|
165 | return new Promise((fulfill, reject) => {
|
166 | sendRequest(options, fulfill, reject, data, this.proxyOptions_)
|
167 | })
|
168 | }
|
169 | }
|
170 |
|
171 |
|
172 |
|
173 |
|
174 |
|
175 |
|
176 |
|
177 |
|
178 |
|
179 |
|
180 |
|
181 | function sendRequest(options, onOk, onError, opt_data, opt_proxy, opt_retries) {
|
182 | var hostname = options.hostname
|
183 | var port = options.port
|
184 |
|
185 | if (opt_proxy) {
|
186 | let proxy = (opt_proxy)
|
187 |
|
188 |
|
189 |
|
190 |
|
191 | let absoluteUri = url.format(options)
|
192 |
|
193 |
|
194 |
|
195 |
|
196 |
|
197 | let targetHost = options.hostname
|
198 | if (options.port) {
|
199 | targetHost += ':' + options.port
|
200 | }
|
201 |
|
202 |
|
203 | options.headers['Host'] = targetHost
|
204 | options.path = absoluteUri
|
205 | options.host = proxy.host
|
206 | options.hostname = proxy.hostname
|
207 | options.port = proxy.port
|
208 |
|
209 |
|
210 |
|
211 | options.protocol = opt_proxy.protocol
|
212 |
|
213 | if (proxy.auth) {
|
214 | options.headers['Proxy-Authorization'] =
|
215 | 'Basic ' + Buffer.from(proxy.auth).toString('base64')
|
216 | }
|
217 | }
|
218 |
|
219 | let requestFn = options.protocol === 'https:' ? https.request : http.request
|
220 | var request = requestFn(options, function onResponse(response) {
|
221 | if (response.statusCode == 302 || response.statusCode == 303) {
|
222 | try {
|
223 |
|
224 | var location = url.parse(response.headers['location'])
|
225 | } catch (ex) {
|
226 | onError(
|
227 | Error(
|
228 | 'Failed to parse "Location" header for server redirect: ' +
|
229 | ex.message +
|
230 | '\nResponse was: \n' +
|
231 | new httpLib.Response(response.statusCode, response.headers, '')
|
232 | )
|
233 | )
|
234 | return
|
235 | }
|
236 |
|
237 | if (!location.hostname) {
|
238 | location.hostname = hostname
|
239 | location.port = port
|
240 | location.auth = options.auth
|
241 | }
|
242 |
|
243 | request.abort()
|
244 | sendRequest(
|
245 | {
|
246 | method: 'GET',
|
247 | protocol: location.protocol || options.protocol,
|
248 | hostname: location.hostname,
|
249 | port: location.port,
|
250 | path: location.path,
|
251 | auth: location.auth,
|
252 | pathname: location.pathname,
|
253 | search: location.search,
|
254 | hash: location.hash,
|
255 | headers: {
|
256 | Accept: 'application/json; charset=utf-8',
|
257 | 'User-Agent': options.headers['User-Agent'] || USER_AGENT,
|
258 | },
|
259 | },
|
260 | onOk,
|
261 | onError,
|
262 | undefined,
|
263 | opt_proxy
|
264 | )
|
265 | return
|
266 | }
|
267 |
|
268 | var body = []
|
269 | response.on('data', body.push.bind(body))
|
270 | response.on('end', function () {
|
271 | var resp = new httpLib.Response(
|
272 | (response.statusCode),
|
273 | (response.headers),
|
274 | Buffer.concat(body).toString('utf8').replace(/\0/g, '')
|
275 | )
|
276 | onOk(resp)
|
277 | })
|
278 | })
|
279 |
|
280 | request.on('error', function (e) {
|
281 | if (typeof opt_retries === 'undefined') {
|
282 | opt_retries = 0
|
283 | }
|
284 |
|
285 | if (shouldRetryRequest(opt_retries, e)) {
|
286 | opt_retries += 1
|
287 | setTimeout(function () {
|
288 | sendRequest(options, onOk, onError, opt_data, opt_proxy, opt_retries)
|
289 | }, 15)
|
290 | } else {
|
291 | var message = e.message
|
292 | if (e.code) {
|
293 | message = e.code + ' ' + message
|
294 | }
|
295 | onError(new Error(message))
|
296 | }
|
297 | })
|
298 |
|
299 | if (opt_data) {
|
300 | request.write(opt_data)
|
301 | }
|
302 |
|
303 | request.end()
|
304 | }
|
305 |
|
306 | const MAX_RETRIES = 3
|
307 |
|
308 |
|
309 |
|
310 |
|
311 |
|
312 |
|
313 |
|
314 |
|
315 |
|
316 |
|
317 | function shouldRetryRequest(retries, err) {
|
318 | return retries < MAX_RETRIES && isRetryableNetworkError(err)
|
319 | }
|
320 |
|
321 |
|
322 |
|
323 |
|
324 |
|
325 | function isRetryableNetworkError(err) {
|
326 | if (err && err.code) {
|
327 | return (
|
328 | err.code === 'ECONNABORTED' ||
|
329 | err.code === 'ECONNRESET' ||
|
330 | err.code === 'ECONNREFUSED' ||
|
331 | err.code === 'EADDRINUSE' ||
|
332 | err.code === 'EPIPE' ||
|
333 | err.code === 'ETIMEDOUT'
|
334 | )
|
335 | }
|
336 |
|
337 | return false
|
338 | }
|
339 |
|
340 |
|
341 |
|
342 | exports.Agent = http.Agent
|
343 | exports.Executor = httpLib.Executor
|
344 | exports.HttpClient = HttpClient
|
345 | exports.Request = httpLib.Request
|
346 | exports.Response = httpLib.Response
|