Skip to content

Commit

Permalink
haproxy: T7081: Support HTTP compression
Browse files Browse the repository at this point in the history
  • Loading branch information
Embezzle committed Jan 24, 2025
1 parent 62680c8 commit da33e2e
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 0 deletions.
5 changes: 5 additions & 0 deletions data/templates/load-balancing/haproxy.cfg.j2
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,11 @@ frontend {{ front }}
http-response set-header {{ header }} '{{ header_config['value'] }}'
{% endfor %}
{% endif %}
{% if front_config.http_compression is vyos_defined %}
filter compression
compression algo {{ front_config.http_compression.algorithm }}
compression type {{ front_config.http_compression.mime_type | join(' ') }}
{% endif %}
{% if front_config.rule is vyos_defined %}
{% for rule, rule_config in front_config.rule.items() %}
# rule {{ rule }}
Expand Down
32 changes: 32 additions & 0 deletions interface-definitions/load-balancing_haproxy.xml.in
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,38 @@
<valueless/>
</properties>
</leafNode>
<node name="http-compression">
<properties>
<help>Compress HTTP responses</help>
</properties>
<children>
<leafNode name="algorithm">
<properties>
<help>Compression algorithm</help>
<completionHelp>
<list>gzip deflate identity raw-deflate</list>
</completionHelp>
<constraint>
<regex>(gzip|deflate|identity|raw-deflate)</regex>
</constraint>
</properties>
</leafNode>
<leafNode name="mime-type">
<properties>
<help>MIME types to compress</help>
<valueHelp>
<format>txt</format>
<description>MIME type to compress</description>
</valueHelp>
<multi/>
<constraint>
<regex>\w+\/[-+.\w]+</regex>
</constraint>
<constraintErrorMessage>Invalid MIME type specified</constraintErrorMessage>
</properties>
</leafNode>
</children>
</node>
<node name="ssl">
<properties>
<help>SSL Certificate, SSL Key and CA</help>
Expand Down
23 changes: 23 additions & 0 deletions smoketest/scripts/cli/test_load-balancing_haproxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -498,5 +498,28 @@ def test_09_lb_reverse_proxy_logging(self):
self.assertIn('log /dev/log local5 notice', config)
self.assertIn('log /dev/log local6 crit', config)

def test_10_lb_reverse_proxy_http_compression(self):
# Setup base
self.configure_pki()
self.base_config()

# Configure compression in frontend
self.cli_set(base_path + ['service', 'https_front', 'http-compression', 'algorithm', 'gzip'])
self.cli_set(base_path + ['service', 'https_front', 'http-compression', 'mime-type', 'text/html'])
self.cli_set(base_path + ['service', 'https_front', 'http-compression', 'mime-type', 'text/javascript'])
self.cli_set(base_path + ['service', 'https_front', 'http-compression', 'mime-type', 'text/plain'])
self.cli_commit()

# Test compression is present in generated configuration file
config = read_file(HAPROXY_CONF)
self.assertIn('filter compression', config)
self.assertIn('compression algo gzip', config)
self.assertIn('compression type text/html text/javascript text/plain', config)

# Test setting compression without specifying any mime-types fails verification
self.cli_delete(base_path + ['service', 'https_front', 'http-compression', 'mime-type'])
with self.assertRaises(ConfigSessionError) as e:
self.cli_commit()

if __name__ == '__main__':
unittest.main(verbosity=2)
7 changes: 7 additions & 0 deletions src/conf_mode/load-balancing_haproxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,13 @@ def verify(lb):
not is_listen_port_bind_service(int(tmp_port), 'haproxy'):
raise ConfigError(f'"TCP" port "{tmp_port}" is used by another service')

if 'http_compression' in front_config:
if front_config['mode'] != 'http':
raise ConfigError(f'service {front} must be set to http mode to use http-compression!')
if len(front_config['http_compression']['mime_type']) == 0:
raise ConfigError(f'service {front} must have at least one mime-type configured to use'
f'http_compression!')

for back, back_config in lb['backend'].items():
if 'http_check' in back_config:
http_check = back_config['http_check']
Expand Down

0 comments on commit da33e2e

Please sign in to comment.