2 """A session demonstration app."""
5 from datetime
import datetime
8 from cherrypy
.lib
import sessions
9 from cherrypy
._cpcompat
import copyitems
15 <style type='text/css'>
16 table { border-collapse: collapse; border: 1px solid #663333; }
17 th { text-align: right; background-color: #663333; color: white; padding: 0.5em; }
18 td { white-space: pre-wrap; font-family: monospace; padding: 0.5em;
19 border: 1px solid #663333; }
20 .warn { font-family: serif; color: #990000; }
22 <script type="text/javascript">
24 function twodigit(d) { return d < 10 ? "0" + d : d; }
25 function formattime(t) {
26 var month = t.getUTCMonth() + 1;
27 var day = t.getUTCDate();
28 var year = t.getUTCFullYear();
29 var hours = t.getUTCHours();
30 var minutes = t.getUTCMinutes();
31 return (year + "/" + twodigit(month) + "/" + twodigit(day) + " " +
32 hours + ":" + twodigit(minutes) + " UTC");
35 function interval(s) {
36 // Return the given interval (in seconds) as an English phrase
37 var seconds = s %% 60;
38 s = Math.floor(s / 60);
39 var minutes = s %% 60;
40 s = Math.floor(s / 60);
42 var v = twodigit(hours) + ":" + twodigit(minutes) + ":" + twodigit(seconds);
43 var days = Math.floor(s / 24);
44 if (days != 0) v = days + ' days, ' + v;
48 var fudge_seconds = 5;
51 // Set the content of the 'btime' cell.
52 var currentTime = new Date();
53 var bunixtime = Math.floor(currentTime.getTime() / 1000);
55 var v = formattime(currentTime);
56 v += " (Unix time: " + bunixtime + ")";
58 var diff = Math.abs(%(serverunixtime)s - bunixtime);
59 if (diff > fudge_seconds) v += "<p class='warn'>Browser and Server times disagree.</p>";
61 document.getElementById('btime').innerHTML = v;
63 // Warn if response cookie expires is not close to one hour in the future.
64 // Yes, we want this to happen when wit hit the 'Expire' link, too.
65 var expires = Date.parse("%(expires)s") / 1000;
66 var onehour = (60 * 60);
67 if (Math.abs(expires - (bunixtime + onehour)) > fudge_seconds) {
68 diff = Math.floor(expires - bunixtime);
69 if (expires > (bunixtime + onehour)) {
70 var msg = "Response cookie 'expires' date is " + interval(diff) + " in the future.";
72 var msg = "Response cookie 'expires' date is " + interval(0 - diff) + " in the past.";
74 document.getElementById('respcookiewarn').innerHTML = msg;
81 <body onload='init()'>
83 <p>Reload this page. The session ID should not change from one reload to the next</p>
84 <p><a href='../'>Index</a> | <a href='expire'>Expire</a> | <a href='regen'>Regenerate</a></p>
86 <tr><th>Session ID:</th><td>%(sessionid)s<p class='warn'>%(changemsg)s</p></td></tr>
87 <tr><th>Request Cookie</th><td>%(reqcookie)s</td></tr>
88 <tr><th>Response Cookie</th><td>%(respcookie)s<p id='respcookiewarn' class='warn'></p></td></tr>
89 <tr><th>Session Data</th><td>%(sessiondata)s</td></tr>
90 <tr><th>Server Time</th><td id='stime'>%(servertime)s (Unix time: %(serverunixtime)s)</td></tr>
91 <tr><th>Browser Time</th><td id='btime'> </td></tr>
92 <tr><th>Cherrypy Version:</th><td>%(cpversion)s</td></tr>
93 <tr><th>Python Version:</th><td>%(pyversion)s</td></tr>
102 if cherrypy
.session
.id != cherrypy
.session
.originalid
:
103 if cherrypy
.session
.originalid
is None:
104 changemsg
.append('Created new session because no session id was given.')
105 if cherrypy
.session
.missing
:
106 changemsg
.append('Created new session due to missing (expired or malicious) session.')
107 if cherrypy
.session
.regenerated
:
108 changemsg
.append('Application generated a new session.')
111 expires
= cherrypy
.response
.cookie
['session_id']['expires']
116 'sessionid': cherrypy
.session
.id,
117 'changemsg': '<br>'.join(changemsg
),
118 'respcookie': cherrypy
.response
.cookie
.output(),
119 'reqcookie': cherrypy
.request
.cookie
.output(),
120 'sessiondata': copyitems(cherrypy
.session
),
121 'servertime': datetime
.utcnow().strftime("%Y/%m/%d %H:%M") + " UTC",
122 'serverunixtime': calendar
.timegm(datetime
.utcnow().timetuple()),
123 'cpversion': cherrypy
.__version
__,
124 'pyversion': sys
.version
,
129 # Must modify data or the session will not be saved.
130 cherrypy
.session
['color'] = 'green'
137 expire
.exposed
= True
140 cherrypy
.session
.regenerate()
141 # Must modify data or the session will not be saved.
142 cherrypy
.session
['color'] = 'yellow'
146 if __name__
== '__main__':
147 cherrypy
.config
.update({
148 #'environment': 'production',
150 'tools.sessions.on': True,
152 cherrypy
.quickstart(Root())