| 19 | self.show_form = show_form |
| 20 | |
| 21 | def __call__(self, environ, start_response): |
| 22 | req = webob.Request(environ) |
| 23 | if req.path_info == '/form.html' and req.method == 'GET': |
| 24 | resp = webob.Response(content_type='text/html') |
| 25 | resp.body = self.form |
| 26 | return resp(environ, start_response) |
| 27 | |
| 28 | if 'error' in req.GET: |
| 29 | raise Exception('Exception requested') |
| 30 | |
| 31 | if 'errorlog' in req.GET: |
| 32 | log = req.GET['errorlog'] |
| 33 | req.environ['wsgi.errors'].write(log) |
| 34 | |
| 35 | status = str(req.GET.get('status', '200 OK')) |
| 36 | |
| 37 | parts = [] |
| 38 | if not self.show_form: |
| 39 | for name, value in sorted(environ.items()): |
| 40 | if name.upper() != name: |
| 41 | value = repr(value) |
| 42 | parts.append(f'{name}: {value}\n') |
| 43 | |
| 44 | body = ''.join(parts) |
| 45 | if not isinstance(body, bytes): |
| 46 | body = body.encode('latin1') |
| 47 | |
| 48 | if req.content_length: |
| 49 | body += b'-- Body ----------\n' |
| 50 | body += req.body |
| 51 | else: |
| 52 | body = '' |
| 53 | for name, value in req.POST.items(): |
| 54 | body += f'{name}={value}\n' |
| 55 | |
| 56 | if status[:3] in ('204', '304') and not req.content_length: |
| 57 | body = '' |
| 58 | |
| 59 | headers = [ |
| 60 | ('Content-Type', 'text/plain'), |
| 61 | ('Content-Length', str(len(body)))] |
| 62 | |
| 63 | if not self.show_form: |
| 64 | for name, value in req.GET.items(): |
| 65 | if name.startswith('header-'): |
| 66 | header_name = name[len('header-'):] |
| 67 | if isinstance(header_name, str): |
| 68 | header_name = str(header_name) |
| 69 | header_name = header_name.title() |
| 70 | headers.append((header_name, str(value))) |
| 71 | |
| 72 | resp = webob.Response() |
| 73 | resp.status = status |
| 74 | resp.headers.update(headers) |
| 75 | if req.method != 'HEAD': |
| 76 | if isinstance(body, str): |
| 77 | resp.body = body.encode('utf8') |
| 78 | else: |