Merge ~cjwatson/launchpad:redirection-view-default-ports into launchpad:master

Proposed by Colin Watson
Status: Merged
Approved by: Colin Watson
Approved revision: 8f93d6d103cd75adb6add55fadd2d37063f05761
Merge reported by: Otto Co-Pilot
Merged at revision: not available
Proposed branch: ~cjwatson/launchpad:redirection-view-default-ports
Merge into: launchpad:master
Diff against target: 85 lines (+27/-5)
3 files modified
lib/lp/registry/browser/tests/test_distribution.py (+10/-2)
lib/lp/registry/browser/tests/test_product.py (+10/-2)
lib/lp/services/webapp/publisher.py (+7/-1)
Reviewer Review Type Date Requested Status
Thiago F. Pappacena (community) Approve
Review via email: mp+393345@code.launchpad.net

Commit message

Normalize default ports in RedirectionView.context

Description of the change

If haproxy is terminating HTTPS, then HTTP_HOST will likely be set to something like api.launchpad.net:443, resulting in corresponding redirection targets in some cases. However, VirtualHostConfig.rooturl omits the port unless it's configured explicitly. Cope with this by normalizing redirection targets before checking them against the set of supported root URLs.

To post a comment you must log in.
Revision history for this message
Thiago F. Pappacena (pappacena) wrote :

LGTM

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
diff --git a/lib/lp/registry/browser/tests/test_distribution.py b/lib/lp/registry/browser/tests/test_distribution.py
index d47eb99..5407286 100644
--- a/lib/lp/registry/browser/tests/test_distribution.py
+++ b/lib/lp/registry/browser/tests/test_distribution.py
@@ -127,7 +127,11 @@ class TestDistributionNavigation(TestCaseWithFactory):
127 distroseries.distribution.name, distroseries.name)127 distroseries.distribution.name, distroseries.name)
128 self.assertDereferences(128 self.assertDereferences(
129 distroseries_url, distroseries,129 distroseries_url, distroseries,
130 environ={"HTTPS": "on", "SERVER_URL": None})130 environ={
131 "HTTPS": "on",
132 "HTTP_HOST": "api.launchpad.test:443",
133 "SERVER_URL": None,
134 })
131135
132 # Objects subordinate to the redirected series work too.136 # Objects subordinate to the redirected series work too.
133 distroarchseries = self.factory.makeDistroArchSeries(137 distroarchseries = self.factory.makeDistroArchSeries(
@@ -138,7 +142,11 @@ class TestDistributionNavigation(TestCaseWithFactory):
138 distroarchseries.architecturetag)142 distroarchseries.architecturetag)
139 self.assertDereferences(143 self.assertDereferences(
140 distroarchseries_url, distroarchseries,144 distroarchseries_url, distroarchseries,
141 environ={"HTTPS": "on", "SERVER_URL": None})145 environ={
146 "HTTPS": "on",
147 "HTTP_HOST": "api.launchpad.test:443",
148 "SERVER_URL": None,
149 })
142150
143151
144class TestDistributionPage(TestCaseWithFactory):152class TestDistributionPage(TestCaseWithFactory):
diff --git a/lib/lp/registry/browser/tests/test_product.py b/lib/lp/registry/browser/tests/test_product.py
index 192980c..22a4aec 100644
--- a/lib/lp/registry/browser/tests/test_product.py
+++ b/lib/lp/registry/browser/tests/test_product.py
@@ -178,7 +178,11 @@ class TestProductNavigation(TestCaseWithFactory):
178 productseries.product.name, productseries.name)178 productseries.product.name, productseries.name)
179 self.assertDereferences(179 self.assertDereferences(
180 productseries_url, productseries,180 productseries_url, productseries,
181 environ={"HTTPS": "on", "SERVER_URL": None})181 environ={
182 "HTTPS": "on",
183 "HTTP_HOST": "api.launchpad.test:443",
184 "SERVER_URL": None,
185 })
182186
183 # Objects subordinate to the redirected series work too.187 # Objects subordinate to the redirected series work too.
184 productrelease = self.factory.makeProductRelease(188 productrelease = self.factory.makeProductRelease(
@@ -188,7 +192,11 @@ class TestProductNavigation(TestCaseWithFactory):
188 productrelease.version)192 productrelease.version)
189 self.assertDereferences(193 self.assertDereferences(
190 productrelease_url, productrelease,194 productrelease_url, productrelease,
191 environ={"HTTPS": "on", "SERVER_URL": None})195 environ={
196 "HTTPS": "on",
197 "HTTP_HOST": "api.launchpad.test:443",
198 "SERVER_URL": None,
199 })
192200
193201
194class TestProductConfiguration(BrowserTestCase):202class TestProductConfiguration(BrowserTestCase):
diff --git a/lib/lp/services/webapp/publisher.py b/lib/lp/services/webapp/publisher.py
index 17c9d81..e0eb82e 100644
--- a/lib/lp/services/webapp/publisher.py
+++ b/lib/lp/services/webapp/publisher.py
@@ -1134,13 +1134,19 @@ class RedirectionView(URLDereferencingMixin):
1134 raise AttributeError(1134 raise AttributeError(
1135 "RedirectionView.context is not supported for URLs with "1135 "RedirectionView.context is not supported for URLs with "
1136 "query strings.")1136 "query strings.")
1137 # Normalize default ports.
1138 if (parsed_target.scheme, parsed_target.port) in {
1139 ("http", 80), ("https", 443)}:
1140 target_root = (parsed_target.scheme, parsed_target.hostname)
1141 else:
1142 target_root = (parsed_target.scheme, parsed_target.netloc)
1137 # This only supports the canonical root hostname/port for each site,1143 # This only supports the canonical root hostname/port for each site,
1138 # not any althostnames, but we assume that internally-generated1144 # not any althostnames, but we assume that internally-generated
1139 # redirections always use the canonical hostname/port.1145 # redirections always use the canonical hostname/port.
1140 supported_roots = {1146 supported_roots = {
1141 urlparse(section.rooturl)[:2]1147 urlparse(section.rooturl)[:2]
1142 for section in allvhosts.configs.values()}1148 for section in allvhosts.configs.values()}
1143 if parsed_target[:2] not in supported_roots:1149 if target_root not in supported_roots:
1144 raise AttributeError(1150 raise AttributeError(
1145 "RedirectionView.context is only supported for URLs served "1151 "RedirectionView.context is only supported for URLs served "
1146 "by the main Launchpad application, not '%s'." % self.target)1152 "by the main Launchpad application, not '%s'." % self.target)

Subscribers

People subscribed via source and target branches

to status/vote changes: