-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
rewrite.go
57 lines (50 loc) · 1.28 KB
/
rewrite.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
package rest
import (
"context"
"net/http"
"net/url"
"path"
"regexp"
"strings"
)
// Rewrite middleware with from->to rule. Supports regex (like nginx) and prevents multiple rewrites
// example: Rewrite(`^/sites/(.*)/settings/$`, `/sites/settings/$1`
func Rewrite(from, to string) func(http.Handler) http.Handler {
reFrom := regexp.MustCompile(from)
f := func(next http.Handler) http.Handler {
fn := func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
// prevent double rewrites
if ctx != nil {
if _, ok := ctx.Value(contextKey("rewrite")).(bool); ok {
next.ServeHTTP(w, r)
return
}
}
if !reFrom.MatchString(r.URL.Path) {
next.ServeHTTP(w, r)
return
}
ru := reFrom.ReplaceAllString(r.URL.Path, to)
cru := path.Clean(ru)
if strings.HasSuffix(ru, "/") { // don't drop trailing slash
cru += "/"
}
u, e := url.Parse(cru)
if e != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
r.Header.Set("X-Original-URL", r.URL.RequestURI())
r.URL.Path = u.Path
r.URL.RawPath = u.RawPath
if u.RawQuery != "" {
r.URL.RawQuery = u.RawQuery
}
ctx = context.WithValue(ctx, contextKey("rewrite"), true)
next.ServeHTTP(w, r.WithContext(ctx))
}
return http.HandlerFunc(fn)
}
return f
}