-
Notifications
You must be signed in to change notification settings - Fork 228
Expand file tree
/
Copy pathreqRequestRedirect.go
More file actions
70 lines (60 loc) · 1.67 KB
/
Copy pathreqRequestRedirect.go
File metadata and controls
70 lines (60 loc) · 1.67 KB
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
package rules
import (
"errors"
"fmt"
"github.com/haproxytech/client-native/v6/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
}
const DefaultHTTPSPort = 443
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 errors.New("request redirection cannot be configured in TCP mode")
}
var httpRule models.HTTPRequestRule
if r.SSLRedirect {
httpRule = r.sslRedirect()
} else {
httpRule = r.hostRedirect()
}
return client.FrontendHTTPRequestRuleCreate(0, frontend.Name, httpRule, ingressACL)
}
func (r RequestRedirect) sslRedirect() models.HTTPRequestRule {
httpRule := models.HTTPRequestRule{
Type: "redirect",
RedirCode: utils.PtrInt64(r.RedirectCode),
}
if r.RedirectPort == DefaultHTTPSPort {
httpRule.RedirType = "scheme"
httpRule.RedirValue = "https"
} else {
rule := fmt.Sprintf("https://%%[hdr(host),field(1,:)]:%d%%[capture.req.uri]", r.RedirectPort)
httpRule.RedirType = "location"
httpRule.RedirValue = rule
}
return httpRule
}
func (r RequestRedirect) hostRedirect() models.HTTPRequestRule {
scheme := "http"
if r.SSLRequest {
scheme = "https"
}
rule := fmt.Sprintf(scheme+"://%s%%[capture.req.uri]", r.Host)
httpRule := models.HTTPRequestRule{
Type: "redirect",
RedirCode: utils.PtrInt64(r.RedirectCode),
RedirValue: rule,
RedirType: "location",
}
return httpRule
}