pub enum Proxy {
None,
Environment,
Variables(Vec<(String, String)>),
Url(String),
}Expand description
The proxy through which a Fetcher reaches each origin.
Fetcher::new resolves each form once, and refuses a proxy URL that the
fetcher cannot connect through. A request does not read the environment
again.
The Debug output does not show the userinfo of a proxy URL. So the log
of a struct that holds proxy credentials does not show them.
§Variables
http_proxy serves http origins, and https_proxy serves https
origins. If the variable of the scheme is unset, all_proxy serves the
origin. The fetcher also reads each name in upper case, except HTTP_PROXY.
A CGI gateway gives a request header Proxy to the program as HTTP_PROXY,
so a client can set that variable with a request.
The lower-case name has priority over the upper-case name. An empty value counts as unset.
§White space
If the value of http_proxy, https_proxy, or all_proxy, or of an
upper-case name, has white space at its start or end, Fetcher::new
fails with Error::Unsupported. A value of white space alone also fails.
It is not empty, so it counts as set and hides the upper-case name.
The fetcher checks the value of each variable that the lookup reads, when it
builds the fetcher. It checks a variable also if no request goes through
that proxy. The message names the variable and holds no part of the value,
which can hold a password. The fetcher trims the URL of Proxy::Url and
each no_proxy entry.
§Proxy URL
A proxy URL has the form http://host[:port], socks5://host[:port], or
socks5h://host[:port]. The port is 80 by default for http:// and 1080
for socks5:// and socks5h://. The URL has no path other than /, no
query, and no fragment. For an http:// proxy, the fetcher percent-decodes
the userinfo and sends it to the proxy as Proxy-Authorization: Basic.
SOCKS proxies states the rules of a SOCKS5 proxy.
Any other form fails Fetcher::new with Error::Unsupported, for
example an https:// or a socks4:// proxy. This applies to a URL from the
options and to a URL from the environment. The message names the value
without its userinfo.
§Exemptions
no_proxy is a comma-separated list of the origins that the two
environment forms exempt. An entry holds a host text, with one leading .
if it is written with one, and :port if it names a port. The fetcher
ignores the space around an entry. An entry that names no host exempts
nothing, for example an empty entry, ., or :8080.
If the port text of an entry is not a number from 0 to 65535, the whole entry is one host text. No origin holds that text, so the entry exempts nothing.
* as a whole entry exempts all hosts. It is the one wildcard that the list
reads. *.example.com is a host text, and no origin holds it.
An entry matches an origin if it equals the host, or if the host ends with a
. and the entry. The fetcher removes one leading . from the entry first,
so .example.com and example.com both exempt a.example.com. A second
leading . is part of the host text of the entry. An entry with :port
matches that port alone.
The match compares the host text of the URL, with ASCII case ignored. It
never compares the address of the host. So localhost exempts no origin
written as 127.0.0.1, and an entry that is an IP literal matches that text
alone.
An entry in CIDR notation names no network. The fetcher reads it as a host
text, which no origin holds. This is a divergence from curl 7.86 and later,
which read such an entry as a network. An empty no_proxy value exempts
nothing, and Proxy::Url reads no exemptions.
§Cleartext origins
The fetcher reaches a cleartext origin behind an http:// proxy over a
connection to the proxy. The request carries the absolute-form target, the whole
http://host/path URL, and the Host header of the origin. Such a
connection uses HTTP/1.1, because cleartext HTTP/2 needs prior knowledge or
an upgrade.
The pool keeps such a connection under the proxy endpoint, so one proxy connection carries requests for all cleartext origins. The pool key marks a connection as a proxy connection. So the pool never gives a direct connection to the proxy endpoint to a proxied request, or the reverse. The two request forms differ, and a server answers the unexpected form with a 404.
§TLS origins
The fetcher reaches a TLS origin behind an http:// proxy over a CONNECT
tunnel. The
request names the target as host:port. The port is always written, and an
IPv6 literal keeps its brackets. If the proxy URL holds userinfo, the
CONNECT carries the same Proxy-Authorization.
On a 2xx answer, the fetcher gets the tunneled socket back. The TLS
handshake, the ALPN selection, and the pool entry are then the same as for a
direct connection to that origin. Nothing follows a CONNECT response, so a
byte that arrives before the client sends fails the connect.
A non-2xx answer is a retryable failure, and a 407 is a definitive failure.
Both are Error::Fetch and name the proxy.
connect_timeout is the limit for all
these steps together: the connect to the proxy, the CONNECT exchange, the
TLS handshake, and the HTTP handshake.
§SOCKS proxies
A socks5:// or a socks5h:// URL names a SOCKS5 proxy (RFC 1928). With
socks5://, the fetcher resolves the host name of the origin and sends an
address to the proxy. With socks5h://, the fetcher sends the host name
of the origin to the proxy, and the proxy resolves the name. If the origin
host is an IP address, the fetcher sends the address under both schemes.
socks4:// and socks4a:// fail Fetcher::new with
Error::Unsupported.
The fetcher opens a connection to the proxy and asks the proxy to connect
to the origin. Then the connection carries the bytes of the origin. A
cleartext origin and a TLS origin use this one way. The TLS handshake, the
ALPN selection, the request, and the pool entry are the same as for a
direct connection to the origin. So the request has the origin form, and
the proxy reads no HTTP. A Proxy-Authorization header of the caller goes
to the origin.
If the URL has no userinfo, the fetcher offers the method “no authentication” alone. If the URL has userinfo, the fetcher also offers the user name and password method of RFC 1929. It sends the user name and the password only if the proxy selects that method. The fetcher does not offer GSSAPI.
The userinfo is user:password. The fetcher percent-decodes each field as
for an http:// proxy, and each field must be UTF-8 after the decode. Each
field must be 1 to 255 bytes after the decode. Fetcher::new refuses
other userinfo with Error::Unsupported, for example user@ or
user:@. The message names the URL without its userinfo.
A message names the proxy with its URL without the userinfo. The message leaves out the port if it is 1080.
SOCKS5 sends a host name of 1 to 255 bytes. A longer origin host fails the
hop as a definitive failure. A host name with a % also fails as a
definitive failure, because a SOCKS5 request has no field for the zone id
of an IPv6 address. The fetcher applies these two checks under both
schemes, before it resolves the name. In these two cases, the fetcher
resolves no name and opens no connection to the proxy.
Under socks5://, the fetcher asks the proxy to connect to each resolved
address in turn, in the order that a direct connection tries them. Each
address gets a new connection to the proxy, because a refused request ends
the connection. The first address that the proxy connects wins. A
definitive failure stops the loop, and a failed connect to the proxy also
stops it. If the proxy refuses each address, the retryable failure states
the last failure and the number of addresses. A failed resolve, or a
resolve that gives no address, is a retryable failure and names the origin
host.
Two answers of the proxy are definitive failures: the proxy accepts none of
the offered methods, or it refuses the user name and the password. A retry
gets the same answer. Each other failure is retryable. This includes each
reply code other than success, a reply that breaks the protocol, and an I/O
failure. The fetcher ignores the value of the reserved byte of the reply.
Each failure is Error::Fetch. A failure of the SOCKS5 exchange names
the proxy and the origin.
connect_timeout is the limit for all
these steps together: the resolve, the connect to the proxy, the SOCKS5
exchange, the TLS handshake, and the HTTP handshake. Under socks5://,
the limit covers the whole address loop. An attempt that stalls can use
the full limit, and then the fetcher tries no later address. A timeout
names the proxy.
§Proxy credential
The proxy credential belongs to the connection layer. It goes to the proxy
on a proxied cleartext request and on a CONNECT, and to no origin. It is
not in the merged header list, so the cleartext credential check and the
redirect scoping do not read it. A tunnel carries no part of it. A SOCKS
proxy gets its credential in the RFC 1929 exchange alone.
A Proxy-Authorization header that a caller sets keeps the meaning that it
has without a proxy. It is a credential in the merged list. A fetch refuses
it for a cleartext destination, as it refuses an Authorization or a
Cookie header.
On a proxied cleartext request, the header of the caller replaces the proxy credential of the fetcher. Two values for one header give two answers to one question. Over a tunnel, the two never meet. The header of the caller goes to the origin, and the credential of the fetcher goes to the proxy.
§Redirects
The fetcher makes the proxy decision for each hop, from the origin of that hop. So a redirect to another origin goes the same way as a route that names that origin. A proxy does not change the cleartext rule: a cleartext origin is cleartext also through a proxy.
§Examples
use ostrya_fetch::{Fetcher, FetcherOptions, Proxy};
let mut options = FetcherOptions::new("https://example.com/repo");
options.proxy = Proxy::Variables(vec![
("https_proxy".into(), "http://proxy.example.com:3128".into()),
("no_proxy".into(), ".internal.example.com".into()),
]);
let fetcher = Fetcher::new(options).await?;Variants§
None
A direct connection, whatever the environment holds.
Environment
The proxy variables of the process environment.
Fetcher::new reads the variables once. This is the default.
Variables states the names, and
White space states the check of each value.
Variables(Vec<(String, String)>)
The proxy variables, given as name and value pairs.
The fetcher does not read the process environment. It ignores a name that the environment forms do not read. Of two entries of one name, the first entry with a value counts. White space states the check of each value.
Url(String)
One http://, socks5://, or socks5h:// proxy URL for all origins.
The fetcher sends the userinfo of an http:// URL as
Proxy-Authorization: Basic, and the userinfo of a SOCKS5 URL in the
RFC 1929 exchange. No origin is exempt, because the fetcher
reads the environment for neither the proxy nor the exemptions. The
fetcher trims white space around the URL.