lib: Transfer the responsibility of fopencookie bugs to the generic implementation.
[ashd.git] / python3 / ashd / wsgiutil.py
1 import time
2
3 def htmlquote(text):
4     ret = ""
5     for c in text:
6         if c == '&':
7             ret += "&"
8         elif c == '<':
9             ret += "&lt;"
10         elif c == '>':
11             ret += "&gt;"
12         elif c == '"':
13             ret += "&quot;"
14         else:
15             ret += c
16     return ret
17
18 def simpleerror(env, startreq, code, title, msg):
19     buf = """<?xml version="1.0" encoding="US-ASCII"?>
20 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
21 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US">
22 <head>
23 <title>%s</title>
24 </head>
25 <body>
26 <h1>%s</h1>
27 <p>%s</p>
28 </body>
29 </html>""" % (title, title, htmlquote(msg))
30     buf = buf.encode("ascii")
31     startreq("%i %s" % (code, title), [("Content-Type", "text/html"), ("Content-Length", str(len(buf)))])
32     return [buf]
33
34 def httpdate(ts):
35     return time.strftime("%a, %d %b %Y %H:%M:%S +0000", time.gmtime(ts))
36
37 def phttpdate(dstr):
38     tz = dstr[-6:]
39     dstr = dstr[:-6]
40     if tz[0] != " " or (tz[1] != "+" and tz[1] != "-") or not tz[2:].isdigit():
41         return None
42     tz = int(tz[1:])
43     tz = (((tz / 100) * 60) + (tz % 100)) * 60
44     return time.mktime(time.strptime(dstr, "%a, %d %b %Y %H:%M:%S")) - tz - time.altzone