golib: github.com/levenlabs/golib/proxyutil Index | Files

package proxyutil

import "github.com/levenlabs/golib/proxyutil"

Package proxyutil implements helper types and functions for proxying (generally non-rpc) http requests to/from backing services.


Package Files

brw.go proxy.go xff.go


var BufferedResponseWriterEncodings = []string{"gzip", "deflate"}

BufferedResponseWriterEncodings is the set of encodings that BufferedResponseWriter supports

func AddXForwardedFor Uses

func AddXForwardedFor(dst, src *http.Request)

AddXForwardedFor populates the X-Forwarded-For header on dst to convey that src is being proxied by this server. dst and src may be the same pointer.

func AddXForwardedForIP Uses

func AddXForwardedForIP(dst *http.Request, ip string)

AddXForwardedForIP populates the X-Forwarded-For header on dst to convey that ip is making the request.

func DecodeResponse Uses

func DecodeResponse(resp *http.Response) error

DecodeResponse takes a http.Response from Do() and replaces the body with an uncompressed form. It also sets resp.Uncompressed to true. If the body is already decompressed (because you didn't use Do()) then, this does nothing.

func FilterEncodings Uses

func FilterEncodings(r *http.Request, encodings ...string)

FilterEncodings adjusts the Accept-Encoding header, if set, so that it only allows at most the given encodings. If it previously had a subset of the given encodings only those will be left. At least one encoding must be passed in.

func RequestIP Uses

func RequestIP(r *http.Request) string

RequestIP returns the string form of the original requester's IP address for the given request, taking into account X-Forwarded-For if applicable. If the request was from a loopback address, then we will take the first non-loopback X-Forwarded-For address. This is under the assumption that your golang server is running behind a reverse proxy.

func WriteResponse Uses

func WriteResponse(dst http.ResponseWriter, src *http.Response) error

WriteResponse writes the given response into the ResponseWriter. It will encode the Response's Body according to its Content-Encoding header, if it has one. If you're writing the response to a HEAD or OPTIONS request, make sure that ContentLength on the http.Response is 0 to prevent writing.

type BufferedResponseWriter Uses

type BufferedResponseWriter struct {

    Code int
    // contains filtered or unexported fields

BufferedResponseWriter is a wrapper around a real ResponseWriter which actually writes all data to a buffer instead of the ResponseWriter. It also catches calls to WriteHeader. Once writing is done, GetBody can be called to get the buffered body, and SetBody can be called to set a new body to be used. When ActuallyWrite is called all headers will be written to the ResponseWriter (with corrected Content-Length if SetBody was called), followed by the body.

BufferedResponseWriter will transparently handle uncompressing and recompressing the response body when it sees a known Content-Encoding.

func NewBufferedResponseWriter Uses

func NewBufferedResponseWriter(rw http.ResponseWriter) *BufferedResponseWriter

NewBufferedResponseWriter returns an initialized BufferedResponseWriter, which will catch writes going to the given ResponseWriter

func (*BufferedResponseWriter) ActuallyWrite Uses

func (brw *BufferedResponseWriter) ActuallyWrite() (int64, error)

ActuallyWrite takes all the buffered data and actually writes to the wrapped ResponseWriter. Returns the number of bytes written as the body

func (*BufferedResponseWriter) GetBody Uses

func (brw *BufferedResponseWriter) GetBody() (io.ReadCloser, error)

GetBody returns a ReadCloser which gives the body of the response which was buffered. If the response was compressed the body will be transparently decompressed. Close must be called in order to clean up resources. GetBody may be called multiple times, each time it will return an independent ReadCloser for the original response body.

func (*BufferedResponseWriter) MarshalBinary Uses

func (brw *BufferedResponseWriter) MarshalBinary() ([]byte, error)

MarshalBinary returns a binary form of the BufferedResponseWriter. Can only be called after a response has been buffered.

func (*BufferedResponseWriter) SetBody Uses

func (brw *BufferedResponseWriter) SetBody(in io.Reader)

SetBody sets the io.Reader from which the new body of the buffered response should be read from. This io.Reader will be drained once ActuallyWrite is called. If this is never called the original body will be used.

Note: there is no need to send in compressed data here, the new response body will be automatically compressed in the same way the original was.

func (*BufferedResponseWriter) UnmarshalBinary Uses

func (brw *BufferedResponseWriter) UnmarshalBinary(b []byte) error

UnmarshalBinary takes in a byte slice produced by calling MarshalBinary on another BufferedResponseWriter, and makes this one identical to it.

func (*BufferedResponseWriter) Write Uses

func (brw *BufferedResponseWriter) Write(b []byte) (int, error)

func (*BufferedResponseWriter) WriteHeader Uses

func (brw *BufferedResponseWriter) WriteHeader(code int)

WriteHeader catches the call to WriteHeader and doesn't actually do anything, except store the given code for later use when ActuallyWrite is called

type ReverseProxyClient Uses

type ReverseProxyClient struct {
    // If nil then http.DefaultClient will beused
    Client *http.Client

ReverseProxyClient is a wrapper around a basic *http.Client which performs requests as a reverse http proxy instead of a normal client. This involves adding/removing certain headers and dealing with Keep-Alive.

func (*ReverseProxyClient) DisableCompression Uses

func (rp *ReverseProxyClient) DisableCompression()

DisableCompression disables the built-in Transport's decompression. It seems like the built-in decompression is only done when no Accept-Encoding header was sent. If you want to remove uncertainty, call this. If you're not reading the body at all, you should call this as an optimization. Note: This will change the DisableCompression value on the Transport, which if you didn't pass any, is the DefaultTransport, which will affect more than just requests sent through this client. Use genapi.HTTPDefaultClient instead Additionally: https://github.com/golang/go/issues/18779 If you call this, you must call DecodeResponse, before reading the response's body from Do().

func (*ReverseProxyClient) Do Uses

func (rp *ReverseProxyClient) Do(req *http.Request) (*http.Response, error)

Do will perform the given request, which should have been taken in from an http.Handler and is now being forwarded on with a new URL set. If the request's context is cancelled then the request will be cancelled.

If you expect an encoded (compressed) response, use DecodeResponse to decode it before reading. If you're just piping this to WriteResponse, you don't need DecodeResponse.

Package proxyutil imports 10 packages (graph) and is imported by 2 packages. Updated 2017-04-28. Refresh now. Tools for package owners.