Documentation ¶
Overview ¶
httpredir is a simple HTTP redirector. This is useful if you plan to build an entire HTTP reverse proxy on top of the proxy.TLSProxy struct, so you don't have to do the legwork of creating a redirector yourself. It is not necessary to use this, though, and everything this package provides can be accomplished with a simple Nginx config.
Index ¶
- type HTTPRedirector
- func (r *HTTPRedirector) Accept() (net.Conn, error)
- func (r *HTTPRedirector) AddRoute(pattern string, handler RedirectHandler)
- func (r *HTTPRedirector) Addr() net.Addr
- func (r *HTTPRedirector) Close() error
- func (r *HTTPRedirector) Listen() error
- func (r *HTTPRedirector) ListenAndServe() error
- func (r *HTTPRedirector) Serve() error
- type PatternNotPresentError
- type RedirectHandler
- type TLSRedirector
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type HTTPRedirector ¶
type HTTPRedirector struct { Mux *muxie.Mux RedirHandler RedirectHandler net.Listener // contains filtered or unexported fields }
HTTPRedirector is the main HTTP redirector struct. It implements the net.Listener interface, and has an internal *muxie.Mux, which is used to handle incoming requests.
func NewHTTPRedirector ¶
func NewHTTPRedirector(lnetwork, laddr string) *HTTPRedirector
NewHTTPRedirector will create a new *HTTPRedirector with the given parameters. It uses Muxie for routing internally, and can be have its variables modified and accessed by anyone wishing to extend it.
func (*HTTPRedirector) Accept ¶
func (r *HTTPRedirector) Accept() (net.Conn, error)
Accept waits for a new net.Conn, and returns it, along with any error encountered.
func (*HTTPRedirector) AddRoute ¶
func (r *HTTPRedirector) AddRoute(pattern string, handler RedirectHandler)
AddRoute adds a new route to the HTTPRedirector, where traffic is handled by the specified RedirectHandler. Note that this will override the default RedirectHandler, or a RedirectHandler stored in the struct. It is recommended to have either all traffic handled by the RedirectHandler specified in the struct, or all traffic handled by custom handlers. In addition, the handler specified in the struct can be used as the "default", with any other handlers specified directly by calling this function.
func (*HTTPRedirector) Addr ¶
func (r *HTTPRedirector) Addr() net.Addr
Addr retusn the net.Addr of the internal net.Listener. It returns nil if the internal net.Listener is nil.
func (*HTTPRedirector) Close ¶
func (r *HTTPRedirector) Close() error
Close closes the internal net.Listener, and returns any error encountered.
func (*HTTPRedirector) Listen ¶
func (r *HTTPRedirector) Listen() error
Listen starts the HTTPRedirector on the previously specified network and address. It will return any error encountered starting the listener.
func (*HTTPRedirector) ListenAndServe ¶
func (r *HTTPRedirector) ListenAndServe() error
ListenAndServe starts the HTTPRedirector, and returns any error encountered during its lifetime. It is implemented by calling the Listen() and Serve() methods, and returning any error encountered by either call.
func (*HTTPRedirector) Serve ¶
func (r *HTTPRedirector) Serve() error
Serve starts the HTTPRedirector, and returns any error encountered during its lifetime.
type PatternNotPresentError ¶
type PatternNotPresentError struct {
Pattern string
}
PatternNotPresentError is an error type, which is returned when a pattern for a request URL is not found.
func (*PatternNotPresentError) Error ¶
func (e *PatternNotPresentError) Error() string
Error returns the error message for the PatternNotPresentError.
type RedirectHandler ¶
type RedirectHandler func(http.ResponseWriter, *http.Request)
RedirectHandler is a function which can be used to handle an HTTP request. It should redirect the request to a different URL, and return the status code of the redirect.
type TLSRedirector ¶
type TLSRedirector struct { StatusCode int // contains filtered or unexported fields }
TLSRedirector is a TLS redirector, which is a wrapper around an HTTPRedirector. It will automatically return a 301 redirect to HTTPS, unless a different status code is specified in the struct.
func NewTLSRedirector ¶
func NewTLSRedirector(lnetwork, laddr string) *TLSRedirector
NewTLSRedirector extends the HTTPRedirector to handle HTTP requests, and redirect them to HTTPS. It accepts the same parameters as NewHTTPRedirector, and returns a new *TLSRedirector.
func (*TLSRedirector) Close ¶
func (r *TLSRedirector) Close() error
Close closes the TLSRedirector, by closing the internal HTTPredirector. It will return any error encountered
func (*TLSRedirector) Listen ¶
func (r *TLSRedirector) Listen() error
Listen starts the internal HTTPRedirector on the previously specified network and address. It will return any error encuntered by starting the listener.
func (*TLSRedirector) ListenAndServe ¶
func (r *TLSRedirector) ListenAndServe() error
ListenAndServe starts the TLSRedirector, and returns any error encountered during its lifetime. It is implemented by calling the Listen and Serve functions, and returning any error encountered by either call.
func (*TLSRedirector) Serve ¶
func (r *TLSRedirector) Serve() error
Serve serves requests for the TLSRedirector, using the internal HTTPRedirector. It will return any error encountered during its lifetime.