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

# Source Code for Module Products.ZenWidgets.FileGzipper'

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