forked from txtdirect/txtdirect
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathplaceholders.go
150 lines (142 loc) · 5.08 KB
/
placeholders.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
148
149
150
/*
Copyright 2019 - The TXTDirect Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package txtdirect
import (
"fmt"
"net/http"
"net/url"
"path"
"reflect"
"regexp"
"strconv"
"strings"
)
// PlaceholderRegex finds the placeholders like {x}
var PlaceholderRegex = regexp.MustCompile("{[~>?$]?\\w+}")
// parsePlaceholders gets a string input and looks for placeholders inside
// the string. it will then replace them with the actual data from the request
func parsePlaceholders(input string, r *http.Request, pathSlice []string) (string, error) {
placeholders := PlaceholderRegex.FindAllStringSubmatch(input, -1)
for _, placeholder := range placeholders {
switch placeholder[0] {
case "{uri}":
input = strings.Replace(input, "{uri}", r.URL.RequestURI(), -1)
case "{dir}":
dir, _ := path.Split(r.URL.Path)
input = strings.Replace(input, "{dir}", dir, -1)
case "{file}":
_, file := path.Split(r.URL.Path)
input = strings.Replace(input, "{file}", file, -1)
case "{host}":
input = strings.Replace(input, "{host}", r.Host, -1)
case "{hostonly}":
// Removes port from host
host := r.Host
if strings.Contains(r.Host, ":") {
hostSlice := strings.Split(r.Host, ":")
host = hostSlice[0]
}
input = strings.Replace(input, "{hostonly}", host, -1)
case "{method}":
input = strings.Replace(input, "{method}", r.Method, -1)
case "{path}":
input = strings.Replace(input, "{path}", r.URL.Path, -1)
case "{path_escaped}":
input = strings.Replace(input, "{path_escaped}", url.QueryEscape(r.URL.Path), -1)
case "{port}":
input = strings.Replace(input, "{port}", r.URL.Port(), -1)
case "{query}":
input = strings.Replace(input, "{query}", r.URL.RawQuery, -1)
case "{query_escaped}":
input = strings.Replace(input, "{query_escaped}", url.QueryEscape(r.URL.RawQuery), -1)
case "{uri_escaped}":
input = strings.Replace(input, "{uri_escaped}", url.QueryEscape(r.URL.RequestURI()), -1)
case "{user}":
user, _, ok := r.BasicAuth()
if !ok {
input = strings.Replace(input, "{user}", "", -1)
}
input = strings.Replace(input, "{user}", user, -1)
}
/* For multi-level tlds such as "example.co.uk", "co" would be used as {label2},
"example" would be {label1} and "uk" would be {label3} */
if strings.HasPrefix(placeholder[0], "{label") {
nStr := placeholder[0][6 : len(placeholder[0])-1] // get the integer N in "{labelN}"
n, err := strconv.Atoi(nStr)
if err != nil {
return "", err
}
if n < 1 {
return "", fmt.Errorf("{label0} is not supported")
}
// Removes port from host
host := r.Host
if strings.Contains(r.Host, ":") {
hostSlice := strings.Split(r.Host, ":")
host = hostSlice[0]
}
labels := strings.Split(host, ".")
if n > len(labels) {
return "", fmt.Errorf("Cannot parse a label greater than %d", len(labels))
}
input = strings.Replace(input, placeholder[0], labels[n-1], -1)
}
if placeholder[0][1] == '>' {
want := placeholder[0][2 : len(placeholder[0])-1]
for key, values := range r.Header {
// Header placeholders (case-insensitive)
if strings.EqualFold(key, want) {
input = strings.Replace(input, placeholder[0], strings.Join(values, ","), -1)
}
}
}
if placeholder[0][1] == '~' {
name := placeholder[0][2 : len(placeholder[0])-1]
if cookie, err := r.Cookie(name); err == nil {
input = strings.Replace(input, placeholder[0], cookie.Value, -1)
}
}
if placeholder[0][1] == '?' {
query := r.URL.Query()
name := placeholder[0][2 : len(placeholder[0])-1]
input = strings.Replace(input, placeholder[0], query.Get(name), -1)
}
// Numbered Regex matches
if regexp.MustCompile("(\\d+)").MatchString(string(placeholder[0][1])) {
matches := r.Context().Value("regexMatches")
index, err := strconv.Atoi(string(placeholder[0][1]))
if err != nil {
return "", fmt.Errorf("couldn't get index of regex match")
}
input = strings.Replace(input, placeholder[0],
reflect.ValueOf(matches).Index(index-1).String(), -1)
}
// Named regex matches
if regexp.MustCompile("(\\$[a-zA-Z]+[0-9]*)").MatchString(string(placeholder[0][1 : len(placeholder[0])-1])) {
matches := r.Context().Value("regexMatches")
mapReflect := reflect.ValueOf(matches)
if mapReflect.Kind() == reflect.Map {
iterator := reflect.ValueOf(matches).MapRange()
for iterator.Next() {
if iterator.Key().String() == string(placeholder[0][2:len(placeholder[0])-1]) {
input = strings.Replace(input, placeholder[0], iterator.Value().String(), -1)
}
}
}
}
}
for k, v := range pathSlice {
input = strings.Replace(input, fmt.Sprintf("{$%d}", k+1), v, -1)
}
return input, nil
}