-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdumdum.py
476 lines (421 loc) · 17 KB
/
dumdum.py
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
# -*- coding: utf-8 -*-
"""
A grammar to define "dummy" endpoints and behaviors based on parameters.
"""
import os, sys, json, re, time, logging
from urlparse import urlparse
from pyparsing import *
from cgi import parse_qs
import pprint
logger = logging.getLogger(__name__)
# Debug stuff
PP = pprint.PrettyPrinter(indent=2)
def dbg(o):
if 'DUMDEBUG' in os.environ:
print('DEBUG ---------------->')
PP.pprint(o)
print('---------------------->')
# Status codes
http_status_codes_map = {
100: "Continue",
101: "Switching Protocols",
102: "Processing",
200: "OK",
201: "Created",
202: "Accepted",
203: "Non-authoritative Information",
204: "No Content",
205: "Reset Content",
206: "Partial Content",
207: "Multi-Status",
208: "Already Reported",
226: "IM Used",
300: "Multiple Choices",
301: "Moved Permanently",
302: "Found",
303: "See Other",
304: "Not Modified",
305: "Use Proxy",
307: "Temporary Redirect",
308: "Permanent Redirect",
400: "Bad Request",
401: "Unauthorized",
402: "Payment Required",
403: "Forbidden",
404: "Not Found",
405: "Method Not Allowed",
406: "Not Acceptable",
407: "Proxy Authentication Required",
408: "Request Timeout",
409: "Conflict",
410: "Gone",
411: "Length Required",
412: "Precondition Failed",
413: "Payload Too Large",
414: "Request-URI Too Long",
415: "Unsupported Media Type",
416: "Requested Range Not Satisfiable",
417: "Expectation Failed",
418: "I'm a teapot",
421: "Misdirected Request",
422: "Unprocessable Entity",
423: "Locked",
424: "Failed Dependency",
426: "Upgrade Required",
428: "Precondition Required",
429: "Too Many Requests",
431: "Request Header Fields Too Large",
444: "Connection Closed Without Response",
451: "Unavailable For Legal Reasons",
499: "Client Closed Request",
500: "Internal Server Error",
501: "Not Implemented",
503: "Service Unavailable",
505: "HTTP Version Not Supported",
506: "Variant Also Negotiates",
507: "Insufficient Storage",
508: "Loop Detected",
510: "Not Extended",
511: "Network Authentication Required"
}
def status_map(code):
return str(int(code)) + ' ' + http_status_codes_map[int(code)]
# Grammar hence
# Defines some common symbols
newline = '\n'
request_start = Keyword('>')
response_start = Keyword('<')
comment = OneOrMore(Word('#') + Word(printables + ' '))
unicodePrintables = u''.join(unichr(c) for c in xrange(sys.maxunicode)
if not unichr(c).isspace())
# Defines HTTP verbs
verb_get = Keyword('GET')
verb_post = Keyword('POST')
verb_put = Keyword('PUT')
verb_delete = Keyword('DELETE')
verb_head = Keyword('HEAD')
verb = verb_get | verb_post | verb_put | verb_delete | verb_head
verb_line = request_start + verb + Optional(Suppress(',')) + Optional(delimitedList(verb))
# Defines relative url path
xalphas = Word(alphanums + '$-_@.&+!*"\'(),%/')
rel_path = request_start + xalphas
# Defines parameters
param = Keyword('param') | Keyword('PARAM')
param_maybe = Keyword('param_maybe') | Keyword('PARAM_MAYBE')
kw_param_is = Keyword('is') | Keyword('IS')
kw_param_like = Keyword('like') | Keyword('LIKE')
param_name = xalphas
param_val = xalphas
param_re = QuotedString(quoteChar='//')
param_S = request_start + (param ^ param_maybe) + param_name + kw_param_is + param_val
param_R = request_start + (param ^ param_maybe) + param_name + kw_param_like + param_re
header = request_start + Keyword('header') + Word(printables + ' ')
# Defines response values
delay = response_start + Keyword('delay') + Word(nums) # NOTE milliseconds
header_resp = response_start + Keyword('header') + Word(printables + ' ')
status = response_start + Keyword('status') + Word(nums)
body_resp = response_start + Keyword('body') + \
(Word(' ' + printables + unicodePrintables) ^ \
QuotedString(quoteChar='<<<', escQuote='<<<<<<', multiline=True))
# This echos the request body, used as an alt to '< body ...' above
echo_resp = response_start + Keyword('echo')
# Defines the stanza list
stanza_end = LineStart() + '.' + LineEnd()
# The parser
class DumdumParser(object):
class EchoFlag(object):
pass
def __init__(self, user_input):
self.S = {}
self.stanza = Group(
verb_line.setParseAction(self.process_verbs) + \
Optional(Suppress(comment))
) + \
Group(
rel_path.setParseAction(self.process_path) + \
Optional(Suppress(comment))
) + \
ZeroOrMore(
Group(header.setParseAction(self.process_header)) + \
Optional(Suppress(comment))
) + \
ZeroOrMore(
Group(
param_S.setParseAction(self.process_param) ^ \
param_R.setParseAction(self.process_param)
) + \
Optional(Suppress(comment))
) + \
ZeroOrMore(
Group(
delay.setParseAction(self.process_resp_delay) + \
Optional(Suppress(comment))
)
) + \
ZeroOrMore(
Group(
status.setParseAction(self.process_resp_status) + \
Optional(Suppress(comment))
)
) + \
ZeroOrMore(
Group(
header_resp.setParseAction(self.process_resp_header) + \
Optional(Suppress(comment))
)
) + \
ZeroOrMore(
Group(
body_resp.setParseAction(self.process_resp_body) ^ \
echo_resp.setParseAction(self.process_resp_echo)
) + \
Optional(Suppress(comment))
) + \
Suppress(stanza_end.setParseAction(self.save_stanza))
self.stanzas = OneOrMore(Group(
Optional(Suppress(comment)) + \
self.stanza + \
Optional(Suppress(comment))
))
# This is a top-down parser, but we register handlers on specific
# pattern matches, and using these "internal globals" allows us to keep
# state straight.
self.current_path = None
self.current_verbs = []
self.reqobj = None # stanza object from user input
self.respobj = None # stanza object from user input
dbg(user_input.decode('utf-8'))
# Parse the user data
self.stanzas.parseString(user_input.decode('utf-8'))
def process_verbs(self, tokens): # The VERB line ALWAYS starts a request stanza
self.reqobj = {}
# Defaults
self.respobj = {
'status': status_map(200),
'body':'',
}
self.current_verbs = tokens[1:]
for verb in self.current_verbs:
if verb not in self.S: self.S[verb] = {}
def process_path(self, tokens):
self.current_path = tokens[1]
for verb in self.current_verbs:
if self.current_path not in self.S[verb]:
self.S[verb][self.current_path] = []
def process_param(self, tokens):
p = tokens[2:]
if 'params' not in self.reqobj: self.reqobj['params'] = {}
if p[1] == 'like':
dbg("process_param() %s" % p)
self.reqobj['params'][p[0]] = re.compile(p[2])
else:
self.reqobj['params'][p[0]] = p[2]
def process_header(self, tokens):
def header_cgi_key(header):
h = header.upper().replace('-', '_')
if h != 'CONTENT_TYPE':
return 'HTTP_' + h # In CGI this precedes most headers
else:
return h
h = tokens[2:][0].split(': ')
if 'headers' not in self.reqobj: self.reqobj['headers'] = {}
self.reqobj['headers'][header_cgi_key(h[0])] = h[1]
def process_resp_status(self, tokens):
self.respobj['status'] = status_map(tokens[2])
def process_resp_delay(self, tokens):
self.respobj['delay'] = tokens[2]
def process_resp_header(self, tokens):
h = tokens[2:][0].split(': ')
if 'headers' not in self.respobj: self.respobj['headers'] = {}
self.respobj['headers'][h[0]] = h[1]
def process_resp_body(self, tokens):
b = tokens[2:][0]
self.respobj['body'] = b.strip() # TODO is this wise?
def process_resp_echo(self, tokens):
self.respobj['body'] = DumdumParser.EchoFlag()
def save_stanza(self, tokens):
if self.reqobj:
for verb in self.current_verbs:
if self.respobj: self.reqobj['response'] = self.respobj
self.S[verb][self.current_path].append(self.reqobj)
elif not self.reqobj and self.respobj:
self.reqobj = { 'response': self.respobj }
for verb in self.current_verbs:
self.S[verb][self.current_path].append(self.reqobj)
# The server
class Dumdum(object):
def __init__(self, user_stanzas):
self.user_stanzas = user_stanzas
self.parser = DumdumParser(user_stanzas)
self.Stanzas = self.parser.S
def server(self, env, start_response):
# Helper functions hence
def bad_req(msg="Sorry I couldn't help."):
status = '400 Bad Request' # HTTP Status
headers = [('Content-type', 'text/plain')] # HTTP Headers
start_response(status, headers)
return [msg]
def flatten_json(y):
delim='.'
out = {}
def flatten(x, name=''):
if type(x) is dict:
for a in x:
flatten(x[a], name + a + delim)
elif type(x) is list:
i = 0
for a in x:
flatten(a, name)
i += 1
else:
nam = name[:-len(delim)]
if nam not in out:
out[nam] = [x]
else:
out[nam].append(x)
flatten(y)
return out
# Handler hence
# The environment variable CONTENT_LENGTH may be empty or missing
try:
request_body_size = int(env.get('CONTENT_LENGTH', 0))
except (ValueError):
request_body_size = 0
try:
request_body = env['wsgi.input'].read(request_body_size)
except Exception:
pass
verb = env['REQUEST_METHOD']
url = urlparse(env['PATH_INFO'])
logger.info(
"\nCGI ENV: %s\n\n%s %s\n\n%s\n\n" %
(PP.pformat(env), verb, url.path, request_body))
if verb in self.Stanzas and url.path in self.Stanzas[verb]:
for curr_Stz in self.Stanzas[verb][url.path]: # check every stanza under this path
is_json = False
match = True
# compare headers
if 'headers' in curr_Stz:
for h in curr_Stz['headers']:
sh = curr_Stz['headers']
if h in env and env[h] == sh[h]:
if h == 'CONTENT_TYPE' and sh[h].lower() == 'application/json':
is_json = True
else:
match = False
break
# compare params
if match:
sp = None
if 'params' in curr_Stz: sp = curr_Stz['params']
dbg('Stanza params=%s' % sp)
if is_json and request_body and sp:
try:
j_rb = json.loads(request_body)
fj_rb = flatten_json(j_rb)
dbg(json.dumps(fj_rb, indent=2,
sort_keys=True))
except ValueError:
return bad_req("Error parsing: %s" % request_body)
for szparam in sp:
dbg('szparam=%s match %s' % (szparam, match))
if match == False: break
if szparam not in fj_rb:
match = False
break
else:
# Check all provided values from input, and if
# one of them matches, then
matchone = False
for v in fj_rb[szparam]:
dbg('v=%s szparam=%s' % (v, sp[szparam]))
if isinstance(sp[szparam], re._pattern_type):
if sp[szparam].search(v):
dbg('Match! regex')
matchone = True
break
else:
if v == sp[szparam]:
dbg('Match! exact')
matchone = True
break
match = matchone
elif sp:
if 'QUERY_STRING' in env and env['QUERY_STRING'] != '':
qs = parse_qs(env['QUERY_STRING'])
elif 'wsgi.input' in env:
# TODO assumes urlencode form params...
qs = parse_qs(request_body)
print qs
print sp
for szparam in sp:
dbg('szparam=%s match %s' % (szparam, match))
if match == False: break
if szparam not in qs:
match = False
break
else:
matchone = False
for v in qs[szparam]:
dbg('v=%s szparam=%s' % (v, sp[szparam]))
if isinstance(sp[szparam], re._pattern_type):
print '....?'
print sp[szparam].search(v)
if sp[szparam].search(v):
dbg('Match! regex')
matchone = True
break
else:
if v == sp[szparam]:
dbg('Match! exact')
matchone = True
break
match = matchone
if match:
if 'response' in curr_Stz:
resp = curr_Stz['response']
if 'delay' in resp:
time.sleep(float(resp['delay'])/1000.0)
status = resp['status']
hdrs = []
if 'headers' in resp:
for h in resp['headers']:
hdrs.append( (str(h),
str(resp['headers'][h])) )
start_response(status, hdrs)
if type(resp['body']) is DumdumParser.EchoFlag:
return request_body
else:
return [resp['body'].encode('utf-8')]
# We matched nothing
return bad_req()
if __name__ == "__main__":
import argparse
from wsgiref.simple_server import make_server
def read_from_stdin():
stz = ''
for line in sys.stdin:
stz += line
return stz
parser = argparse.ArgumentParser()
parser.add_argument('--port', help='set port for server (default 8001)')
parser.add_argument('--file', help='file containing dumdum stanzas')
parser.add_argument('--verbose', help='dump request and response info',
action='store_true')
args = parser.parse_args()
if args.verbose:
logging.basicConfig(stream=sys.stderr, level=logging.INFO)
else:
logging.basicConfig(stream=sys.stderr, level=logging.WARNING)
if not args.file:
user_stanza = read_from_stdin()
else:
with open(args.file, 'r') as f:
user_stanza = f.read()
dumdum = Dumdum(user_stanza)
port = args.port or 8001
srv = make_server('', int(port), dumdum.server)
try:
srv.serve_forever()
except KeyboardInterrupt:
print('\nBye.')