From 31d39e75eea7fb6cdf3bb8bfd8b490d45de04ee9 Mon Sep 17 00:00:00 2001 From: Dan McGee Date: Tue, 16 Apr 2013 21:59:32 -0500 Subject: Add shortcut for HEAD requests on slower views We sometimes see some web bots and crawlers make HEAD requests to verify existence of certain pages in the application. However, they are less than kind as 20-50 requests might arrive at the same time, and package search and details pages are some of the slowest rendering pages we have due to the Django template engine. Rather than waste time generating the content only to throw it away, response as soon as we can with either a 404 or 200 response as appropriate, omitting the 'Content-Length' header completely, which seems to be acceptable by the HTTP spec. Signed-off-by: Dan McGee --- main/utils.py | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'main/utils.py') diff --git a/main/utils.py b/main/utils.py index cdd4ff71..8394e5cd 100644 --- a/main/utils.py +++ b/main/utils.py @@ -8,6 +8,7 @@ import hashlib from django.core.cache import cache from django.db import connections, router +from django.http import HttpResponse from django.utils.timezone import now from django.template.defaultfilters import slugify @@ -55,6 +56,14 @@ def clear_cache_function(func, args, kwargs): cache.delete(key) +def empty_response(): + empty = HttpResponse('') + # designating response as 'streaming' forces ConditionalGetMiddleware to + # not add a 'Content-Length: 0' header + empty.streaming = True + return empty + + def format_http_headers(request): headers = sorted((k, v) for k, v in request.META.items() if k.startswith('HTTP_')) -- cgit v1.2.3-2-g168b From b7b24740640e24883cd17fd683e1d465fbb343f8 Mon Sep 17 00:00:00 2001 From: Dan McGee Date: Tue, 16 Apr 2013 22:12:01 -0500 Subject: Various minor code cleanups and fixes Most of these were suggested by PyCharm, and include everything from little syntax issues and other bad smells to dead or bad code. Signed-off-by: Dan McGee --- main/utils.py | 1 - 1 file changed, 1 deletion(-) (limited to 'main/utils.py') diff --git a/main/utils.py b/main/utils.py index 8394e5cd..9ee8db58 100644 --- a/main/utils.py +++ b/main/utils.py @@ -3,7 +3,6 @@ try: except ImportError: import pickle -from datetime import datetime import hashlib from django.core.cache import cache -- cgit v1.2.3-2-g168b