-
Notifications
You must be signed in to change notification settings - Fork 75
/
middleware.go
92 lines (78 loc) · 2.63 KB
/
middleware.go
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
// Copyright (c) 2021 The Decred developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package main
import (
"fmt"
"net/http"
"net/http/httputil"
"runtime/debug"
"time"
v2 "github.com/decred/politeia/politeiad/api/v2"
"github.com/decred/politeia/util"
)
// closeBodyMiddleware closes the request body.
func closeBodyMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
next.ServeHTTP(w, r)
r.Body.Close()
})
}
// loggingMiddleware logs all incoming commands before calling the next
// function.
func loggingMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Trace incoming request
log.Tracef("%v", newLogClosure(func() string {
trace, err := httputil.DumpRequest(r, true)
if err != nil {
trace = []byte(fmt.Sprintf("logging: "+
"DumpRequest %v", err))
}
return string(trace)
}))
// Log incoming connection
log.Infof("%v %v %v %v", util.RemoteAddr(r), r.Method, r.URL, r.Proto)
// Call next handler
next.ServeHTTP(w, r)
})
}
// recoverMiddleware recovers from any panics by logging the panic and
// returning a 500 response.
func recoverMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Defer the function so that it gets executed when the request
// is being closed out, not when its being opened.
defer func() {
if err := recover(); err != nil {
errorCode := time.Now().Unix()
log.Criticalf("%v %v %v %v Internal error %v: %v",
util.RemoteAddr(r), r.Method, r.URL, r.Proto, errorCode, err)
log.Criticalf("Stacktrace (THIS IS AN ACTUAL PANIC): %s",
debug.Stack())
util.RespondWithJSON(w, http.StatusInternalServerError,
v2.ServerErrorReply{
ErrorCode: errorCode,
})
}
}()
next.ServeHTTP(w, r)
})
}
// middleware contains the middleware that use configurable settings.
type middleware struct {
reqBodySizeLimit int64 // In bytes
}
// reqBodySizeLimitMiddleware applies a maximum request body size limit to
// requests.
//
// NOTE: This will only cause an error if the request body is read by the
// request handler, e.g. the JSON from a POST request is decoded into a struct.
func (m *middleware) reqBodySizeLimitMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
log.Tracef("Applying a max body size of %v bytes to the request body",
m.reqBodySizeLimit)
r.Body = http.MaxBytesReader(w, r.Body, m.reqBodySizeLimit)
next.ServeHTTP(w, r)
})
}