proxy

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Apr 4, 2019 License: BSD-3-Clause Imports: 13 Imported by: 0

README

Conditional Proxy

GoDoc Widget Build Widget


A reverse proxy for golang that allows requests to be blocked/aborted before being sent upstream.

The Director function, unlike the ReverseProxy in the httputil package, returns a bool that determines whether a Request should be sent to the upstream server or not. If false is returned, by default, a 403 response will be returned. This can be customized via the CreateResponse function. Otherwise, the ConditionalReverseProxy behaves exactly the same way as the ReverseProxy from httputil.

Requires Go 1.12+.

Documentation

Overview

Package proxy implements an HTTP conditional reverse proxy handler

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ConditionalReverseProxy

type ConditionalReverseProxy struct {
	// Director must be a function which
	// modifies the request into a new request to be
	// sent using Transport. It must return true if
	// the request should be sent, and false if the
	// request should be aborted and a new response
	// should be generated.
	// Director must not access the provided
	// Request after returning.
	Director func(*http.Request) bool

	// The transport used to perform proxy requests.
	// If nil, http.DefaultTransport is used.
	Transport http.RoundTripper

	// FlushInterval specifies the flush interval
	// to flush to the client while copying the
	// response body.
	// If zero, no periodic flushing is done.
	// A negative value means to flush immediately
	// after each write to the client.
	// The FlushInterval is ignored when ReverseProxy
	// recognizes a response as a streaming response;
	// for such responses, writes are flushed to the client
	// immediately.
	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 httputil.BufferPool

	// CreateResponse is an optional function that creates a
	// Response without sending a request to the backend.
	// It is called if Director returns false.
	//
	// CreateResponse should not modify the request, except for
	// consuming and closing the Request's Body. CreateResponse may
	// read fields of the request in a separate goroutine. Callers
	// should not mutate or reuse the request until the Response's
	// Body has been closed.
	//
	// CreateResponse must always close the body, including on errors,
	// but depending on the implementation may do so in a separate
	// goroutine even after CreateResponse returns. This means that
	// callers wanting to reuse the body for subsequent requests
	// must arrange to wait for the Close call before doing so.
	//
	// The Request's URL and Header fields must be initialized.
	//
	// If CreateResponse is not set, the default implementation
	// will return a 403 response.
	//
	// If CreateResponse returns an error, ErrorHandler is called
	// with its error value. If ErrorHandler is nil, its default
	// implementation is used.
	CreateResponse func(http.ResponseWriter, *http.Request) error

	// ModifyResponse is an optional function that modifies the
	// Response from the backend. It is called if the backend
	// returns a response at all, with any HTTP status code.
	// If the backend is unreachable, the optional ErrorHandler is
	// called without any call to ModifyResponse.
	//
	// If ModifyResponse returns an error, ErrorHandler is called
	// with its error value. If ErrorHandler is nil, its default
	// implementation is used.
	ModifyResponse func(*http.Response) error

	// ErrorHandler is an optional function that handles errors
	// reaching the backend or errors from ModifyResponse.
	//
	// If nil, the default is to log the provided error and return
	// a 502 Status Bad Gateway response.
	ErrorHandler func(http.ResponseWriter, *http.Request, error)
}

ConditionalReverseProxy is an HTTP Handler that takes an incoming request and conditionally sends it to another server, proxying the response back to the client. It may also short circuit and handle the response directly itself.

func NewSingleHostReverseProxy

func NewSingleHostReverseProxy(target *url.URL) *ConditionalReverseProxy

NewSingleHostReverseProxy returns a new ConditionalReverseProxy 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. NewSingleHostConditionalReverseProxy does not rewrite the Host header. To rewrite Host headers, use ConditionalReverseProxy directly with a custom Director policy.

func (*ConditionalReverseProxy) ServeHTTP

func (p *ConditionalReverseProxy) ServeHTTP(rw http.ResponseWriter, req *http.Request)

Jump to

Keyboard shortcuts

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