net: Index | Files

package httpproxy

import ""

Package httpproxy provides support for HTTP proxy determination based on environment variables, as provided by net/http's ProxyFromEnvironment function.

The API is not subject to the Go 1 compatibility promise and may change at any time.


Package Files


type Config Uses

type Config struct {
    // HTTPProxy represents the value of the HTTP_PROXY or
    // http_proxy environment variable. It will be used as the proxy
    // URL for HTTP requests and HTTPS requests unless overridden by
    // HTTPSProxy or NoProxy.
    HTTPProxy string

    // HTTPSProxy represents the HTTPS_PROXY or https_proxy
    // environment variable. It will be used as the proxy URL for
    // HTTPS requests unless overridden by NoProxy.
    HTTPSProxy string

    // NoProxy represents the NO_PROXY or no_proxy environment
    // variable. It specifies a string that contains comma-separated values
    // specifying hosts that should be excluded from proxying. Each value is
    // represented by an IP address prefix (, an IP address prefix in
    // CIDR notation (, a domain name, or a special DNS label (*).
    // An IP address prefix and domain name can also include a literal port
    // number (
    // A domain name matches that name and all subdomains. A domain name with
    // a leading "." matches subdomains only. For example "" matches
    // "" and ""; "" matches "" but not "".
    // A single asterisk (*) indicates that no proxying should be done.
    // A best effort is made to parse the string and errors are
    // ignored.
    NoProxy string

    // CGI holds whether the current process is running
    // as a CGI handler (FromEnvironment infers this from the
    // presence of a REQUEST_METHOD environment variable).
    // When this is set, ProxyForURL will return an error
    // when HTTPProxy applies, because a client could be
    // setting HTTP_PROXY maliciously. See
    CGI bool

Config holds configuration for HTTP proxy settings. See FromEnvironment for details.

func FromEnvironment Uses

func FromEnvironment() *Config

FromEnvironment returns a Config instance populated from the environment variables HTTP_PROXY, HTTPS_PROXY and NO_PROXY (or the lowercase versions thereof). HTTPS_PROXY takes precedence over HTTP_PROXY for https requests.

The environment values may be either a complete URL or a "host[:port]", in which case the "http" scheme is assumed. An error is returned if the value is a different form.

func (*Config) ProxyFunc Uses

func (cfg *Config) ProxyFunc() func(reqURL *url.URL) (*url.URL, error)

ProxyFunc returns a function that determines the proxy URL to use for a given request URL. Changing the contents of cfg will not affect proxy functions created earlier.

A nil URL and nil error are returned if no proxy is defined in the environment, or a proxy should not be used for the given request, as defined by NO_PROXY.

As a special case, if req.URL.Host is "localhost" (with or without a port number), then a nil URL and nil error will be returned.

Package httpproxy imports 8 packages (graph) and is imported by 31 packages. Updated 2020-09-27. Refresh now. Tools for package owners.