diff --git a/collective/xsendfile/configure.zcml b/collective/xsendfile/configure.zcml index c4257a4..4de680b 100644 --- a/collective/xsendfile/configure.zcml +++ b/collective/xsendfile/configure.zcml @@ -1,25 +1,40 @@ + xmlns:five="http://namespaces.zope.org/five" + xmlns:browser="http://namespaces.zope.org/browser" + xmlns:genericsetup="http://namespaces.zope.org/genericsetup" + xmlns:i18n="http://namespaces.zope.org/i18n" + i18n_domain="collective.xsendfile" + > - + + + + + + - - - + + diff --git a/collective/xsendfile/interfaces.py b/collective/xsendfile/interfaces.py index fcad59e..6cb3bc1 100644 --- a/collective/xsendfile/interfaces.py +++ b/collective/xsendfile/interfaces.py @@ -23,6 +23,13 @@ class IxsendfileSettings(Interface): required=False, ) + xsendfile_enable_fallback = schema.Bool( + title=_(u"Enable fallback based on HTTP_X_FORWARDED_FOR proxy header"), + description=_(u"Check if REQUEST header contains HTTP_X_FORWARDED_FOR and re-enable fallback (zope) file delivery if no proxy is detected."), + default=True, + required=False, + ) + xsendfile_pathregex_search = schema.TextLine( title=_(u"Blob-path rewriting regex"), description=_(u"This regex will be used to modify the files path, in case you are using a different mountpoint on the xsendfile Server. If you are using nginx you have to prepend the id of the location you configured."), diff --git a/collective/xsendfile/utils.py b/collective/xsendfile/utils.py index 753345d..05c19f9 100644 --- a/collective/xsendfile/utils.py +++ b/collective/xsendfile/utils.py @@ -4,11 +4,17 @@ from Products.Archetypes.utils import contentDispositionHeader from plone.i18n.normalizer.interfaces import IUserPreferredFileNameNormalizer import logging +from zope.component import getUtility +from plone.registry.interfaces import IRegistry +from collective.xsendfile.interfaces import IxsendfileSettings +import re def index_html(self, instance=None, REQUEST=None, RESPONSE=None, disposition='inline'): """ Inject X-Sendfile and X-Accel-Redirect headers into response. """ logger = logging.getLogger('collective.xsendfile') + registry = getUtility(IRegistry) + settings = registry.forInterface(IxsendfileSettings) if REQUEST is None: REQUEST = instance.REQUEST @@ -29,15 +35,29 @@ def index_html(self, instance=None, REQUEST=None, RESPONSE=None, disposition='in file_path = blob_file.name blob_file.close() + responseheader = settings.xsendfile_responseheader + pathregex_search = settings.xsendfile_pathregex_search + pathregex_substitute = settings.xsendfile_pathregex_substitute + enable_fallback = settings.xsendfile_enable_fallback + + if responseheader and pathregex_substitute: + file_path = re.sub(pathregex_search,pathregex_substitute,file_path) + RESPONSE.setHeader('Last-Modified', rfc1123_date(instance._p_mtime)) RESPONSE.setHeader("Content-Length", blob.get_size()) RESPONSE.setHeader('Content-Type', self.getContentType(instance)) - if REQUEST.get('HTTP_X_FORWARDED_FOR') == '': + fallback = False + if not responseheader: + fallback = True + if enable_fallback: + if (not REQUEST.get('HTTP_X_FORWARDED_FOR')): + fallback = True + + if fallback: logger.log(logging.WARNING, "Falling back to sending object %s.%s via Zope - no HTTP_X_FORWARDED_FOR header found."%(repr(instance),repr(self), )) return zodb_blob.open().read() else: - logger.log(logging.INFO, "Sending object %s.%s with xsendfile headers, path: %s"%(repr(instance),repr(self), repr(file_path))) - RESPONSE.setHeader("X-Accel-Redirect", "/xsendfile"+file_path) - RESPONSE.setHeader("X-Sendfile", file_path ) - return file_path + logger.log(logging.INFO, "Sending object %s.%s with xsendfile header %s, path: %s"%(repr(instance), repr(self), repr(responseheader), repr(file_path))) + RESPONSE.setHeader(responseheader, file_path) + return "collective.xsendfile - proxy missing?" diff --git a/setup.py b/setup.py index 74f8b1b..d5e30ff 100644 --- a/setup.py +++ b/setup.py @@ -26,6 +26,10 @@ 'setuptools', # -*- Extra requirements: -*- 'collective.monkeypatcher', + 'plone.app.registry', + 'plone.registry', + 'ore.bigfile', + 'z3c.form', ], entry_points=""" # -*- Entry points: -*-