3 import sys, os, io, logging
4 import ashd.ssi, ashd.wsgiutil
6 def simpleerror(out, code, title, msg):
7 html = """<?xml version="1.0" encoding="US-ASCII"?>
8 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
9 <html xmlns="http://www.w3.org/1999/xhtml" lang="en-US" xml:lang="en-US">
18 """ % (title, title, ashd.wsgiutil.htmlquote(msg))
19 out.write("HTTP/1.1 %d %s\n" % (code, title))
20 out.write("Content-Type: text/html\n")
21 out.write("Content-Length: %d\n" % len(html))
26 sys.stderr.write("usage: serve-ssi METHOD URL REST\n")
28 method, url, rest = sys.argv[1:]
29 path = os.getenv("REQ_X_ASH_FILE")
31 simpleerror(sys.stdout, 500, "Server Error", "The server is erroneously configured.")
32 sys.stderr.write("serve-ssi: must be called with the X-Ash-File header\n")
35 simpleerror(sys.stdout, 404, "Not Found", "The resource specified by the URL does not exist.")
38 class encwrap(io.TextIOWrapper):
42 logging.basicConfig(format="serve-ssi: %(message)s")
45 f = ashd.ssi.getfile(path)
46 except Exception as e:
47 sys.stderr.write("server-ssi: %s\n" % e)
48 simpleerror(sys.stdout, 500, "Server Error", "The server could not access its data.")
50 sys.stdout.write("HTTP/1.1 200 OK\n")
51 sys.stdout.write("Content-Type: text/html; charset=UTF-8\n")
52 sys.stdout.write("\n")
54 wrap = encwrap(sys.stdout.buffer, encoding="utf8")
55 f.process(ashd.ssi.context(wrap, f))
58 # This is for catching EPIPE, when the client has closed the
59 # connection. This shouldn't *really* be necessary since the
60 # process should terminate with SIGPIPE, but apparently Python