wrapHandler for http.Handler to redirect to 404 handler
Cette révision appartient à :
Parent
fb2b171d11
révision
e312581643
2 fichiers modifiés avec 58 ajouts et 9 suppressions
|
@ -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")
|
||||
|
|
49
router/wrapHandler.go
Fichier normal
49
router/wrapHandler.go
Fichier normal
|
@ -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}
|
||||
}
|
Référencer dans un nouveau ticket