Exemple #1
0
    def __call__(self, environ, start_response):
        request = webob.Request(environ)

        m = re_bitblt.search(request.path_info)
        if m is not None:
            width = m.group('width')
            height = m.group('height')
            signature = m.group('signature')
            verified = verify_signature(width, height, self.secret, signature)

            # remove bitblt part in path info
            request.path_info = re_bitblt.sub("", request.path_info)
        else:
            verified = width = height = None
            
        response = request.get_response(self.app)

        if response.content_type and response.content_type.startswith('text/html'):
            if not len(response.body):
                return response(environ, start_response)
            if self.limit_to_application_url:
                app_url = request.application_url
            else:
                app_url = None
            response.body = rewrite_image_tags(response.body, self.secret,
                                               app_url=app_url,
                                               try_xhtml=self.try_xhtml)
        
        if response.content_type and response.content_type.startswith('image/'):
            if verified and (width or height):
                try:
                    if width == 'None':
                        width = None
                    else:
                        width = int(width)
                    if height == 'None':
                        height = None
                    else:
                        height = int(height)
                    size = (width, height)
                except (ValueError, TypeError):
                    raise ValueError(
                        "Width and height parameters must be integers.")

                app_iter = response.app_iter
                if not hasattr(app_iter, 'read'):
                    app_iter = StringIO("".join(app_iter))

                body = self.process(app_iter, size)
                response.body = body
            
        return response(environ, start_response)
Exemple #2
0
    def __call__(self, environ, start_response):
        path_info = environ['PATH_INFO']
        m = re_bitblt.search(path_info)
        if m is not None:
            width = m.group('width')
            height = m.group('height')
            signature = m.group('signature')
            verified = verify_signature(width, height, self.secret, signature)

            # remove bitblt part in path info
            full_path_info = path_info
            environ['PATH_INFO'] = re_bitblt.sub("", path_info)
        else:
            verified = width = height = None

        request = webob.Request(environ)
        response = request.get_response(self.app)

        if response.content_type and \
               response.content_type.startswith('text/html') and \
               response.charset:
            if not len(response.body):
                return response(environ, start_response)
            if self.limit_to_application_url:
                app_url = request.application_url
            else:
                app_url = None

            response.unicode_body = rewrite_image_tags(
                response.unicode_body, self.secret,
                app_url=app_url)

        if response.content_type and response.content_type.startswith('image/'):
            if verified and (width or height):
                try:
                    if width == 'None':
                        width = None
                    else:
                        width = int(width)
                    if height == 'None':
                        height = None
                    else:
                        height = int(height)

                    if width <= 0:
                        width = 1
                    if height <= 0:
                        height = 1

                    size = (width, height)
                except (ValueError, TypeError):
                    raise ValueError(
                        "Width and height parameters must be integers.")

                if request.method == 'HEAD':
                    # Remove the content_lenght header as it's guarenteed
                    # to be wrong. If this were not a HEAD, we would resize.
                    response.content_length = None
                    # Don't attempt to resize, our body may not be there.
                    return response(environ, start_response)

                app_iter = response.app_iter
                if not hasattr(app_iter, 'read'):
                    app_iter = StringIO("".join(app_iter))

                if self.cache:
                    cache_key = urlsafe_b64encode(full_path_info)
                    cache_file = os.path.join(self.cache, cache_key)
                    if os.path.exists(cache_file):
                        f = open(cache_file)
                        body = f.read()
                        f.close()
                    else:
                        body = self.process(app_iter, size)
                        f = open(cache_file, 'w+')
                        f.write(body)
                        f.close()
                else:
                    body = self.process(app_iter, size)
                response.body = body

        return response(environ, start_response)
Exemple #3
0
    def __call__(self, environ, start_response):
        path_info = environ['PATH_INFO']
        m = re_bitblt.search(path_info)
        if m is not None:
            width = m.group('width')
            height = m.group('height')
            signature = m.group('signature')
            verified = verify_signature(width, height, self.secret, signature)

            # remove bitblt part in path info
            full_path_info = path_info
            environ['PATH_INFO'] = re_bitblt.sub("", path_info)
        else:
            verified = width = height = None

        request = webob.Request(environ)
        response = request.get_response(self.app)

        if response.content_type and \
               response.content_type.startswith('text/html') and \
               response.charset:
            if not len(response.body):
                return response(environ, start_response)
            if self.limit_to_application_url:
                app_url = request.application_url
            else:
                app_url = None

            response.unicode_body = rewrite_image_tags(response.unicode_body,
                                                       self.secret,
                                                       app_url=app_url)

        if response.content_type and response.content_type.startswith(
                'image/'):
            if verified and (width or height):
                try:
                    if width == 'None':
                        width = None
                    else:
                        width = int(width)
                    if height == 'None':
                        height = None
                    else:
                        height = int(height)
                    size = (width, height)
                except (ValueError, TypeError):
                    raise ValueError(
                        "Width and height parameters must be integers.")

                if request.method == 'HEAD':
                    # Remove the content_lenght header as it's guarenteed
                    # to be wrong. If this were not a HEAD, we would resize.
                    response.content_length = None
                    # Don't attempt to resize, our body may not be there.
                    return response(environ, start_response)

                app_iter = response.app_iter
                if not hasattr(app_iter, 'read'):
                    app_iter = StringIO("".join(app_iter))

                if self.cache:
                    cache_key = urlsafe_b64encode(full_path_info)
                    cache_file = os.path.join(self.cache, cache_key)
                    if os.path.exists(cache_file):
                        f = open(cache_file)
                        body = f.read()
                        f.close()
                    else:
                        body = self.process(app_iter, size)
                        f = open(cache_file, 'w+')
                        f.write(body)
                        f.close()
                else:
                    body = self.process(app_iter, size)
                response.body = body

        return response(environ, start_response)