1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
33 """ Interface for cookie codec factories """
34
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
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
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
75 """ Initialization """
76 pass
77
79 """ Determine codec instance """
80 return self
81
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
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
117 """ Encode the cookie value """
118 raise NotImplementedError()
119
121 """ Decode the cookie value """
122 raise NotImplementedError()
123
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
142
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
152
153 @staticmethod
155 """ Translate octal string back to number to char """
156 return chr(int(match.group(1), 8))
157
160 """
161 Unicode cookies
162
163 The codecs takes and gives unicode, translates them using the
164 ``unicode_escape`` codec.
165 """
166
168 """ Encode a cookie value """
169 return self.quote(unicode(value).encode('unicode_escape'))
170
172 """ Decode a cookie value """
173 return self.unquote(value).decode('unicode_escape')
174