Exemplo n.º 1
0
    def respond(self, request):
        """Serve a static file off of the filesystem.

        In staging and deployment modes, we honor any 'If-Modified-Since'
        header, an HTTP header used for caching.

        """

        fs_path = self.translate(request.path)
        fs_path = self.validate(request.path, fs_path)
        fs_path = self.find_default(fs_path)

        ims = request.headers.get('If-Modified-Since', '')

        # Get basic info from the filesystem and start building a response.
        # =================================================================

        stats = os.stat(fs_path)
        mtime = stats[stat.ST_MTIME]
        size = stats[stat.ST_SIZE]
        content_type = mimetypes.guess_type(fs_path)[0] or 'text/plain'
        response = Response(200)

        # Support 304s, but only in deployment mode.
        # ==========================================

        if mode.IS_DEPLOYMENT or mode.IS_STAGING:
            if ims:
                mod_since = rfc822.parsedate(ims)
                last_modified = time.gmtime(mtime)
                if last_modified[:6] <= mod_since[:6]:
                    response.code = 304

        # Finish building the response and return it.
        # ===========================================

        response.headers['Last-Modified'] = rfc822.formatdate(mtime)
        response.headers['Content-Type'] = content_type
        response.headers['Content-Length'] = size
        if response.code != 304:
            response.body = open(fs_path).read()
        return response
Exemplo n.º 2
0
 def respond(self, request):
     response = Response(200, "Greetings, program!")
     response.code = "wheee!"
     raise response