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

Source Code for Module 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 as published by 
 8  # 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 DateTime import DateTime 
22  from webdav.common import rfc1123_date 
23  from Products.CMFCore.FSFile import FSFile 
24  from Products.CMFCore.FSImage import FSImage 
25 -def index_html(self, REQUEST, RESPONSE):
26 """ Modified default view for files to enable 27 gzip compression on js and css files. 28 """ 29 self._updateFromFS() 30 if hasattr(self, '_data'): 31 data = self._data 32 else: 33 data = self._readFile(0) 34 data_len = len(data) 35 last_mod = self._file_mod_time 36 status = 200 37 # HTTP If-Modified-Since header handling. 38 header=REQUEST.get_header('If-Modified-Since', None) 39 if header is not None: 40 header = header.split(';')[0] 41 try: 42 mod_since=long(DateTime(header).timeTime()) 43 except: 44 mod_since=None 45 46 if mod_since is not None: 47 if last_mod > 0 and last_mod <= mod_since: 48 status = 304 49 data = '' 50 now = DateTime() 51 later = now + 30 52 RESPONSE.setStatus(status) 53 RESPONSE.setHeader('Last-Modified', rfc1123_date(last_mod)) 54 RESPONSE.setHeader('Content-Type', self.content_type) 55 RESPONSE.setHeader('Cache-Control', 'public,max-age=%d' % int(3600*24*30)) 56 RESPONSE.setHeader('Expires', later.rfc822()) 57 58 # The key line! 59 RESPONSE.enableHTTPCompression(force=1) 60 61 if status != 304: 62 RESPONSE.setHeader('Content-Length', data_len) 63 if self.ZCacheable_getManager() is not None: 64 self.ZCacheable_set(None) 65 else: 66 _setCacheHeaders(_ViewEmulator().__of__(self), extra_context={}) 67 return data
68 69 FSFile.index_html = index_html 70 FSImage.index_html = index_html 71