1
# Copyright (c) 2001-2004 Twisted Matrix Laboratories.
2
# See LICENSE for details.
5
Default error output filter for twisted.web2.
8
from twisted.web2 import stream, http_headers
9
from twisted.web2.responsecode import *
11
# 300 - Should include entity with choices
13
# 304 - Must include Date, ETag, Content-Location, Expires, Cache-Control, Vary.
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
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>.',
30
USE_PROXY: "Access to this resource must be through the proxy %(location)s.",
32
TEMPORARY_REDIRECT: 'The document has temporarily moved <a href="%(location)s">here</a>.',
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.",
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."
65
# Is there a good place to keep this function?
66
def _escape(original):
69
return original.replace("&", "&").replace("<", "<").replace(">", ">").replace("\"", """)
71
def defaultErrorHandler(request, response):
72
if response.stream is not None:
73
# Already got an error message
75
if response.code < 300:
76
# We only do error messages
79
message = ERROR_MESSAGES.get(response.code, None)
81
# No message specified for that code
85
'uri':_escape(request.uri),
86
'location':_escape(response.headers.getHeader('location')),
87
'method':_escape(request.method)
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)
95
response.headers.setHeader("content-type", http_headers.MimeType('text', 'html'))
96
response.stream = stream.MemoryStream(body)
99
defaultErrorHandler.handleErrors = True
102
__all__ = ['defaultErrorHandler',]