From ac4596780e153ed1d9e46c98a2179eaeed7b034e Mon Sep 17 00:00:00 2001 From: Stephen Burrows Date: Wed, 23 Feb 2011 11:50:50 -0500 Subject: [PATCH] Corrected FeedView handling of incorrect Accept headers. Will now actually return 406 errors. --- contrib/penfield/exceptions.py | 3 +++ contrib/penfield/middleware.py | 14 ++++++++++++++ contrib/penfield/models.py | 7 ++++--- 3 files changed, 21 insertions(+), 3 deletions(-) create mode 100644 contrib/penfield/exceptions.py create mode 100644 contrib/penfield/middleware.py diff --git a/contrib/penfield/exceptions.py b/contrib/penfield/exceptions.py new file mode 100644 index 0000000..96b96ed --- /dev/null +++ b/contrib/penfield/exceptions.py @@ -0,0 +1,3 @@ +class HttpNotAcceptable(Exception): + """This will be raised if an Http-Accept header will not accept the feed content types that are available.""" + pass \ No newline at end of file diff --git a/contrib/penfield/middleware.py b/contrib/penfield/middleware.py new file mode 100644 index 0000000..b25a28b --- /dev/null +++ b/contrib/penfield/middleware.py @@ -0,0 +1,14 @@ +from django.http import HttpResponse +from django.utils.decorators import decorator_from_middleware +from philo.contrib.penfield.exceptions import HttpNotAcceptable + + +class HttpNotAcceptableMiddleware(object): + """Middleware to catch HttpNotAcceptable errors and return an Http406 response. + See RFC 2616.""" + def process_exception(self, request, exception): + if isinstance(exception, HttpNotAcceptable): + return HttpResponse(status=406) + + +http_not_acceptable = decorator_from_middleware(HttpNotAcceptableMiddleware) \ No newline at end of file diff --git a/contrib/penfield/models.py b/contrib/penfield/models.py index 98dcdd5..b970643 100644 --- a/contrib/penfield/models.py +++ b/contrib/penfield/models.py @@ -10,6 +10,8 @@ from django.utils.datastructures import SortedDict from django.utils.encoding import smart_unicode, force_unicode from django.utils.html import escape from datetime import date, datetime +from philo.contrib.penfield.exceptions import HttpNotAcceptable +from philo.contrib.penfield.middleware import http_not_acceptable from philo.contrib.penfield.validators import validate_pagination_count from philo.exceptions import ViewCanNotProvideSubpath from philo.models import Tag, Titled, Entity, MultiView, Page, register_value_model, TemplateField, Template @@ -62,7 +64,7 @@ class FeedView(MultiView): urlpatterns = patterns('') if self.feeds_enabled: feed_reverse_name = "%s_feed" % reverse_name - feed_view = self.feed_view(get_items_attr, feed_reverse_name) + feed_view = http_not_acceptable(self.feed_view(get_items_attr, feed_reverse_name)) feed_pattern = r'%s%s%s$' % (base, (base and base[-1] != "^") and "/" or "", self.feed_suffix) urlpatterns += patterns('', url(feed_pattern, feed_view, name=feed_reverse_name), @@ -139,8 +141,7 @@ class FeedView(MultiView): else: feed_type = None if not feed_type: - # See RFC 2616 - return HttpResponse(status=406) + raise HttpNotAcceptable return FEEDS[feed_type] def get_feed(self, obj, request, reverse_name): -- 2.20.1