def request_accepts_gzip(request): # MSIE has issues with gzipped response of various content types # but, if we're only gzipping text/css and javascript, we should be ok if CONVOY_CONSERVATIVE_MSIE_GZIP: if "msie" in request.META.get('HTTP_USER_AGENT', '').lower(): return False ae = request.META.get('HTTP_ACCEPT_ENCODING', '') if re_accepts_gzip.search(ae): return True return False
def path(self): """ Serve gzip file if client accept it. Generate or update the gzip file if needed. """ path = self.object.geojson.path statobj = os.stat(path) ae = self.request.META.get('HTTP_ACCEPT_ENCODING', '') if re_accepts_gzip.search(ae) and getattr(settings, 'LEAFLET_STORAGE_GZIP', True): gzip_path = "{path}{ext}".format(path=path, ext=self.EXT) up_to_date = True if not os.path.exists(gzip_path): up_to_date = False else: gzip_statobj = os.stat(gzip_path) if statobj.st_mtime > gzip_statobj.st_mtime: up_to_date = False if not up_to_date: gzip_file(path, gzip_path) path = gzip_path return path
def path(self): """ Serve gzip file if client accept it. Generate or update the gzip file if needed. """ path = self._path() statobj = os.stat(path) ae = self.request.META.get('HTTP_ACCEPT_ENCODING', '') if re_accepts_gzip.search(ae) and getattr(settings, 'UMAP_GZIP', True): gzip_path = "{path}{ext}".format(path=path, ext=self.EXT) up_to_date = True if not os.path.exists(gzip_path): up_to_date = False else: gzip_statobj = os.stat(gzip_path) if statobj.st_mtime > gzip_statobj.st_mtime: up_to_date = False if not up_to_date: gzip_file(path, gzip_path) path = gzip_path return path
def render_to_response(self, context, **response_kwargs): path = self.object.geojson.path statobj = os.stat(path) response = None ext = '.gz' ae = self.request.META.get('HTTP_ACCEPT_ENCODING', '') if re_accepts_gzip.search(ae) and getattr(settings, 'LEAFLET_STORAGE_GZIP', True): gzip_path = "{path}{ext}".format(path=path, ext=ext) up_to_date = True if not os.path.exists(gzip_path): up_to_date = False else: gzip_statobj = os.stat(gzip_path) if statobj.st_mtime > gzip_statobj.st_mtime: up_to_date = False if not up_to_date: gzip_file(path, gzip_path) path = gzip_path if getattr(settings, 'LEAFLET_STORAGE_ACCEL_REDIRECT', False): response = HttpResponse() response['X-Accel-Redirect'] = path elif getattr(settings, 'LEAFLET_STORAGE_X_SEND_FILE', False): response = HttpResponse() response['X-Sendfile'] = path else: # TODO IMS response = CompatibleStreamingHttpResponse( open(path, 'rb'), content_type='application/json' ) response["Last-Modified"] = http_date(statobj.st_mtime) response['ETag'] = '"%s"' % hashlib.md5(response.content).hexdigest() response['Content-Length'] = str(len(response.content)) if path.endswith(ext): response['Content-Encoding'] = 'gzip' return response