Exemplo n.º 1
0
 def send_update(self, update):
     response = Response(
         response=update['body'],
         content_type=update['content_type'],
     )
     response.last_modified = update['published_on']
     response.headers['If-Modified-Since'] = update['published_on']
     
     return response
Exemplo n.º 2
0
    def handle (self, request):
        """
            Handle request for an image
        """
        
        name, path, type = self.lookup(request.path)
        
        # determine handler
        if not type:
            return self.handle_dir(request, name, path)

        elif not request.args:
            return self.handle_image(request, name, path)

        elif 'w' in request.args and 'h' in request.args and 'x' in request.args and 'y' in request.args:
            render_func = self.render_region

        elif 'x' in request.args and 'y' in request.args:
            render_func = self.render_tile

        else:
            raise exceptions.BadRequest("Unknown args")
        
        # handle image
        image, name = self.open(request.path)

        # http caching
        mtime = image.cache_mtime()

        if 't' in request.args:
            try:
                ttime = datetime.datetime.utcfromtimestamp(int(request.args['t']))
            except ValueError:
                ttime = None
        else:
            ttime = None

        if request.if_modified_since and mtime == request.if_modified_since:
            response = Response(status=304)
        else:
            # render 
            png = render_func(request, image)
        
            response = Response(png, content_type='image/png')
        
        # cache out
        response.last_modified = mtime

        if not ttime:
            # cached item may change while url stays the same
            response.headers['Cache-Control'] = 'max-age={min_age:d}'.format(min_age=self.MIN_AGE)

        elif ttime == mtime:
            # url will change if content changes
            response.headers['Cache-Control'] = 'max-age={max_age:d}'.format(max_age=self.MAX_AGE)

        else:
            # XXX: mismatch wtf
            response.headers['Cache-Control'] = 'must-revalidate'

        return response