paulb@87 | 1 | #!/usr/bin/env python |
paulb@87 | 2 | |
paulb@87 | 3 | "A form submission test." |
paulb@87 | 4 | |
paulb@87 | 5 | import WebStack.Generic |
paulb@87 | 6 | |
paulb@87 | 7 | class FormResource: |
paulb@87 | 8 | |
paulb@87 | 9 | """ |
paulb@87 | 10 | A resource which handles incoming form submissions. |
paulb@87 | 11 | """ |
paulb@87 | 12 | |
paulb@87 | 13 | def respond(self, trans): |
paulb@87 | 14 | |
paulb@87 | 15 | """ |
paulb@87 | 16 | Examine the incoming request, decoding the form information. |
paulb@87 | 17 | """ |
paulb@87 | 18 | |
paulb@87 | 19 | # NOTE: Some frameworks do not pass in the content type. |
paulb@87 | 20 | |
paulb@87 | 21 | content_type = trans.get_content_type() |
paulb@87 | 22 | input = trans.get_request_stream() |
paulb@87 | 23 | received_data = input.read() |
paulb@87 | 24 | |
paulb@87 | 25 | # NOTE: Send the appropriate kind of response. |
paulb@87 | 26 | |
paulb@87 | 27 | trans.set_content_type(WebStack.Generic.ContentType("text/html")) |
paulb@87 | 28 | out = trans.get_response_stream() |
paulb@87 | 29 | out.write(""" |
paulb@87 | 30 | <html> |
paulb@87 | 31 | <head> |
paulb@87 | 32 | <title>Form Test</title> |
paulb@87 | 33 | </head> |
paulb@87 | 34 | <body> |
paulb@87 | 35 | <h1>Form Test</h1> |
paulb@87 | 36 | <h2>Normal</h2> |
paulb@87 | 37 | <form method="post" action=""> |
paulb@87 | 38 | <input name="x" type="text" value="1"/><br/> |
paulb@87 | 39 | <input name="x" type="text" value="2"/><br/> |
paulb@87 | 40 | <input name="y" type="text" value="3"/><br/> |
paulb@87 | 41 | <input name="f" type="file"/><br/> |
paulb@87 | 42 | <input name="send" type="submit" value="Send!"/> |
paulb@87 | 43 | </form> |
paulb@87 | 44 | <h2>Multipart</h2> |
paulb@87 | 45 | <form method="post" action="" enctype="multipart/form-data"> |
paulb@87 | 46 | <input name="x" type="text" value="1"/><br/> |
paulb@87 | 47 | <input name="x" type="text" value="2"/><br/> |
paulb@87 | 48 | <input name="y" type="text" value="3"/><br/> |
paulb@87 | 49 | <input name="f" type="file"/><br/> |
paulb@87 | 50 | <input name="send" type="submit" value="Send!"/> |
paulb@87 | 51 | </form> |
paulb@87 | 52 | <h2>Content Type</h2> |
paulb@87 | 53 | <p>%s</p> |
paulb@87 | 54 | <h2>Received Data</h2> |
paulb@87 | 55 | <p>Start</p> |
paulb@87 | 56 | <pre>%s</pre> |
paulb@87 | 57 | <p>Stop</p> |
paulb@87 | 58 | </body> |
paulb@87 | 59 | </html> |
paulb@87 | 60 | """ % (content_type.content_type, received_data.replace("<", "<").replace(">", ">").replace("&", "&"))) |
paulb@87 | 61 | |
paulb@87 | 62 | # vim: tabstop=4 expandtab shiftwidth=4 |