hnakra/router/mux/http.go
2023-05-26 01:18:16 -04:00

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