khttp

package
v0.0.0-...-5206ba3 Latest Latest
Warning

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

Go to latest
Published: Apr 30, 2024 License: BSD-3-Clause, BSD-3-Clause Imports: 14 Imported by: 0

Documentation

Index

Examples

Constants

View Source
const DefaultPort = 9999

Variables

View Source
var DefaultCache = configdir.LocalCache("enkit-certs")

Functions

func CleanPreserve

func CleanPreserve(urlpath string) string

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

func JoinPreserve(add ...string) string

JoinPreserve joins multiple path fragments with one another, while preserving the final '/', if any. JoinPreserve internally calls path.Clean.

func JoinURLQuery

func JoinURLQuery(q1, q2 string) string

JoinURLQuery takes two escaped query strings (eg, what follows after the ? in a URL) and joins them into one query string.

func LogRequest

func LogRequest(log logger.Printer, r *http.Request)

LogRequest will simply log the important parts of an http request.

func LooselyGetHost

func LooselyGetHost(hostport string) string

LooselyGetHost is a version of net.SplitHostPort which performs no checks, and returns the host part only of an hostport pair.

func OptionallyJoinHostPort

func OptionallyJoinHostPort(host string, port int) string

OptionallyJoinHostPort is like net.JoinHostPort, but the port is added only if != 0.

func RequestURL

func RequestURL(req *http.Request) *url.URL

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

func SplitHostPort(hostport string) (host, port string, err error)

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

type Dumper struct {
	Real http.Handler
	Log  logger.Printer
}

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})

func (*Dumper) ServeHTTP

func (d *Dumper) ServeHTTP(w http.ResponseWriter, r *http.Request)

type Flags

type Flags struct {
	HttpPort    int
	HttpAddress string

	HttpsPort    int
	HttpsAddress string

	Cache string
}

func DefaultFlags

func DefaultFlags() *Flags

func (*Flags) Register

func (f *Flags) Register(set kflags.FlagSet, prefix string) *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

type HostDispatcher map[string]http.Handler

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

type Server struct {
	HttpAddress  string
	HttpsAddress string
	CacheDir     string
}

func DefaultServer

func DefaultServer() *Server

func FromFlags

func FromFlags(flags *Flags) (*Server, error)

func (*Server) Run

func (p *Server) Run(log logger.Printer, handler http.Handler, domains ...string) error

Directories

Path Synopsis
Collection of utilities to more easily compose cookies.
Collection of utilities to more easily compose cookies.

Jump to

Keyboard shortcuts

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