Commit c02f19d5 authored by Łukasz Nowak's avatar Łukasz Nowak

caddy-frontend: Do path normalization

By using simple rewrite with full regexp the Caddy's rewrite system will do
path normalization, which is required in many cases.
parent 346760af
......@@ -58,7 +58,7 @@ md5sum = f20d6c3d2d94fb685f8d26dfca1e822b
[template-default-slave-virtualhost]
filename = templates/default-virtualhost.conf.in
md5sum = f0ba7b878f374b05265fadea469c73ca
md5sum = a7aff60871c66846a623bcecca1851c4
[template-cached-slave-virtualhost]
filename = templates/cached-virtualhost.conf.in
......
......@@ -42,12 +42,17 @@
log / {{ slave_parameter.get('access_log') }} "{remote} {>REMOTE_USER} [{when}] \"{method} {uri} {proto}\" {status} {size} \"{>Referer}\" \"{>User-Agent}\" {latency_ms}"
errors {{ slave_parameter.get('error_log') }}
{%- if prefer_gzip and not (slave_type == 'zope' and backend_url) %}
{%- if not (slave_type == 'zope' and backend_url) %}
rewrite {
regexp (.*)
{% if prefer_gzip %}
if {>Accept-Encoding} match "(^gzip,.*|.*, gzip,.*|.*, gzip$|^gzip$)"
to /prefer-gzip{uri}
to /prefer-gzip{1}
{% else %}
to {1}
{% endif %}
}
{%- endif %} {#- if prefer_gzip and not (slave_type == 'zope' and backend_url) #}
{%- endif %} {#- if not (slave_type == 'zope' and backend_url) #}
{%- if slave_type == 'zope' and backend_url %}
# Zope configuration
......@@ -161,12 +166,17 @@
log / {{ slave_parameter.get('access_log') }} "{remote} {>REMOTE_USER} [{when}] \"{method} {uri} {proto}\" {status} {size} \"{>Referer}\" \"{>User-Agent}\" {latency_ms}"
errors {{ slave_parameter.get('error_log') }}
{%- if prefer_gzip and not (slave_type == 'zope' and backend_url) %}
{%- if not (slave_type == 'zope' and backend_url) %}
rewrite {
regexp (.*)
{%- if prefer_gzip %}
if {>Accept-Encoding} match "(^gzip,.*|.*, gzip,.*|.*, gzip$|^gzip$)"
to /prefer-gzip{uri}
to /prefer-gzip{1}
{% else %}
to {1}
{% endif %}
}
{%- endif %} {#- if prefer_gzip and not (slave_type == 'zope' and backend_url) #}
{%- endif %} {#- if not (slave_type == 'zope' and backend_url) #}
{%- if https_only %}
# Enforced redirection to SSL-enabled host
......
......@@ -1147,13 +1147,14 @@ http://apachecustomhttpsaccepted.example.com:%%(http_port)s {
)
result = self.fakeHTTPSResult(
parameter_dict['domain'], parameter_dict['public-ipv4'], 'test-path')
parameter_dict['domain'], parameter_dict['public-ipv4'],
'test-path/deep/.././deeper')
self.assertEqual(
self.certificate_pem,
der2pem(result.peercert))
self.assertEqualResultJson(result, 'Path', '/test-path')
self.assertEqualResultJson(result, 'Path', '/test-path/deeper')
try:
j = result.json()
......@@ -1172,8 +1173,9 @@ http://apachecustomhttpsaccepted.example.com:%%(http_port)s {
)
result_http = self.fakeHTTPResult(
parameter_dict['domain'], parameter_dict['public-ipv4'], 'test-path')
self.assertEqualResultJson(result_http, 'Path', '/test-path')
parameter_dict['domain'], parameter_dict['public-ipv4'],
'test-path/deep/.././deeper')
self.assertEqualResultJson(result_http, 'Path', '/test-path/deeper')
try:
j = result_http.json()
......@@ -1238,7 +1240,8 @@ http://apachecustomhttpsaccepted.example.com:%%(http_port)s {
)
result = self.fakeHTTPSResult(
parameter_dict['domain'], parameter_dict['public-ipv4'], 'test-path')
parameter_dict['domain'], parameter_dict['public-ipv4'],
'test-path/deep/.././deeper')
self.assertEqual(
self.certificate_pem,
......@@ -1248,7 +1251,7 @@ http://apachecustomhttpsaccepted.example.com:%%(http_port)s {
result,
'Path',
'/VirtualHostBase/https//'
'typezopepath.example.com:443/path/VirtualHostRoot/test-path'
'typezopepath.example.com:443/path/VirtualHostRoot/test-path/deeper'
)
def test_type_zope_default_path(self):
......@@ -1295,25 +1298,28 @@ http://apachecustomhttpsaccepted.example.com:%%(http_port)s {
)
result = self.fakeHTTPSResult(
parameter_dict['domain'], parameter_dict['public-ipv4'], 'test-path')
parameter_dict['domain'], parameter_dict['public-ipv4'],
'test-path/deep/.././deeper')
self.assertEqual(
self.certificate_pem,
der2pem(result.peercert))
self.assertEqualResultJson(result, 'Path', '/test-path')
self.assertEqualResultJson(result, 'Path', '/test-path/deeper')
result = self.fakeHTTPSResult(
'alias1.example.com', parameter_dict['public-ipv4'], 'test-path')
'alias1.example.com', parameter_dict['public-ipv4'],
'test-path/deep/.././deeper')
self.assertEqual(
self.certificate_pem,
der2pem(result.peercert))
self.assertEqualResultJson(result, 'Path', '/test-path')
self.assertEqualResultJson(result, 'Path', '/test-path/deeper')
result = self.fakeHTTPSResult(
'alias2.example.com', parameter_dict['public-ipv4'], 'test-path')
'alias2.example.com', parameter_dict['public-ipv4'],
'test-path/deep/.././deeper')
self.assertEqual(
self.certificate_pem,
......@@ -1511,19 +1517,21 @@ http://apachecustomhttpsaccepted.example.com:%%(http_port)s {
)
result = self.fakeHTTPSResult(
parameter_dict['domain'], parameter_dict['public-ipv4'], 'test-path')
parameter_dict['domain'], parameter_dict['public-ipv4'],
'test-path/deep/.././deeper')
self.assertEqual(
self.certificate_pem,
der2pem(result.peercert))
self.assertEqualResultJson(result, 'Path', '/test-path')
self.assertEqualResultJson(result, 'Path', '/test-path/deeper')
result_http = self.fakeHTTPResult(
parameter_dict['domain'], parameter_dict['public-ipv4'], 'test-path')
parameter_dict['domain'], parameter_dict['public-ipv4'],
'test-path/deep/.././deeper')
self.assertEqual(
'https://httpsonly.example.com/test-path',
'https://httpsonly.example.com/test-path/deeper',
result_http.headers['Location']
)
......@@ -1617,7 +1625,8 @@ http://apachecustomhttpsaccepted.example.com:%%(http_port)s {
)
result = self.fakeHTTPSResult(
parameter_dict['domain'], parameter_dict['public-ipv4'], 'test-path')
parameter_dict['domain'], parameter_dict['public-ipv4'],
'test-path/deep/.././deeper')
self.assertEqual(
self.certificate_pem,
......@@ -1633,17 +1642,18 @@ http://apachecustomhttpsaccepted.example.com:%%(http_port)s {
result,
'Path',
'/VirtualHostBase/https//typezope.example.com:443/'
'/VirtualHostRoot/test-path'
'/VirtualHostRoot/test-path/deeper'
)
result = self.fakeHTTPResult(
parameter_dict['domain'], parameter_dict['public-ipv4'], 'test-path')
parameter_dict['domain'], parameter_dict['public-ipv4'],
'test-path/deep/.././deeper')
self.assertEqualResultJson(
result,
'Path',
'/VirtualHostBase/http//typezope.example.com:80/'
'/VirtualHostRoot/test-path'
'/VirtualHostRoot/test-path/deeper'
)
def test_type_zope_prefer_gzip_encoding_to_backend(self):
......@@ -1664,7 +1674,8 @@ http://apachecustomhttpsaccepted.example.com:%%(http_port)s {
)
result = self.fakeHTTPSResult(
parameter_dict['domain'], parameter_dict['public-ipv4'], 'test-path')
parameter_dict['domain'], parameter_dict['public-ipv4'],
'test-path/deep/.././deeper')
self.assertEqual(
self.certificate_pem,
......@@ -1681,22 +1692,24 @@ http://apachecustomhttpsaccepted.example.com:%%(http_port)s {
'Path',
'/VirtualHostBase/https//'
'typezopeprefergzipencodingtobackend.example.com:443/'
'/VirtualHostRoot/test-path'
'/VirtualHostRoot/test-path/deeper'
)
result = self.fakeHTTPResult(
parameter_dict['domain'], parameter_dict['public-ipv4'], 'test-path')
parameter_dict['domain'], parameter_dict['public-ipv4'],
'test-path/deep/.././deeper')
self.assertEqualResultJson(
result,
'Path',
'/VirtualHostBase/http//'
'typezopeprefergzipencodingtobackend.example.com:80/'
'/VirtualHostRoot/test-path'
'/VirtualHostRoot/test-path/deeper'
)
result = self.fakeHTTPSResult(
parameter_dict['domain'], parameter_dict['public-ipv4'], 'test-path',
parameter_dict['domain'], parameter_dict['public-ipv4'],
'test-path/deep/.././deeper',
headers={'Accept-Encoding': 'gzip, deflate'})
self.assertEqual(
......@@ -1714,13 +1727,14 @@ http://apachecustomhttpsaccepted.example.com:%%(http_port)s {
'Path',
'/VirtualHostBase/https//'
'typezopeprefergzipencodingtobackend.example.com:443/'
'/VirtualHostRoot/test-path'
'/VirtualHostRoot/test-path/deeper'
)
self.assertEqual(
'gzip', result.json()['Incoming Headers']['accept-encoding'])
result = self.fakeHTTPResult(
parameter_dict['domain'], parameter_dict['public-ipv4'], 'test-path',
parameter_dict['domain'], parameter_dict['public-ipv4'],
'test-path/deep/.././deeper',
headers={'Accept-Encoding': 'gzip, deflate'})
self.assertEqualResultJson(
......@@ -1728,7 +1742,7 @@ http://apachecustomhttpsaccepted.example.com:%%(http_port)s {
'Path',
'/VirtualHostBase/http//'
'typezopeprefergzipencodingtobackend.example.com:80/'
'/VirtualHostRoot/test-path'
'/VirtualHostRoot/test-path/deeper'
)
self.assertEqual(
'gzip', result.json()['Incoming Headers']['accept-encoding'])
......@@ -1807,14 +1821,15 @@ http://apachecustomhttpsaccepted.example.com:%%(http_port)s {
)
result = self.fakeHTTPSResult(
parameter_dict['domain'], parameter_dict['public-ipv4'], 'test-path',
parameter_dict['domain'], parameter_dict['public-ipv4'],
'test-path/deep/.././deeper',
NGINX_HTTPS_PORT)
self.assertEqual(
self.certificate_pem,
der2pem(result.peercert))
self.assertEqualResultJson(result, 'Path', '/test-path')
self.assertEqualResultJson(result, 'Path', '/test-path/deeper')
@skip('Feature postponed')
def test_type_websocket(self):
......@@ -1889,14 +1904,15 @@ http://apachecustomhttpsaccepted.example.com:%%(http_port)s {
)
result = self.fakeHTTPSResult(
parameter_dict['domain'], parameter_dict['public-ipv4'], 'test-path')
parameter_dict['domain'], parameter_dict['public-ipv4'],
'test-path/deep/.././deeper')
self.assertEqual(
self.certificate_pem,
der2pem(result.peercert))
self.assertEqual(
'%s/test-path' % (self.backend_url,),
'%s/test-path/deeper' % (self.backend_url,),
result.headers['Location']
)
......@@ -2052,7 +2068,8 @@ http://apachecustomhttpsaccepted.example.com:%%(http_port)s {
)
result = self.fakeHTTPSResult(
parameter_dict['domain'], parameter_dict['public-ipv4'], 'test-path')
parameter_dict['domain'], parameter_dict['public-ipv4'],
'test-path/deep/.././deeper')
self.assertEqual(
self.certificate_pem,
......@@ -2064,7 +2081,8 @@ http://apachecustomhttpsaccepted.example.com:%%(http_port)s {
)
result_http = self.fakeHTTPResult(
parameter_dict['domain'], parameter_dict['public-ipv4'], 'test-path')
parameter_dict['domain'], parameter_dict['public-ipv4'],
'test-path/deeper')
self.assertEqual(
httplib.BAD_GATEWAY,
......@@ -2091,13 +2109,14 @@ http://apachecustomhttpsaccepted.example.com:%%(http_port)s {
)
result = self.fakeHTTPSResult(
parameter_dict['domain'], parameter_dict['public-ipv4'], 'test-path')
parameter_dict['domain'], parameter_dict['public-ipv4'],
'test-path/deep/.././deeper')
self.assertEqual(
self.certificate_pem,
der2pem(result.peercert))
self.assertEqualResultJson(result, 'Path', '/test-path')
self.assertEqualResultJson(result, 'Path', '/test-path/deeper')
headers = result.headers.copy()
......@@ -2458,13 +2477,14 @@ http://apachecustomhttpsaccepted.example.com:%%(http_port)s {
)
result = self.fakeHTTPSResult(
parameter_dict['domain'], parameter_dict['public-ipv4'], 'test-path')
parameter_dict['domain'], parameter_dict['public-ipv4'],
'test-path/deep/.././deeper')
self.assertEqual(
self.certificate_pem,
der2pem(result.peercert))
self.assertEqualResultJson(result, 'Path', '/test-path')
self.assertEqualResultJson(result, 'Path', '/test-path/deeper')
headers = result.headers.copy()
......@@ -2746,40 +2766,44 @@ http://apachecustomhttpsaccepted.example.com:%%(http_port)s {
)
result = self.fakeHTTPSResult(
parameter_dict['domain'], parameter_dict['public-ipv4'], 'test-path',
parameter_dict['domain'], parameter_dict['public-ipv4'],
'test-path/deep/.././deeper',
headers={'Accept-Encoding': 'gzip, deflate'})
self.assertEqual(
self.certificate_pem,
der2pem(result.peercert))
self.assertEqualResultJson(result, 'Path', '/test-path')
self.assertEqualResultJson(result, 'Path', '/test-path/deeper')
self.assertEqual(
'gzip', result.json()['Incoming Headers']['accept-encoding'])
result = self.fakeHTTPSResult(
parameter_dict['domain'], parameter_dict['public-ipv4'], 'test-path',
parameter_dict['domain'], parameter_dict['public-ipv4'],
'test-path/deep/.././deeper',
headers={'Accept-Encoding': 'deflate'})
self.assertEqualResultJson(result, 'Path', '/test-path')
self.assertEqualResultJson(result, 'Path', '/test-path/deeper')
self.assertEqual(
'deflate', result.json()['Incoming Headers']['accept-encoding'])
result = self.fakeHTTPSResult(
parameter_dict['domain'], parameter_dict['public-ipv4'], 'test-path')
parameter_dict['domain'], parameter_dict['public-ipv4'],
'test-path/deep/.././deeper')
self.assertEqual(
self.certificate_pem,
der2pem(result.peercert))
self.assertEqualResultJson(result, 'Path', '/test-path')
self.assertEqualResultJson(result, 'Path', '/test-path/deeper')
result = self.fakeHTTPSResult(
parameter_dict['domain'], parameter_dict['public-ipv4'], 'test-path')
parameter_dict['domain'], parameter_dict['public-ipv4'],
'test-path/deep/.././deeper')
self.assertEqualResultJson(result, 'Path', '/test-path')
self.assertEqualResultJson(result, 'Path', '/test-path/deeper')
def test_disabled_cookie_list(self):
parameter_dict = self.parseSlaveParameterDict('disabled-cookie-list')
......@@ -3004,17 +3028,19 @@ http://apachecustomhttpsaccepted.example.com:%%(http_port)s {
)
result = self.fakeHTTPSResult(
parameter_dict['domain'], parameter_dict['public-ipv4'], 'test-path')
parameter_dict['domain'], parameter_dict['public-ipv4'],
'test-path/deep/.././deeper')
self.assertEqual(
self.certificate_pem,
der2pem(result.peercert))
self.assertEqualResultJson(result, 'Path', '/https/test-path')
self.assertEqualResultJson(result, 'Path', '/https/test-path/deeper')
result_http = self.fakeHTTPResult(
parameter_dict['domain'], parameter_dict['public-ipv4'], 'test-path')
self.assertEqualResultJson(result_http, 'Path', '/http/test-path')
parameter_dict['domain'], parameter_dict['public-ipv4'],
'test-path/deep/.././deeper')
self.assertEqualResultJson(result_http, 'Path', '/http/test-path/deeper')
class TestReplicateSlave(SlaveHttpFrontendTestCase, TestDataMixin):
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment