From 595c9b0c3cdc1dedd8eb423655c881971419b3dd Mon Sep 17 00:00:00 2001 From: Jack Cushman Date: Sun, 19 Jan 2014 15:08:14 -0500 Subject: [PATCH] wsgiref compatibility fixes. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Manually set env[‘REQUEST_URI’] (which is nonstandard) the same way it’s set by uwsgi. - Include HTTP error code reasons in error response. (wsgiref checks that error code is at least 4 characters, i.e. includes reason) --- pywb/utils.py | 8 ++++++++ pywb/wbapp.py | 2 ++ pywb/wbexceptions.py | 16 ++++++++-------- 3 files changed, 18 insertions(+), 8 deletions(-) diff --git a/pywb/utils.py b/pywb/utils.py index 4bcbd68e..c74b777f 100644 --- a/pywb/utils.py +++ b/pywb/utils.py @@ -107,6 +107,14 @@ def iso_date_to_timestamp(string): return datetime_to_timestamp(iso_date_to_datetime(string)) +# adapted from wsgiref.request_uri, but doesn't include domain name and allows ':' in url +def request_uri(environ, include_query=1): + """Return the requested path, optionally including the query string""" + from urllib import quote + url = quote(environ.get('SCRIPT_NAME', '')+environ.get('PATH_INFO',''),safe='/;=,:') + if include_query and environ.get('QUERY_STRING'): + url += '?' + environ['QUERY_STRING'] + return url if __name__ == "__main__": diff --git a/pywb/wbapp.py b/pywb/wbapp.py index 75b6bd7b..b81467f0 100644 --- a/pywb/wbapp.py +++ b/pywb/wbapp.py @@ -1,3 +1,4 @@ +from utils import request_uri from query import QueryHandler, EchoEnv, EchoRequest from replay import WBHandler import wbexceptions @@ -81,6 +82,7 @@ except: def application(env, start_response): + env['REQUEST_URI'] = request_uri(env) response = None try: diff --git a/pywb/wbexceptions.py b/pywb/wbexceptions.py index 22975422..e1c9a5bf 100644 --- a/pywb/wbexceptions.py +++ b/pywb/wbexceptions.py @@ -1,28 +1,28 @@ class RequestParseException(Exception): def status(_): - return '400' + return '400 Bad Request' class BadUrlException(Exception): def status(_): - return '400' + return '400 Bad Request' class AccessException(Exception): def status(_): - return '403' + return '403 Forbidden' class InvalidCDXException(Exception): def status(_): - return '500' + return '500 Internal Server Error' class NotFoundException(Exception): def status(_): - return '404' + return '404 Not Found' # Exceptions that effect a specific capture and result in a retry class CaptureException(Exception): def status(_): - return '500' + return '500 Internal Server Error' class UnresolvedArchiveFileException(CaptureException): pass @@ -45,7 +45,7 @@ class ArchiveLoadFailed(CaptureException): self.reason = reason def status(_): - return '503' + return '503 Service Unavailable' class InternalRedirect(Exception): def __init__(self, location, status = '302 Internal Redirect'): @@ -53,6 +53,6 @@ class InternalRedirect(Exception): self.status = status self.httpHeaders = [('Location', location)] - def status(_): + def status(self): return self.status