Merge branch 'master' into python3
authorFredrik Tolf <fredrik@dolda2000.com>
Wed, 16 Jan 2013 00:17:51 +0000 (01:17 +0100)
committerFredrik Tolf <fredrik@dolda2000.com>
Wed, 16 Jan 2013 00:17:51 +0000 (01:17 +0100)
Conflicts:
wrw/dispatch.py

wrw/dispatch.py
wrw/proto.py
wrw/util.py

index e51559e..10cc2f6 100644 (file)
@@ -1,5 +1,5 @@
 import sys, traceback
-from . import env
+from . import env, req, proto
 
 __all__ = ["restart"]
 
@@ -73,3 +73,9 @@ def handle(req, startreq, handler):
         return resp
     finally:
         req.cleanup()
+
+def handleenv(env, startreq, handler):
+    if not "HTTP_HOST" in env:
+        return proto.simpleerror(env, startreq, 400, "Bad Request", "Request must include Host header.")
+    r = req.origrequest(env)
+    return handle(r, startreq, handler)
index 8474b3a..4cf1951 100644 (file)
@@ -85,6 +85,22 @@ def htmlq(html):
             ret += c
     return ret
 
+def simpleerror(env, startreq, code, title, msg):
+    buf = """<?xml version="1.0" encoding="US-ASCII"?>
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
+<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US">
+<head>
+<title>%s</title>
+</head>
+<body>
+<h1>%s</h1>
+<p>%s</p>
+</body>
+</html>
+""" % (title, title, htmlq(msg))
+    startreq("%i %s" % (code, title), [("Content-Type", "text/html"), ("Content-Length", str(len(buf)))])
+    return [buf]
+
 def urlq(url):
     ret = ""
     for c in url:
index a7f1e4c..5347ce4 100644 (file)
@@ -3,7 +3,7 @@ from . import req, dispatch, session, form
 
 def wsgiwrap(callable):
     def wrapper(env, startreq):
-        return dispatch.handle(req.origrequest(env), startreq, callable)
+        return dispatch.handleenv(env, startreq, callable)
     return wrapper
 
 def stringwrap(charset):