Scoop API

scoop:scoop.http.client:0.1.0

src/proxy/environment.scoop

Selected production source · src/proxy/environment.scoop

1package scoop.http.client
3import scoop.asyncio.Socks5Credentials
4import scoop.asyncio.Socks5Resolution
5import scoop.collections.*
6import scoop.http.HttpCredentials
7import scoop.platform.env.environment
8import scoop.platform.system.OsString
9import scoop.text.*
10import scoop.url.*
12internal fun currentClientProxyEnvironment(): Map<String, String> {
13 val values = environment()
14 val selected = LinkedHashMap<String, String>()
15 val cgi = values.containsKey(OsString.fromString("REQUEST_METHOD"))
16 if (cgi)
17 selected.put("REQUEST_METHOD", "")
18 for (name in ["http_proxy", "https_proxy", "all_proxy", "no_proxy"]) {
19 val lower = values[OsString.fromString(name)]
20 val value: OsString? =
21 if (clientPresent(lower))
22 lower
23 else
24 if (cgi && name == "http_proxy")
25 None
26 else
27 values[OsString.fromString(name.uppercaseAscii())]
28 if (clientPresent(value))
29 selected.put(name, value!!.decodeUtf8())
30 }
31 return selected.toMap()
34internal fun clientEnvironmentProxy(
35 variables: Map<String, String>,
36 tls: HttpClientTlsConfig
37): HttpProxySelector {
38 val cgi = variables.containsKey("REQUEST_METHOD")
39 val http = parseClientEnvironmentProxy(clientProxyVariable(variables, "http_proxy", cgi), tls)
40 val https =
41 parseClientEnvironmentProxy(clientProxyVariable(variables, "https_proxy", false), tls)
42 val fallback =
43 parseClientEnvironmentProxy(clientProxyVariable(variables, "all_proxy", false), tls)
44 val bypass = parseClientProxyBypass(clientProxyVariable(variables, "no_proxy", false) ?: "")
45 return HttpProxySelector(
46 suspend {
47 request ->
48 val origin = request.origin
49 var direct = false
50 for (rule in bypass)
51 if (rule.matches(origin))
52 direct = true
53 if (direct)
54 HttpProxy.Direct
55 else
56 (
57 if (origin.scheme == "https")
58 https
59 else
60 http
61 ) ?:
62 fallback ?:
63 HttpProxy.Direct
64 }
65 )
68private fun clientProxyVariable(
69 values: Map<String, String>,
70 name: String,
71 ignoreUppercase: Boolean
72): String? {
73 val lower = values[name]
74 if (clientPresent(lower))
75 return lower
76 return if (ignoreUppercase)
77 None
78 else
79 values[name.uppercaseAscii()]
82private fun parseClientEnvironmentProxy(value: String?, tls: HttpClientTlsConfig): HttpProxy? {
83 val text = value ?: return None
84 if (text.charLength == 0L)
85 return None
86 val url =
87 Url.parse(
88 if (text.contains("://"))
89 text
90 else
91 "http://" + text
92 )
93 val authority =
94 url.authority ?: throw IllegalArgumentException(Some("proxy address requires an authority"))
95 val clean =
96 url.copy {
97 encodedUserInfo = None
98 if (scheme == Some("socks5h"))
99 scheme = Some("socks5")
100 }
101 val userInfo = authority.encodedUserInfo
102 var username = ""
103 var password = ""
104 if (clientPresent(userInfo)) {
105 val separator = userInfo!!.indexOf(':')
106 username =
107 if (separator < 0L)
108 userInfo!!
109 else
110 userInfo!!.slice(0L, separator)
111 password =
112 if (separator < 0L)
113 ""
114 else
115 userInfo!!.slice(separator + 1L, userInfo!!.charLength)
116 }
117 if (url.scheme == Some("socks5") || url.scheme == Some("socks5h")) {
118 val resolution =
119 if (url.scheme == Some("socks5h"))
120 Socks5Resolution.Proxy
121 else
122 Socks5Resolution.Local
123 val credentials =
124 if (clientPresent(userInfo))
125 Some(
126 Socks5Credentials(
127 PercentEncoding.decode(username),
128 PercentEncoding.decode(password)
129 )
130 )
131 else
132 None
133 return Some(HttpProxy.Socks5(HttpSocks5Endpoint(clean, credentials, resolution)))
134 }
135 val credentials: HttpCredentials? =
136 if (clientPresent(userInfo))
137 Some(
138 clientBasicCredentials(
139 HttpPasswordCredentials(
140 PercentEncoding.decodeUtf8(username),
141 PercentEncoding.decodeUtf8(password)
142 ),
143 true
144 )
145 )
146 else
147 None
148 return Some(HttpProxy.Http(HttpProxyEndpoint(clean, tls, credentials)))