예제 #1
0
class PkgResourcesParser(StaticURLParser):

    def __init__(self, egg_or_spec, resource_name, manager=None, root_resource=None):
        if pkg_resources is None:
            raise NotImplementedError("This class requires pkg_resources.")
        if isinstance(egg_or_spec, (str, unicode)):
            self.egg = pkg_resources.get_distribution(egg_or_spec)
        else:
            self.egg = egg_or_spec
        self.resource_name = resource_name
        if manager is None:
            manager = pkg_resources.ResourceManager()
        self.manager = manager
        if root_resource is None:
            root_resource = resource_name
        self.root_resource = os.path.normpath(root_resource)

    def __repr__(self):
        return '<%s for %s:%r>' % (
            self.__class__.__name__,
            self.egg.project_name,
            self.resource_name)

    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.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 self.egg.has_resource(resource):
            return self.not_found(environ, start_response)
        if self.egg.resource_isdir(resource):
            # @@: Cache?
            child_root = self.root_resource is not None and self.root_resource or \
                self.resource_name
            return self.__class__(self.egg, resource, self.manager,
                                  root_resource=child_root)(environ, start_response)
        if environ.get('PATH_INFO') and environ.get('PATH_INFO') != '/':
            return self.error_extra_path(environ, start_response)
        
        type, encoding = mimetypes.guess_type(resource)
        if not type:
            type = 'application/octet-stream'
        # @@: I don't know what to do with the encoding.
        try:
            file = self.egg.get_resource_stream(self.manager, resource)
        except (IOError, OSError), e:
            exc = httpexceptions.HTTPForbidden(
                'You are not permitted to view this file (%s)' % e)
            return exc.wsgi_application(environ, start_response)
        start_response('200 OK',
                       [('content-type', type)])
        return fileapp._FileIter(file)
예제 #2
0
 def get(self, environ, start_response):
     is_head = environ['REQUEST_METHOD'].upper() == 'HEAD'
     headers = self.headers[:]
     if self.content_length is not None:
         CONTENT_LENGTH.update(headers, self.content_length)
     start_response('200 OK', headers)
     if is_head:
         return ['']
     self.content_file.seek(0)
     return fileapp._FileIter(self.content_file, size=self.content_length)
예제 #3
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 self.egg.has_resource(resource):
            return self.not_found(environ, start_response)
        if self.egg.resource_isdir(resource):
            # @@: Cache?
            child_root = self.root_resource is not None and self.root_resource or \
                self.resource_name
            return self.__class__(self.egg,
                                  resource,
                                  self.manager,
                                  root_resource=child_root)(environ,
                                                            start_response)
        if environ.get('PATH_INFO') and environ.get('PATH_INFO') != '/':
            return self.error_extra_path(environ, start_response)

        type, encoding = mimetypes.guess_type(resource)
        if not type:
            type = 'application/octet-stream'
        # @@: I don't know what to do with the encoding.
        try:
            file = self.egg.get_resource_stream(self.manager, resource)
        except (IOError, OSError) as e:
            exc = httpexceptions.HTTPForbidden(
                'You are not permitted to view this file (%s)' % e)
            return exc.wsgi_application(environ, start_response)
        start_response('200 OK', [('content-type', type)])
        return fileapp._FileIter(file)
예제 #4
0
파일: urlparser.py 프로젝트: 10sr/hue
    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 self.egg.has_resource(resource):
            return self.not_found(environ, start_response)
        if self.egg.resource_isdir(resource):
            # @@: Cache?
            child_root = self.root_resource is not None and self.root_resource or \
                self.resource_name
            return self.__class__(self.egg, resource, self.manager,
                                  root_resource=child_root)(environ, start_response)
        if environ.get('PATH_INFO') and environ.get('PATH_INFO') != '/':
            return self.error_extra_path(environ, start_response)

        type, encoding = mimetypes.guess_type(resource)
        if not type:
            type = 'application/octet-stream'
        # @@: I don't know what to do with the encoding.
        try:
            file = self.egg.get_resource_stream(self.manager, resource)
        except (IOError, OSError) as e:
            exc = httpexceptions.HTTPForbidden(
                'You are not permitted to view this file (%s)' % e)
            return exc.wsgi_application(environ, start_response)
        start_response('200 OK',
                       [('content-type', type)])
        return fileapp._FileIter(file)