scoop:scoop.http.client:0.1.0
src/proxy/environment.scoop
Selected production source · src/proxy/environment.scoop
1
package scoop.http.client3
import scoop.asyncio.Socks5Credentials4
import scoop.asyncio.Socks5Resolution5
import scoop.collections.*6
import scoop.http.HttpCredentials7
import scoop.platform.env.environment8
import scoop.platform.system.OsString9
import scoop.text.*10
import scoop.url.*12
internal 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
lower23
else24
if (cgi && name == "http_proxy")25
None26
else27
values[OsString.fromString(name.uppercaseAscii())]28
if (clientPresent(value))29
selected.put(name, value!!.decodeUtf8())30
}31
return selected.toMap()32
}34
internal fun clientEnvironmentProxy(35
variables: Map<String, String>,36
tls: HttpClientTlsConfig37
): 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.origin49
var direct = false50
for (rule in bypass)51
if (rule.matches(origin))52
direct = true53
if (direct)54
HttpProxy.Direct55
else56
(57
if (origin.scheme == "https")58
https59
else60
http61
) ?:62
fallback ?:63
HttpProxy.Direct64
}65
)66
}68
private fun clientProxyVariable(69
values: Map<String, String>,70
name: String,71
ignoreUppercase: Boolean72
): String? {73
val lower = values[name]74
if (clientPresent(lower))75
return lower76
return if (ignoreUppercase)77
None78
else79
values[name.uppercaseAscii()]80
}82
private fun parseClientEnvironmentProxy(value: String?, tls: HttpClientTlsConfig): HttpProxy? {83
val text = value ?: return None84
if (text.charLength == 0L)85
return None86
val url =87
Url.parse(88
if (text.contains("://"))89
text90
else91
"http://" + text92
)93
val authority =94
url.authority ?: throw IllegalArgumentException(Some("proxy address requires an authority"))95
val clean =96
url.copy {97
encodedUserInfo = None98
if (scheme == Some("socks5h"))99
scheme = Some("socks5")100
}101
val userInfo = authority.encodedUserInfo102
var username = ""103
var password = ""104
if (clientPresent(userInfo)) {105
val separator = userInfo!!.indexOf(':')106
username =107
if (separator < 0L)108
userInfo!!109
else110
userInfo!!.slice(0L, separator)111
password =112
if (separator < 0L)113
""114
else115
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.Proxy121
else122
Socks5Resolution.Local123
val credentials =124
if (clientPresent(userInfo))125
Some(126
Socks5Credentials(127
PercentEncoding.decode(username),128
PercentEncoding.decode(password)129
)130
)131
else132
None133
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
true144
)145
)146
else147
None148
return Some(HttpProxy.Http(HttpProxyEndpoint(clean, tls, credentials)))149
}