Skip to content

Commit

Permalink
Merge pull request #1 from eillarra/patch-1
Browse files Browse the repository at this point in the history
Add a "Vary" header for DNT, useful for caching.
  • Loading branch information
jwhitlock committed Feb 16, 2017
2 parents 269289b + ba5f50c commit f471249
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions dnt/middleware.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,21 @@
from django.utils.cache import patch_vary_headers


class DoNotTrackMiddleware(object):
"""Sets request.dnt to True or False based on the presence of the
DNT HTTP header."""

def process_request(self, request):
"""
Sets request.DNT to True or False based on the presence of the DNT HTTP header.
"""
if 'HTTP_DNT' in request.META and request.META['HTTP_DNT'] == '1':
request.DNT = True
else:
request.DNT = False

def process_response(self, request, response):
"""
Adds a "Vary" header for DNT, useful for caching.
"""
patch_vary_headers(response, ['DNT'])

return response

0 comments on commit f471249

Please sign in to comment.