|
| 1 | +/* eslint-disable complexity */ |
| 2 | + |
| 3 | +/** |
| 4 | + * Vendored in from https://github.com/open-telemetry/opentelemetry-js/commit/87bd98edd24c98a5fbb9a56fed4b673b7f17a724 |
| 5 | + */ |
| 6 | + |
| 7 | +/* |
| 8 | + * Copyright The OpenTelemetry Authors |
| 9 | + * |
| 10 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 11 | + * you may not use this file except in compliance with the License. |
| 12 | + * You may obtain a copy of the License at |
| 13 | + * |
| 14 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 15 | + * |
| 16 | + * Unless required by applicable law or agreed to in writing, software |
| 17 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 18 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 19 | + * See the License for the specific language governing permissions and |
| 20 | + * limitations under the License. |
| 21 | + */ |
| 22 | +import type { RequestOptions } from 'node:http'; |
| 23 | +import * as url from 'url'; |
| 24 | +import type { DiagLogger } from '@opentelemetry/api'; |
| 25 | + |
| 26 | +/** |
| 27 | + * Makes sure options is an url object |
| 28 | + * return an object with default value and parsed options |
| 29 | + * @param logger component logger |
| 30 | + * @param options original options for the request |
| 31 | + * @param [extraOptions] additional options for the request |
| 32 | + */ |
| 33 | +export const getRequestInfo = ( |
| 34 | + logger: DiagLogger, |
| 35 | + options: url.URL | RequestOptions | string, |
| 36 | + extraOptions?: RequestOptions, |
| 37 | +): { |
| 38 | + origin: string; |
| 39 | + pathname: string; |
| 40 | + method: string; |
| 41 | + invalidUrl: boolean; |
| 42 | + optionsParsed: RequestOptions; |
| 43 | +} => { |
| 44 | + let pathname: string; |
| 45 | + let origin: string; |
| 46 | + let optionsParsed: RequestOptions; |
| 47 | + let invalidUrl = false; |
| 48 | + if (typeof options === 'string') { |
| 49 | + try { |
| 50 | + const convertedOptions = stringUrlToHttpOptions(options); |
| 51 | + optionsParsed = convertedOptions; |
| 52 | + pathname = convertedOptions.pathname || '/'; |
| 53 | + } catch (e) { |
| 54 | + invalidUrl = true; |
| 55 | + logger.verbose( |
| 56 | + 'Unable to parse URL provided to HTTP request, using fallback to determine path. Original error:', |
| 57 | + e, |
| 58 | + ); |
| 59 | + // for backward compatibility with how url.parse() behaved. |
| 60 | + optionsParsed = { |
| 61 | + path: options, |
| 62 | + }; |
| 63 | + pathname = optionsParsed.path || '/'; |
| 64 | + } |
| 65 | + |
| 66 | + origin = `${optionsParsed.protocol || 'http:'}//${optionsParsed.host}`; |
| 67 | + if (extraOptions !== undefined) { |
| 68 | + Object.assign(optionsParsed, extraOptions); |
| 69 | + } |
| 70 | + } else if (options instanceof url.URL) { |
| 71 | + optionsParsed = { |
| 72 | + protocol: options.protocol, |
| 73 | + hostname: |
| 74 | + typeof options.hostname === 'string' && options.hostname.startsWith('[') |
| 75 | + ? options.hostname.slice(1, -1) |
| 76 | + : options.hostname, |
| 77 | + path: `${options.pathname || ''}${options.search || ''}`, |
| 78 | + }; |
| 79 | + if (options.port !== '') { |
| 80 | + optionsParsed.port = Number(options.port); |
| 81 | + } |
| 82 | + if (options.username || options.password) { |
| 83 | + optionsParsed.auth = `${options.username}:${options.password}`; |
| 84 | + } |
| 85 | + pathname = options.pathname; |
| 86 | + origin = options.origin; |
| 87 | + if (extraOptions !== undefined) { |
| 88 | + Object.assign(optionsParsed, extraOptions); |
| 89 | + } |
| 90 | + } else { |
| 91 | + optionsParsed = Object.assign({ protocol: options.host ? 'http:' : undefined }, options); |
| 92 | + |
| 93 | + const hostname = |
| 94 | + optionsParsed.host || |
| 95 | + (optionsParsed.port != null ? `${optionsParsed.hostname}${optionsParsed.port}` : optionsParsed.hostname); |
| 96 | + origin = `${optionsParsed.protocol || 'http:'}//${hostname}`; |
| 97 | + |
| 98 | + pathname = (options as url.URL).pathname; |
| 99 | + if (!pathname && optionsParsed.path) { |
| 100 | + try { |
| 101 | + const parsedUrl = new URL(optionsParsed.path, origin); |
| 102 | + pathname = parsedUrl.pathname || '/'; |
| 103 | + } catch (e) { |
| 104 | + pathname = '/'; |
| 105 | + } |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + // some packages return method in lowercase.. |
| 110 | + // ensure upperCase for consistency |
| 111 | + const method = optionsParsed.method ? optionsParsed.method.toUpperCase() : 'GET'; |
| 112 | + |
| 113 | + return { origin, pathname, method, optionsParsed, invalidUrl }; |
| 114 | +}; |
| 115 | + |
| 116 | +/** |
| 117 | + * Mimics Node.js conversion of URL strings to RequestOptions expected by |
| 118 | + * `http.request` and `https.request` APIs. |
| 119 | + * |
| 120 | + * See https://github.com/nodejs/node/blob/2505e217bba05fc581b572c685c5cf280a16c5a3/lib/internal/url.js#L1415-L1437 |
| 121 | + * |
| 122 | + * @param stringUrl |
| 123 | + * @throws TypeError if the URL is not valid. |
| 124 | + */ |
| 125 | +function stringUrlToHttpOptions(stringUrl: string): RequestOptions & { pathname: string } { |
| 126 | + // This is heavily inspired by Node.js handling of the same situation, trying |
| 127 | + // to follow it as closely as possible while keeping in mind that we only |
| 128 | + // deal with string URLs, not URL objects. |
| 129 | + const { hostname, pathname, port, username, password, search, protocol, hash, href, origin, host } = new URL( |
| 130 | + stringUrl, |
| 131 | + ); |
| 132 | + |
| 133 | + const options: RequestOptions & { |
| 134 | + pathname: string; |
| 135 | + hash: string; |
| 136 | + search: string; |
| 137 | + href: string; |
| 138 | + origin: string; |
| 139 | + } = { |
| 140 | + protocol: protocol, |
| 141 | + hostname: hostname && hostname[0] === '[' ? hostname.slice(1, -1) : hostname, |
| 142 | + hash: hash, |
| 143 | + search: search, |
| 144 | + pathname: pathname, |
| 145 | + path: `${pathname || ''}${search || ''}`, |
| 146 | + href: href, |
| 147 | + origin: origin, |
| 148 | + host: host, |
| 149 | + }; |
| 150 | + if (port !== '') { |
| 151 | + options.port = Number(port); |
| 152 | + } |
| 153 | + if (username || password) { |
| 154 | + options.auth = `${decodeURIComponent(username)}:${decodeURIComponent(password)}`; |
| 155 | + } |
| 156 | + return options; |
| 157 | +} |
0 commit comments