private static _getUrlFromRequestOptions()

in AutoCollection/HttpDependencyParser.ts [114:194]


    private static _getUrlFromRequestOptions(options: any, request: http.ClientRequest) {
        if (typeof options === 'string') {
            if (options.indexOf("http://") === 0 || options.indexOf("https://") === 0) {
                // protocol exists, parse normally
                try {
                    options = new url.URL(options);
                }
                catch (ex) { }
            } else {
                // protocol not found, insert http/https where appropriate
                try {
                    const parsed = new url.URL("http://" + options);
                    if (parsed.port === "443") {
                        options = new url.URL("https://" + options);
                    } else {
                        options = new url.URL("http://" + options);
                    }
                }
                catch (ex) { }
            }
        } else if (options && typeof url.URL === 'function' && options instanceof url.URL) {
            return url.format(options);
        } else {
            // Avoid modifying the original options object.
            let originalOptions = options;
            options = {};
            if (originalOptions) {
                Object.keys(originalOptions).forEach(key => {
                    options[key] = originalOptions[key];
                });
            }
        }

        // Oddly, url.format ignores path and only uses pathname and search,
        // so create them from the path, if path was specified
        if (options.path && options.host) {
            // need to force a protocol to make parameter valid - base url is required when input is a relative url
            try {
                const parsedQuery = new url.URL(options.path, 'http://' + options.host + options.path);
                options.pathname = parsedQuery.pathname;
                options.search = parsedQuery.search;
            }
            catch (ex) { }
        }

        // Sometimes the hostname is provided but not the host
        // Add in the path when this occurs
        if (options.path && options.hostname && !options.host) {
            // need to force a protocol to make parameter valid - base url is required when input is a relative url
            try {
                const parsedQuery = new url.URL(options.path, 'http://' + options.hostname + options.path);
                options.pathname = parsedQuery.pathname;
                options.search = parsedQuery.search;
            }
            catch (ex) { }
        }

        // Similarly, url.format ignores hostname and port if host is specified,
        // even if host doesn't have the port, but http.request does not work
        // this way. It will use the port if one is not specified in host,
        // effectively treating host as hostname, but will use the port specified
        // in host if it exists.
        if (options.host && options.port) {
            // Force a protocol so it will parse the host as the host, not path.
            // It is discarded and not used, so it doesn't matter if it doesn't match
            try {
                const parsedHost = new url.URL(`http://${options.host}`);
                if (!parsedHost.port && options.port) {
                    options.hostname = options.host;
                    delete options.host;
                }
            }
            catch (ex) { }
        }

        // Mix in default values used by http.request and others
        options.protocol = options.protocol || ((<any>request).agent && (<any>request).agent.protocol) || ((<any>request).protocol) || undefined;
        options.hostname = options.hostname || 'localhost';

        return url.format(options);
    }