Package wtf :: Package app :: Module cookie
[hide private]
[frames] | no frames]

Source Code for Module wtf.app.cookie

  1  # -*- coding: ascii -*- 
  2  # 
  3  # Copyright 2007-2012 
  4  # Andr\xe9 Malo or his licensors, as applicable 
  5  # 
  6  # Licensed under the Apache License, Version 2.0 (the "License"); 
  7  # you may not use this file except in compliance with the License. 
  8  # You may obtain a copy of the License at 
  9  # 
 10  #     http://www.apache.org/licenses/LICENSE-2.0 
 11  # 
 12  # Unless required by applicable law or agreed to in writing, software 
 13  # distributed under the License is distributed on an "AS IS" BASIS, 
 14  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
 15  # See the License for the specific language governing permissions and 
 16  # limitations under the License. 
 17  """ 
 18  Request object 
 19  ============== 
 20   
 21  This module implements a request object. 
 22  """ 
 23  __author__ = u"Andr\xe9 Malo" 
 24  __docformat__ = "restructuredtext en" 
 25   
 26  import itertools as _it 
 27  import re as _re 
 28   
 29  from wtf import httputil as _httputil 
30 31 32 -class CookieCodecFactoryInterface(object):
33 """ Interface for cookie codec factories """ 34
35 - def __init__(self, config, opts, args):
36 """ 37 Initialization 38 39 :Parameters: 40 - `config`: Configuration 41 - `opts`: Command line options 42 - `args`: Positional command line arguments 43 44 :Types: 45 - `config`: `wtf.config.Config` 46 - `opts`: ``optparse.OptionContainer`` 47 - `args`: ``list`` 48 """
49
50 - def __call__(self):
51 """ 52 Create the codec instance (doesn't have to be a new one) 53 54 :return: The codec instance 55 :rtype: `CookieCodecInterface` 56 """
57
58 59 -class BaseCookieCodec(object):
60 """ 61 Base class for some codecs 62 63 :CVariables: 64 - `_UNSAFE_SEARCH`: Unsafe char detection function 65 66 :Types: 67 - `_UNSAFE_SEARCH`: ``callable`` 68 """ 69 __implements__ = [ 70 CookieCodecFactoryInterface, _httputil.CookieCodecInterface 71 ] 72 UNSAFE_SEARCH = _httputil.CookieMaker.UNSAFE_SEARCH 73
74 - def __init__(self, config, opts, args):
75 """ Initialization """ 76 pass
77
78 - def __call__(self):
79 """ Determine codec instance """ 80 return self
81
82 - def quote(self, value):
83 """ 84 Quote a value if necessary 85 86 :Parameters: 87 - `value`: The value to inspect 88 89 :Types: 90 - `value`: ``str`` 91 92 :return: The quoted value (or the original if no quoting is needed) 93 :rtype: ``str`` 94 """ 95 if self.UNSAFE_SEARCH(value): 96 return '"%s"' % value.replace('"', '\\"') 97 return value
98
99 - def unquote(self, value):
100 """ 101 Unquote a value if applicable 102 103 :Parameters: 104 - `value`: The value to inspect 105 106 :Types: 107 - `value`: ``str`` 108 109 :return: The unquoted value (or the original if no unquoting is needed) 110 :rtype: ``str`` 111 """ 112 if value.startswith('"') and value.endswith('"'): 113 return value[1:-1].replace('\\"', '"') 114 return value
115
116 - def encode(self, value):
117 """ Encode the cookie value """ 118 raise NotImplementedError()
119
120 - def decode(self, value):
121 """ Decode the cookie value """ 122 raise NotImplementedError()
123
124 125 -class DefaultCookie(BaseCookieCodec):
126 """ 127 Standard python behaviour 128 129 :CVariables: 130 - `_TRANS`: Translation getter 131 - `_UNTRANS`: Untranslation substituter 132 133 :Types: 134 - `_TRANS`: ``callable`` 135 - `_UNTRANS`: ``callable`` 136 """ 137 _TRANS = dict([('\\', '\\\\')] + [(chr(_key), "\\%03o" % _key) 138 for _key in _it.chain(xrange(32), xrange(127, 256)) 139 ]).get 140 _UNTRANS = _re.compile(r'\\([0-3][0-7][0-7])').sub 141 del _key # pylint: disable = W0631 142
143 - def encode(self, value):
144 """ Encode a cookie value """ 145 if self.UNSAFE_SEARCH(value): 146 value = ''.join(map(self._TRANS, value, value)) 147 return self.quote(value)
148
149 - def decode(self, value):
150 """ Decode a cookie value """ 151 return self._UNTRANS(self._untranssub, self.unquote(value))
152 153 @staticmethod
154 - def _untranssub(match):
155 """ Translate octal string back to number to char """ 156 return chr(int(match.group(1), 8))
157
158 159 -class UnicodeCookie(BaseCookieCodec):
160 """ 161 Unicode cookies 162 163 The codecs takes and gives unicode, translates them using the 164 ``unicode_escape`` codec. 165 """ 166
167 - def encode(self, value):
168 """ Encode a cookie value """ 169 return self.quote(unicode(value).encode('unicode_escape'))
170
171 - def decode(self, value):
172 """ Decode a cookie value """ 173 return self.unquote(value).decode('unicode_escape')
174