Exemplo n.º 1
0
 def serve(self, f, environ, start_response, download=False, **kwargs):
     if isinstance(f,list):
         fullpath = self.path(*f[1:])
     elif isinstance(f,file):
         fullpath = f.name
     else:
         fullpath = f
     if not fullpath:
         return self.site.not_found_exception(environ, start_response)
     if not os.path.isabs(fullpath):
         fullpath = os.path.normpath(os.path.join(self.site_path, fullpath))
     existing_doc = os.path.exists(fullpath)
     if not existing_doc and '_lazydoc' in kwargs:
         existing_doc = self.build_lazydoc(kwargs['_lazydoc'],ext=os.path.splitext(fullpath)[-1])
     if not existing_doc:
         if kwargs.get('_lazydoc'):
             headers = []
             start_response('200 OK', headers)
             return ['']
         return self.site.not_found_exception(environ, start_response)
     if_none_match = environ.get('HTTP_IF_NONE_MATCH')
     if if_none_match:
         mytime = os.stat(fullpath).st_mtime
         if str(mytime) == if_none_match:
             headers = []
             ETAG.update(headers, mytime)
             start_response('304 Not Modified', headers)
             return [''] # empty body
     file_args = dict()
     if download:
         file_args['content_disposition'] = "attachment; filename=%s" % os.path.basename(fullpath)
     file_responder = fileapp.FileApp(fullpath, **file_args)
     if self.site.cache_max_age:
         file_responder.cache_control(max_age=self.site.cache_max_age)
     return file_responder(environ, start_response)
Exemplo n.º 2
0
 def __call__(self, environ, start_response):
     path_info = environ.get('PATH_INFO', '')
     if not path_info:
         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.FileApp(
         full,
         headers=[('Access-Control-Allow-Origin', '*'),
                  ('Access-Control-Allow-Headers',
                   'Content-Type, X-Requested-With, X-Requested-By'),
                  ('Access-Control-Allow-Methods', 'POST, GET')])
     if self.cache_seconds:
         app.cache_control(max_age=int(self.cache_seconds))
     return app(environ, start_response)
Exemplo n.º 3
0
 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.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.FileApp(full)
     if self.cache_seconds:
         app.cache_control( max_age=int( self.cache_seconds ) )
     return app(environ, start_response)
Exemplo n.º 4
0
 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.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.FileApp(full)
     if self.cache_seconds:
         app.cache_control( max_age = int( self.cache_seconds ) )
     return app(environ, start_response)
Exemplo n.º 5
0
 def serve(self,
           f,
           environ,
           start_response,
           download=False,
           download_name=None,
           **kwargs):
     if isinstance(f, list):
         fullpath = self.path(*f[1:])
     elif isinstance(f, file):
         fullpath = f.name
     else:
         fullpath = f
     if not fullpath:
         return self.site.not_found_exception(environ, start_response)
     if not os.path.isabs(fullpath):
         fullpath = os.path.normpath(os.path.join(self.site_path, fullpath))
     existing_doc = os.path.exists(fullpath)
     if not existing_doc and '_lazydoc' in kwargs:
         existing_doc = self.build_lazydoc(
             kwargs['_lazydoc'], ext=os.path.splitext(fullpath)[-1])
     if not existing_doc:
         if kwargs.get('_lazydoc'):
             headers = []
             start_response('200 OK', headers)
             return ['']
         return self.site.not_found_exception(environ, start_response)
     if_none_match = environ.get('HTTP_IF_NONE_MATCH')
     if if_none_match:
         if_none_match = if_none_match.replace('"', '')
         stats = os.stat(fullpath)
         mytime = stats.st_mtime
         size = stats.st_size
         my_none_match = "%s-%s" % (str(mytime), str(size))
         if my_none_match == if_none_match:
             headers = []
             ETAG.update(headers, my_none_match)
             start_response('304 Not Modified', headers)
             return ['']  # empty body
     file_args = dict()
     if download or download_name:
         download_name = download_name or os.path.basename(fullpath)
         file_args[
             'content_disposition'] = "attachment; filename=%s" % download_name
     file_responder = fileapp.FileApp(fullpath, **file_args)
     if self.site.cache_max_age:
         file_responder.cache_control(max_age=self.site.cache_max_age)
     return file_responder(environ, start_response)
