[PATCH 2 of 2] urls: allow canonical_url to contain more than just a hostname

Thomas De Schampheleire patrickdepinguin at gmail.com
Tue Sep 18 18:53:26 UTC 2018


# HG changeset patch
# User Thomas De Schampheleire <thomas.de_schampheleire at nokia.com>
# Date 1537216449 -7200
#      Mon Sep 17 22:34:09 2018 +0200
# Node ID 3bf6c77dd1501eff27ae928e5d07114b48026cd3
# Parent  e97e2effbc17f9209e388afa1f3618401cc34432
urls: allow canonical_url to contain more than just a hostname

Although the .ini file gives the example:

    canonical_url = https://kallithea.example.com/repos

it does not actually work. The '/repos' part is stripped off by the
canonical_url method.

The 'host' entry in the arguments passed to routes.url does not strictly
need to be a pure hostname. At least, the implementation does no validation
of this fact, it is concatenated verbatim between the protocol and the rest
of the URL.

As mapping Kallithea to a subpath of a base hostname is a valid
implementation, the canonical_url feature should allow it.

diff --git a/kallithea/lib/helpers.py b/kallithea/lib/helpers.py
--- a/kallithea/lib/helpers.py
+++ b/kallithea/lib/helpers.py
@@ -58,7 +58,7 @@ def canonical_url(*args, **kargs):
     from kallithea import CONFIG
     try:
         parts = CONFIG.get('canonical_url', '').split('://', 1)
-        kargs['host'] = parts[1].split('/', 1)[0]
+        kargs['host'] = parts[1]
         kargs['protocol'] = parts[0]
     except IndexError:
         kargs['qualified'] = True
diff --git a/kallithea/tests/other/test_libs.py b/kallithea/tests/other/test_libs.py
--- a/kallithea/tests/other/test_libs.py
+++ b/kallithea/tests/other/test_libs.py
@@ -559,6 +559,8 @@ class TestLibs(TestController):
         ('http://www.example.org', '/abc/xyz/', 'http://www.example.org/abc/xyz/'),
         ('http://www.example.org', 'abc/xyz/', 'http://www.example.org/abc/xyz/'),
         ('http://www.example.org', 'about', 'http://www.example.org/about-page'),
+        ('http://www.example.org/repos/', 'abc/xyz/', 'http://www.example.org/repos/abc/xyz/'),
+        ('http://www.example.org/kallithea/repos/', 'abc/xyz/', 'http://www.example.org/kallithea/repos/abc/xyz/'),
     ])
     def test_canonical_url(self, canonical, test, expected):
         from kallithea.lib.helpers import canonical_url
@@ -581,6 +583,8 @@ class TestLibs(TestController):
 
     @parametrize('canonical,expected', [
         ('http://www.example.org', 'www.example.org'),
+        ('http://www.example.org/repos/', 'www.example.org'),
+        ('http://www.example.org/kallithea/repos/', 'www.example.org'),
     ])
     def test_canonical_hostname(self, canonical, expected):
         from kallithea.lib.helpers import canonical_hostname


More information about the kallithea-general mailing list