Skip to content

Commit 16e42e3

Browse files
committed
CLEANUP/MINOR: restructure redirect creation for clarity
* Split host and ssl redirects conceptually, as they do distinct things. The ssl redirect does not look at the host setting when redirecting.
1 parent bfa7675 commit 16e42e3

1 file changed

Lines changed: 26 additions & 8 deletions

File tree

pkg/haproxy/rules/reqRequestRedirect.go

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,21 +26,39 @@ func (r RequestRedirect) Create(client api.HAProxyClient, frontend *models.Front
2626
if frontend.Mode == "tcp" {
2727
return errors.New("request redirection cannot be configured in TCP mode")
2828
}
29-
var rule string
29+
var httpRule models.HTTPRequestRule
3030
if r.SSLRedirect {
31-
rule = fmt.Sprintf("https://%%[hdr(host),field(1,:)]:%d%%[capture.req.uri]", r.RedirectPort)
31+
httpRule = r.sslRedirect()
3232
} else {
33-
scheme := "http"
34-
if r.SSLRequest {
35-
scheme = "https"
36-
}
37-
rule = fmt.Sprintf(scheme+"://%s%%[capture.req.uri]", r.Host)
33+
httpRule = r.hostRedirect()
3834
}
35+
36+
return client.FrontendHTTPRequestRuleCreate(0, frontend.Name, httpRule, ingressACL)
37+
}
38+
39+
func (r RequestRedirect) sslRedirect() models.HTTPRequestRule {
40+
rule := fmt.Sprintf("https://%%[hdr(host),field(1,:)]:%d%%[capture.req.uri]", r.RedirectPort)
3941
httpRule := models.HTTPRequestRule{
4042
Type: "redirect",
4143
RedirCode: utils.PtrInt64(r.RedirectCode),
4244
RedirValue: rule,
4345
RedirType: "location",
4446
}
45-
return client.FrontendHTTPRequestRuleCreate(0, frontend.Name, httpRule, ingressACL)
47+
48+
return httpRule
49+
}
50+
51+
func (r RequestRedirect) hostRedirect() models.HTTPRequestRule {
52+
scheme := "http"
53+
if r.SSLRequest {
54+
scheme = "https"
55+
}
56+
rule := fmt.Sprintf(scheme+"://%s%%[capture.req.uri]", r.Host)
57+
httpRule := models.HTTPRequestRule{
58+
Type: "redirect",
59+
RedirCode: utils.PtrInt64(r.RedirectCode),
60+
RedirValue: rule,
61+
RedirType: "location",
62+
}
63+
return httpRule
4664
}

0 commit comments

Comments
 (0)