def __call__(self, environ, start_response): path_info = environ.get('PATH_INFO', '') if not path_info: # See if this is a static file hackishly mapped. if os.path.exists(self.directory) and os.path.isfile(self.directory): app = FileApp(self.directory) if self.cache_seconds: app.cache_control(max_age=int(self.cache_seconds)) return app(environ, start_response) return self.add_slash(environ, start_response) if path_info == '/': # @@: This should obviously be configurable filename = 'index.html' else: filename = request.path_info_pop(environ) full = os.path.join(self.directory, filename) if not os.path.exists(full): return self.not_found(environ, start_response) if os.path.isdir(full): # @@: Cache? return self.__class__(full)(environ, start_response) if environ.get('PATH_INFO') and environ.get('PATH_INFO') != '/': return self.error_extra_path(environ, start_response) if_none_match = environ.get('HTTP_IF_NONE_MATCH') if if_none_match: mytime = os.stat(full).st_mtime if str(mytime) == if_none_match: headers = [] ETAG.update(headers, mytime) start_response('304 Not Modified', headers) return [''] # empty body app = FileApp(full) if self.cache_seconds: app.cache_control(max_age=int(self.cache_seconds)) return app(environ, start_response)
def static_handler(request): path = request.urlmatch.group('static') # TODO: fix security checks, maybe preload static? target_path = os.path.normpath(os.path.join(request.store.root, path)) app = FileApp(target_path) app.cache_control(public=True, max_age=3600) return request.wsgi_pass(app)
def __call__(self, environ, start_response): request_path = environ.get('PATH_INFO', '') app = self.cached_apps.get(request_path) if app: return app(environ, start_response) if not request_path.endswith('/'): relative_request_path = request_path.lstrip('/') for root_dir, max_cache in self.path_items: file_asset_path = normcase( join(root_dir, relative_request_path)) if isfile(file_asset_path): content_type, _ = guess_type(file_asset_path) if content_type in self.utf8_mimetypes: content_type += '; charset=utf-8' app = FileApp(file_asset_path, content_type=content_type) if max_cache: app.cache_control(max_age=max_cache) else: app.cache_control(max_age=0) self.cached_apps[request_path] = app return app(environ, start_response) return self.app(environ, start_response)
def __call__(self, environ, start_response): request_path = environ.get('PATH_INFO', '') app = self.cached_apps.get(request_path) if app: return app(environ, start_response) if not request_path.endswith('/'): relative_request_path = request_path.lstrip('/') for root_dir, max_cache in self.path_items: file_asset_path = normcase(join(root_dir, relative_request_path)) if isfile(file_asset_path): content_type, _ = guess_type(file_asset_path) if content_type in self.utf8_mimetypes: content_type += '; charset=utf-8' app = FileApp(file_asset_path, content_type=content_type) if max_cache: app.cache_control(max_age=max_cache) else: app.cache_control(max_age=0) self.cached_apps[request_path] = app return app(environ, start_response) return self.app(environ, start_response)
def __call__(self, environ, start_response): request_path = environ.get('PATH_INFO', '') # check if the request is for static files at all path_parts = request_path.strip('/').split('/', 2) if len(path_parts) == 3 and path_parts[0] in ['play', 'game-meta']: slug = path_parts[1] game = self.game_list.get_by_slug(slug) if game and game.path.is_set(): asset_path = path_parts[2] file_asset_path = normpath(join(get_absolute_path(game.path), asset_path)) def build_file_iter(f, block_size): return StaticFileIter(file_asset_path, normpath(join(slug, asset_path)), f, block_size) def remove_ranges_start_response(status, headers, exc_info=None): if status == '200 OK': headers = [t for t in headers if t[0] != 'Accept-Ranges' and t[0] != 'Content-Range'] return start_response(status, headers, exc_info) # check if the request is already cached app = self.cached_apps.get(request_path) if app: environ['wsgi.file_wrapper'] = build_file_iter try: return app(environ, remove_ranges_start_response) except OSError as e: LOG.error(e) elif access(file_asset_path, R_OK): content_type, _ = guess_type(file_asset_path) if content_type in self.utf8_mimetypes: content_type += '; charset=utf-8' app = FileApp(file_asset_path, content_type=content_type) if asset_path.startswith('staticmax'): app.cache_control(max_age=self.staticmax_max_age) else: app.cache_control(max_age=0) self.cached_apps[request_path] = app environ['wsgi.file_wrapper'] = build_file_iter return app(environ, remove_ranges_start_response) start_response( '404 Not Found', [('Content-Type', 'text/html; charset=UTF-8'), ('Content-Length', '0')] ) return [''] return self.app(environ, start_response)
def __call__(self, environ, start_response): path_info = environ.get('PATH_INFO', '') if not path_info: # See if this is a static file hackishly mapped. if os.path.exists(self.directory) and os.path.isfile( self.directory): app = FileApp(self.directory) if self.cache_seconds: app.cache_control(max_age=int(self.cache_seconds)) return app(environ, start_response) return self.add_slash(environ, start_response) if path_info == '/': # @@: This should obviously be configurable filename = 'index.html' else: filename = request.path_info_pop(environ) directory = self.directory host = environ.get('HTTP_HOST') if self.directory_per_host and host: for host_key, host_val in self.directory_per_host.items(): if host_key in host: directory = host_val break full = os.path.join(directory, filename) if not os.path.exists(full): return self.not_found(environ, start_response) if os.path.isdir(full): # @@: Cache? return self.__class__(full)(environ, start_response) if environ.get('PATH_INFO') and environ.get('PATH_INFO') != '/': return self.error_extra_path(environ, start_response) if_none_match = environ.get('HTTP_IF_NONE_MATCH') if if_none_match: mytime = os.stat(full).st_mtime if str(mytime) == if_none_match: headers = [] ETAG.update(headers, mytime) start_response('304 Not Modified', headers) return [''] # empty body app = FileApp(full) if self.cache_seconds: app.cache_control(max_age=int(self.cache_seconds)) return app(environ, start_response)