or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

bpf.mdcontext-ctxhttp.mdcontext.mddict.mddns-dnsmessage.mdhtml-atom.mdhtml-charset.mdhtml.mdhttp-httpguts.mdhttp-httpproxy.mdhttp2-h2c.mdhttp2-hpack.mdhttp2.mdicmp.mdidna.mdindex.mdipv4.mdipv6.mdnettest.mdnetutil.mdproxy.mdpublicsuffix.mdquic-qlog.mdquic.mdtrace.mdwebdav.mdwebsocket.mdxsrftoken.md
tile.json

http-httpproxy.mddocs/

HTTP Proxy Configuration

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.

Import

import "golang.org/x/net/http/httpproxy"

Types

// Config holds configuration for HTTP proxy settings
type Config struct {
    // HTTPProxy represents the value of the HTTP_PROXY or http_proxy environment variable
    HTTPProxy string

    // HTTPSProxy represents the HTTPS_PROXY or https_proxy environment variable
    HTTPSProxy string

    // NoProxy represents the NO_PROXY or no_proxy environment variable
    NoProxy string

    // CGI holds whether the current process is running as a CGI handler
    CGI bool
}

Config Fields

  • HTTPProxy: Used as the proxy URL for HTTP requests unless overridden by NoProxy
  • HTTPSProxy: Used as the proxy URL for HTTPS requests unless overridden by NoProxy
  • NoProxy: Comma-separated values specifying hosts that should be excluded from proxying. Each value can be:
    • IP address prefix (1.2.3.4)
    • IP address prefix in CIDR notation (1.2.3.4/8)
    • Domain name (foo.com matches foo.com and bar.foo.com)
    • Domain name with leading "." (.y.com matches x.y.com but not y.com)
    • Special DNS label (*) to indicate no proxying
    • IP address or domain with literal port number (1.2.3.4:80)
  • CGI: When set, ProxyForURL will return an error when HTTPProxy applies to prevent malicious proxy settings (see https://golang.org/s/cgihttpproxy)

Functions

// FromEnvironment returns a Config instance populated from environment variables
func FromEnvironment() *Config

// ProxyFunc returns a function that determines the proxy URL to use for a given request URL
func (cfg *Config) ProxyFunc() func(reqURL *url.URL) (*url.URL, error)

FromEnvironment

FromEnvironment returns a Config instance populated from the environment variables HTTP_PROXY, HTTPS_PROXY and NO_PROXY (or the lowercase versions thereof).

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.

ProxyFunc

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" or a loopback address (with or without a port number), then a nil URL and nil error will be returned.

Usage Examples

Using Environment Variables

import (
    "fmt"
    "golang.org/x/net/http/httpproxy"
    "net/url"
)

func getProxyFromEnv() {
    // Read proxy configuration from environment
    config := httpproxy.FromEnvironment()

    fmt.Printf("HTTP Proxy: %s\n", config.HTTPProxy)
    fmt.Printf("HTTPS Proxy: %s\n", config.HTTPSProxy)
    fmt.Printf("No Proxy: %s\n", config.NoProxy)

    // Create proxy function
    proxyFunc := config.ProxyFunc()

    // Determine proxy for a specific URL
    targetURL, _ := url.Parse("https://example.com/api")
    proxyURL, err := proxyFunc(targetURL)
    if err != nil {
        fmt.Printf("Error: %v\n", err)
        return
    }

    if proxyURL != nil {
        fmt.Printf("Use proxy: %s\n", proxyURL)
    } else {
        fmt.Println("No proxy needed")
    }
}

Manual Configuration

func customProxyConfig() {
    config := &httpproxy.Config{
        HTTPProxy:  "http://proxy.example.com:8080",
        HTTPSProxy: "http://proxy.example.com:8443",
        NoProxy:    "localhost,127.0.0.1,.local,internal.example.com",
        CGI:        false,
    }

    proxyFunc := config.ProxyFunc()

    // Test different URLs
    testURLs := []string{
        "http://example.com",
        "https://secure.example.com",
        "http://localhost:8080",
        "http://internal.example.com",
    }

    for _, urlStr := range testURLs {
        targetURL, _ := url.Parse(urlStr)
        proxyURL, _ := proxyFunc(targetURL)

        if proxyURL != nil {
            fmt.Printf("%s -> proxy: %s\n", urlStr, proxyURL)
        } else {
            fmt.Printf("%s -> direct connection\n", urlStr)
        }
    }
}

Integration with HTTP Client

import (
    "golang.org/x/net/http/httpproxy"
    "net/http"
    "net/url"
)

func createClientWithProxy() *http.Client {
    config := httpproxy.FromEnvironment()
    proxyFunc := config.ProxyFunc()

    transport := &http.Transport{
        Proxy: func(req *http.Request) (*url.URL, error) {
            return proxyFunc(req.URL)
        },
    }

    return &http.Client{
        Transport: transport,
    }
}