-
Notifications
You must be signed in to change notification settings - Fork 0
/
shield.go
77 lines (72 loc) · 2.23 KB
/
shield.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
// Package shield provides a net/http compatible middleware which blocks or allows request based on a predicate.
//
// Usage:
// package main
//
// import (
// "net/http"
//
// "github.com/psampaz/shield"
// )
//
// func main() {
//
// shieldMiddleware := shield.New(shield.Options{
// Block: func(r *http.Request) bool {
// return r.Method != "GET"
// },
// Code: http.StatusMethodNotAllowed,
// Headers: http.Header{"Content-Type": {"text/plain"}},
// Body: []byte(http.StatusText(http.StatusMethodNotAllowed)),
// })
//
// helloWorldHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// w.Write([]byte("hello world"))
// })
//
// http.ListenAndServe(":8080", shieldMiddleware.Handler(helloWorldHandler))
// }
package shield
import "net/http"
// Options holds configuration params for the shield
type Options struct {
// Block is a predicate responsible for blocking the request.
// Return true when the request should be blocked, false otherwise
Block func(r *http.Request) bool
// Code is the status code of the response, sent when the request is blocked
Code int
// Headers are the headers of the response, sent when the request is blocked
Headers http.Header
// Body is the body of the response, sent when the request is blocked
Body []byte
}
// Shield is an net/http compatible middleware
type Shield struct {
options Options
}
// New creates a new Shield from Options
func New(o Options) *Shield {
return &Shield{options: o}
}
// Handler middleware blocks a request based on a user defined predicate. When the request is blocked,
// the middleware responses back with user defined headers, status code and body. When the request is not blocked,
// the middleware just calls the next handler in the chain without altering the request.
func (s *Shield) Handler(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if s.options.Block(r) {
for header, values := range s.options.Headers {
for idx, value := range values {
if idx == 0 {
w.Header().Set(header, value)
} else {
w.Header().Add(header, value)
}
}
}
w.WriteHeader(s.options.Code)
w.Write(s.options.Body)
return
}
next.ServeHTTP(w, r)
})
}