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