Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ( // ErrHijackFailed is delivered to the OnError callback when the server // cannot take ownership of the connection from the HTTP server. ErrHijackFailed = errors.New("hijack failed") // ErrWriteHandshake is delivered when the 101 response cannot be written. ErrWriteHandshake = errors.New("write handshake response failed") // ErrFlushHandshake is delivered when the 101 response cannot be flushed. ErrFlushHandshake = errors.New("flush handshake response failed") // ErrHandlerPanic is delivered when a HijackHandler panics. // The recovered value is appended to the error message. ErrHandlerPanic = errors.New("handler panic") )
var ( // ErrMissingUpgradeHeader is returned by Upgrade when the request has no // Upgrade header set. ErrMissingUpgradeHeader = errors.New("missing Upgrade header") // ErrSetDeadline is returned by Upgrade when the handshake deadline cannot // be set on the connection. ErrSetDeadline = errors.New("set handshake deadline failed") // ErrSendRequest is returned by Upgrade when the HTTP request cannot be // written to the connection. ErrSendRequest = errors.New("send upgrade request failed") // ErrReadResponse is returned by Upgrade when the server response cannot // be read or parsed. ErrReadResponse = errors.New("read upgrade response failed") // ErrUnexpectedStatus is returned by Upgrade when the server responds with // a status other than 101 Switching Protocols. ErrUnexpectedStatus = errors.New("unexpected response status") // ErrProtocolMismatch is returned by Upgrade when the Upgrade header in // the server's response does not match the requested protocol. ErrProtocolMismatch = errors.New("protocol mismatch in response") // ErrMissingConnectionHeader is returned by Upgrade when the server's // response is missing the required Connection: Upgrade header. ErrMissingConnectionHeader = errors.New("response missing Connection: Upgrade header") // ErrResponseValidator is returned by Upgrade when a ResponseValidator // option rejects the server's response. ErrResponseValidator = errors.New("response validation failed") )
var ErrDrainBuffer = errors.New("drain read buffer failed")
ErrDrainBuffer is returned or delivered when leftover buffered bytes from the HTTP layer cannot be read before the connection is handed to the caller.
Functions ¶
func Hijack ¶
func Hijack(handler HijackHandler, opts ...Option) http.Handler
Hijack returns an http.Handler that performs a full HTTP/1.1 protocol upgrade handshake and hands the raw connection to handler.
Use Protocols to restrict accepted upgrade values. Use OnError to receive errors that occur after the response writer is no longer available.
Ownership of the connection transfers to handler on success; Hijack closes conn only when the handshake itself fails.
func Upgrade ¶
Upgrade performs an HTTP/1.1 protocol upgrade on conn using req. On success it returns a net.Conn.
The caller is fully responsible for constructing req: method, URL, headers, and an optional body may all be set. The only hard requirement is that req.Header must contain "Upgrade: <proto>" (used for response validation).
Types ¶
type HijackHandler ¶
HijackHandler is called after a successful HTTP/1.1 upgrade handshake. conn is the raw connection ready for protocol use; proto is the negotiated protocol value from the Upgrade header.
Ownership of conn transfers to the handler — it is responsible for closing it.
type Option ¶
type Option func(*config)
Option configures the behaviour of Hijack.
func OnError ¶
OnError sets a callback that receives errors occurring during or after the handshake. This includes write failures and recovered handler panics. If not set, these errors are silently discarded.
func Protocols ¶
Protocols restricts the server to the given protocol names. Matching is case-insensitive. Clients requesting a protocol not in the list receive a 426 Upgrade Required response listing the accepted protocols. When Protocols is not set, any non-empty Upgrade value is accepted.
func ResponseHeaders ¶
ResponseHeaders sets a function that produces additional headers to include in the 101 Switching Protocols response. It receives the incoming request and the negotiated protocol name so challenge headers (e.g. Sec-WebSocket-Accept) can be derived from them. Headers returned here are merged into the response after the mandatory Connection and Upgrade headers.
type UpgradeOption ¶
type UpgradeOption func(*upgradeConfig)
UpgradeOption configures the behaviour of Upgrade.
func ResponseValidator ¶
ResponseValidator sets a function that is called after Cooper's own header checks pass (101 status, Upgrade match, Connection: Upgrade). The validator receives the original request and the server response, and may inspect any additional headers — for example to verify Sec-WebSocket-Accept. If it returns an error, Upgrade returns that error wrapped with ErrResponseValidator.