paulb@99 | 1 | #!/usr/bin/env python |
paulb@99 | 2 | |
paulb@99 | 3 | "A test of cookies." |
paulb@99 | 4 | |
paulb@99 | 5 | import WebStack.Generic |
paulb@99 | 6 | import time |
paulb@99 | 7 | |
paulb@99 | 8 | class CookiesResource: |
paulb@99 | 9 | |
paulb@99 | 10 | "A resource adding and removing cookies." |
paulb@99 | 11 | |
paulb@449 | 12 | urlencoding = "utf-8" |
paulb@449 | 13 | |
paulb@99 | 14 | def respond(self, trans): |
paulb@99 | 15 | trans.set_content_type(WebStack.Generic.ContentType("text/html")) |
paulb@99 | 16 | |
paulb@99 | 17 | # Get the fields and choose an action. |
paulb@99 | 18 | |
paulb@262 | 19 | fields_from_path = trans.get_fields_from_path() |
paulb@449 | 20 | path = trans.get_path_without_query(self.urlencoding) |
paulb@262 | 21 | |
paulb@262 | 22 | # If the "set method" form was used, set the new method. |
paulb@262 | 23 | |
paulb@262 | 24 | if fields_from_path.has_key("set"): |
paulb@262 | 25 | method = (fields_from_path.get("method") or ["GET"])[0] |
paulb@262 | 26 | message = "Form method set to %s." % method |
paulb@262 | 27 | cookie_name = cookie_value = None |
paulb@99 | 28 | |
paulb@262 | 29 | # Otherwise, discover the incoming fields. |
paulb@99 | 30 | |
paulb@262 | 31 | else: |
paulb@262 | 32 | if fields_from_path.has_key("add") or fields_from_path.has_key("delete"): |
paulb@262 | 33 | fields = fields_from_path |
paulb@262 | 34 | method = (fields.get("method") or ["GET"])[0] |
paulb@262 | 35 | else: |
paulb@262 | 36 | fields = trans.get_fields_from_body() |
paulb@262 | 37 | method = (fields.get("method") or ["GET"])[0] |
paulb@99 | 38 | |
paulb@262 | 39 | cookie_name_list = fields.get("name") or ["test"] |
paulb@262 | 40 | cookie_value_list = fields.get("value") or ["test"] |
paulb@262 | 41 | cookie_path_list = fields.get("path") or ["/"] |
paulb@262 | 42 | cookie_expires_list = fields.get("expires") or ["60"] |
paulb@262 | 43 | |
paulb@262 | 44 | cookie_name = cookie_name_list[0] |
paulb@262 | 45 | cookie_value = cookie_value_list[0] |
paulb@262 | 46 | cookie_path = cookie_path_list[0] |
paulb@262 | 47 | cookie_expires = int(cookie_expires_list[0]) |
paulb@262 | 48 | |
paulb@262 | 49 | message = "No action taken - use add or delete to change the cookies." |
paulb@99 | 50 | |
paulb@262 | 51 | if fields.has_key("add"): |
paulb@262 | 52 | trans.set_cookie_value( |
paulb@262 | 53 | cookie_name, |
paulb@262 | 54 | cookie_value, |
paulb@262 | 55 | cookie_path, |
paulb@262 | 56 | time.time() + cookie_expires |
paulb@262 | 57 | ) |
paulb@262 | 58 | message = "Cookie %s added!" % cookie_name |
paulb@99 | 59 | |
paulb@262 | 60 | elif fields.has_key("delete"): |
paulb@262 | 61 | trans.delete_cookie(cookie_name) |
paulb@262 | 62 | message = "Cookie %s deleted!" % cookie_name |
paulb@99 | 63 | |
paulb@99 | 64 | # Get some information. |
paulb@99 | 65 | |
paulb@99 | 66 | out = trans.get_response_stream() |
paulb@99 | 67 | out.write(""" |
paulb@99 | 68 | <html> |
paulb@99 | 69 | <head> |
paulb@99 | 70 | <title>Cookies Example</title> |
paulb@99 | 71 | </head> |
paulb@99 | 72 | <body> |
paulb@99 | 73 | <h1>Cookies</h1> |
paulb@99 | 74 | <p>%s</p> |
paulb@99 | 75 | <ul> |
paulb@99 | 76 | %s |
paulb@99 | 77 | </ul> |
paulb@262 | 78 | <h2>Method</h2> |
paulb@262 | 79 | <form method="GET"> |
paulb@262 | 80 | <p>Method: <select name="method"><option value="GET" %s>GET</option><option value="POST" %s>POST</option></select></p> |
paulb@262 | 81 | <p><input name="set" type="submit" value="Set method..."/></p> |
paulb@262 | 82 | </form> |
paulb@106 | 83 | <h2>Cookie</h2> |
paulb@262 | 84 | <form method="%s" action="%s"> |
paulb@262 | 85 | <input name="method" type="hidden" value="%s"/> |
paulb@260 | 86 | <p>Name specified: <input name="name" value="%s"/></p> |
paulb@260 | 87 | <p>Value found: <input name="value" value="%s"/></p> |
paulb@374 | 88 | <p> |
paulb@374 | 89 | <input name="add" type="submit" value="Add..."/> |
paulb@374 | 90 | <input name="delete" type="submit" value="Delete..."/> |
paulb@374 | 91 | <input name="refresh" type="submit" value="Refresh..."/> |
paulb@374 | 92 | </p> |
paulb@260 | 93 | </form> |
paulb@99 | 94 | </body> |
paulb@99 | 95 | </html> |
paulb@99 | 96 | """ % ( |
paulb@99 | 97 | message, |
paulb@118 | 98 | self._format_cookies(trans.get_cookies()), |
paulb@262 | 99 | self._is_selected(method == "GET"), |
paulb@262 | 100 | self._is_selected(method == "POST"), |
paulb@262 | 101 | method, |
paulb@262 | 102 | path, |
paulb@262 | 103 | method, |
paulb@106 | 104 | cookie_name, |
paulb@260 | 105 | cookie_value, |
paulb@99 | 106 | )) |
paulb@99 | 107 | |
paulb@118 | 108 | def _format_cookies(self, d): |
paulb@99 | 109 | return "".join([ |
paulb@118 | 110 | "<dt>%s</dt><dd>%s</dd>" % (key, value.value) |
paulb@99 | 111 | for key, value in d.items() |
paulb@99 | 112 | ]) |
paulb@99 | 113 | |
paulb@99 | 114 | def _format_list(self, l): |
paulb@99 | 115 | return "".join([ |
paulb@99 | 116 | "<li>%s</li>" % value |
paulb@99 | 117 | for value in l |
paulb@99 | 118 | ]) |
paulb@99 | 119 | |
paulb@262 | 120 | def _is_selected(self, value): |
paulb@262 | 121 | if value: |
paulb@262 | 122 | return 'selected="selected"' |
paulb@262 | 123 | else: |
paulb@262 | 124 | return "" |
paulb@262 | 125 | |
paulb@99 | 126 | # vim: tabstop=4 expandtab shiftwidth=4 |