40 lines
889 B
Go
40 lines
889 B
Go
package mux
|
|
|
|
import "net/url"
|
|
import "net/http"
|
|
|
|
// HTTP is an HTTP request multiplexer.
|
|
type HTTP struct {
|
|
Mux[http.Handler]
|
|
}
|
|
|
|
// NewHTTP creates a new HTTP request multiplexer.
|
|
func NewHTTP (resolver Resolver) *HTTP {
|
|
mux := &HTTP { }
|
|
mux.Mux.Redirect = mux.newRedirect
|
|
mux.Mux.NotFound = mux.newNotFound
|
|
mux.Mux.Resolver = resolver
|
|
return mux
|
|
}
|
|
|
|
func (mux *HTTP) ServeHTTP (res http.ResponseWriter, req *http.Request) {
|
|
if req.RequestURI == "*" {
|
|
if req.ProtoAtLeast(1, 1) {
|
|
res.Header().Set("Connection", "close")
|
|
}
|
|
res.WriteHeader(http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
handler, _ := mux.Handler(req.URL)
|
|
handler.ServeHTTP(res, req)
|
|
}
|
|
|
|
func (mux *HTTP) newNotFound (where *url.URL) http.Handler {
|
|
return http.NotFoundHandler()
|
|
}
|
|
|
|
func (mux *HTTP) newRedirect (where *url.URL) http.Handler {
|
|
return http.RedirectHandler(where.String(), http.StatusMovedPermanently)
|
|
}
|