http1

package
v0.4.2 Latest Latest
Warning

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

Go to latest
Published: Dec 7, 2022 License: Apache-2.0 Imports: 32 Imported by: 0

Documentation

Index

Constants

View Source
const NextProtoTLS = suite.HTTP1

NextProtoTLS is the NPN/ALPN protocol negotiated during HTTP/1.1's TLS setup. Also used for server addressing

Variables

This section is empty.

Functions

func NewHostClient

func NewHostClient(c *ClientOptions) client.HostClient

Types

type ClientOptions added in v0.2.0

type ClientOptions struct {
	// Client name. Used in User-Agent request header.
	Name string

	// NoDefaultUserAgentHeader when set to true, causes the default
	// User-Agent header to be excluded from the Request.
	NoDefaultUserAgentHeader bool

	// Callback for establishing new connection to the host.
	//
	// Default Dialer is used if not set.
	Dialer network.Dialer

	// Timeout for establishing new connections to hosts.
	//
	// Default DialTimeout is used if not set.
	DialTimeout time.Duration

	// Attempt to connect to both ipv4 and ipv6 host addresses
	// if set to true.
	//
	// This option is used only if default TCP dialer is used,
	// i.e. if Dialer is blank.
	//
	// By default client connects only to ipv4 addresses,
	// since unfortunately ipv6 remains broken in many networks worldwide :)
	DialDualStack bool

	// Whether to use TLS (aka SSL or HTTPS) for host connections.
	// Optional TLS config.
	TLSConfig *tls.Config

	// Maximum number of connections which may be established to all hosts
	// listed in Addr.
	//
	// You can change this value while the HostClient is being used
	// using HostClient.SetMaxConns(value)
	//
	// DefaultMaxConnsPerHost is used if not set.
	MaxConns int

	// Keep-alive connections are closed after this duration.
	//
	// By default connection duration is unlimited.
	MaxConnDuration time.Duration

	// Idle keep-alive connections are closed after this duration.
	//
	// By default idle connections are closed
	// after DefaultMaxIdleConnDuration.
	MaxIdleConnDuration time.Duration

	// Maximum duration for full response reading (including body).
	//
	// By default response read timeout is unlimited.
	ReadTimeout time.Duration

	// Maximum duration for full request writing (including body).
	//
	// By default request write timeout is unlimited.
	WriteTimeout time.Duration

	// Maximum response body size.
	//
	// The client returns errBodyTooLarge if this limit is greater than 0
	// and response body is greater than the limit.
	//
	// By default response body size is unlimited.
	MaxResponseBodySize int

	// Header names are passed as-is without normalization
	// if this option is set.
	//
	// Disabled header names' normalization may be useful only for proxying
	// responses to other clients expecting case-sensitive header names.
	//
	// By default request and response header names are normalized, i.e.
	// The first letter and the first letters following dashes
	// are uppercased, while all the other letters are lowercased.
	// Examples:
	//
	//     * HOST -> Host
	//     * content-type -> Content-Type
	//     * cONTENT-lenGTH -> Content-Length
	DisableHeaderNamesNormalizing bool

	// Path values are sent as-is without normalization
	//
	// Disabled path normalization may be useful for proxying incoming requests
	// to servers that are expecting paths to be forwarded as-is.
	//
	// By default path values are normalized, i.e.
	// extra slashes are removed, special characters are encoded.
	DisablePathNormalizing bool

	// Maximum duration for waiting for a free connection.
	//
	// By default will not wait, return errNoFreeConns immediately
	MaxConnWaitTimeout time.Duration

	// ResponseBodyStream enables response body streaming
	ResponseBodyStream bool

	// All configurations related to retry
	RetryConfig *retry.Config

	RetryIfFunc client.RetryIfFunc
}

type HostClient

type HostClient struct {
	*ClientOptions

	// Comma-separated list of upstream HTTP server host addresses,
	// which are passed to Dialer in a round-robin manner.
	//
	// Each address may contain port if default dialer is used.
	// For example,
	//
	//    - foobar.com:80
	//    - foobar.com:443
	//    - foobar.com:8080
	Addr     string
	IsTLS    bool
	ProxyURI *protocol.URI
	// contains filtered or unexported fields
}

HostClient balances http requests among hosts listed in Addr.

HostClient may be used for balancing load among multiple upstream hosts. While multiple addresses passed to HostClient.Addr may be used for balancing load among them, it would be better using LBClient instead, since HostClient may unevenly balance load among upstream hosts.

It is forbidden copying HostClient instances. Create new instances instead.

It is safe calling HostClient methods from concurrently running goroutines.

func (*HostClient) CloseIdleConnections

func (c *HostClient) CloseIdleConnections()

CloseIdleConnections closes any connections which were previously connected from previous requests but are now sitting idle in a "keep-alive" state. It does not interrupt any connections currently in use.

func (*HostClient) ConnectionCount

func (c *HostClient) ConnectionCount() (count int)

func (*HostClient) Do

func (c *HostClient) Do(ctx context.Context, req *protocol.Request, resp *protocol.Response) error

Do performs the given http request and sets the corresponding response.

Request must contain at least non-zero RequestURI with full url (including scheme and host) or non-zero Host header + RequestURI.

The function doesn't follow redirects. Use Get* for following redirects.

Response is ignored if resp is nil.

errNoFreeConns is returned if all HostClient.MaxConns connections to the host are busy.

It is recommended obtaining req and resp via AcquireRequest and AcquireResponse in performance-critical code.

func (*HostClient) DoDeadline

