paulb@38 | 1 | #!/usr/bin/env python |
paulb@38 | 2 | |
paulb@38 | 3 | "A simple application for test purposes." |
paulb@38 | 4 | |
paulb@38 | 5 | import WebStack.Generic |
paulb@38 | 6 | |
paulb@38 | 7 | class SimpleResource: |
paulb@38 | 8 | |
paulb@38 | 9 | "A simple resource." |
paulb@38 | 10 | |
paulb@38 | 11 | def respond(self, trans): |
paulb@233 | 12 | trans.set_content_type(WebStack.Generic.ContentType("text/html", "utf-8")) |
paulb@38 | 13 | |
paulb@38 | 14 | # Get some information. |
paulb@38 | 15 | |
paulb@38 | 16 | out = trans.get_response_stream() |
paulb@175 | 17 | if trans.get_content_type(): |
paulb@226 | 18 | content_type = trans.get_content_type().media_type |
paulb@175 | 19 | charset = trans.get_content_type().charset |
paulb@175 | 20 | else: |
paulb@175 | 21 | content_type, charset = None, None |
paulb@233 | 22 | |
paulb@233 | 23 | # Use Unicode strings for correct character encoding. |
paulb@233 | 24 | |
paulb@233 | 25 | out.write(u""" |
paulb@38 | 26 | <html> |
paulb@66 | 27 | <head> |
paulb@66 | 28 | <title>Simple Example</title> |
paulb@66 | 29 | </head> |
paulb@38 | 30 | <body> |
paulb@38 | 31 | <h1>Test</h1> |
paulb@38 | 32 | <ul> |
paulb@38 | 33 | <li>Path: %s</li> |
paulb@163 | 34 | <li>Path without query: %s</li> |
paulb@38 | 35 | <li>Path info: %s</li> |
paulb@70 | 36 | <li>Query string: %s</li> |
paulb@38 | 37 | <li>Request method: %s</li> |
paulb@132 | 38 | <li>User: %s</li> |
paulb@38 | 39 | <li>Content type: %s</li> |
paulb@38 | 40 | <li>Charset: %s</li> |
paulb@66 | 41 | <li>Headers: |
paulb@66 | 42 | <dl> |
paulb@66 | 43 | %s |
paulb@66 | 44 | </dl> |
paulb@66 | 45 | </li> |
paulb@66 | 46 | <li>User-Agent: |
paulb@66 | 47 | <ul> |
paulb@66 | 48 | %s |
paulb@66 | 49 | </ul> |
paulb@66 | 50 | </li> |
paulb@66 | 51 | <li>user-agent: |
paulb@66 | 52 | <ul> |
paulb@66 | 53 | %s |
paulb@66 | 54 | </ul> |
paulb@66 | 55 | </li> |
paulb@72 | 56 | <li>Fields from path: |
paulb@72 | 57 | <ul> |
paulb@72 | 58 | %s |
paulb@72 | 59 | </ul> |
paulb@72 | 60 | </li> |
paulb@74 | 61 | <li>Fields from body: |
paulb@74 | 62 | <ul> |
paulb@74 | 63 | %s |
paulb@74 | 64 | </ul> |
paulb@74 | 65 | </li> |
paulb@251 | 66 | <li>Fields from path and body: |
paulb@251 | 67 | <ul> |
paulb@251 | 68 | %s |
paulb@251 | 69 | </ul> |
paulb@251 | 70 | </li> |
paulb@100 | 71 | <li>Cookies: |
paulb@100 | 72 | <ul> |
paulb@100 | 73 | %s |
paulb@100 | 74 | </ul> |
paulb@100 | 75 | </li> |
paulb@38 | 76 | </ul> |
paulb@38 | 77 | </body> |
paulb@38 | 78 | </html> |
paulb@38 | 79 | """ % ( |
paulb@38 | 80 | trans.get_path(), |
paulb@163 | 81 | trans.get_path_without_query(), |
paulb@38 | 82 | trans.get_path_info(), |
paulb@70 | 83 | trans.get_query_string(), |
paulb@38 | 84 | trans.get_request_method(), |
paulb@132 | 85 | trans.get_user(), |
paulb@175 | 86 | content_type, |
paulb@175 | 87 | charset, |
paulb@100 | 88 | self._format_dict(trans.get_headers()), |
paulb@100 | 89 | self._format_list(trans.get_header_values("User-Agent")), |
paulb@100 | 90 | self._format_list(trans.get_header_values("user-agent")), |
paulb@170 | 91 | self._format_fields(trans.get_fields_from_path()), |
paulb@170 | 92 | self._format_fields(trans.get_fields_from_body()), |
paulb@251 | 93 | self._format_fields(trans.get_fields()), |
paulb@113 | 94 | self._format_cookies(trans.get_cookies()), |
paulb@38 | 95 | )) |
paulb@38 | 96 | |
paulb@100 | 97 | def _format_dict(self, d): |
paulb@100 | 98 | return "".join([ |
paulb@100 | 99 | "<dt>%s</dt><dd>%s</dd>" % (key, value) |
paulb@100 | 100 | for key, value in d.items() |
paulb@100 | 101 | ]) |
paulb@100 | 102 | |
paulb@170 | 103 | def _format_fields(self, d): |
paulb@170 | 104 | return "".join([ |
paulb@170 | 105 | "<li>%s<ul>%s</ul></li>" % (key, self._format_list(value)) |
paulb@170 | 106 | for key, value in d.items() |
paulb@170 | 107 | ]) |
paulb@170 | 108 | |
paulb@113 | 109 | def _format_cookies(self, d): |
paulb@113 | 110 | return "".join([ |
paulb@113 | 111 | "<dt>%s</dt><dd>%s</dd>" % (key, value.value) |
paulb@113 | 112 | for key, value in d.items() |
paulb@113 | 113 | ]) |
paulb@113 | 114 | |
paulb@100 | 115 | def _format_list(self, l): |
paulb@100 | 116 | return "".join([ |
paulb@251 | 117 | "<li>%s</li>" % (value or "<em>empty</em>") |
paulb@100 | 118 | for value in l |
paulb@100 | 119 | ]) |
paulb@100 | 120 | |
paulb@38 | 121 | # vim: tabstop=4 expandtab shiftwidth=4 |