-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathre_subst.go
44 lines (39 loc) · 858 Bytes
/
re_subst.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
package main
import (
"log"
"net/http"
"regexp"
"strings"
)
type ReSubst struct {
Re *regexp.Regexp
Template string
Target string
}
func NewReSubst(s string) *ReSubst {
sep := s[:1]
parts := strings.SplitN(s, sep, 4)
if len(parts) != 4 {
log.Fatalf("regexp substitution string not in format @regexp@template@: %#v", s)
}
return &ReSubst{
Re: regexp.MustCompile(parts[1]),
Template: parts[2],
Target: parts[3],
}
}
func (rs *ReSubst) Subst(src string) string {
dst := []byte{}
for _, submatch := range rs.Re.FindAllStringSubmatchIndex(src, -1) {
dst = rs.Re.ExpandString(dst, rs.Template, src, submatch)
}
return string(dst)
}
func (rs *ReSubst) SubstReq(r *http.Request) string {
switch rs.Target {
case "path", "":
return rs.Subst(r.URL.Path)
}
log.Printf("unknown target: %#v", rs.Target)
return ""
}