Exemplo n.º 6
0
    def __call__(self, environ, start_response):
        path_info = environ.get('PATH_INFO', '')
        if not path_info:
            return self.add_slash(environ, start_response)
        directory = "%s" % self.directory
        if not path_info.startswith(
                '/%s/' % self.version) and version_re.match(
                    path_info) is None and directory == self.root_directory:
            directory = os.path.join(directory, self.version)
        if path_info == '/':
            # @@: This should obviously be configurable
            filename = 'index.html'
        else:
            filename = request.path_info_pop(environ)
        full = self.normpath(os.path.join(directory, filename))
        if not full.startswith(self.root_directory):
            # Out of bounds
            return self.not_found(environ, start_response)
        if not os.path.exists(full):
            return self.not_found(environ, start_response)
        if os.path.isdir(full):
            # @@: Cache?
            return self.__class__(full,
                                  root_directory=self.root_directory,
                                  version=self.version,
                                  cache_max_age=self.cache_max_age)(
                                      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 = []
                ## FIXME: probably should be
                ## ETAG.update(headers, '"%s"' % mytime)
                ETAG.update(headers, mytime)
                start_response('304 Not Modified', headers)
                return ['']  # empty body

        fa = self.make_app(full)
        if self.cache_max_age:
            fa.cache_control(max_age=self.cache_max_age)
        return fa(environ, start_response)
Exemplo n.º 7
0
Arquivo: static.py Projeto: anantn/f1
    def __call__(self, environ, start_response):
        path_info = environ.get("PATH_INFO", "")
        if not path_info:
            return self.add_slash(environ, start_response)
        directory = "%s" % self.directory
        if (
            not path_info.startswith("/%s/" % self.version)
            and version_re.match(path_info) is None
            and directory == self.root_directory
        ):
            directory = os.path.join(directory, self.version)
        if path_info == "/":
            # @@: This should obviously be configurable
            filename = "index.html"
        else:
            filename = request.path_info_pop(environ)
        full = self.normpath(os.path.join(directory, filename))
        if not full.startswith(self.root_directory):
            # Out of bounds
            return self.not_found(environ, start_response)
        if not os.path.exists(full):
            return self.not_found(environ, start_response)
        if os.path.isdir(full):
            # @@: Cache?
            return self.__class__(
                full, root_directory=self.root_directory, version=self.version, cache_max_age=self.cache_max_age
            )(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 = []
                ## FIXME: probably should be
                ## ETAG.update(headers, '"%s"' % mytime)
                ETAG.update(headers, mytime)
                start_response("304 Not Modified", headers)
                return [""]  # empty body

        fa = self.make_app(full)
        if self.cache_max_age:
            fa.cache_control(max_age=self.cache_max_age)
        return fa(environ, start_response)
Exemplo n.º 8
0
    def __call__(self, environ, start_response):
        path_info = environ.get('PATH_INFO', '')
        if not path_info:
            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)
        resource = os.path.normcase(
            os.path.normpath(self.resource_name + '/' + filename))
        if ((self.root_resource is not None)
                and (not resource.startswith(self.root_resource))):
            # Out of bounds
            return self.not_found(environ, start_response)
        if not pkg_resources.resource_exists(self.package_name, resource):
            return self.not_found(environ, start_response)
        if pkg_resources.resource_isdir(self.package_name, resource):
            # @@: Cache?
            child_root = (self.root_resource is not None and self.root_resource
                          or self.resource_name)
            return self.__class__(self.package_name,
                                  resource,
                                  root_resource=child_root,
                                  cache_max_age=self.cache_max_age)(
                                      environ, start_response)
        if (environ.get('PATH_INFO')
                and environ.get('PATH_INFO') != '/'):  # pragma: no cover
            return self.error_extra_path(environ, start_response)
        full = pkg_resources.resource_filename(self.package_name, resource)
        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

        fa = self.make_app(full)
        if self.cache_max_age:
            fa.cache_control(max_age=self.cache_max_age)
        return fa(environ, start_response)
Exemplo n.º 9
0
 def __call__(self, environ, start_response):
     path_info = environ['PATH_INFO']
     #log.debug ('static search for %s' % path_info)
     if path_info in self.files:
         path, app = self.files.get(path_info)
         if not app:
             app = FileApp (path).cache_control (max_age=60*60*24*7*6) #6 weeks
             self.files[path_info] = (path, app)
         log.info ( "STATIC REQ %s" %  path_info)
         if_none_match = environ.get('HTTP_IF_NONE_MATCH')
         if if_none_match:
             mytime = os.stat(path).st_mtime
             if str(mytime) == if_none_match:
                 headers = []
                 ETAG.update(headers, mytime)
                 start_response('304 Not Modified', headers)
                 return [''] # empty body
     else:
         app = HTTPNotFound(comment=path_info)
     return app(environ, start_response)
Exemplo n.º 10
0
    def __call__(self, environ, start_response):
        path_info = environ.get('PATH_INFO', '')
        if not path_info:
            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)
        resource = os.path.normcase(os.path.normpath(
                    self.resource_name + '/' + filename))
        if ( (self.root_resource is not None) and
            (not resource.startswith(self.root_resource)) ):
            # Out of bounds
            return self.not_found(environ, start_response)
        if not pkg_resources.resource_exists(self.package_name, resource):
            return self.not_found(environ, start_response)
        if pkg_resources.resource_isdir(self.package_name, resource):
            # @@: Cache?
            child_root = (self.root_resource is not None and
                          self.root_resource or self.resource_name)
            return self.__class__(
                self.package_name, resource, root_resource=child_root,
                cache_max_age=self.cache_max_age)(environ, start_response)
        if (environ.get('PATH_INFO')
            and environ.get('PATH_INFO') != '/'): # pragma: no cover
            return self.error_extra_path(environ, start_response) 
        full = pkg_resources.resource_filename(self.package_name, resource)
        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

        fa = self.make_app(full)
        if self.cache_max_age:
            fa.cache_control(max_age=self.cache_max_age)
        return fa(environ, start_response)
Exemplo n.º 11
0
 def serve(self, path_list, environ, start_response, download=False, **kwargs):
     fullpath = self.path(*path_list[1:])
     if fullpath and not os.path.isabs(fullpath):
         fullpath = os.path.normpath(os.path.join(self.site_path, fullpath))
     if fullpath and not os.path.exists(fullpath):
         return self.site.not_found_exception(environ, start_response)
     if_none_match = environ.get('HTTP_IF_NONE_MATCH')
     if if_none_match:
         mytime = os.stat(fullpath).st_mtime
         if str(mytime) == if_none_match:
             headers = []
             ETAG.update(headers, mytime)
             start_response('304 Not Modified', headers)
             return [''] # empty body
     file_args = dict()
     if download:
         file_args['content_disposition'] = "attachment; filename=%s" % os.path.basename(fullpath)
     file_responder = fileapp.FileApp(fullpath, **file_args)
     if self.site.cache_max_age:
         file_responder.cache_control(max_age=self.site.cache_max_age)
     return file_responder(environ, start_response)