Package Products :: Package ZenWidgets :: Module FileGzipper' |
[hide private]
[frames] | no frames]

# Source Code for Module Products.ZenWidgets.FileGzipper'

     1  ##############################################################################
     2  #
     3  # Copyright (C) Zenoss, Inc. 2007, all rights reserved.
     4  #
     5  # This content is made available according to terms specified in
     6  # License.zenoss under the directory where your Zenoss product is installed.
     7  #
     8  ##############################################################################
     9
    10
    11  """ FileGzipper
    12
    13  A monkey patch that enables gzip compression on static files
    14
    15  """
    16
    17  from Products.CMFCore.utils import _setCacheHeaders, _ViewEmulator
    18  from Products.CMFCore.utils import _checkConditionalGET
    19  from Products.CMFCore.FSFile import FSFile
    20  from Products.CMFCore.FSImage import FSImage
    21 -def index_html(self, REQUEST, RESPONSE):
    22      """
    23      The default view of the contents of a File or Image.
    24
    25      Returns the contents of the file or image.  Also, sets the
    26      Content-Type HTTP header to the objects content type.
    27      """
    28      self._updateFromFS()
    29      view = _ViewEmulator().__of__(self)
    30
    31      # If we have a conditional get, set status 304 and return
    32      # no content
    33      if _checkConditionalGET(view, extra_context={}):
    34          return ''
    35
    36      ###### ZENOSS PATCH #####
    37      # Patch to ensure a static charset
    38      if ";" not in self.content_type:
    39          self.content_type += "; charset=utf-8"
    40      #########################
    41      RESPONSE.setHeader('Content-Type', self.content_type)
    42
    43
    44      # old-style If-Modified-Since header handling.
    45      if self._setOldCacheHeaders():
    46          # Make sure the CachingPolicyManager gets a go as well
    47          _setCacheHeaders(view, extra_context={})
    48          return ''
    49
    50      data = self._readFile(0)
    51      data_len = len(data)
    52      RESPONSE.setHeader('Content-Length', data_len)
    53
    54      ###### ZENOSS PATCH #####
    55      # Patch to use gzip compression
    56      RESPONSE.enableHTTPCompression(REQUEST)
    57      #########################
    58
    59      #There are 2 Cache Managers which can be in play....
    60      #need to decide which to use to determine where the cache headers
    61      #are decided on.
    62      if self.ZCacheable_getManager() is not None:
    63          self.ZCacheable_set(None)
    64      else:
    65          _setCacheHeaders(view, extra_context={})
    66      return data
    67
    68  FSFile.index_html = index_html
    69  FSImage.index_html = index_html
    70
    71  from zope.browserresource.file import FileResource
    72  from Products.ZenUtils.Utils import monkeypatch
    73  from Products.Five.browser.pagetemplatefile import ViewPageTemplateFile
    74
    75  @monkeypatch(ViewPageTemplateFile)
    76 -def __call__(self, __instance, *args, **keywords):
    77      try:
    78          response = __instance.request.response
    79          response.enableHTTPCompression(REQUEST=__instance.request)
    80      except AttributeError:
    81          pass
    82      return original(self, __instance, *args, **keywords)
    83
    84  oldget = FileResource.GET
    85  @monkeypatch(FileResource)
    86 -def GET(self):
    87      self.request.response.enableHTTPCompression(self.request)
    88      return oldget(self)
    89