1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
|
# -*- coding: utf-8 -*-
class CMSFormFields:
UPPERCASE = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
LOWERCASE = 'abcdefghijklmnopqrstuvwxyz'
NUMBERS = '0123456789'
defaultCharset = LOWERCASE + UPPERCASE + NUMBERS + '-_. \t'
defaultCharsetBool = LOWERCASE + UPPERCASE + NUMBERS + ' \t'
defaultCharsetInt = NUMBERS + 'xXabcdefABCDEF- \t'
def __init__(self, forms):
assert isinstance(forms, dict)
self.__forms = forms
def getStr(self, name, default='', maxlen=32, charset=defaultCharset):
field = self.__forms.get(name, default.encode('UTF-8', 'strict'))
if field is None:
return None
assert isinstance(field, bytes)
field = field.decode('UTF-8', 'strict')
if maxlen is not None and len(field) > maxlen:
raise self.CMSPostException('Form data is too long.')
if charset is not None and [ c for c in field if c not in charset ]:
raise self.CMSPostException('Invalid character in form data')
return field
def getBool(self, name, default=False, maxlen=32, charset=defaultCharsetBool):
field = self.getStr(name, '', maxlen, charset)
if not field:
return default
s = field.lower()
if s in ("true", "yes", "on", "1"):
return True
if s in ("false", "no", "off", "0"):
return False
try:
return bool(int(s))
except ValueError:
return default
def getInt(self, name, default=0, maxlen=32, charset=defaultCharsetInt):
field = self.getStr(name, '', maxlen, charset)
if not field:
return default
try:
field = field.lower().strip()
if field.startswith('0x'):
return int(field[2:], 16)
return int(field)
except ValueError:
return default
# Import the post handler module file.
import importlib
import importlib.machinery
loader = importlib.machinery.SourceFileLoader(handler_mod_name, handler_mod_path)
module = loader.load_module()
module.CMSPostException = CMSPostException
CMSFormFields.CMSPostException = CMSPostException
# Get the post() handler function from the module.
post_handler = getattr(module, 'post', None)
if post_handler is None:
raise CMSPostException('No post() handler function found in module.')
# Add post.py directory to include search path so that
# the post handler can import from it.
import sys
if handler_mod_dir not in sys.path:
sys.path.insert(0, handler_mod_dir)
# Run the post handler.
reply_body, reply_mime = post_handler(
CMSFormFields(request_form_fields),
request_query,
)
# vim: ts=4 sw=4 expandtab
|