Scoop API

scoop:scoop.http.client:0.1.0

src/proxy/options.scoop

Selected production source · src/proxy/options.scoop

1package scoop.http.client
3import scoop.collections.Map
4import scoop.asyncio.Socks5Credentials
5import scoop.asyncio.Socks5Resolution
6import scoop.http.HttpCredentials
7import scoop.http.HttpResponseHead
8import scoop.url.Url
9import scoop.url.UrlHost
11/**
12 * The selected route for a request: direct, HTTP proxy, or SOCKS5.
13 */
14public 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)
29/**
30 * A suspendable per-request proxy choice. Environment variables are consulted only through an
31 * explicit environment selector.
32 */
33public class HttpProxySelector public constructor(
34 internal val select: suspend (HttpRequest) -> HttpProxy
35) {
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; HTTPS
46 * 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; HTTPS
53 * 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 }
67/**
68 * An HTTP/HTTPS proxy authority with independent TLS and authentication configuration. URL
69 * userinfo, query, and fragment are rejected.
70 */
71public 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)
92/**
93 * A SOCKS5 authority with explicit optional credentials and local/proxy DNS resolution.
94 */
95public 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.Proxy
108) {
109 internal val address: ClientProxyAddress = clientProxyAddress(url, true)
112/**
113 * An HTTP proxy rejected CONNECT with a non-success response head.
114 */
115public 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: HttpResponseHead
124): Exception(Some("HTTP proxy rejected CONNECT with status " + responseHead.status.toString()))
126internal class ClientProxyAddress(val host: UrlHost, val port: Int, val secure: Boolean)
128private fun clientProxyAddress(url: Url, socks: Boolean): ClientProxyAddress {
129 val scheme = url.scheme
130 if (
131 if (socks)
132 scheme != Some("socks5")
133 else
134 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 None
145 )
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 1080
157 else
158 if (scheme == Some("https"))
159 443
160 else
161 80
162 )
163 if (port == 0)
164 throw IllegalArgumentException(Some("proxy port must be positive"))
165 return ClientProxyAddress(authority.networkHost(), port, scheme == Some("https"))