Documentation ¶
Index ¶
- Constants
- Variables
- func CleanPreserve(urlpath string) string
- func JoinPreserve(add ...string) string
- func JoinURLQuery(q1, q2 string) string
- func LogRequest(log logger.Printer, r *http.Request)
- func LooselyGetHost(hostport string) string
- func OptionallyJoinHostPort(host string, port int) string
- func RequestURL(req *http.Request) *url.URL
- func SplitHostPort(hostport string) (host, port string, err error)
- type Dumper
- type Flags
- type FuncHandler
- type HostDispatch
- type HostDispatcher
- type Server
Examples ¶
Constants ¶
const DefaultPort = 9999
Variables ¶
var DefaultCache = configdir.LocalCache("enkit-certs")
Functions ¶
func CleanPreserve ¶
CleanPreserve cleans an URL path (eg, eliminating .., //, useless . and so on) while preserving the '/' at the end of the path (path.Clean eliminates trailing /) and returning an empty string "" instead of . for an empty path.
func JoinPreserve ¶
JoinPreserve joins multiple path fragments with one another, while preserving the final '/', if any. JoinPreserve internally calls path.Clean.
func JoinURLQuery ¶
JoinURLQuery takes two escaped query strings (eg, what follows after the ? in a URL) and joins them into one query string.
func LogRequest ¶
LogRequest will simply log the important parts of an http request.
func LooselyGetHost ¶
LooselyGetHost is a version of net.SplitHostPort which performs no checks, and returns the host part only of an hostport pair.
func OptionallyJoinHostPort ¶
OptionallyJoinHostPort is like net.JoinHostPort, but the port is added only if != 0.
func RequestURL ¶
RequestURL approximates the URL the browser requested from an http.Request.
Note that RequestURL can only return an approximation: it assumes that if the connection was encrypted it must have been done using https, while if it wasn't, it must have been done via HTTP.
Further, most modern deployments rely on reverse proxies and load balancers. Any one of those things may end up mingling with the request headers, so by the time RequestURL is called, who knows what the browser actually supplied.
func SplitHostPort ¶
SplitHostPort is like net.SplitHostPort, but the port is optional. If no port is specified, an empty string will be returned.
Example ¶
fmt.Println(SplitHostPort("1.2.3.4:53")) fmt.Println(SplitHostPort("1.2.3.4")) fmt.Println(SplitHostPort(":53")) fmt.Println(SplitHostPort(":")) // Taken to be a host, no port. fmt.Println(SplitHostPort("53")) // Strings are supported, as they can be resolved into // numbers using net.LookupPort and similar. fmt.Println(SplitHostPort("server:ssh")) // IPv6 is well supported. fmt.Println(SplitHostPort("[::1]:12"))
Output: 1.2.3.4 53 <nil> 1.2.3.4 <nil> 53 <nil> <nil> 53 <nil> server ssh <nil> ::1 12 <nil>
Types ¶
type Dumper ¶
Dumper is an http.Handler capable of logging the content of the request.
Use it anywhere an http.Handler would be accepted (eg, with http.Serve, http.Handle, wrapping a Mux, ...).
Example:
mux := http.NewServeMux() ... http.ListenAndServe(":8080", &Dumper{Real: mux, Log: log.Printf})
type Flags ¶
type Flags struct { HttpPort int HttpAddress string HttpsPort int HttpsAddress string Cache string }
func DefaultFlags ¶
func DefaultFlags() *Flags
type FuncHandler ¶
type FuncHandler func(w http.ResponseWriter, r *http.Request)
type HostDispatch ¶
type HostDispatch struct { // Which host the client has to request in the Host HTTP header for the Handler // below to be invoked. If empty, the handler will be invoked for every request // for which a direct match cannot be found. // // Matching is case insensitive. Domains with a period appended are considered // equivalent to domains without a period. Example: www.mydomain.com is considered // equivalent to www.mydomain.com. (trailing period). // // The Host string can also specify a port number for example: www.mydomain.com:1001. // Port 80 or 443 are stripped by default and matched without port, as the RFC // recommandation is that port 80 and 443 are to be stripped in host headers. Host string // Handler is the handler to invoke for all requests matching this host. Handler http.Handler }
HostDispatch mapping. Maps a string to a http handler.
If the host is empty, the corresponding http.Handler will be invoked when the host is unknown, as well as for every host that has not been configured explicitly.
type HostDispatcher ¶
HostDispatcher is an http.Handler (implements ServeHTTP) that invokes a different http.Handler based on the host specified in the HTTP request.
It complements http.ServeMUX in the sense that http.ServeMUX invokes different http.Handler based on the path of the request, while HostDispatcher invokes them based on the host header.
By combining the two, you can easily create handlers with multiple virtual hosts involved.
func NewHostDispatcher ¶
func NewHostDispatcher(todispatch []HostDispatch) (HostDispatcher, error)
NewHostDispatcher creates a new HostDispatcher.
func (HostDispatcher) ServeHTTP ¶
func (hd HostDispatcher) ServeHTTP(w http.ResponseWriter, r *http.Request)
type Server ¶
func DefaultServer ¶
func DefaultServer() *Server