Documentation ¶
Overview ¶
Package safehttp provides types and functions that allow http.Client functions to be used in a manner that ensures that HTTP connections are not leaked. The default implementation of http.Client functions such as "Get" and "Post" return a reader as part of the response object. The connection used by the call can be reused only if the body of the response is fully drained and closed. In practice, it is easy to forget that both of these actions are necessary, which can lead to a large number of leaked/persistent http connections.
The safehttp package provides functions that wrap the http.Client functionality for functions that return responses in a manner that also returns a cleanup function that drains and closes the body of the response. Callers can simply defer the returned cleanup function to ensure that the connections are properly relinquished. It is safe for the cleanup function to execute even if the body has already been drained or closed.
Index ¶
- func Do(c *http.Client, req *http.Request) (resp *http.Response, cleanup func(), err error)
- func Get(c *http.Client, url string) (resp *http.Response, cleanup func(), err error)
- func Head(c *http.Client, url string) (resp *http.Response, cleanup func(), err error)
- func Post(c *http.Client, url string, contentType string, body io.Reader) (resp *http.Response, cleanup func(), err error)
- func PostForm(c *http.Client, url string, data url.Values) (resp *http.Response, cleanup func(), err error)
- type Client
- func (c *Client) Do(req *http.Request) (resp *http.Response, cleanup func(), err error)
- func (c *Client) Get(url string) (resp *http.Response, cleanup func(), err error)
- func (c *Client) Head(url string) (resp *http.Response, cleanup func(), err error)
- func (c *Client) Post(url string, contentType string, body io.Reader) (resp *http.Response, cleanup func(), err error)
- func (c *Client) PostForm(url string, data url.Values) (resp *http.Response, cleanup func(), err error)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Client ¶
Client is a type alias for http.Client that redefines the request functions (Get, Head, Post, PostForm, Do) to return an additional cleanup function. The returned cleanup function should be deferred after the call is made, and ensures that the response body is fully drained and closed so that subsequent calls that are made using the same client will properly reuse connections.