-
-
Notifications
You must be signed in to change notification settings - Fork 32
/
handler.go
119 lines (96 loc) · 3.34 KB
/
handler.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
// Copyright Dwi Siswanto and/or licensed to Dwi Siswanto under one
// or more contributor license agreements. Licensed under the Elastic License 2.0;
// you may not use this file except in compliance with the Elastic License 2.0.
// See the LICENSE-ELASTIC file in the project root for more information.
package teler
import (
"net/http"
"github.com/caddyserver/caddy/v2/modules/caddyhttp"
"github.com/valyala/fasttemplate"
)
// rejectHandler is default rejection handler
func rejectHandler(w http.ResponseWriter, r *http.Request) {
// Set Content-Type to text/html
w.Header().Set("Content-Type", "text/html")
// Set the status code
w.WriteHeader(respStatus)
// Set template interfaces
data := map[string]any{
// NOTE(dwisiswant0): Should we include *http.Request?
"ID": w.Header().Get(xTelerReqId),
"message": w.Header().Get(xTelerMsg),
"threat": w.Header().Get(xTelerThreat),
}
// Use custom response HTML page template if non-empty
if customHTMLResponse != "" {
respTemplate = customHTMLResponse
}
// Parse response template
tpl := fasttemplate.New(respTemplate, "{{", "}}")
// Write a response from the template
// TODO(dwisiswant0): Add error handling here.
_, _ = tpl.Execute(w, data)
}
// SetHandler sets the handler to call when the teler rejects a request.
func (t *Teler) SetHandler(handler http.Handler) {
t.handler = handler
}
// Handler implements the http.HandlerFunc for integration with the standard net/http library.
func (t *Teler) Handler(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Let teler analyze the request. If it returns an error,
// that indicates the request should not continue.
k, err := t.analyzeRequest(w, r)
if err != nil {
// Process the analyzeRequest
t.postAnalyze(w, r, k, err)
return
}
h.ServeHTTP(w, r)
})
}
// CaddyHandler is a special HTTP handler implementation for Caddy.
func (t *Teler) CaddyHandler(h caddyhttp.Handler) caddyhttp.HandlerFunc {
return caddyhttp.HandlerFunc(func(w http.ResponseWriter, r *http.Request) error {
// Let teler analyze the request. If it returns an error,
// that indicates the request should not continue.
k, err := t.analyzeRequest(w, r)
if err != nil {
// Process the analyzeRequest
t.postAnalyze(w, r, k, err)
return err
}
return h.ServeHTTP(w, r)
})
}
// HandlerFuncWithNext is a special implementation for Negroni, but could be used elsewhere.
func (t *Teler) HandlerFuncWithNext(w http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
// Let teler analyze the request. If it returns an error,
// that indicates the request should not continue.
k, err := t.analyzeRequest(w, r)
if err != nil {
// Process the analyzeRequest
t.postAnalyze(w, r, k, err)
return
}
// If next handler is not nil, call it.
if next != nil {
next(w, r)
}
}
// CaddyHandlerFuncWithNext is a special implementation for Caddy.
func (t *Teler) CaddyHandlerFuncWithNext(w http.ResponseWriter, r *http.Request, next caddyhttp.HandlerFunc) error {
// Let teler analyze the request. If it returns an error,
// that indicates the request should not continue.
k, err := t.analyzeRequest(w, r)
if err != nil {
// Process the analyzeRequest
t.postAnalyze(w, r, k, err)
return err
}
// If next handler is not nil, call it.
if next != nil {
return next(w, r)
}
return nil
}