httpaux

package module
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Jul 6, 2023 License: Apache-2.0 Imports: 5 Imported by: 8

README

httpaux

The missing functionality from net/http

Build Status codecov.io Go Report Card Quality Gate Status Apache V2 License GitHub Release GoDoc

Summary

httpaux augments golang's net/http package with a few extra goodies.

  • Middleware for clients in the form of http.RoundTripper decoration
  • Standardized middleware interfaces
  • Busy server middleware to limit serverside traffic
  • Observable http.ResponseWriter middleware that provides a standard way for http.Handler decorators to examine what was written to the response by nested handlers
  • Gate middleware to control server and client traffic
  • An efficient, immutable Header type for writing static headers
  • A configurable ConstantHandler that writes hardcoded information to responses
  • httpmock package for mock-style testing with HTTP clients
  • retry package with configurable retry for clients including exponential backoff
  • erraux package with a configurable Encoder ruleset for representing errors as HTTP responses

Table of Contents

Code of Conduct

This project and everyone participating in it are governed by the XMiDT Code Of Conduct. By participating, you agree to this Code.

Install

go get github.com/xmidt-org/httpaux

Contributing

Refer to CONTRIBUTING.md.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Cleanup added in v0.1.2

func Cleanup(r *http.Response)

Cleanup is a utility function for ensuring that a client response's Body is drained and closed. This function does not set the Body to nil.

If either the response or the response.Body field is nil, this function does nothing.

Types

type Client added in v0.1.2

type Client interface {
	Do(*http.Request) (*http.Response, error)
}

Client is the canonical interface implemented by *http.Client

type ConstantHandler

type ConstantHandler struct {
	// StatusCode is the response code to pass to http.ResponseWriter.WriteHeader.
	// If this value is less than 100 (which also includes being unset), then no
	// response code is written which will trigger the default of http.StatusOK.
	StatusCode int

	// Header is the set of headers added to every response
	Header Header

	// ContentType is the MIME type of the Body.  This will override anything written by
	// the Headers closure.  This field has no effect if Body is not also set.  If Body is
	// set and this field is unset, then no Content-Type header is written by this handler.
	// A Content-Type may still be written by other infrastructure or by the Headers closure, however.
	ContentType string

	// Body is the optional HTTP entity body.  If unset, nothing is written for the response body.
	// A Content-Length header will be explicitly set if this field is set.
	Body []byte
}

ConstantHandler is an http.Handler that writes a statically defined HTTP response. Very useful for testing and for default behavior.

func ConstantJSON

func ConstantJSON(v interface{}) (ch ConstantHandler, err error)

ConstantJSON is a convenience for constructing a ConstantHandler that returns a static JSON message. The encoding/json package is used to marshal v.

func (ConstantHandler) ServeHTTP

func (ch ConstantHandler) ServeHTTP(response http.ResponseWriter, _ *http.Request)

ServeHTTP returns the constant information in the response.

type Header struct {
	// contains filtered or unexported fields
}

Header is a more efficient version of http.Header for situations where a number of HTTP headers are stored in memory and reused. Rather than a map, a simple list of headers is maintained in canonicalized form. This is much faster to iterate over than a map, which becomes important when the same Header is used to add headers to many requests.

A Header instance is immutable once created. The zero value is an usable, empty Header which is immutable.

func EmptyHeader added in v0.1.6

func EmptyHeader() Header

EmptyHeader returns a canonical singleton Header that is empty. Useful as a "nil" value or as a default.

func NewHeader

func NewHeader(v http.Header) Header

NewHeader creates an immutable, preprocessed Header given an http.Header.

If v is empty, the canonical EmptyHeader is returned.

func NewHeaderFromMap

func NewHeaderFromMap(v map[string]string) Header

NewHeaderFromMap creates an immutable, preprocessed Header given a simple map of string-to-string.

If v is empty, the canonical EmptyHeader is returned.

func NewHeaders

func NewHeaders(v ...string) Header

NewHeaders creates an immutable, preprocessed Header given a variadic list of names and values in {name1, value1, name2, value2, ...} order. Duplicate names are allowed, which will result in multi-valued headers. If v contains an odd number of values, the last value is interpreted as a header name with a single blank value.

If v is empty, the canonical EmptyHeader is returned.

func (Header) AddTo added in v0.1.6

func (h Header) AddTo(dst http.Header)

AddTo adds each name/value defined in this Header to the supplied http.Header.

func (Header) Append added in v0.1.6

func (h Header) Append(more http.Header) Header

Append returns a new Header instance with the given http.Header values appended to these header values. If more is empty, the original Header is returned.

This method does not modify the original Header.

func (Header) AppendHeaders added in v0.1.6

func (h Header) AppendHeaders(more ...string) Header

AppendHeaders return a new Header with the variadic slice of name/value pairs appended. If more has an odd length, the last value is assumed to be a name with a blank value. If more is empty, the original Header is returned. Duplicate names are allowed, which result in multi-valued headers.

This method does not modify the original Header.

func (Header) AppendMap added in v0.1.6

func (h Header) AppendMap(more map[string]string) Header

AppendMap returns a new Header instance with the given map of headers added. This function is useful in the simple cases where only single-valued headers are supported by application code. If more is empty, the original Header is returned.

This method does not modify the original Header.

func (Header) Extend added in v0.1.6

func (h Header) Extend(more Header) Header

Extend returns the merger of this Header with the given Header. If this Header is empty, more is returned. If more is empty, this Header is returned. Otherwise, a new Header that is the union of the two is returned.

This method does not modify the original Header.

func (Header) Len added in v0.1.6

func (h Header) Len() int

Len returns the number of distinct header names in this Header.

func (Header) SetTo

func (h Header) SetTo(dst http.Header)

SetTo replaces each name/value defined in this Header in the supplied http.Header.

Directories

Path Synopsis
Package erraux provides auxiliary functionality for marshaling and returning errors over HTTP.
Package erraux provides auxiliary functionality for marshaling and returning errors over HTTP.
Package gate implements a simple atomic boolean that controls access to HTTP client or server functionality
Package gate implements a simple atomic boolean that controls access to HTTP client or server functionality
Package httpmock provides stretchr/testify/mock integration
Package httpmock provides stretchr/testify/mock integration
Package observe exposes a standard way of decorating http.ResponseWriter objects so that they can be examined by infrastructure such as logging and metrics.
Package observe exposes a standard way of decorating http.ResponseWriter objects so that they can be examined by infrastructure such as logging and metrics.
Package recovery implements an http.Handler that recovers from panics, allowing configurable actions to take when a panic occurs.
Package recovery implements an http.Handler that recovers from panics, allowing configurable actions to take when a panic occurs.
Package retry implements retry logic for HTTP clients.
Package retry implements retry logic for HTTP clients.
Package roundtrip provides middleware and a few utility types for use with http.RoundTripper
Package roundtrip provides middleware and a few utility types for use with http.RoundTripper
Package server provides extra functionality specific to HTTP servers and handlers.
Package server provides extra functionality specific to HTTP servers and handlers.

Jump to

Keyboard shortcuts

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