import { BaseRequestPolicy , RequestPolicy , RequestPolicyFactory , RequestPolicyOptionsLike , } from "./requestPolicy" ; import { HttpOperationResponse } from "../httpOperationResponse" ; import { ProxySettings } from "../serviceClient" ; import { WebResourceLike } from "../webResource" ; import { Constants } from "../util/constants" ; import { URLBuilder } from "../url" ; internal export const noProxyList = loadNoProxy ( ) ; const byPassedList = new Map ( ) ; internal export function getEnvironmentValue ( name ) { if ( process . env [ name ] ) { return process . env [ name ] ; } else if ( process . env [ name . toLowerCase ( ) ] ) { return process . env [ name . toLowerCase ( ) ] ; } return undefined ; } function loadEnvironmentProxyValue ( ) { if ( ! process ) { return undefined ; } const httpsProxy = getEnvironmentValue ( Constants . HTTPS_PROXY ) ; const allProxy = getEnvironmentValue ( Constants . ALL_PROXY ) ; const httpProxy = getEnvironmentValue ( Constants . HTTP_PROXY ) ; return httpsProxy || allProxy || httpProxy ; } function isBypassed ( uri ) { if ( noProxyList . length === 0 ) { return false ; } const host = URLBuilder . parse ( uri ) . getHost ( ) ! ; if ( byPassedList . has ( host ) ) { return byPassedList . get ( host ) ; } let isBypassedFlag = false ; for ( const pattern of noProxyList ) { if ( pattern [ 0 ] === "." ) { if ( host . endsWith ( pattern ) ) { isBypassedFlag = true ; } else { if ( host . length === pattern . length - 1 && host === pattern . slice ( 1 ) ) { isBypassedFlag = true ; } } } else { if ( host === pattern ) { isBypassedFlag = true ; } } } byPassedList . set ( host , isBypassedFlag ) ; return isBypassedFlag ; } internal export function loadNoProxy ( ) { const noProxy = getEnvironmentValue ( Constants . NO_PROXY ) ; if ( noProxy ) { return noProxy . split ( "," ) . map ( ( item ) => item . trim ( ) ) . filter ( ( item ) => item . length ) ; } return [ ] ; } internal function extractAuthFromUrl ( url ) { const atIndex = url . indexOf ( "@" ) ; if ( atIndex === - 1 ) { return { urlWithoutAuth : url } ; } const schemeIndex = url . indexOf ( "://" ) ; const authStart = schemeIndex !== - 1 ? schemeIndex + 3 : 0 ; const auth = url . substring ( authStart , atIndex ) ; const colonIndex = auth . indexOf ( ":" ) ; const hasPassword = colonIndex !== - 1 ; const username = hasPassword ? auth . substring ( 0 , colonIndex ) : auth ; const password = hasPassword ? auth . substring ( colonIndex + 1 ) : undefined ; const urlWithoutAuth = url . substring ( 0 , authStart ) + url . substring ( atIndex + 1 ) ; return { username , password , urlWithoutAuth , } ; } export function getDefaultProxySettings ( proxyUrl ? ) { if ( ! proxyUrl ) { proxyUrl = loadEnvironmentProxyValue ( ) ; if ( ! proxyUrl ) { return undefined ; } } const { username , password , urlWithoutAuth } = extractAuthFromUrl ( proxyUrl ) ; const parsedUrl = URLBuilder . parse ( urlWithoutAuth ) ; const schema = parsedUrl . getScheme ( ) ? parsedUrl . getScheme ( ) + "://" : "" ; return { host : schema + parsedUrl . getHost ( ) , port : Number . parseInt ( parsedUrl . getPort ( ) || "80" ) , username , password , } ; } export function proxyPolicy ( proxySettings ? ) { if ( ! proxySettings ) { proxySettings = getDefaultProxySettings ( ) ; } return { create : ( nextPolicy , options ) => { return new ProxyPolicy ( nextPolicy , options , proxySettings ! ) ; } , } ; } export class ProxyPolicy extends BaseRequestPolicy { proxySettings : ProxySettings ; constructor ( nextPolicy , options , proxySettings ) { super ( nextPolicy , options ) ; this . proxySettings = proxySettings ; } public sendRequest ( request ) { if ( ! request . proxySettings && ! isBypassed ( request . url ) ) { request . proxySettings = this . proxySettings ; } return this . _nextPolicy . sendRequest ( request ) ; } }