forked from echocat/caddy-filter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathruleReplaceAction.go
147 lines (134 loc) · 3.88 KB
/
ruleReplaceAction.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package filter
import (
"fmt"
"log"
"net/http"
"os"
"regexp"
"strconv"
"strings"
"time"
)
var paramReplacementPattern = regexp.MustCompile("\\{[a-zA-Z0-9_\\-.]+}")
type ruleReplaceAction struct {
request *http.Request
responseHeader *http.Header
searchPattern *regexp.Regexp
replacement []byte
}
func (instance *ruleReplaceAction) replacer(input []byte) []byte {
pattern := instance.searchPattern
if pattern == nil {
return input
}
rawReplacement := instance.replacement
if len(rawReplacement) <= 0 {
return []byte{}
}
groups := pattern.FindSubmatch(input)
replacement := paramReplacementPattern.ReplaceAllFunc(rawReplacement, func(input2 []byte) []byte {
return instance.paramReplacer(input2, groups)
})
return replacement
}
func (instance *ruleReplaceAction) paramReplacer(input []byte, groups [][]byte) []byte {
if len(input) < 3 {
return input
}
name := string(input[1 : len(input)-1])
if index, err := strconv.Atoi(name); err == nil {
if index >= 0 && index < len(groups) {
return groups[index]
}
return input
}
if value, ok := instance.contextValueBy(name); ok {
return []byte(value)
}
return input
}
func (instance *ruleReplaceAction) contextValueBy(name string) (string, bool) {
if strings.HasPrefix(name, "request_") {
return instance.contextRequestValueBy(name[8:])
}
if strings.HasPrefix(name, "response_") {
return instance.contextResponseValueBy(name[9:])
}
if strings.HasPrefix(name, "env_") {
return instance.contextEnvironmentValueBy(name[4:])
}
if name == "now" {
return instance.contextNowValueBy("")
}
if strings.HasPrefix(name, "now:") {
return instance.contextNowValueBy(name[4:])
}
return "", false
}
func (instance *ruleReplaceAction) contextRequestValueBy(name string) (string, bool) {
request := instance.request
if strings.HasPrefix(name, "header_") {
return request.Header.Get(name[7:]), true
}
switch name {
case "url":
return request.URL.String(), true
case "path":
return request.URL.Path, true
case "method":
return request.Method, true
case "host":
return request.Host, true
case "proto":
return request.Proto, true
case "remoteAddress":
return request.RemoteAddr, true
}
return "", false
}
func (instance *ruleReplaceAction) contextResponseValueBy(name string) (string, bool) {
if name == "header_last_modified" || name == "header_last-modified" {
return instance.contextLastModifiedValueBy("")
}
if strings.HasPrefix(name, "header_last_modified:") || strings.HasPrefix(name, "header_last-modified:") {
return instance.contextLastModifiedValueBy(name[21:])
}
if strings.HasPrefix(name, "header_") {
return (*instance.responseHeader).Get(name[7:]), true
}
return "", false
}
func (instance *ruleReplaceAction) contextEnvironmentValueBy(name string) (string, bool) {
return os.Getenv(name), true
}
func (instance *ruleReplaceAction) contextNowValueBy(pattern string) (string, bool) {
return instance.formatTimeBy(time.Now(), pattern), true
}
func (instance *ruleReplaceAction) contextLastModifiedValueBy(pattern string) (string, bool) {
plain := instance.responseHeader.Get("last-Modified")
if plain == "" {
// Fallback to now
return instance.contextNowValueBy(pattern)
}
t, err := time.Parse(time.RFC1123, plain)
if err != nil {
log.Printf("[WARN] Serving illegal 'Last-Modified' header value '%v' for '%v': Got: %v", plain, instance.request.URL, err)
// Fallback to now
return instance.contextNowValueBy(pattern)
}
return instance.formatTimeBy(t, pattern), true
}
func (instance *ruleReplaceAction) formatTimeBy(t time.Time, pattern string) string {
if pattern == "" || pattern == "RFC" || pattern == "RFC3339" {
return t.Format(time.RFC3339)
}
if pattern == "unix" {
return fmt.Sprintf("%d", t.Unix())
}
if pattern == "timestamp" {
stamp := t.Unix() * 1000
stamp += int64(t.Nanosecond()) / int64(1000000)
return fmt.Sprintf("%d", stamp)
}
return t.Format(pattern)
}