def dump_cookie(key, value='', max_age=None, expires=None, path='/', domain=None, secure=None, httponly=False, charset='utf-8', sync_expires=True): try: key = str(key) except UnicodeError: raise TypeError('invalid key %r' % key) if isinstance(value, unicode): value = value.encode(charset) value = quote_header_value(value) morsel = _ExtendedMorsel(key, value) if isinstance(max_age, timedelta): max_age = max_age.days * 60 * 60 * 24 + max_age.seconds if expires is not None: if not isinstance(expires, basestring): expires = cookie_date(expires) morsel['expires'] = expires elif max_age is not None and sync_expires: morsel['expires'] = cookie_date(time() + max_age) for k, v in (('path', path), ('domain', domain), ('secure', secure), ('max-age', max_age), ('httponly', httponly)): if v is not None and v is not False: morsel[k] = str(v) return morsel.output(header='').lstrip()
def dump_cookie(key, value = '', max_age = None, expires = None, path = '/', domain = None, secure = None, httponly = False, charset = 'utf-8', sync_expires = True): try: key = str(key) except UnicodeError: raise TypeError('invalid key %r' % key) if isinstance(value, unicode): value = value.encode(charset) value = quote_header_value(value) morsel = _ExtendedMorsel(key, value) if isinstance(max_age, timedelta): max_age = max_age.days * 60 * 60 * 24 + max_age.seconds if expires is not None: if not isinstance(expires, basestring): expires = cookie_date(expires) morsel['expires'] = expires elif max_age is not None and sync_expires: morsel['expires'] = cookie_date(time() + max_age) for k, v in (('path', path), ('domain', domain), ('secure', secure), ('max-age', max_age), ('httponly', httponly)): if v is not None and v is not False: morsel[k] = str(v) return morsel.output(header='').lstrip()
def dump_cookie(key, value='', max_age=None, expires=None, path='/', domain=None, secure=None, httponly=False, charset='utf-8', sync_expires=True): """Creates a new Set-Cookie header without the ``Set-Cookie`` prefix The parameters are the same as in the cookie Morsel object in the Python standard library but it accepts unicode data, too. :param max_age: should be a number of seconds, or `None` (default) if the cookie should last only as long as the client's browser session. Additionally `timedelta` objects are accepted, too. :param expires: should be a `datetime` object or unix timestamp. :param path: limits the cookie to a given path, per default it will span the whole domain. :param domain: Use this if you want to set a cross-domain cookie. For example, ``domain=".example.com"`` will set a cookie that is readable by the domain ``www.example.com``, ``foo.example.com`` etc. Otherwise, a cookie will only be readable by the domain that set it. :param secure: The cookie will only be available via HTTPS :param httponly: disallow JavaScript to access the cookie. This is an extension to the cookie standard and probably not supported by all browsers. :param charset: the encoding for unicode values. :param sync_expires: automatically set expires if max_age is defined but expires not. """ try: key = str(key) except UnicodeError: raise TypeError('invalid key %r' % key) if isinstance(value, unicode): value = value.encode(charset) value = quote_header_value(value) morsel = _ExtendedMorsel(key, value) if isinstance(max_age, timedelta): max_age = (max_age.days * 60 * 60 * 24) + max_age.seconds if expires is not None: if not isinstance(expires, basestring): expires = cookie_date(expires) morsel['expires'] = expires elif max_age is not None and sync_expires: morsel['expires'] = cookie_date(time() + max_age) if domain and ':' in domain: # The port part of the domain should NOT be used. Strip it domain = domain.split(':', 1)[0] if domain: assert '.' in domain, ( "Setting \"domain\" for a cookie on a server running localy (ex: " "localhost) is not supportted by complying browsers. You should " "have something like: \"127.0.0.1 localhost dev.localhost\" on " "your hosts file and then point your server to run on " "\"dev.localhost\" and also set \"domain\" for \"dev.localhost\"" ) for k, v in (('path', path), ('domain', domain), ('secure', secure), ('max-age', max_age), ('httponly', httponly)): if v is not None and v is not False: morsel[k] = str(v) return morsel.output(header='').lstrip()
def test_success(self): response = self.request() self.assertEqual(response.status_code, 200) self.assertEqual(response.mimetype, self.mimetype) self.assertEqual( response.headers.get("content-disposition"), "attachment; filename=%s" % quote_header_value(self.filename)) self.assertTupleEqual(response.get_etag(), (self.digest, False)) self.assertEqual(response.accept_ranges, "bytes") self.assertGreater(response.cache_control.max_age, 0) self.assertTrue(response.cache_control.private) self.assertFalse(response.cache_control.public) self.assertEqual(response.get_data(), self.content) self.file_cacher.get_file.assert_called_once_with(self.digest)
def to_header(self): d = dict(self) auth_type = d.pop('__auth_type__', None) or 'basic' return '%s %s' % (auth_type.title(), ', '.join([ '%s=%s' % (key, quote_header_value(value, allow_token=key not in self._require_quoting)) for key, value in d.iteritems() ]))