safehttp

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Sep 30, 2022 License: BSD-3-Clause Imports: 4 Imported by: 4

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

Constants

This section is empty.

Variables

This section is empty.

Functions

func Do

func Do(c *http.Client, req *http.Request) (resp *http.Response, cleanup func(), err error)

func Get

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

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

func PostForm

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

Types

type Client

type Client http.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.

func (*Client) Do

func (c *Client) Do(req *http.Request) (resp *http.Response, cleanup func(), err error)

func (*Client) Get

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

func (*Client) Head

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

func (*Client) Post

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

func (*Client) PostForm

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

Jump to

Keyboard shortcuts

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