-
Notifications
You must be signed in to change notification settings - Fork 195
/
reqRequestRedirect.go
46 lines (40 loc) · 1.14 KB
/
reqRequestRedirect.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
package rules
import (
"fmt"
"github.com/haproxytech/client-native/v5/models"
"github.com/haproxytech/kubernetes-ingress/pkg/haproxy/api"
"github.com/haproxytech/kubernetes-ingress/pkg/utils"
)
type RequestRedirect struct {
Host string
RedirectCode int64
RedirectPort int
SSLRequest bool
SSLRedirect bool
}
func (r RequestRedirect) GetType() Type {
return REQ_REDIRECT
}
func (r RequestRedirect) Create(client api.HAProxyClient, frontend *models.Frontend, ingressACL string) error {
if frontend.Mode == "tcp" {
return fmt.Errorf("request redirection cannot be configured in TCP mode")
}
var rule string
if r.SSLRedirect {
rule = fmt.Sprintf("https://%%[hdr(host),field(1,:)]:%d%%[capture.req.uri]", r.RedirectPort)
} else {
scheme := "http"
if r.SSLRequest {
scheme = "https"
}
rule = fmt.Sprintf(scheme+"://%s%%[capture.req.uri]", r.Host)
}
httpRule := models.HTTPRequestRule{
Index: utils.PtrInt64(0),
Type: "redirect",
RedirCode: utils.PtrInt64(r.RedirectCode),
RedirValue: rule,
RedirType: "location",
}
return client.FrontendHTTPRequestRuleCreate(frontend.Name, httpRule, ingressACL)
}