From c4a1832996ffd64aafae4fbc1ad0f54f77857ce8 Mon Sep 17 00:00:00 2001 From: Brian Wu Date: Fri, 17 Jun 2016 11:26:12 -0700 Subject: [PATCH] update prerender white list and some bug fixes --- django_seo_js/backends/base.py | 17 +++++---- django_seo_js/backends/prerender.py | 1 - django_seo_js/helpers.py | 47 +++++++++++++++---------- django_seo_js/settings.py | 2 -- django_seo_js/tests/test_middlewares.py | 6 ++-- 5 files changed, 43 insertions(+), 30 deletions(-) diff --git a/django_seo_js/backends/base.py b/django_seo_js/backends/base.py index 186a814..1bffe3d 100644 --- a/django_seo_js/backends/base.py +++ b/django_seo_js/backends/base.py @@ -61,10 +61,15 @@ def __init__(self, *args, **kwargs): self.session = requests.Session() def build_django_response_from_requests_response(self, response): - r = HttpResponse(response.content) + if response.content: + resp = HttpResponse(response.content) + resp['Content-Length'] = len(response.content) + else: + resp = HttpResponse() + + resp.status_code = response.status_code + for k, v in response.headers.items(): - if k not in IGNORED_HEADERS: - r[k] = v - r['content-length'] = len(response.content) - r.status_code = response.status_code - return r + if k.lower() not in IGNORED_HEADERS: + resp[k] = v + return resp diff --git a/django_seo_js/backends/prerender.py b/django_seo_js/backends/prerender.py index 8d6ec9d..e14a165 100644 --- a/django_seo_js/backends/prerender.py +++ b/django_seo_js/backends/prerender.py @@ -30,7 +30,6 @@ def get_response_for_url(self, url): 'X-Prerender-Token': self.token, } r = self.session.get(render_url, headers=headers, allow_redirects=False) - assert r.status_code < 500 return self.build_django_response_from_requests_response(r) diff --git a/django_seo_js/helpers.py b/django_seo_js/helpers.py index 4a07e85..4143538 100644 --- a/django_seo_js/helpers.py +++ b/django_seo_js/helpers.py @@ -12,36 +12,45 @@ def update_cache_for_url(url): def request_should_be_ignored(request): - if settings.ALLOWED_URLS: - return not request_in_allowed_url_list(request) + path = request.path + if settings.IGNORE_URLS and is_path_in_black_list(path): + # black list is enabled and path is in the black list + return True - return request_in_ignore_url_list(request) or request_in_ingore_extensions_list(request) + if settings.IGNORE_EXTENSIONS and is_path_in_ignore_extensions_list(path): + return True + if settings.ALLOWED_URLS and not is_path_in_white_list(path): + # white list is enabled and path is not in the white list + return True -def request_in_ignore_url_list(request): - for url in settings.IGNORE_URLS: - if url in request.path: + return False + + +def is_path_in_list(path, pattern_list): + for pattern in pattern_list: + regex = fnmatch.translate(pattern) + re_obj = re.compile(regex) + if re_obj.match(path): return True return False -def request_in_ingore_extensions_list(request): +def is_path_in_white_list(path): + return is_path_in_list(path, settings.ALLOWED_URLS) + + +def is_path_in_black_list(path): + return is_path_in_list(path, settings.IGNORE_URLS) + + +def is_path_in_ignore_extensions_list(path): extension = None - last_dot = request.path.rfind(".") + last_dot = path.rfind(".") if last_dot == -1: # No extension found return False - extension = request.path[last_dot:] + extension = path[last_dot:] return extension and extension in settings.IGNORE_EXTENSIONS - - -def request_in_allowed_url_list(request): - for pattern in settings.ALLOWED_URLS: - regex = fnmatch.translate(pattern) - re_obj = re.compile(regex) - if re_obj.match(request.path): - return True - - return False diff --git a/django_seo_js/settings.py b/django_seo_js/settings.py index 166f985..00df610 100644 --- a/django_seo_js/settings.py +++ b/django_seo_js/settings.py @@ -4,8 +4,6 @@ IGNORE_URLS = frozenset(getattr(django_settings, 'SEO_JS_IGNORE_URLS', ['/sitemap.xml'])) -# ALLOWED_URLS will override IGNORE_URLS -# i.e. If a url exists at both IGNORE_URLS and ALLOWED_URLS, it's considered ALLOWED. ALLOWED_URLS = frozenset(getattr(django_settings, 'SEO_JS_ALLOWED_URLS', [])) IGNORE_EXTENSIONS = frozenset(getattr(django_settings, 'SEO_JS_IGNORE_EXTENSIONS', ( diff --git a/django_seo_js/tests/test_middlewares.py b/django_seo_js/tests/test_middlewares.py index 8b7be74..4359f09 100644 --- a/django_seo_js/tests/test_middlewares.py +++ b/django_seo_js/tests/test_middlewares.py @@ -91,18 +91,20 @@ def test_overriding_skips_custom_overrides_gifs_by_default(self): @override_settings( BACKEND='django_seo_js.backends.TestBackend', ALLOWED_URLS=['/bar/*', '/test/abc/*', '/foo/'], - IGNORE_URLS=[], + IGNORE_URLS=['/bar/1234/'], IGNORE_EXTENSIONS=[], ) def test_overriding_with_allowed_urls(self): self.middleware = EscapedFragmentMiddleware() self.request.GET = {"_escaped_fragment_": None} + # In the white list, not in black list -> "Test" self.request.path = '/bar/' self.assertEqual(self.middleware.process_request(self.request).content, "Test") + # In the white list and black list -> None self.request.path = '/bar/1234/' - self.assertEqual(self.middleware.process_request(self.request).content, "Test") + self.assertEqual(self.middleware.process_request(self.request), None) self.request.path = '/bar/foo/5678/' self.assertEqual(self.middleware.process_request(self.request).content, "Test")