Exemple #1
0
 def test_max_age_expiration(self):
     "Cookie will expire if max_age is provided"
     response = wsgi.WsgiResponse()
     response.set_cookie('max_age', max_age=10)
     max_age_cookie = response.cookies['max_age']
     self.assertEqual(max_age_cookie['max-age'], 10)
     self.assertEqual(max_age_cookie['expires'], http_date(time.time()+10))
Exemple #2
0
def file_response(request, filepath, block=None, status_code=None,
                  content_type=None, encoding=None, cache_control=None):
    """Utility for serving a local file

    Typical usage::

        from pulsar.apps import wsgi

        class MyRouter(wsgi.Router):

            def get(self, request):
                return wsgi.file_response(request, "<filepath>")

    :param request: Wsgi request
    :param filepath: full path of file to serve
    :param block: Optional block size (default 1MB)
    :param status_code: Optional status code (default 200)
    :return: a :class:`~.WsgiResponse` object
    """
    file_wrapper = request.get('wsgi.file_wrapper')
    if os.path.isfile(filepath):
        response = request.response
        info = os.stat(filepath)
        size = info[stat.ST_SIZE]
        modified = info[stat.ST_MTIME]
        header = request.get('HTTP_IF_MODIFIED_SINCE')
        if not was_modified_since(header, modified, size):
            response.status_code = 304
        else:
            if not content_type:
                content_type, encoding = mimetypes.guess_type(filepath)
            file = open(filepath, 'rb')
            response.headers['content-length'] = str(size)
            response.content = file_wrapper(file, block)
            response.content_type = content_type
            response.encoding = encoding
            if status_code:
                response.status_code = status_code
            else:
                response.headers["Last-Modified"] = http_date(modified)
            if cache_control:
                etag = digest('modified: %d - size: %d' % (modified, size))
                cache_control(response.headers, etag=etag)
        return response
    raise Http404
Exemple #3
0
 def test_http_date(self):
     now = time.time()
     fmt = http_date(now)
     self.assertTrue(fmt.endswith(' GMT'))
     self.assertEqual(fmt[3:5], ', ')
Exemple #4
0
 def test_http_date(self):
     now = time.time()
     fmt = http_date(now)
     self.assertTrue(fmt.endswith(' GMT'))
     self.assertEqual(fmt[3:5], ', ')
Exemple #5
0
 def test_http_date_cython(self):
     http_date(time())