func (c *HostClient) DoDeadline(ctx context.Context, req *protocol.Request, resp *protocol.Response, deadline time.Time) error

DoDeadline performs the given request and waits for response until the given deadline.

Request must contain at least non-zero RequestURI with full url (including scheme and host) or non-zero Host header + RequestURI.

The function doesn't follow redirects. Use Get* for following redirects.

Response is ignored if resp is nil.

errTimeout is returned if the response wasn't returned until the given deadline.

errNoFreeConns is returned if all HostClient.MaxConns connections to the host are busy.

It is recommended obtaining req and resp via AcquireRequest and AcquireResponse in performance-critical code.

func (*HostClient) DoRedirects

func (c *HostClient) DoRedirects(ctx context.Context, req *protocol.Request, resp *protocol.Response, maxRedirectsCount int) error

DoRedirects performs the given http request and fills the given http response, following up to maxRedirectsCount redirects. When the redirect count exceeds maxRedirectsCount, ErrTooManyRedirects is returned.

Request must contain at least non-zero RequestURI with full url (including scheme and host) or non-zero Host header + RequestURI.

Client determines the server to be requested in the following order:

  • from RequestURI if it contains full url with scheme and host;
  • from Host header otherwise.

Response is ignored if resp is nil.

errNoFreeConns is returned if all DefaultMaxConnsPerHost connections to the requested host are busy.

It is recommended obtaining req and resp via AcquireRequest and AcquireResponse in performance-critical code.

func (*HostClient) DoTimeout

func (c *HostClient) DoTimeout(ctx context.Context, req *protocol.Request, resp *protocol.Response, timeout time.Duration) error

DoTimeout performs the given request and waits for response during the given timeout duration.

Request must contain at least non-zero RequestURI with full url (including scheme and host) or non-zero Host header + RequestURI.

The function doesn't follow redirects. Use Get* for following redirects.

Response is ignored if resp is nil.

errTimeout is returned if the response wasn't returned during the given timeout.

errNoFreeConns is returned if all HostClient.MaxConns connections to the host are busy.

It is recommended obtaining req and resp via AcquireRequest and AcquireResponse in performance-critical code.

Warning: DoTimeout does not terminate the request itself. The request will continue in the background and the response will be discarded. If requests take too long and the connection pool gets filled up please try setting a ReadTimeout.

func (*HostClient) Get

func (c *HostClient) Get(ctx context.Context, dst []byte, url string) (statusCode int, body []byte, err error)

Get returns the status code and body of url.

The contents of dst will be replaced by the body and returned, if the dst is too small a new slice will be allocated.

The function follows redirects. Use Do* for manually handling redirects.

func (*HostClient) GetDeadline

func (c *HostClient) GetDeadline(ctx context.Context, dst []byte, url string, deadline time.Time) (statusCode int, body []byte, err error)

GetDeadline returns the status code and body of url.

The contents of dst will be replaced by the body and returned, if the dst is too small a new slice will be allocated.

The function follows redirects. Use Do* for manually handling redirects.

errTimeout error is returned if url contents couldn't be fetched until the given deadline.

func (*HostClient) GetTimeout

func (c *HostClient) GetTimeout(ctx context.Context, dst []byte, url string, timeout time.Duration) (statusCode int, body []byte, err error)

GetTimeout returns the status code and body of url.

The contents of dst will be replaced by the body and returned, if the dst is too small a new slice will be allocated.

The function follows redirects. Use Do* for manually handling redirects.

errTimeout error is returned if url contents couldn't be fetched during the given timeout.

func (*HostClient) LastUseTime

func (c *HostClient) LastUseTime() time.Time

LastUseTime returns time the client was last used

func (*HostClient) PendingRequests

func (c *HostClient) PendingRequests() int

PendingRequests returns the current number of requests the client is executing.

This function may be used for balancing load among multiple HostClient instances.

func (*HostClient) Post

func (c *HostClient) Post(ctx context.Context, dst []byte, url string, postArgs *protocol.Args) (statusCode int, body []byte, err error)

Post sends POST request to the given url with the given POST arguments.

The contents of dst will be replaced by the body and returned, if the dst is too small a new slice will be allocated.

The function follows redirects. Use Do* for manually handling redirects.

Empty POST body is sent if postArgs is nil.

func (*HostClient) SetDynamicConfig added in v0.2.0

func (c *HostClient) SetDynamicConfig(dc *client.DynamicConfig)

func (*HostClient) SetMaxConns

func (c *HostClient) SetMaxConns(newMaxConns int)

SetMaxConns sets up the maximum number of connections which may be established to all hosts listed in Addr.

func (*HostClient) ShouldRemove

func (c *HostClient) ShouldRemove() bool

func (*HostClient) WantConnectionCount

func (c *HostClient) WantConnectionCount() (count int)

type Option

type Option struct {
	StreamRequestBody            bool
	GetOnly                      bool
	DisablePreParseMultipartForm bool
	DisableKeepalive             bool
	NoDefaultServerHeader        bool
	MaxRequestBodySize           int
	IdleTimeout                  time.Duration
	ReadTimeout                  time.Duration
	ServerName                   []byte
	TLS                          *tls.Config
	HTMLRender                   render.HTMLRender
	EnableTrace                  bool
	ContinueHandler              func(header *protocol.RequestHeader) bool
	HijackConnHandle             func(c network.Conn, h app.HijackHandler)
}

type Server

type Server struct {
	Option
	Core suite.Core
}

func (Server) Serve

func (s Server) Serve(c context.Context, conn network.Conn) (err error)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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