util
v0.0.0-20180504072447-bdcd46c54acf
Documentation
pkg.go.dev ↗Functions
DumpRequest
func DumpRequest(req *Request, body bool) ([]byte, error)
DumpRequest returns the given request in its HTTP/1.x wire representation. It should only be used by servers to debug client requests. The returned representation is an approximation only; some details of the initial request are lost while parsing it into an Request. In particular, the order and case of header field names are lost. The order of values in multi-valued headers is kept intact. HTTP/2 requests are dumped in HTTP/1.x form, not in their original binary representations.
If body is true, DumpRequest also returns the body. To do so, it consumes req.Body and then replaces it with a new io.ReadCloser that yields the same bytes. If DumpRequest returns an error, the state of req is undefined.
The documentation for Request.Write details which fields of req are included in the dump. noinspection GoUnusedExportedFunction
DumpRequestOut
func DumpRequestOut(r *Request, body bool) ([]byte, error)
DumpRequestOut is like DumpRequest but for outgoing client requests. It includes any headers that the standard Transport adds, such as User-Agent.
DumpResponse
func DumpResponse(resp *Response, body bool) ([]byte, error)
DumpResponse is like DumpRequest but dumps a response.
Types
BufferPool
type BufferPool interface {
Get() []byte
Put([]byte)
}
A BufferPool is an interface for getting and returning temporary byte slices for use by io.CopyBuffer. TODO : @badu - check it out. Why use of interface?
ReverseProxy
type ReverseProxy struct {
// Director must be a function which modifies
// the request into a new request to be sent
// using Transport. Its response is then copied
// back to the original client unmodified.
// Director must not access the provided Request
// after returning.
Director func(*Request)
// The transport used to perform proxy requests.
// If nil, http.DefaultTransport is used.
Transport RoundTripper
// FlushInterval specifies the flush interval
// to flush to the client while copying the
// response body.
// If zero, no periodic flushing is done.
FlushInterval time.Duration
// ErrorLog specifies an optional logger for errors
// that occur when attempting to proxy the request.
// If nil, logging goes to os.Stderr via the log package's
// standard logger.
ErrorLog *log.Logger
// BufferPool optionally specifies a buffer pool to
// get byte slices for use by io.CopyBuffer when
// copying HTTP response bodies.
BufferPool BufferPool
// ModifyResponse is an optional function that
// modifies the Response from the backend.
// If it returns an error, the proxy returns a StatusBadGateway error.
ModifyResponse func(*Response) error
}
ReverseProxy is an HTTP Handler that takes an incoming request and sends it to another server, proxying the response back to the client.
NewSingleHostReverseProxy
func NewSingleHostReverseProxy(target *url.URL) *ReverseProxy
NewSingleHostReverseProxy returns a new ReverseProxy that routes URLs to the scheme, host, and base path provided in target. If the target's path is "/base" and the incoming request was for "/dir", the target request will be for /base/dir. NewSingleHostReverseProxy does not rewrite the Host header. To rewrite Host headers, use ReverseProxy directly with a custom Director policy. noinspection GoUnusedExportedFunction
ReverseProxy.ServeHTTP
func (p *ReverseProxy) ServeHTTP(w ResponseWriter, r *Request)