Documentation ¶
Overview ¶
Package proxyutil implements helper types and functions for proxying (generally non-rpc) http requests to/from backing services.
Index ¶
- Variables
- func AddXForwardedFor(dst, src *http.Request)
- func AddXForwardedForIP(dst *http.Request, ip string)
- func DecodeResponse(resp *http.Response) error
- func FilterEncodings(r *http.Request, encodings ...string)
- func RequestIP(r *http.Request) string
- func WriteResponse(dst http.ResponseWriter, src *http.Response) error
- type BufferedResponseWriter
- func (brw *BufferedResponseWriter) ActuallyWrite() (int64, error)
- func (brw *BufferedResponseWriter) GetBody() (io.ReadCloser, error)
- func (brw *BufferedResponseWriter) MarshalBinary() ([]byte, error)
- func (brw *BufferedResponseWriter) SetBody(in io.Reader)
- func (brw *BufferedResponseWriter) UnmarshalBinary(b []byte) error
- func (brw *BufferedResponseWriter) Write(b []byte) (int, error)
- func (brw *BufferedResponseWriter) WriteHeader(code int)
- type ReverseProxyClient
Constants ¶
This section is empty.
Variables ¶
var BufferedResponseWriterEncodings = []string{"gzip", "deflate"}
BufferedResponseWriterEncodings is the set of encodings that BufferedResponseWriter supports
Functions ¶
func AddXForwardedFor ¶
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 ¶
AddXForwardedForIP populates the X-Forwarded-For header on dst to convey that ip is making the request.
func DecodeResponse ¶
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 ¶
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 ¶
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 ¶
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.
Types ¶
type BufferedResponseWriter ¶
type BufferedResponseWriter struct { http.ResponseWriter 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 ¶
func NewBufferedResponseWriter(rw http.ResponseWriter) *BufferedResponseWriter
NewBufferedResponseWriter returns an initialized BufferedResponseWriter, which will catch writes going to the given ResponseWriter
func (*BufferedResponseWriter) ActuallyWrite ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
func (brw *BufferedResponseWriter) Write(b []byte) (int, error)
func (*BufferedResponseWriter) WriteHeader ¶
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 ¶
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 ¶
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 ¶
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.