Changeset 28:db48fe4154b6
- Timestamp:
- 12/16/05 16:58:01
(3 years ago)
- Author:
- Allan Saddi <allan@saddi.com>
- branch:
- default
- convert_revision:
- svn:46762da8-4eb7-0310-94e9-d918b60927c8/flup/trunk@1833
- Message:
Move custom 404 code out of resolvers and wholly into Publisher.
-
Files:
-
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
| r27 |
r28 |
|
| 3 | 3 | * Switch to setuptools for egg support. |
|---|
| 4 | 4 | * Add higher-level 404 error page support. Thanks to Scot Doyle |
|---|
| 5 | | for suggesting the idea and providing code. |
|---|
| | 5 | for suggesting the idea and providing code. If you previously |
|---|
| | 6 | subclassed Publisher to provide a custom 404 error page, this |
|---|
| | 7 | is now broken. It will have to be massaged to fit the new |
|---|
| | 8 | calling convention. |
|---|
| 6 | 9 | |
|---|
| 7 | 10 | 2005-11-28 Allan Saddi <asaddi@kalahari.flup.org> |
|---|
| r25 |
r28 |
|
| 422 | 422 | _transactionClass = Transaction |
|---|
| 423 | 423 | |
|---|
| 424 | | def __init__(self, resolver, transactionClass=None): |
|---|
| | 424 | def __init__(self, resolver, transactionClass=None, error404=None): |
|---|
| 425 | 425 | self._resolver = resolver |
|---|
| 426 | 426 | |
|---|
| 427 | 427 | if transactionClass is not None: |
|---|
| 428 | 428 | self._transactionClass = transactionClass |
|---|
| | 429 | |
|---|
| | 430 | if error404 is not None: |
|---|
| | 431 | self._error404 = error404 |
|---|
| 429 | 432 | |
|---|
| 430 | 433 | def _get_resolver(self): |
|---|
| … | … | |
| 451 | 454 | redirect=redirect) |
|---|
| 452 | 455 | if func is None: |
|---|
| 453 | | # See if there's a higher-level 404 page |
|---|
| 454 | | if hasattr(self._resolver, 'error404') and \ |
|---|
| 455 | | self._resolver.error404 is not None: |
|---|
| 456 | | func = self._resolver.error404 |
|---|
| 457 | | else: |
|---|
| 458 | | return self._error404(environ, start_response) |
|---|
| | 456 | func = self._error404 |
|---|
| 459 | 457 | |
|---|
| 460 | 458 | try: |
|---|
| … | … | |
| 485 | 483 | return transaction.response.body |
|---|
| 486 | 484 | |
|---|
| 487 | | def _error404(self, environ, start_response): |
|---|
| | 485 | def _error404(self, transaction): |
|---|
| 488 | 486 | """Error page to display when resolver fails.""" |
|---|
| 489 | | start_response('404 Not Found', [('Content-Type', 'text/html')]) |
|---|
| 490 | | request_uri = environ.get('REQUEST_URI') |
|---|
| | 487 | transaction.response.status = '404 Not Found' |
|---|
| | 488 | request_uri = transaction.request.environ.get('REQUEST_URI') |
|---|
| 491 | 489 | if request_uri is None: |
|---|
| 492 | | request_uri = environ.get('SCRIPT_NAME', '') + \ |
|---|
| 493 | | environ.get('PATH_INFO', '') |
|---|
| | 490 | request_uri = transaction.request.environ.get('SCRIPT_NAME', '') + \ |
|---|
| | 491 | transaction.request.environ.get('PATH_INFO', '') |
|---|
| 494 | 492 | return ["""<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN"> |
|---|
| 495 | 493 | <html><head> |
|---|
| … | … | |
| 500 | 498 | <hr> |
|---|
| 501 | 499 | %s</body></html> |
|---|
| 502 | | """ % (request_uri, environ.get('SERVER_SIGNATURE', ''))] |
|---|
| | 500 | """ % (request_uri, transaction.request.environ.get('SERVER_SIGNATURE', ''))] |
|---|
| 503 | 501 | |
|---|
| 504 | 502 | class File(object): |
|---|
| r25 |
r28 |
|
| 55 | 55 | _slashRE = re.compile(r'''/{2,}''') |
|---|
| 56 | 56 | |
|---|
| 57 | | def __init__(self, **kw): |
|---|
| 58 | | super(ComplexResolver, self).__init__(**kw) |
|---|
| | 57 | def __init__(self): |
|---|
| 59 | 58 | self.resolverMap = {} |
|---|
| 60 | 59 | |
|---|
| r25 |
r28 |
|
| 40 | 40 | the ComplexResolver. |
|---|
| 41 | 41 | """ |
|---|
| 42 | | def __init__(self, func, **kw): |
|---|
| 43 | | super(FunctionResolver, self).__init__(**kw) |
|---|
| | 42 | def __init__(self, func): |
|---|
| 44 | 43 | self._func = func |
|---|
| 45 | 44 | |
|---|
| r25 |
r28 |
|
| 63 | 63 | index_page = 'index' |
|---|
| 64 | 64 | |
|---|
| 65 | | def __init__(self, path, defaultModule=NoDefault, index=NoDefault, **kw): |
|---|
| 66 | | super(ImportingModuleResolver, self).__init__(**kw) |
|---|
| | 65 | def __init__(self, path, defaultModule=NoDefault, index=NoDefault): |
|---|
| 67 | 66 | self.path = path |
|---|
| 68 | 67 | if defaultModule is not NoDefault: |
|---|
| r25 |
r28 |
|
| 53 | 53 | index_page = 'index' |
|---|
| 54 | 54 | |
|---|
| 55 | | def __init__(self, module, index=NoDefault, **kw): |
|---|
| 56 | | super(ModuleResolver, self).__init__(**kw) |
|---|
| | 55 | def __init__(self, module, index=NoDefault): |
|---|
| 57 | 56 | self.module = module |
|---|
| 58 | 57 | if index is not NoDefault: |
|---|
| r25 |
r28 |
|
| 38 | 38 | slash. |
|---|
| 39 | 39 | """ |
|---|
| 40 | | def __init__(self, resolver, allowTrailingSlash=False, **kw): |
|---|
| 41 | | super(NoPathInfoResolver, self).__init__(**kw) |
|---|
| | 40 | def __init__(self, resolver, allowTrailingSlash=False): |
|---|
| 42 | 41 | self._resolver = resolver |
|---|
| 43 | 42 | self._allowTrailingSlash = allowTrailingSlash |
|---|
| r25 |
r28 |
|
| 57 | 57 | |
|---|
| 58 | 58 | def __init__(self, root, index=NoDefault, default=NoDefault, |
|---|
| 59 | | favorIndex=True, **kw): |
|---|
| | 59 | favorIndex=True): |
|---|
| 60 | 60 | """ |
|---|
| 61 | 61 | root is the root object of your URL hierarchy. In CherryPy, this |
|---|
| … | … | |
| 67 | 67 | True, the index method will be called. Otherwise, the default method. |
|---|
| 68 | 68 | """ |
|---|
| 69 | | super(ObjectPathResolver, self).__init__(**kw) |
|---|
| 70 | 69 | self.root = root |
|---|
| 71 | 70 | if index is not NoDefault: |
|---|
| r25 |
r28 |
|
| 44 | 44 | When resolving an InternalRedirect, redirect will be True. |
|---|
| 45 | 45 | """ |
|---|
| 46 | | def __init__(self, error404=None): |
|---|
| 47 | | self.error404 = error404 |
|---|
| 48 | | |
|---|
| 49 | 46 | def resolve(self, request, redirect=False): |
|---|
| 50 | 47 | raise NotImplementedError, self.__class__.__name__ + '.resolve' |
|---|