From e3125816430b582475511bc7afcc42022c998750 Mon Sep 17 00:00:00 2001 From: SpamNeko Date: Tue, 9 May 2017 23:17:25 +0100 Subject: [PATCH] wrapHandler for http.Handler to redirect to 404 handler --- router/router.go | 18 ++++++++-------- router/wrapHandler.go | 49 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+), 9 deletions(-) create mode 100644 router/wrapHandler.go diff --git a/router/router.go b/router/router.go index 7aa7fe7ee..998c2a0ba 100644 --- a/router/router.go +++ b/router/router.go @@ -56,21 +56,21 @@ func init() { Router = mux.NewRouter() // Routes - http.Handle("/css/", http.StripPrefix("/css/", gzipCSSHandler)) - http.Handle("/js/", http.StripPrefix("/js/", gzipJSHandler)) - http.Handle("/img/", http.StripPrefix("/img/", imgHandler)) + http.Handle("/css/", http.StripPrefix("/css/", wrapHandler(gzipCSSHandler))) + http.Handle("/js/", http.StripPrefix("/js/", wrapHandler(gzipJSHandler))) + http.Handle("/img/", http.StripPrefix("/img/", wrapHandler(imgHandler))) Router.Handle("/", gzipHomeHandler).Name("home") - Router.Handle("/page/{page:[0-9]+}", gzipHomeHandler).Name("home_page") + Router.Handle("/page/{page:[0-9]+}", wrapHandler(gzipHomeHandler)).Name("home_page") Router.Handle("/search", gzipSearchHandler).Name("search") Router.Handle("/search/{page}", gzipSearchHandler).Name("search_page") Router.Handle("/api", gzipAPIHandler).Methods("GET") - Router.Handle("/api/{page:[0-9]*}", gzipAPIHandler).Methods("GET") - Router.Handle("/api/view/{id}", gzipAPIViewHandler).Methods("GET") + Router.Handle("/api/{page:[0-9]*}", wrapHandler(gzipAPIHandler)).Methods("GET") + Router.Handle("/api/view/{id}", wrapHandler(gzipAPIViewHandler)).Methods("GET") Router.Handle("/api/upload", gzipAPIUploadHandler).Methods("POST") Router.Handle("/api/update", gzipAPIUpdateHandler).Methods("PUT") Router.Handle("/faq", gzipFaqHandler).Name("faq") Router.Handle("/feed", gzipRSSHandler).Name("feed") - Router.Handle("/view/{id}", gzipViewHandler).Methods("GET").Name("view_torrent") + Router.Handle("/view/{id}", wrapHandler(gzipViewHandler)).Methods("GET").Name("view_torrent") Router.HandleFunc("/view/{id}", PostCommentHandler).Methods("POST").Name("post_comment") Router.Handle("/upload", gzipUploadHandler).Name("upload") Router.Handle("/user/register", gzipUserRegisterFormHandler).Name("user_register").Methods("GET") @@ -79,9 +79,9 @@ func init() { Router.Handle("/user/register", gzipUserRegisterPostHandler).Name("user_register").Methods("POST") Router.Handle("/user/login", gzipUserLoginPostHandler).Name("user_login").Methods("POST") Router.Handle("/user/logout", gzipUserLogoutHandler).Name("user_logout") - Router.Handle("/user/{id}/{username}", gzipUserProfileHandler).Name("user_profile").Methods("GET") + Router.Handle("/user/{id}/{username}", wrapHandler(gzipUserProfileHandler)).Name("user_profile").Methods("GET") Router.Handle("/user/{id}/{username}/follow", gzipUserFollowHandler).Name("user_follow").Methods("GET") - Router.Handle("/user/{id}/{username}", gzipUserProfileFormHandler).Name("user_profile").Methods("POST") + Router.Handle("/user/{id}/{username}", wrapHandler(gzipUserProfileFormHandler)).Name("user_profile").Methods("POST") Router.Handle("/mod/", gzipIndexModPanel).Name("mod_index") Router.Handle("/mod/torrents", gzipTorrentsListPanel).Name("mod_tlist") diff --git a/router/wrapHandler.go b/router/wrapHandler.go new file mode 100644 index 000000000..d0170f44e --- /dev/null +++ b/router/wrapHandler.go @@ -0,0 +1,49 @@ +package router + +import ( + "net/http" +) + +type wrappedResponseWriter struct { + Rw http.ResponseWriter + Ignore bool +} + +func (wrw *wrappedResponseWriter) WriteHeader(status int) { + if status==404 { + wrw.Ignore=true + } else { + wrw.Rw.WriteHeader(status) + } +} + +func (wrw *wrappedResponseWriter) Write(p []byte) (int, error) { + if wrw.Ignore { + return 0, nil + } + return wrw.Rw.Write(p) +} + +func (wrw *wrappedResponseWriter) Header() http.Header { + return wrw.Rw.Header() +} + + +type wrappedHandler struct { + h http.Handler +} + +func (wh *wrappedHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { + wrw := wrappedResponseWriter{w, false} + wh.h.ServeHTTP(&wrw, r) + if wrw.Ignore==true { + wrw.Rw.Header().Del("Content-Encoding") + wrw.Rw.Header().Del("Vary") + wrw.Rw.Header().Set("Content-Type", "text/html; charset=utf-8") + NotFoundHandler(wrw.Rw, r) + } +} + +func wrapHandler(handler http.Handler) http.Handler { + return &wrappedHandler{handler} +} \ No newline at end of file