hnakra/router/mux/http.go

38 lines
798 B
Go

package mux
import "net/url"
import "net/http"
type HTTP struct {
Mux[http.Handler]
}
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)
}