From 9dd743861babdd14119e9bff5249fad69b9c4574 Mon Sep 17 00:00:00 2001 From: Kumar McMillan Date: Tue, 3 Jul 2012 16:48:35 -0500 Subject: [PATCH] Revert "HTTP_HOST middleware for storefronts (bug 769421)" This reverts commit 6b1bf8bc9f8bfdba508b0fc86d804ab5a282e040. --- docs/settings/settings_local.dev.py | 3 -- lib/httphost/__init__.py | 1 - lib/httphost/context_processors.py | 7 --- lib/httphost/httphost.py | 48 ----------------- lib/httphost/middleware.py | 13 ----- lib/httphost/tests.py | 83 ----------------------------- lib/settings_base.py | 10 ---- settings_test.py | 1 - 8 files changed, 166 deletions(-) delete mode 100644 lib/httphost/__init__.py delete mode 100644 lib/httphost/context_processors.py delete mode 100644 lib/httphost/httphost.py delete mode 100644 lib/httphost/middleware.py delete mode 100644 lib/httphost/tests.py diff --git a/docs/settings/settings_local.dev.py b/docs/settings/settings_local.dev.py index 774c4093cd..9674a94973 100644 --- a/docs/settings/settings_local.dev.py +++ b/docs/settings/settings_local.dev.py @@ -41,9 +41,6 @@ DATABASES = { }, } -# To get browser ID to work on your local dev instance, add this. -#SITE_URL_OVERRIDE = 'http://localhost:8000' - # Skip indexing ES to speed things up? SKIP_SEARCH_INDEX = False diff --git a/lib/httphost/__init__.py b/lib/httphost/__init__.py deleted file mode 100644 index 9557d9d92f..0000000000 --- a/lib/httphost/__init__.py +++ /dev/null @@ -1 +0,0 @@ -from .httphost import * diff --git a/lib/httphost/context_processors.py b/lib/httphost/context_processors.py deleted file mode 100644 index 62f86757f0..0000000000 --- a/lib/httphost/context_processors.py +++ /dev/null @@ -1,7 +0,0 @@ -from lib import httphost - - -def httphost_context(request): - """Exposes SITE_URL and SUBDOMAIN to the request context.""" - return {'SITE_URL': httphost.site_url(), - 'SUBDOMAIN': httphost.subdomain()} diff --git a/lib/httphost/httphost.py b/lib/httphost/httphost.py deleted file mode 100644 index 1fcd3b4158..0000000000 --- a/lib/httphost/httphost.py +++ /dev/null @@ -1,48 +0,0 @@ -import re -import threading - -from django.conf import settings -from django.core.exceptions import ImproperlyConfigured - -app = threading.local() -non_host_chars = re.compile(r'[^a-zA-Z0-9\.-]+') - -__all__ = ['site_url', 'subdomain'] - - -def site_url(): - """ - Gets the full subdomain of the host serving the request. - - Example: https://marketplace.mozilla.org - """ - if settings.SITE_URL_OVERRIDE: - if not settings.DEBUG: - raise ImproperlyConfigured('Cannot set SITE_URL_OVERRIDE in ' - 'production; it is set from HTTP_HOST') - return settings.SITE_URL_OVERRIDE - try: - return app.site_url - except AttributeError: - raise RuntimeError('Cannot access site_url() on a thread before a ' - 'request has been made') - - -def subdomain(): - """ - Gets the top-most subdomain of the host serving the request. - - Example telefonica from telefonica.marketplace.mozilla.org - """ - try: - return app.subdomain - except AttributeError: - raise RuntimeError('Cannot access site_url() on a thread before a ' - 'request has been made') - - -def set_host_info(host): - # In case someone is messing with the host header, scrub it. - host = non_host_chars.sub('', host) - app.site_url = 'https://%s' % host - app.subdomain = host.split('.')[0] diff --git a/lib/httphost/middleware.py b/lib/httphost/middleware.py deleted file mode 100644 index 1e5a5c0603..0000000000 --- a/lib/httphost/middleware.py +++ /dev/null @@ -1,13 +0,0 @@ -from .httphost import set_host_info - - -class HTTPHostMiddleware: - """ - Stores globally accessible metadata about the host. - - This requires the server (Apache or whatever) to set HTTP_HOST. - """ - - def process_request(self, request): - # e.g. telefonica.marketplace.mozilla.org - set_host_info(request.META.get('HTTP_HOST', '')) diff --git a/lib/httphost/tests.py b/lib/httphost/tests.py deleted file mode 100644 index 17a49515c7..0000000000 --- a/lib/httphost/tests.py +++ /dev/null @@ -1,83 +0,0 @@ -import unittest - -from django.conf import settings -from django.core.exceptions import ImproperlyConfigured -from django.test.client import RequestFactory - -import mock -from nose.tools import raises, eq_ - -from lib import httphost -from lib.httphost.context_processors import httphost_context -from lib.httphost.middleware import HTTPHostMiddleware - - -class UninitializedApp: - """stub for httphost.app""" - - -class TestHTTPHost(unittest.TestCase): - - def middleware(self, http_host): - serv = RequestFactory() - req = serv.get('/some/url') - req.META['HTTP_HOST'] = http_host - mw = HTTPHostMiddleware() - mw.process_request(req) - - def context(self): - serv = RequestFactory() - return httphost_context(serv.get('/some/url')) - - @mock.patch('lib.httphost.httphost.app', UninitializedApp) - @raises(RuntimeError) - def test_not_set(self): - httphost.site_url() - - def test_site_url(self): - self.middleware('telefonica.marketplace.mozilla.org') - eq_(httphost.site_url(), - 'https://telefonica.marketplace.mozilla.org') - - @mock.patch.object(settings, 'DEBUG', True) - @mock.patch.object(settings, 'SITE_URL_OVERRIDE', - 'http://localhost:8000') - def test_site_url_override(self): - self.middleware('telefonica.marketplace.mozilla.org') - eq_(httphost.site_url(), 'http://localhost:8000') - - @raises(ImproperlyConfigured) - @mock.patch.object(settings, 'DEBUG', False) - @mock.patch.object(settings, 'SITE_URL_OVERRIDE', - 'http://localhost:8000') - def test_cannot_override_site_url_in_prod(self): - httphost.site_url() - - def test_subdomain(self): - self.middleware('telefonica.marketplace.mozilla.org') - eq_(httphost.subdomain(), 'telefonica') - - def test_subdomain_amo(self): - self.middleware('addons.mozilla.org') - eq_(httphost.subdomain(), 'addons') - - @mock.patch('lib.httphost.httphost.app', UninitializedApp) - @raises(RuntimeError) - def test_subdomain_not_set(self): - httphost.subdomain() - - def test_subdomain_context(self): - self.middleware('telefonica.marketplace.mozilla.org') - eq_(self.context()['SUBDOMAIN'], 'telefonica') - - def test_site_url_context(self): - self.middleware('marketplace.mozilla.org') - eq_(self.context()['SITE_URL'], 'https://marketplace.mozilla.org') - - def test_site_url_is_scrubbed(self): - self.middleware('file:///etc/passwd') - eq_(httphost.site_url(), 'https://fileetcpasswd') - - def test_subdomain_is_scrubbed(self): - self.middleware('file:///etc/passwd') - eq_(httphost.subdomain(), 'fileetcpasswd') diff --git a/lib/settings_base.py b/lib/settings_base.py index 4e88242358..ca8af5fd63 100644 --- a/lib/settings_base.py +++ b/lib/settings_base.py @@ -163,15 +163,8 @@ DOMAIN = HOSTNAME # Full base URL for your main site including protocol. No trailing slash. # Example: https://addons.mozilla.org -# TODO(Kumar) when ready, remove this in place of httphost.site_url() SITE_URL = 'http://%s' % DOMAIN -# The site URL is automatically determined with lib.httphost.HTTPHostMiddleware. -# However, if you want to explicitly set it for local development, -# you can do so here. This setting only works when DEBUG is True. -# Example: http://localhost:8000 -SITE_URL_OVERRIDE = None - # Domain of the services site. This is where your API, and in-product pages # live. SERVICES_DOMAIN = 'services.%s' % DOMAIN @@ -275,7 +268,6 @@ TEMPLATE_CONTEXT_PROCESSORS = ( 'amo.context_processors.global_settings', 'amo.context_processors.static_url', 'jingo_minify.helpers.build_ids', - 'lib.httphost.context_processors.httphost_context', ) TEMPLATE_DIRS = ( @@ -336,8 +328,6 @@ MIDDLEWARE_CLASSES = ( 'access.middleware.ACLMiddleware', 'commonware.middleware.ScrubRequestOnException', - # Set storefront related vars based on host. - 'lib.httphost.middleware.HTTPHostMiddleware', ) # Auth diff --git a/settings_test.py b/settings_test.py index fc269c4096..4b7571df6e 100644 --- a/settings_test.py +++ b/settings_test.py @@ -52,7 +52,6 @@ PAYPAL_PERMISSIONS_URL = '' STATIC_URL = '' SITE_URL = '' -SITE_URL_OVERRIDE = None MOBILE_SITE_URL = '' MEDIA_URL = '/media/' # Reset these URLs to the defaults so your settings_local doesn't clobber them: