Esempio n. 1
0
 def prepare(self,
             escape_func=html_escape,
             noescape=False,
             syntax=None,
             **ka):
     self.cache = {}
     enc = self.encoding
     self._str = lambda x: touni(x, enc)
     self._escape = lambda x: escape_func(touni(x, enc))
     self.syntax = syntax
     if noescape:
         self._str, self._escape = self._escape, self._str
Esempio n. 2
0
 def code(self):
     source = self.source
     if not source:
         with open(self.filename, 'rb') as f:
             source = f.read()
     try:
         source, encoding = touni(source), 'utf8'
     except UnicodeError:
         depr('Template encodings other than utf8 are no longer supported.'
              )  #0.11
         source, encoding = touni(source, 'latin1'), 'latin1'
     parser = StplParser(source, encoding=encoding, syntax=self.syntax)
     code = parser.translate()
     self.encoding = parser.encoding
     return code
Esempio n. 3
0
 def __init__(self, source, syntax=None, encoding='utf8'):
     self.source, self.encoding = touni(source, encoding), encoding
     self.set_syntax(syntax or self.default_syntax)
     self.code_buffer, self.text_buffer = [], []
     self.lineno, self.offset = 1, 0
     self.indent, self.indent_mod = 0, 0
     self.paren_depth = 0
Esempio n. 4
0
def parse_auth(header):
    """ Parse rfc2617 HTTP authentication header string (basic) and return (user,pass) tuple or None"""
    try:
        method, data = header.split(None, 1)
        if method.lower() == 'basic':
            user, pwd = touni(base64.b64decode(tob(data))).split(':', 1)
            return user, pwd
    except (KeyError, ValueError):
        return None
Esempio n. 5
0
    def set_cookie(self, name, value, secret=None, **options):
        ''' Create a new cookie or replace an old one. If the `secret` parameter is
            set, create a `Signed Cookie` (described below).

            :param name: the name of the cookie.
            :param value: the value of the cookie.
            :param secret: a signature key required for signed cookies.

            Additionally, this method accepts all RFC 2109 attributes that are
            supported by :class:`cookie.Morsel`, including:

            :param max_age: maximum age in seconds. (default: None)
            :param expires: a datetime object or UNIX timestamp. (default: None)
            :param domain: the domain that is allowed to read the cookie.
              (default: current domain)
            :param path: limits the cookie to a given path (default: current path)
            :param secure: limit the cookie to HTTPS connections (default: off).
            :param httponly: prevents client-side javascript to read this cookie
              (default: off, requires Python 2.6 or newer).

            If neither `expires` nor `max_age` is set (default), the cookie will
            expire at the end of the browser session (as soon as the browser
            window is closed).

            Signed cookies may store any pickle-able object and are
            cryptographically signed to prevent manipulation. Keep in mind that
            cookies are limited to 4kb in most browsers.

            Warning: Signed cookies are not encrypted (the client can still see
            the content) and not copy-protected (the client can restore an old
            cookie). The main intention is to make pickling and unpickling
            save, not to store secret information at client side.
        '''
        if not self._cookies:
            self._cookies = SimpleCookie()

        if secret:
            value = touni(cookie_encode((name, value), secret))
        elif not isinstance(value, basestring):
            raise TypeError('Secret key missing for non-string Cookie.')

        if len(value) > 4096: raise ValueError('Cookie value to long.')
        self._cookies[name] = value

        for key, value in options.items():
            if key == 'max_age':
                if isinstance(value, timedelta):
                    value = value.seconds + value.days * 24 * 3600
            if key == 'expires':
                if isinstance(value, (datedate, datetime)):
                    value = value.timetuple()
                elif isinstance(value, (int, float)):
                    value = time.gmtime(value)
                value = time.strftime("%a, %d %b %Y %H:%M:%S GMT", value)
            self._cookies[name][key.replace('_', '-')] = value