forked from evolves-fr/traefik-plugin-redirect
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugin.go
131 lines (111 loc) · 2.95 KB
/
plugin.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
package traefik_plugin_redirect
import (
"context"
"net/http"
"net/url"
"strconv"
"strings"
)
const (
schemeHTTP = "http"
schemeHTTPS = "https"
)
// Config the plugin configuration.
type Config struct {
Debug bool `json:"debug,omitempty" yaml:"debug,omitempty"`
Redirects []string `json:"redirects,omitempty" yaml:"redirects,omitempty"`
}
// CreateConfig creates the default plugin configuration.
func CreateConfig() *Config {
return &Config{}
}
// Plugin this is a Traefik redirect plugin.
type Plugin struct {
next http.Handler
name string
debug bool
redirects []redirect
}
type redirect struct {
Source string `json:"source,omitempty"`
Destination string `json:"destination,omitempty"`
StatusCode int `json:"statusCode,omitempty"`
}
// New created a new plugin.
func New(ctx context.Context, next http.Handler, config *Config, name string) (http.Handler, error) {
plugin := &Plugin{
next: next,
name: name,
debug: config.Debug,
redirects: make([]redirect, 0),
}
for _, cfg := range config.Redirects {
parts := strings.Split(cfg, ":")
if len(parts) < 2 {
continue
}
r := redirect{
Source: parts[0],
Destination: parts[1],
StatusCode: 302,
}
if len(parts) > 2 {
if statusCode, err := strconv.Atoi(parts[2]); err == nil {
r.StatusCode = statusCode
}
}
plugin.redirects = append(plugin.redirects, r)
}
return plugin, nil
}
func (p *Plugin) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
// If no redirection registered, skip to the next handler.
if p.redirects == nil || len(p.redirects) == 0 {
p.next.ServeHTTP(rw, req)
return
}
// Loop all redirections
for _, r := range p.redirects {
if r.Source == req.RequestURI {
// Add headers for debug
if p.debug {
rw.Header().Set("X-Middleware-Name", p.name)
rw.Header().Set("X-Middleware-Source", r.Source)
rw.Header().Set("X-Middleware-Destination", r.Destination)
rw.Header().Set("X-Middleware-StatusCode", strconv.Itoa(r.StatusCode))
rw.Header().Set("X-Middleware-Old-URL", req.RequestURI)
}
u, err := url.Parse(r.Destination)
if err != nil {
continue
}
// Check if identical url, and redirect
handler := &moveHandler{location: u, statusCode: r.StatusCode}
handler.ServeHTTP(rw, req)
return
}
}
p.next.ServeHTTP(rw, req)
}
type moveHandler struct {
location *url.URL
statusCode int
}
func (m *moveHandler) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
rw.Header().Set("Location", m.location.String())
status := m.statusCode
if status == 0 {
status = http.StatusFound
if req.Method != http.MethodGet {
status = http.StatusTemporaryRedirect
}
}
if req.Method != http.MethodGet && status == http.StatusMovedPermanently {
status = http.StatusPermanentRedirect
}
rw.WriteHeader(status)
_, err := rw.Write([]byte(http.StatusText(status)))
if err != nil {
http.Error(rw, err.Error(), http.StatusInternalServerError)
}
}