~landscape/zope3/newer-from-ztk

« back to all changes in this revision

Viewing changes to src/twisted/web2/error.py

  • Committer: Thomas Hervé
  • Date: 2009-07-08 13:52:04 UTC
  • Revision ID: thomas@canonical.com-20090708135204-df5eesrthifpylf8
Remove twisted copy

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (c) 2001-2004 Twisted Matrix Laboratories.
2
 
# See LICENSE for details.
3
 
 
4
 
"""
5
 
Default error output filter for twisted.web2.
6
 
"""
7
 
 
8
 
from twisted.web2 import stream, http_headers
9
 
from twisted.web2.responsecode import *
10
 
 
11
 
# 300 - Should include entity with choices
12
 
# 301 -
13
 
# 304 - Must include Date, ETag, Content-Location, Expires, Cache-Control, Vary.
14
 
15
 
# 401 - Must include WWW-Authenticate.
16
 
# 405 - Must include Allow.
17
 
# 406 - Should include entity describing allowable characteristics
18
 
# 407 - Must include Proxy-Authenticate
19
 
# 413 - May  include Retry-After
20
 
# 416 - Should include Content-Range
21
 
# 503 - Should include Retry-After
22
 
 
23
 
ERROR_MESSAGES = {
24
 
    # 300
25
 
    # no MULTIPLE_CHOICES
26
 
    MOVED_PERMANENTLY: 'The document has permanently moved <a href="%(location)s">here</a>.',
27
 
    FOUND: 'The document has temporarily moved <a href="%(location)s">here</a>.',
28
 
    SEE_OTHER: 'The results are available <a href="%(location)s">here</a>.',
29
 
    # no NOT_MODIFIED
30
 
    USE_PROXY: "Access to this resource must be through the proxy %(location)s.",
31
 
    # 306 unused
32
 
    TEMPORARY_REDIRECT: 'The document has temporarily moved <a href="%(location)s">here</a>.',
33
 
 
34
 
    # 400
35
 
    BAD_REQUEST: "Your browser sent an invalid request.",
36
 
    UNAUTHORIZED: "You are not authorized to view the resource at %(uri)s. Perhaps you entered a wrong password, or perhaps your browser doesn't support authentication.",
37
 
    PAYMENT_REQUIRED: "Payment Required (useful result code, this...).",
38
 
    FORBIDDEN: "You don't have permission to access %(uri)s.",
39
 
    NOT_FOUND: "The resource %(uri)s cannot be found.",
40
 
    NOT_ALLOWED: "The requested method %(method)s is not supported by %(uri)s.",
41
 
    NOT_ACCEPTABLE: "No representation of %(uri)s that is acceptable to your client could be found.",
42
 
    PROXY_AUTH_REQUIRED: "You are not authorized to view the resource at %(uri)s. Perhaps you entered a wrong password, or perhaps your browser doesn't support authentication.",
43
 
    REQUEST_TIMEOUT: "Server timed out waiting for your client to finish sending the HTTP request.",
44
 
    CONFLICT: "Conflict (?)",
45
 
    GONE: "The resource %(uri)s has been permanently removed.",
46
 
    LENGTH_REQUIRED: "The resource %(uri)s requires a Content-Length header.",
47
 
    PRECONDITION_FAILED: "A precondition evaluated to false.",
48
 
    REQUEST_ENTITY_TOO_LARGE: "The provided request entity data is too longer than the maximum for the method %(method)s at %(uri)s.",
49
 
    REQUEST_URI_TOO_LONG: "The request URL is longer than the maximum on this server.",
50
 
    UNSUPPORTED_MEDIA_TYPE: "The provided request data has a format not understood by the resource at %(uri)s.",
51
 
    REQUESTED_RANGE_NOT_SATISFIABLE: "None of the ranges given in the Range request header are satisfiable by the resource %(uri)s.",
52
 
    EXPECTATION_FAILED: "The server does support one of the expectations given in the Expect header.",
53
 
 
54
 
    # 500
55
 
    INTERNAL_SERVER_ERROR: "An internal error occurred trying to process your request. Sorry.",
56
 
    NOT_IMPLEMENTED: "Some functionality requested is not implemented on this server.",
57
 
    BAD_GATEWAY: "An upstream server returned an invalid response.",
58
 
    SERVICE_UNAVAILABLE: "This server cannot service your request becaues it is overloaded.",
59
 
    GATEWAY_TIMEOUT: "An upstream server is not responding.",
60
 
    HTTP_VERSION_NOT_SUPPORTED: "HTTP Version not supported.",
61
 
    INSUFFICIENT_STORAGE_SPACE: "There is insufficient storage space available to perform that request.",
62
 
    NOT_EXTENDED: "This server does not support the a mandatory extension requested."
63
 
}
64
 
 
65
 
# Is there a good place to keep this function?
66
 
def _escape(original):
67
 
    if original is None:
68
 
        return None
69
 
    return original.replace("&", "&amp;").replace("<", "&lt;").replace(">", "&gt;").replace("\"", "&quot;")
70
 
 
71
 
def defaultErrorHandler(request, response):
72
 
    if response.stream is not None:
73
 
        # Already got an error message
74
 
        return response
75
 
    if response.code < 300:
76
 
        # We only do error messages
77
 
        return response
78
 
    
79
 
    message = ERROR_MESSAGES.get(response.code, None)
80
 
    if message is None:
81
 
        # No message specified for that code
82
 
        return response
83
 
    
84
 
    message = message % {
85
 
        'uri':_escape(request.uri),
86
 
        'location':_escape(response.headers.getHeader('location')),
87
 
        'method':_escape(request.method)
88
 
        }
89
 
 
90
 
    title = RESPONSES.get(response.code, "")
91
 
    body = ("<html><head><title>%d %s</title></head>"
92
 
            "<body><h1>%s</h1>%s</body></html>") % (
93
 
        response.code, title, title, message)
94
 
    
95
 
    response.headers.setHeader("content-type", http_headers.MimeType('text', 'html'))
96
 
    response.stream = stream.MemoryStream(body)
97
 
    
98
 
    return response
99
 
defaultErrorHandler.handleErrors = True
100
 
 
101
 
 
102
 
__all__ = ['defaultErrorHandler',]