|
10 | 10 | String $http_edge_frontend = 'default_fe', |
11 | 11 |
|
12 | 12 | String $http_edge_redirect_marker = '###http-edge-redirects###', |
| 13 | + String $http_edge_canonical_marker = '###http-edge-canonicals###', |
| 14 | + String $http_edge_nofollow_marker = '###http-edge-nofollow###', |
| 15 | + String $http_edge_status410_marker = '###http-edge-status410###', |
13 | 16 | Array $http_edge_redirect_types = [ 'str', 'beg', 'end', 'sub', 'dir', 'regm' ], |
14 | 17 | Array $http_edge_services = [ 'redirects' ], |
15 | 18 | Hash $http_edge_domains_envs = {}, |
|
29 | 32 | replace => false, |
30 | 33 | } |
31 | 34 |
|
32 | | - $http_edge_redirect_rules = flatten($http_edge_domains_envs.map |$domain, $options| { |
33 | | - $match_type = pick($options['match_type'], 'str') |
34 | | - $hdr_match = $match_type ? { |
35 | | - 'str' => 'hdr', |
36 | | - default => "hdr_${match_type}" |
37 | | - } |
38 | | - $domain_separator = $match_type ? { |
39 | | - 'reg' => '\.', |
40 | | - default => '.', |
41 | | - } |
42 | | - $options['_envs'].map |$env| { |
43 | | - $path_env = "${http_edge_path}/${env}" |
44 | | - if $options[$env] { |
45 | | - $domain_env = $options[$env] |
| 35 | + $http_edge_services.each | $service | { |
| 36 | + $http_edge_rules = flatten($http_edge_domains_envs.map |$domain, $options| { |
| 37 | + $match_type = pick($options['match_type'], 'str') |
| 38 | + $hdr_match = $match_type ? { |
| 39 | + 'str' => 'hdr', |
| 40 | + default => "hdr_${match_type}" |
46 | 41 | } |
47 | | - else { |
48 | | - $parts = [$options['_domain_prefix'], $env, $options['_domain_suffix']].filter |$item| { !$item.empty } |
49 | | - $domain_env = join($parts, $domain_separator) |
| 42 | + $domain_separator = $match_type ? { |
| 43 | + 'reg' => '\.', |
| 44 | + default => '.', |
50 | 45 | } |
| 46 | + $options['_envs'].map |$env| { |
| 47 | + $path_env = "${http_edge_path}/${env}" |
| 48 | + if $options[$env] { |
| 49 | + $domain_env = $options[$env] |
| 50 | + } |
| 51 | + else { |
| 52 | + $parts = [$options['_domain_prefix'], $env, $options['_domain_suffix']].filter |$item| { !$item.empty } |
| 53 | + $domain_env = join($parts, $domain_separator) |
| 54 | + } |
51 | 55 |
|
52 | | - ensure_resource('file', [$path_env, "$path_env/current"], $file_args) |
| 56 | + if $service in $options['_services'] { |
53 | 57 |
|
54 | | - $options['_services'].map |$service| { |
| 58 | + ensure_resource('file', [$path_env, "$path_env/current", "$path_env/current/$service"], $file_args) |
55 | 59 |
|
56 | | - ensure_resource('file', ["$path_env/current/$service"], $file_args) |
| 60 | + $http_edge_redirect_types.map |$type| { |
| 61 | + $map_file = "$path_env/current/$service/$domain.$type.map" |
| 62 | + $map_func = "map_$type" |
57 | 63 |
|
58 | | - $http_edge_redirect_types.map |$type| { |
59 | | - $map_file = "$path_env/current/$service/$domain.$type.map" |
60 | | - $map_func = "map_$type" |
| 64 | + file { $map_file: |
| 65 | + ensure => present, |
| 66 | + owner => $http_edge_path_owner, |
| 67 | + } |
| 68 | + if $service == 'redirects' { |
| 69 | + "redirect location %[path,$map_func($map_file)] code 301 if { ${hdr_match}(host) -i $domain_env } { path,$map_func($map_file) -m found } !{ path,$map_func($map_file) -m str haproxy-skip }" |
| 70 | + } elsif $service == 'nofollow' { |
| 71 | + "set-var(txn.noindex) always_true if { ${hdr_match}(host) $domain_env } { path,$map_func($map_file) -m found }" |
| 72 | + } elsif $service == 'status410' { |
| 73 | + "set-var(txn.status410) always_true if { ${hdr_match}(host) $domain_env } { path,$map_func($map_file) -m found }" |
| 74 | + } elsif $service == 'canonicals' { |
| 75 | + "set-header X-Canonical-Custom <https://%[${hdr_match}(host)]%[url,$map_func($map_file)]>;\\ rel=\"canonical\" if !{ req.hdr(X-Canonical-Custom) -m found } { ${hdr_match}(host) $domain_env } { url,$map_func($map_file) -m found }" |
| 76 | + } |
61 | 77 |
|
62 | | - file { $map_file: |
63 | | - ensure => present, |
64 | | - owner => $http_edge_path_owner, |
65 | | - } |
66 | | - if $service == 'redirects' { |
67 | | - "redirect location %[path,$map_func($map_file)] code 301 if { ${hdr_match}(host) -i $domain_env } { path,$map_func($map_file) -m found } !{ path,$map_func($map_file) -m str haproxy-skip }" |
68 | | - } elsif $service == 'nofollow' { |
69 | | - "set-var(txn.noindex) always_true if { ${hdr_match}(host) $domain_env } { path,$map_func($map_file) -m found }" |
70 | | - } elsif $service == 'status410' { |
71 | | - "set-var(txn.status410) always_true if { ${hdr_match}(host) $domain_env } { path,$map_func($map_file) -m found }" |
72 | | - } elsif $service == 'canonicals' { |
73 | | - "set-header X-Canonical-Custom <https://%[${hdr_match}(host)]%[url,$map_func($map_file)]>;\\ rel=\"canonical\" if !{ req.hdr(X-Canonical-Custom) -m found } { ${hdr_match}(host) $domain_env } { url,$map_func($map_file) -m found }" |
74 | 78 | } |
75 | | - |
76 | 79 | } |
77 | 80 | } |
78 | | - } |
79 | | - }) |
| 81 | + }) |
80 | 82 |
|
81 | | - $http_edge_rules = flatten($http_edge_redirect_rules, 'set-var(txn.canonical_custom) hdr(X-Canonical-Custom) if { hdr(X-Canonical-Custom) -m found }', 'del-header X-Canonical-Custom if { hdr(X-Canonical-Custom) -m found }') |
| 83 | + case $service { |
| 84 | + 'redirects': { $http_edge_redirect_rules = $http_edge_rules } |
| 85 | + 'nofollow': { $http_edge_nofollow_rules = $http_edge_rules } |
| 86 | + 'status410': { $http_edge_status410_rules = $http_edge_rules } |
| 87 | + 'canonicals': { $http_edge_canonicals_rules = $http_edge_rules } |
| 88 | + } |
| 89 | + } |
82 | 90 |
|
83 | | - $http_response_rules = $ducktape::haproxy::frontends[$http_edge_frontend]['options']['http-response'] |
84 | | - $http_response_rules_real = concat($http_response_rules, 'set-header link %[var(txn.canonical_custom)] if { var(txn.canonical_custom) -m found }') |
| 91 | + if 'canonicals' in $http_edge_services { |
| 92 | + $http_edge_canonicals_rules_real = flatten($http_edge_canonicals_rules, 'set-var(txn.canonical_custom) hdr(X-Canonical-Custom) if { hdr(X-Canonical-Custom) -m found }', 'del-header X-Canonical-Custom if { hdr(X-Canonical-Custom) -m found }') |
| 93 | + $http_response_rules = $ducktape::haproxy::frontends[$http_edge_frontend]['options']['http-response'] |
| 94 | + $http_response_rules_real = concat($http_response_rules, 'set-header link %[var(txn.canonical_custom)] if { var(txn.canonical_custom) -m found }') |
| 95 | + $http_response_rules_hash = {$http_edge_frontend => {'options' => {'http-response' => $http_response_rules_real}}} |
| 96 | + $frontends_real = deep_merge($ducktape::haproxy::frontends, $http_request_rules_hash) |
| 97 | + } |
85 | 98 |
|
86 | 99 | # Insert http-edge redirect rules at the marker position, or at the end if no marker |
87 | 100 | $http_request_rules = $ducktape::haproxy::frontends[$http_edge_frontend]['options']['http-request'] |
88 | 101 | if grep($http_request_rules, $http_edge_redirect_marker) == [] { |
89 | | - $http_request_rules_real = concat($http_request_rules, $http_edge_rules) |
| 102 | + $http_request_rules_with_redirects = concat($http_request_rules, $http_edge_redirect_rules) |
| 103 | + } |
| 104 | + else { |
| 105 | + $http_request_rules_with_redirects = split(regsubst(join($http_request_rules, "\n"), $http_edge_redirect_marker, join($http_edge_redirect_rules, "\n")), "\n") |
| 106 | + } |
| 107 | + |
| 108 | + if grep($http_request_rules_with_redirects, $http_edge_status410_marker) == [] { |
| 109 | + $http_request_rules_with_status410 = concat($http_request_rules_with_redirects, $http_edge_status410_rules) |
| 110 | + } |
| 111 | + else { |
| 112 | + $http_request_rules_with_status410 = split(regsubst(join($http_request_rules_with_redirects, "\n"), $http_edge_status410_marker, join($http_edge_status410_rules, "\n")), "\n") |
| 113 | + } |
| 114 | + |
| 115 | + if grep($http_request_rules_with_status410, $http_edge_nofollow_marker) == [] { |
| 116 | + $http_request_rules_with_nofollow = concat($http_request_rules_with_status410, $http_edge_nofollow_rules) |
| 117 | + } |
| 118 | + else { |
| 119 | + $http_request_rules_with_nofollow = split(regsubst(join($http_request_rules_with_status410, "\n"), $http_edge_nofollow_marker, join($http_edge_nofollow_rules, "\n")), "\n") |
| 120 | + } |
| 121 | + |
| 122 | + if grep($http_request_rules_with_nofollow, $http_edge_canonical_marker) == [] { |
| 123 | + $http_request_rules_real = concat($http_request_rules_with_nofollow, $http_edge_canonicals_rules_real) |
90 | 124 | } |
91 | 125 | else { |
92 | | - $http_request_rules_real = split(regsubst(join($http_request_rules, "\n"), $http_edge_redirect_marker, join($http_edge_rules, "\n")), "\n") |
| 126 | + $http_request_rules_real = split(regsubst(join($http_request_rules_with_nofollow, "\n"), $http_edge_canonical_marker, join($http_edge_canonicals_rules_real, "\n")), "\n") |
93 | 127 | } |
94 | 128 |
|
95 | 129 | $http_request_rules_hash = {$http_edge_frontend => {'options' => {'http-request' => $http_request_rules_real}}} |
|
0 commit comments