* Sends a single HTTP request. * @param {!Object} options The request options. * @param {function(!httpLib.Response)} onOk The function to call if the * request succeeds. * @param {function(!Error)} onError The function to call if the request fails. * @param {?string=} opt_data The data to
(options, onOk, onError, opt_data, opt_proxy, opt_retries)
| 177 | * @param {number=} opt_retries The current number of retries. |
| 178 | */ |
| 179 | function sendRequest(options, onOk, onError, opt_data, opt_proxy, opt_retries) { |
| 180 | var hostname = options.hostname |
| 181 | var port = options.port |
| 182 | |
| 183 | if (opt_proxy) { |
| 184 | let proxy = /** @type {RequestOptions} */ (opt_proxy) |
| 185 | |
| 186 | // RFC 2616, section 5.1.2: |
| 187 | // The absoluteURI form is REQUIRED when the request is being made to a |
| 188 | // proxy. |
| 189 | let absoluteUri = url.format(options) |
| 190 | |
| 191 | // RFC 2616, section 14.23: |
| 192 | // An HTTP/1.1 proxy MUST ensure that any request message it forwards does |
| 193 | // contain an appropriate Host header field that identifies the service |
| 194 | // being requested by the proxy. |
| 195 | let targetHost = options.hostname |
| 196 | if (options.port) { |
| 197 | targetHost += ':' + options.port |
| 198 | } |
| 199 | |
| 200 | // Update the request options with our proxy info. |
| 201 | options.headers['Host'] = targetHost |
| 202 | options.path = absoluteUri |
| 203 | options.host = proxy.host |
| 204 | options.hostname = proxy.hostname |
| 205 | options.port = proxy.port |
| 206 | |
| 207 | // Update the protocol to avoid EPROTO errors when the webdriver proxy |
| 208 | // uses a different protocol from the remote selenium server. |
| 209 | options.protocol = opt_proxy.protocol |
| 210 | |
| 211 | if (proxy.auth) { |
| 212 | options.headers['Proxy-Authorization'] = 'Basic ' + Buffer.from(proxy.auth).toString('base64') |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | let requestFn = options.protocol === 'https:' ? https.request : http.request |
| 217 | var request = requestFn(options, function onResponse(response) { |
| 218 | if (response.statusCode == 302 || response.statusCode == 303) { |
| 219 | let location |
| 220 | try { |
| 221 | // eslint-disable-next-line n/no-deprecated-api |
| 222 | location = url.parse(response.headers['location']) |
| 223 | } catch (ex) { |
| 224 | onError( |
| 225 | Error( |
| 226 | 'Failed to parse "Location" header for server redirect: ' + |
| 227 | ex.message + |
| 228 | '\nResponse was: \n' + |
| 229 | new httpLib.Response(response.statusCode, response.headers, ''), |
| 230 | ), |
| 231 | ) |
| 232 | return |
| 233 | } |
| 234 | |
| 235 | if (!location.hostname) { |
| 236 | location.hostname = hostname |
no test coverage detected