--- a/lib/http.py Fri Feb 06 21:31:02 2009 +0200
+++ b/lib/http.py Fri Feb 06 22:48:00 2009 +0200
@@ -8,6 +8,9 @@
# for header handling
import wsgiref.headers
+# for path handling
+import os.path
+
class Request (object) :
"""
HTTP Request with associated metadata
@@ -26,6 +29,29 @@
# parse query args
self.arg_dict = cgi.parse_qs(self.arg_str, True)
+
+ def get_script_dir (self) :
+ """
+ Returns the URL path to the requested script's directory with no trailing slash, i.e.
+
+ / ->
+ /foo.cgi ->
+ /foo/bar.cgi -> /foo
+ """
+
+ return os.path.dirname(self.env['SCRIPT_NAME'])
+
+ def get_page_name (self) :
+ """
+ Returns the requested page path with no leading slash, i.e.
+
+ /foo.cgi ->
+ /foo.cgi/ ->
+ /foo.cgi/bar -> bar
+ /foo.cgi/quux/ -> quux/
+ """
+
+ return os.path.normpath(self.env['PATH_INFO']).lstrip('/')
class Response (object) :
"""
@@ -91,19 +117,25 @@
A response with an error code / message
"""
- def __init__ (self, status, message) :
+ def __init__ (self, status, message, details=None) :
"""
Build a plain error message response with the given status/message
+
+ @param status HTTP status code
+ @param message short message to describe errors
+ @param details optional details, plaintext
"""
data = """\
<html><head><title>%(title)s</title></head><body>
<h1>%(title)s</h1>
<p>%(message)s</p>
+%(details)s
</body></html>
""" % dict(
title = status,
- message = message
+ message = message,
+ details = '<pre>%s</pre>' % details if details else ''
)
super(ErrorResponse, self).__init__(data, status=status)
@@ -113,14 +145,15 @@
An exception that results in a specfic 4xx ErrorResponse message to the client
"""
- def __init__ (self, message, status='400 Bad Request') :
+ def __init__ (self, message, status='400 Bad Request', details=None) :
self.status = status
self.message = message
+ self.details = details
super(ResponseError, self).__init__(message)
def get_response (self) :
- return ErrorResponse(self.status, self.message)
+ return ErrorResponse(self.status, self.message, self.details)
class Redirect (Response) :
"""