scoop:scoop.http.client:0.1.0
src/proxy/options.scoop
Selected production source · src/proxy/options.scoop
1
package scoop.http.client3
import scoop.collections.Map4
import scoop.asyncio.Socks5Credentials5
import scoop.asyncio.Socks5Resolution6
import scoop.http.HttpCredentials7
import scoop.http.HttpResponseHead8
import scoop.url.Url9
import scoop.url.UrlHost11
/**12
* The selected route for a request: direct, HTTP proxy, or SOCKS5.13
*/14
public enum HttpProxy {15
/**16
* Connects directly to the origin.17
*/18
Direct,19
/**20
* Uses the supplied HTTP or HTTPS proxy endpoint.21
*/22
Http(HttpProxyEndpoint),23
/**24
* Uses the supplied SOCKS5 endpoint and resolution policy.25
*/26
Socks5(HttpSocks5Endpoint)27
}29
/**30
* A suspendable per-request proxy choice. Environment variables are consulted only through an31
* explicit environment selector.32
*/33
public class HttpProxySelector public constructor(34
internal val select: suspend (HttpRequest) -> HttpProxy35
) {36
/**37
* Explicit direct, fixed, and environment-derived selectors.38
*/39
public companion object {40
/**41
* Creates a selector that always connects directly.42
*/43
public fun direct(): HttpProxySelector = fixed(HttpProxy.Direct)44
/**45
* Snapshots the supplied or current environment proxy variables and bypass rules; HTTPS46
* proxy TLS uses the supplied configuration.47
*/48
public fun fromEnvironment(49
tls: HttpClientTlsConfig = httpClientTlsConfig()50
): HttpProxySelector = clientEnvironmentProxy(currentClientProxyEnvironment(), tls)51
/**52
* Snapshots the supplied or current environment proxy variables and bypass rules; HTTPS53
* proxy TLS uses the supplied configuration.54
*/55
public fun fromEnvironment(56
variables: Map<String, String>,57
tls: HttpClientTlsConfig = httpClientTlsConfig()58
): HttpProxySelector = clientEnvironmentProxy(variables, tls)59
/**60
* Creates a selector that always returns the supplied route.61
*/62
public fun fixed(proxy: HttpProxy): HttpProxySelector =63
HttpProxySelector(suspend { _ -> proxy })64
}65
}67
/**68
* An HTTP/HTTPS proxy authority with independent TLS and authentication configuration. URL69
* userinfo, query, and fragment are rejected.70
*/71
public class HttpProxyEndpoint public constructor(72
/**73
* The validated http or https proxy authority.74
*/75
public val url: Url,76
/**77
* TLS settings for an HTTPS proxy connection, independent of origin TLS.78
*/79
public val tls: HttpClientTlsConfig = httpClientTlsConfig(),80
/**81
* Optional preconstructed proxy credentials.82
*/83
public val authorization: HttpCredentials? = None,84
/**85
* Proxy authentication providers and retry policy, independent of origin authentication.86
*/87
public val authentication: HttpAuthenticationOptions = httpAuthenticationOptions()88
) {89
internal val address: ClientProxyAddress = clientProxyAddress(url, false)90
}92
/**93
* A SOCKS5 authority with explicit optional credentials and local/proxy DNS resolution.94
*/95
public class HttpSocks5Endpoint public constructor(96
/**97
* The validated socks5 proxy authority.98
*/99
public val url: Url,100
/**101
* Optional SOCKS5 username/password credentials.102
*/103
public val credentials: Socks5Credentials? = None,104
/**105
* Whether target DNS is resolved locally or by the proxy.106
*/107
public val resolution: Socks5Resolution = Socks5Resolution.Proxy108
) {109
internal val address: ClientProxyAddress = clientProxyAddress(url, true)110
}112
/**113
* An HTTP proxy rejected CONNECT with a non-success response head.114
*/115
public class HttpProxyConnectException internal constructor(116
/**117
* The proxy endpoint that rejected CONNECT.118
*/119
public val proxyUrl: Url,120
/**121
* The proxy's rejection response head.122
*/123
public val responseHead: HttpResponseHead124
): Exception(Some("HTTP proxy rejected CONNECT with status " + responseHead.status.toString()))126
internal class ClientProxyAddress(val host: UrlHost, val port: Int, val secure: Boolean)128
private fun clientProxyAddress(url: Url, socks: Boolean): ClientProxyAddress {129
val scheme = url.scheme130
if (131
if (socks)132
scheme != Some("socks5")133
else134
scheme != Some("http") && scheme != Some("https")135
)136
throw IllegalArgumentException(Some("proxy URL has an invalid scheme"))137
if (138
url.authority ==139
None ||140
(url.encodedPath != "" && url.encodedPath != "/") ||141
url.encodedQuery !=142
None ||143
url.encodedFragment !=144
None145
)146
throw IllegalArgumentException(147
Some("proxy URL must contain only an authority and optional root path")148
)149
val authority = url.authority!!150
if (authority.encodedUserInfo != None)151
throw IllegalArgumentException(Some("proxy URL must not contain userinfo"))152
val port =153
authority.port ?:154
(155
if (socks)156
1080157
else158
if (scheme == Some("https"))159
443160
else161
80162
)163
if (port == 0)164
throw IllegalArgumentException(Some("proxy port must be positive"))165
return ClientProxyAddress(authority.networkHost(), port, scheme == Some("https"))166
}