From 894614cd548de195da0b9270158a52ea389cdaa1 Mon Sep 17 00:00:00 2001 From: Ankit Patial Date: Mon, 4 Nov 2024 11:00:02 +0530 Subject: [PATCH] default options handler --- README.md | 8 ++++---- example/main.go | 8 ++++---- router.go | 3 --- router_options_handler.go | 21 --------------------- router_serve.go | 20 ++++++++++++++++++++ 5 files changed, 28 insertions(+), 32 deletions(-) delete mode 100644 router_options_handler.go diff --git a/README.md b/README.md index 4f7e0fd..b5d490e 100644 --- a/README.md +++ b/README.md @@ -27,7 +27,7 @@ func main() { r.Use(middleware1, middleware2) // let's add a route - r.Get("/hello", func(w http.ResponseWriter, r *http.Request) { + r.GET("/hello", func(w http.ResponseWriter, r *http.Request) { w.Write([]byte("i am route /hello")) }) // r.Post(pattern string, h http.HandlerFunc) @@ -37,7 +37,7 @@ func main() { // you can inline middleware(s) to a route r. With(mwInline). - Get("/hello-2", func(w http.ResponseWriter, r *http.Request) { + GET("/hello-2", func(w http.ResponseWriter, r *http.Request) { w.Write([]byte("i am route /hello-2 with my own middleware")) }) @@ -64,13 +64,13 @@ func main() { // create a group of few routes with their own middlewares r.Group(func(grp *mux.Router) { grp.Use(mwGroup) - grp.Get("/group", func(w http.ResponseWriter, r *http.Request) { + grp.GET("/group", func(w http.ResponseWriter, r *http.Request) { w.Write([]byte("i am route /group")) }) }) // catches all - r.Get("/", func(w http.ResponseWriter, r *http.Request) { + r.GET("/", func(w http.ResponseWriter, r *http.Request) { w.Write([]byte("hello there")) }) diff --git a/example/main.go b/example/main.go index d95ac93..1e3de9b 100644 --- a/example/main.go +++ b/example/main.go @@ -25,7 +25,7 @@ func main() { r.Use(middleware1, middleware2) // let's add a route - r.Get("/hello", func(w http.ResponseWriter, r *http.Request) { + r.GET("/hello", func(w http.ResponseWriter, r *http.Request) { w.Write([]byte("i am route /hello")) }) // r.Post(pattern string, h http.HandlerFunc) @@ -35,7 +35,7 @@ func main() { // you can inline middleware(s) to a route r. With(mwInline). - Get("/hello-2", func(w http.ResponseWriter, r *http.Request) { + GET("/hello-2", func(w http.ResponseWriter, r *http.Request) { w.Write([]byte("i am route /hello-2 with my own middleware")) }) @@ -62,13 +62,13 @@ func main() { // create a group of few routes with their own middlewares r.Group(func(grp *mux.Router) { grp.Use(mwGroup) - grp.Get("/group", func(w http.ResponseWriter, r *http.Request) { + grp.GET("/group", func(w http.ResponseWriter, r *http.Request) { w.Write([]byte("i am route /group")) }) }) // catches all - r.Get("/", func(w http.ResponseWriter, r *http.Request) { + r.GET("/", func(w http.ResponseWriter, r *http.Request) { w.Write([]byte("hello there")) }) diff --git a/router.go b/router.go index 5a4d546..f8b05f4 100644 --- a/router.go +++ b/router.go @@ -17,9 +17,6 @@ func NewRouter() *Router { r := &Router{ mux: http.NewServeMux(), } - - // catch all options - r.mux.Handle("OPTIONS /", optionsHandler{}) return r } diff --git a/router_options_handler.go b/router_options_handler.go deleted file mode 100644 index 223ecfa..0000000 --- a/router_options_handler.go +++ /dev/null @@ -1,21 +0,0 @@ -package mux - -import ( - "io" - "net/http" -) - -type optionsHandler struct{} - -func (optionsHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { - w.Header().Set("Content-Length", "0") - if r.ContentLength != 0 { - // Read up to 4KB of OPTIONS body (as mentioned in the - // spec as being reserved for future use), but anything - // over that is considered a waste of server resources - // (or an attack) and we abort and close the connection, - // courtesy of MaxBytesReader's EOF behavior. - mb := http.MaxBytesReader(w, r.Body, 4<<10) - io.Copy(io.Discard, mb) - } -} diff --git a/router_serve.go b/router_serve.go index 1c3338a..76ba5ae 100644 --- a/router_serve.go +++ b/router_serve.go @@ -3,6 +3,7 @@ package mux import ( "context" "errors" + "io" "log/slog" "net/http" "os" @@ -13,6 +14,10 @@ type ServeCB func(srv *http.Server) error // Serve with graceful shutdown func (r *Router) Serve(cb ServeCB) { + // catch all options + // lets get it thorugh all middlewares + r.mux.Handle("OPTIONS /", optionsHandler{}) + srv := &http.Server{ Handler: r, } @@ -40,3 +45,18 @@ func (r *Router) Serve(cb ServeCB) { <-idleConnsClosed } + +type optionsHandler struct{} + +func (optionsHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Length", "0") + if r.ContentLength != 0 { + // Read up to 4KB of OPTIONS body (as mentioned in the + // spec as being reserved for future use), but anything + // over that is considered a waste of server resources + // (or an attack) and we abort and close the connection, + // courtesy of MaxBytesReader's EOF behavior. + mb := http.MaxBytesReader(w, r.Body, 4<<10) + io.Copy(io.Discard, mb) + } +}