Merge branch 'master' of fludd.seatribe.se:/usr/local/src/ashd
[ashd.git] / python / ashd / wsgiutil.py
CommitLineData
d8509ee5
FT
1import time
2
c06db49a
FT
3def 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
18def 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 startreq("%i %s" % (code, title), [("Content-Type", "text/html"), ("Content-Length", str(len(buf)))])
31 return [buf]
d8509ee5
FT
32
33def httpdate(ts):
34 return time.strftime("%a, %d %b %Y %H:%M:%S +0000", time.gmtime(ts))
35
36def phttpdate(dstr):
37 tz = dstr[-6:]
38 dstr = dstr[:-6]
39 if tz[0] != " " or (tz[1] != "+" and tz[1] != "-") or not tz[2:].isdigit():
40 return None
41 tz = int(tz[1:])
42 tz = (((tz / 100) * 60) + (tz % 100)) * 60
43 return time.mktime(time.strptime(dstr, "%a, %d %b %Y %H:%M:%S")) - tz - time.altzone