diff options
| author | ianb <devnull@localhost> | 2005-12-13 08:20:25 +0000 |
|---|---|---|
| committer | ianb <devnull@localhost> | 2005-12-13 08:20:25 +0000 |
| commit | e6d3daa93b95a5597575eb466b3a580ff26bb810 (patch) | |
| tree | 05787b6102f93677179ac7ba3e8651b1fe56b063 | |
| parent | ac8b22aed75bc39964804e790aecea870862d23e (diff) | |
| download | paste-e6d3daa93b95a5597575eb466b3a580ff26bb810.tar.gz | |
Deprecate the wsgilib.error_response functions; stop using them internally as well, in preference of httpexceptions.*.wsgi_application
| -rw-r--r-- | paste/evalexception/middleware.py | 14 | ||||
| -rw-r--r-- | paste/httpexceptions.py | 3 | ||||
| -rw-r--r-- | paste/urlmap.py | 7 | ||||
| -rw-r--r-- | paste/urlparser.py | 87 | ||||
| -rw-r--r-- | paste/wsgilib.py | 26 | ||||
| -rw-r--r-- | tests/test_urlparser.py | 6 |
6 files changed, 74 insertions, 69 deletions
diff --git a/paste/evalexception/middleware.py b/paste/evalexception/middleware.py index 6c10f08..8f7ca20 100644 --- a/paste/evalexception/middleware.py +++ b/paste/evalexception/middleware.py @@ -164,14 +164,14 @@ class EvalException(object): next_part = wsgilib.path_info_pop(environ) method = getattr(self, next_part, None) if not method: - return wsgilib.error_response_app( - '404 Not Found', '%r not found when parsing %r' - % (next_part, wsgilib.construct_url(environ)))( - environ, start_response) + exc = httpexceptions.HTTPNotFound( + '%r not found when parsing %r' + % (next_part, wsgilib.construct_url(environ))) + return exc.wsgi_application(environ, start_response) if not getattr(method, 'exposed', False): - return wsgilib.error_response_app( - '403 Forbidden', '%r not allowed' % next_part)( - environ, start_response) + exc = httpexceptions.HTTPForbidden( + '%r not allowed' % next_part) + return exc.wsgi_application(environ, start_response) return method(environ, start_response) def media(self, environ, start_response): diff --git a/paste/httpexceptions.py b/paste/httpexceptions.py index 3477c03..af0b7ea 100644 --- a/paste/httpexceptions.py +++ b/paste/httpexceptions.py @@ -214,10 +214,11 @@ class HTTPException(Exception): if isinstance(content, unicode): content = content.encode('utf8') headers['content_type'] += '; charset=utf8' + print "all good", environ, start_response start_response('%s %s' % (self.code, self.title), headers.items(), exc_info) - yield content + return [content] def __repr__(self): diff --git a/paste/urlmap.py b/paste/urlmap.py index 61bba6d..e480848 100644 --- a/paste/urlmap.py +++ b/paste/urlmap.py @@ -4,6 +4,7 @@ from UserDict import DictMixin import re import os +import httpexceptions import wsgilib __all__ = ['URLMap', 'PathProxyURLMap'] @@ -55,9 +56,9 @@ class URLMap(DictMixin): extra = '' extra += '\nSCRIPT_NAME: %r' % environ.get('SCRIPT_NAME') extra += '\nPATH_INFO: %r' % environ.get('PATH_INFO') - app = wsgilib.error_response_app( - '404 Not Found', 'The resource was not found\n<!-- %s -->' - % extra) + app = httpexceptions.HTTPNotFound( + 'The resource was not found', + comment=extra).wsgi_application return app(environ, start_response) def normalize_url(self, url, trim=True): diff --git a/paste/urlparser.py b/paste/urlparser.py index 64e2dcd..0e6efac 100644 --- a/paste/urlparser.py +++ b/paste/urlparser.py @@ -9,6 +9,7 @@ import mimetypes import wsgilib from paste.util import import_string from paste.deploy import converters +import httpexceptions class NoDefault: pass @@ -193,17 +194,13 @@ class URLParser(object): return self.get_application(environ, filename), filename def not_found(self, environ, start_response, debug_message=None): - status, headers, body = wsgilib.error_response( - environ, - '404 Not Found', + exc = httpexceptions.HTTPNotFound( 'The resource at %s could not be found' - '<!-- SCRIPT_NAME=%r; PATH_INFO=%r; looking in %r -->' - % (wsgilib.construct_url(environ), - environ.get('SCRIPT_NAME'), environ.get('PATH_INFO'), - self.directory), - debug_message=debug_message) - start_response(status, headers) - return [body] + % wsgilib.construct_url(environ), + comment='SCRIPT_NAME=%r; PATH_INFO=%r; looking in %r; debug: %s' + % (environ.get('SCRIPT_NAME'), environ.get('PATH_INFO'), + self.directory, debug_message or '(none)')) + return exc.wsgi_application(environ, start_response) def add_slash(self, environ, start_response): """ @@ -214,15 +211,11 @@ class URLParser(object): url += '/' if environ.get('QUERY_STRING'): url += '?' + environ['QUERY_STRING'] - status = '301 Moved Permanently' - status, headers, body = wsgilib.error_response( - environ, - status, - ''' - <p>The resource has moved to <a href="%s">%s</a>. You - should be redirected automatically.</p>''' % (url, url)) - start_response(status, headers + [('Location', url)]) - return [body] + exc = httpexceptions.HTTPMovedPermanently( + 'The resource has moved to %s - you should be redirected ' + 'automatically.''' % url, + headers=[('location', url)]) + return exc.wsgi_application(environ, start_response) def find_file(self, environ, base_filename): possible = [] @@ -441,38 +434,25 @@ class StaticURLParser(object): url += '/' if environ.get('QUERY_STRING'): url += '?' + environ['QUERY_STRING'] - status = '301 Moved Permanently' - status, headers, body = wsgilib.error_response( - environ, - status, - ''' - <p>The resource has moved to <a href="%s">%s</a>. You - should be redirected automatically.</p>''' % (url, url)) - start_response(status, headers + [('Location', url)]) - return [body] + exc = httpexceptions.HTTPMovedPermanently( + 'The resource has moved to %s - you should be redirected ' + 'automatically.''' % url, + headers=[('location', url)]) + return exc.wsgi_application(environ, start_response) def not_found(self, environ, start_response, debug_message=None): - status, headers, body = wsgilib.error_response( - environ, - '404 Not Found', - 'The resource at %s could not be found\n' - '<!-- SCRIPT_NAME: %r\n' - ' PATH_INFO: %r\n' - ' Looked in: %r\n' - % (wsgilib.construct_url(environ), - environ['SCRIPT_NAME'], environ['PATH_INFO'], - self), - debug_message=debug_message) - start_response(status, headers) - return [body] + exc = httpexceptions.HTTPNotFound( + 'The resource at %s could not be found' + % wsgilib.construct_url(environ), + comment='SCRIPT_NAME=%r; PATH_INFO=%r; looking in %r; debug: %s' + % (environ.get('SCRIPT_NAME'), environ.get('PATH_INFO'), + self.directory, debug_message or '(none)')) + return exc.wsgi_application(environ, start_response) def error_extra_path(self, environ, start_response): - status, headers, body = wsgilib.error_response( - environ, - '500 Bad Request', + exc = httpexceptions.HTTPBadRequest( 'The trailing path %r is not allowed' % environ['PATH_INFO']) - start_response(status, headers) - return [body] + return exc.wsgi_application(environ, start_response) def __repr__(self): return '<%s %r>' % (self.__class__.__name__, self.directory) @@ -523,14 +503,21 @@ class PkgResourcesParser(StaticURLParser): try: file = self.egg.get_resource_stream(self.manager, resource) except (IOError, OSError), e: - status, headers, body = wsgilib.error_response( - '403 Forbidden', + exc = httpexceptions.HTTPForbidden( 'You are not permitted to view this file (%s)' % e) - start_response(status, headers) - return [body] + return exc.wsgi_application(environ, start_response) start_response('200 OK', [('content-type', type)]) return wsgilib._FileIter(file) + + def not_found(self, environ, start_response, debug_message=None): + exc = httpexceptions.HTTPNotFound( + 'The resource at %s could not be found' + % wsgilib.construct_url(environ), + comment='SCRIPT_NAME=%r; PATH_INFO=%r; looking in egg:%s#%r; debug: %s' + % (environ.get('SCRIPT_NAME'), environ.get('PATH_INFO'), + self.egg, self.resource_name, debug_message or '(none)')) + return exc.wsgi_application(environ, start_response) def make_pkg_resources(global_conf, egg, resource_name=''): """ diff --git a/paste/wsgilib.py b/paste/wsgilib.py index 69adda4..7bb9904 100644 --- a/paste/wsgilib.py +++ b/paste/wsgilib.py @@ -210,10 +210,15 @@ def dump_environ(environ,start_response): start_response("200 OK",headers) return [output] -def error_body_response(error_code, message): +def error_body_response(error_code, message, __warn=True): """ Returns a standard HTML response page for an HTTP error. """ + if __warn: + warnings.warn( + 'wsgilib.error_body_response is deprecated; use the ' + 'wsgi_application method on an HTTPException object ' + 'instead', DeprecationWarning, 1) return '''\ <html> <head> @@ -229,7 +234,7 @@ def error_body_response(error_code, message): } def error_response(environ, error_code, message, - debug_message=None): + debug_message=None, __warn=True): """ Returns the status, headers, and body of an error response. @@ -241,21 +246,32 @@ def error_response(environ, error_code, message, start_response(status, headers) return [body] """ + if __warn: + warnings.warn( + 'wsgilib.error_response is deprecated; use the ' + 'wsgi_application method on an HTTPException object ' + 'instead', DeprecationWarning, 1) if debug_message and environ.get('paste.config', {}).get('debug'): message += '\n\n<!-- %s -->' % debug_message - body = error_body_response(error_code, message) + body = error_body_response(error_code, message, __warn=False) headers = [('content-type', 'text/html'), ('content-length', str(len(body)))] return error_code, headers, body -def error_response_app(error_code, message, debug_message=None): +def error_response_app(error_code, message, debug_message=None, + __warn=True): """ An application that emits the given error response. """ + if __warn: + warnings.warn( + 'wsgilib.error_response_app is deprecated; use the ' + 'wsgi_application method on an HTTPException object ' + 'instead', DeprecationWarning, 1) def application(environ, start_response): status, headers, body = error_response( environ, error_code, message, - debug_message=debug_message) + debug_message=debug_message, __warn=False) start_response(status, headers) return [body] return application diff --git a/tests/test_urlparser.py b/tests/test_urlparser.py index a6d66bb..ab6b2aa 100644 --- a/tests/test_urlparser.py +++ b/tests/test_urlparser.py @@ -34,7 +34,7 @@ def test_deep(): assert res.status == 301 print res assert res.header('location') == 'http://localhost/sub/' - assert 'href="http://localhost/sub/"' in res + assert 'http://localhost/sub/' in res res = app.get('/sub/') assert 'index3' in res @@ -82,7 +82,7 @@ def test_static_parser(): res = testapp.get('/', status=404) res = testapp.get('/index.txt') assert res.body.strip() == 'index1' - res = testapp.get('/index.txt/foo', status=500) + res = testapp.get('/index.txt/foo', status=400) def test_egg_parser(): app = PkgResourcesParser('Paste', 'paste') @@ -94,4 +94,4 @@ def test_egg_parser(): assert 'ClassInitMeta' in res res = testapp.get('/util/classinit', status=404) res = testapp.get('/util', status=301) - res = testapp.get('/util/classinit.py/foo', status=500) + res = testapp.get('/util/classinit.py/foo', status=400) |
