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