http

package
v1.0.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 27, 2025 License: AGPL-3.0 Imports: 10 Imported by: 5

Documentation

Overview

Package http is a wrapper around the standard http library with enhanced DNS resolution and TLS handling capabilities.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrNoTLSConnection is returned when a TLS connection cannot be established.
	ErrNoTLSConnection = errors.New("no tls connection")
	// ErrEmptyHostAddress is returned when the host address is empty.
	ErrEmptyHostAddress = errors.New("empty host addr")
)
View Source
var DefaultClient = http.Client{
	Transport: &http.Transport{
		Proxy: http.ProxyFromEnvironment,
		DialTLSContext: func(ctx context.Context, network, addr string) (net.Conn, error) {
			host, port, err := net.SplitHostPort(addr)
			if err != nil {
				return nil, err
			}
			addrs, err := dns.LookupHost(ctx, host)
			if err != nil {
				return nil, err
			}
			if len(addrs) == 0 {
				return nil, ErrEmptyHostAddress
			}
			var conn net.Conn
			var tlsConn *tls.Conn
			for _, a := range addrs {

				if defaultDialer.Timeout != 0 {
					var cancel context.CancelFunc
					ctx, cancel = context.WithTimeout(context.Background(), defaultDialer.Timeout)
					defer cancel()
				} else if !defaultDialer.Deadline.IsZero() {
					var cancel context.CancelFunc
					ctx, cancel = context.WithDeadline(context.Background(), defaultDialer.Deadline)
					defer cancel()
				}
				conn, err = defaultDialer.DialContext(ctx, network, net.JoinHostPort(a, port))
				if err != nil {
					continue
				}
				tlsConn = tls.Client(terasu.NewConn(conn), &tls.Config{
					ServerName: host,
					MinVersion: tls.VersionTLS12,
				})

				if defaultDialer.Timeout != 0 {
					var cancel context.CancelFunc
					ctx, cancel = context.WithTimeout(context.Background(), defaultDialer.Timeout)
					defer cancel()
				} else if !defaultDialer.Deadline.IsZero() {
					var cancel context.CancelFunc
					ctx, cancel = context.WithDeadline(context.Background(), defaultDialer.Deadline)
					defer cancel()
				}
				err = tlsConn.HandshakeContext(ctx)
				if err == nil {
					break
				}
				_ = tlsConn.Close()
				tlsConn = nil
				conn, err = defaultDialer.DialContext(ctx, network, net.JoinHostPort(a, port))
				if err != nil {
					continue
				}
				tlsConn = tls.Client(terasu.NewConn(conn), &tls.Config{
					ServerName: host,
					MinVersion: tls.VersionTLS12,
				})
				err = tlsConn.HandshakeContext(ctx)
				if err == nil {
					break
				}
				_ = tlsConn.Close()
				tlsConn = nil
			}
			return tlsConn, err
		},
		ForceAttemptHTTP2:     true,
		MaxIdleConns:          100,
		IdleConnTimeout:       90 * time.Second,
		TLSHandshakeTimeout:   10 * time.Second,
		ExpectContinueTimeout: 1 * time.Second,
	},
}

DefaultClient is the default HTTP client with custom transport settings, including DNS resolution and TLS handling.

Functions

func Get

func Get(url string) (resp *http.Response, err error)

Get performs an HTTP GET request using the default client.

func Head(url string) (resp *http.Response, err error)

Head performs an HTTP HEAD request using the default client.

func Post

func Post(url string, contentType string, body io.Reader) (resp *http.Response, err error)

Post performs an HTTP POST request using the default client.

func PostForm

func PostForm(url string, data url.Values) (resp *http.Response, err error)

PostForm performs an HTTP POST request with form data using the default client.

func SetDefaultClientTimeout

func SetDefaultClientTimeout(t time.Duration)

SetDefaultClientTimeout sets the default timeout for the client's dialer.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL