Albirew/nyaa-pantsu
Archivé
1
0
Bifurcation 0
Ce dépôt a été archivé le 2022-05-07. Vous pouvez voir ses fichiers ou le cloner, mais pas ouvrir de ticket ou de demandes d'ajout, ni soumettre de changements.
nyaa-pantsu/router/wrap_handler.go
tomleb 7eee47b0d3 Fix bug, remove literals (#629)
* Use ModeratorDir variable

* Rename cookieHelper to cookie_helper for consistency

* Use named constant instead of literals

* Fix ability to upload when uploads are disabled

The old code let people upload under the right conditions when uploads
were disabled. (ie: User is banned and config.AdminAreStillAllowedTo is
false)

* Increase timeout (fixes #517)

* Fix inconsistent indentation *.{js, css} (fix #583)

* Fix negative page

Temporary fix. The issue was that going to a negative page caused the
sql query to have a negative offset. This caused an error in the
database query.

We need to cleanup this code, but this will work for now.

* Fix wrong PG_DATA directory due to upgrade to 9.6

* Add server status link to FAQ

* Fix failing tests

* Clarify group_vars/all and hosts doc

* Add a wrapper to protect /mod route

* Fix login page not showing form errors
2017-05-20 09:10:16 +10:00

64 lignes
1,5 Kio
Go

package router
import (
"net/http"
"github.com/NyaaPantsu/nyaa/service/user/permission"
)
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}
}
// Make sure the user is a moderator, otherwise return forbidden
// TODO Clean this
func WrapModHandler(handler func (w http.ResponseWriter, r *http.Request)) (func (w http.ResponseWriter, r *http.Request)) {
return func (w http.ResponseWriter, r *http.Request) {
currentUser := GetUser(r)
if userPermission.HasAdmin(currentUser) {
handler(w, r)
} else {
http.Error(w, "admins only", http.StatusForbidden)
}
}
}