-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathfilter.go
More file actions
52 lines (43 loc) · 1.23 KB
/
Copy pathfilter.go
File metadata and controls
52 lines (43 loc) · 1.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package trygo
import (
"errors"
"net/http"
)
func DefaultFilterHandler(app *App, h http.Handler) http.Handler {
h = BodyLimitHandler(app, h)
if app.Config.StatinfoEnable {
h = RequestStatHandler(app, h)
}
return h
}
func RequestStatHandler(app *App, handler http.Handler) http.Handler {
return &requestStatHandler{app, handler}
}
type requestStatHandler struct {
app *App
handler http.Handler
}
func (h *requestStatHandler) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
h.app.Statinfo.incCurrentRequests()
defer h.app.Statinfo.decCurrentRequests()
h.handler.ServeHTTP(rw, r)
}
func BodyLimitHandler(app *App, handler http.Handler) http.Handler {
return &bodyLimitHandler{app, handler}
}
type bodyLimitHandler struct {
app *App
handler http.Handler
}
var ErrBodyTooLarge = errors.New("http: request body too large")
func (h *bodyLimitHandler) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
if r.ContentLength > h.app.Config.MaxRequestBodySize {
h.app.Logger.Info("%s", buildLoginfo(r, ErrBodyTooLarge))
Error(rw, ErrBodyTooLarge.Error(), http.StatusRequestEntityTooLarge)
return
}
if r.Body != nil {
r.Body = http.MaxBytesReader(rw, r.Body, h.app.Config.MaxRequestBodySize)
}
h.handler.ServeHTTP(rw, r)
}