Unverified Commit 713f9188 authored by Diederik van der Boor's avatar Diederik van der Boor
Browse files

Include Cache-Control headers to avoid caching private files

parent 675651ac
...@@ -4,6 +4,7 @@ Sending files efficiently for different kind of webservers. ...@@ -4,6 +4,7 @@ Sending files efficiently for different kind of webservers.
import os import os
import sys import sys
import time import time
from functools import wraps
from django.conf import settings from django.conf import settings
from django.core.exceptions import ImproperlyConfigured from django.core.exceptions import ImproperlyConfigured
...@@ -32,6 +33,21 @@ def get_server_class(path): ...@@ -32,6 +33,21 @@ def get_server_class(path):
) )
def add_no_cache_headers(func):
"""
Makes sure the retrieved file is not cached on disk, or cached by proxy servers in between.
This would circumvent any checking whether the user may even access the file.
"""
@wraps(func)
def _dec(*args, **kwargs):
response = func(*args, **kwargs)
response['Expires'] = 'Thu, 01 Jan 1970 00:00:00 GMT' # HTTP 1.0 proxies
response['Cache-Control'] = 'max-age=0, no-cache, must-revalidate, proxy-revalidate' # HTTP 1.1
return response
return _dec
class DjangoStreamingServer(object): class DjangoStreamingServer(object):
""" """
Serve static files through ``wsgi.file_wrapper`` or streaming chunks. Serve static files through ``wsgi.file_wrapper`` or streaming chunks.
...@@ -40,6 +56,7 @@ class DjangoStreamingServer(object): ...@@ -40,6 +56,7 @@ class DjangoStreamingServer(object):
""" """
@staticmethod @staticmethod
@add_no_cache_headers
def serve(private_file): def serve(private_file):
# Support If-Last-Modified # Support If-Last-Modified
if sys.version_info >= (3,): if sys.version_info >= (3,):
...@@ -78,6 +95,7 @@ class DjangoServer(DjangoStreamingServer): ...@@ -78,6 +95,7 @@ class DjangoServer(DjangoStreamingServer):
""" """
@staticmethod @staticmethod
@add_no_cache_headers
def serve(private_file): def serve(private_file):
# This supports If-Modified-Since and sends the file in 4KB chunks # This supports If-Modified-Since and sends the file in 4KB chunks
try: try:
...@@ -96,6 +114,7 @@ class ApacheXSendfileServer(object): ...@@ -96,6 +114,7 @@ class ApacheXSendfileServer(object):
""" """
@staticmethod @staticmethod
@add_no_cache_headers
def serve(private_file): def serve(private_file):
response = HttpResponse() response = HttpResponse()
response['X-Sendfile'] = private_file.full_path response['X-Sendfile'] = private_file.full_path
...@@ -117,6 +136,7 @@ class NginxXAccelRedirectServer(object): ...@@ -117,6 +136,7 @@ class NginxXAccelRedirectServer(object):
""" """
@staticmethod @staticmethod
@add_no_cache_headers
def serve(private_file): def serve(private_file):
internal_url = os.path.join(settings.PRIVATE_STORAGE_INTERNAL_URL, private_file.relative_name) internal_url = os.path.join(settings.PRIVATE_STORAGE_INTERNAL_URL, private_file.relative_name)
response = HttpResponse() response = HttpResponse()
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment