/usr/lib/python2.7/dist-packages/twext/web2/error.py is in calendarserver 5.2+dfsg-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 | # -*- test-case-name: twext.web2.test.test_log -*-
##
# Copyright (c) 2001-2004 Twisted Matrix Laboratories.
# Copyright (c) 2010-2014 Apple Computer, Inc. All rights reserved.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#
##
"""
Default error output filter for twext.web2.
"""
from twext.web2 import stream, http_headers
from twext.web2.responsecode import (
MOVED_PERMANENTLY, FOUND, SEE_OTHER, USE_PROXY, TEMPORARY_REDIRECT,
BAD_REQUEST, UNAUTHORIZED, PAYMENT_REQUIRED, FORBIDDEN, NOT_FOUND,
NOT_ALLOWED, NOT_ACCEPTABLE, PROXY_AUTH_REQUIRED, REQUEST_TIMEOUT, CONFLICT,
GONE, LENGTH_REQUIRED, PRECONDITION_FAILED, REQUEST_ENTITY_TOO_LARGE,
REQUEST_URI_TOO_LONG, UNSUPPORTED_MEDIA_TYPE,
REQUESTED_RANGE_NOT_SATISFIABLE, EXPECTATION_FAILED, INTERNAL_SERVER_ERROR,
NOT_IMPLEMENTED, BAD_GATEWAY, SERVICE_UNAVAILABLE, GATEWAY_TIMEOUT,
HTTP_VERSION_NOT_SUPPORTED, INSUFFICIENT_STORAGE_SPACE, NOT_EXTENDED,
RESPONSES,
)
from twisted.web.template import Element, flattenString, XMLString, renderer
# 300 - Should include entity with choices
# 301 -
# 304 - Must include Date, ETag, Content-Location, Expires, Cache-Control, Vary.
# 401 - Must include WWW-Authenticate.
# 405 - Must include Allow.
# 406 - Should include entity describing allowable characteristics
# 407 - Must include Proxy-Authenticate
# 413 - May include Retry-After
# 416 - Should include Content-Range
# 503 - Should include Retry-After
ERROR_MESSAGES = {
# 300
# no MULTIPLE_CHOICES
MOVED_PERMANENTLY:
'The document has permanently moved <a>here<t:attr name="href">'
'<t:slot name="location" /></t:attr></a>.',
FOUND:
'The document has temporarily moved <a>here<t:attr name="href">'
'<t:slot name="location" /></t:attr></a>.',
SEE_OTHER:
'The results are available <a>here<t:attr name="href">'
'<t:slot name="location" /></t:attr></a>.',
# no NOT_MODIFIED
USE_PROXY:
'Access to this resource must be through the proxy '
'<t:slot name="location" />.',
# 306 unused
TEMPORARY_REDIRECT:
'The document has temporarily moved <a><t:attr name="href">'
'<t:slot name="location" /></t:attr>here</a>.',
# 400
BAD_REQUEST:
'Your browser sent an invalid request.',
UNAUTHORIZED:
'You are not authorized to view the resource at <t:slot name="uri" />. '
"Perhaps you entered a wrong password, or perhaps your browser doesn't "
'support authentication.',
PAYMENT_REQUIRED:
'Payment Required (useful result code, this...).',
FORBIDDEN:
'You don\'t have permission to access <t:slot name="uri" />.',
NOT_FOUND:
'The resource <t:slot name="uri" /> cannot be found.',
NOT_ALLOWED:
'The requested method <t:slot name="method" /> is not supported by '
'<t:slot name="uri" />.',
NOT_ACCEPTABLE:
'No representation of <t:slot name="uri" /> that is acceptable to your '
'client could be found.',
PROXY_AUTH_REQUIRED:
'You are not authorized to view the resource at <t:slot name="uri" />. '
'Perhaps you entered a wrong password, or perhaps your browser doesn\'t '
'support authentication.',
REQUEST_TIMEOUT:
'Server timed out waiting for your client to finish sending the request.',
CONFLICT:
'Conflict (?)',
GONE:
'The resource <t:slot name="uri" /> has been permanently removed.',
LENGTH_REQUIRED:
'The resource <t:slot name="uri" /> requires a Content-Length header.',
PRECONDITION_FAILED:
'A precondition evaluated to false.',
REQUEST_ENTITY_TOO_LARGE:
'The provided request entity data is too longer than the maximum for '
'the method <t:slot name="method" /> at <t:slot name="uri" />.',
REQUEST_URI_TOO_LONG:
'The request URL is longer than the maximum on this server.',
UNSUPPORTED_MEDIA_TYPE:
'The provided request data has a format not understood by the resource '
'at <t:slot name="uri" />.',
REQUESTED_RANGE_NOT_SATISFIABLE:
'None of the ranges given in the Range request header are satisfiable by '
'the resource <t:slot name="uri" />.',
EXPECTATION_FAILED:
'The server does support one of the expectations given in the Expect '
'header.',
# 500
INTERNAL_SERVER_ERROR:
'An internal error occurred trying to process your request. Sorry.',
NOT_IMPLEMENTED:
'Some functionality requested is not implemented on this server.',
BAD_GATEWAY:
'An upstream server returned an invalid response.',
SERVICE_UNAVAILABLE:
'This server cannot service your request becaues it is overloaded.',
GATEWAY_TIMEOUT:
'An upstream server is not responding.',
HTTP_VERSION_NOT_SUPPORTED:
'HTTP Version not supported.',
INSUFFICIENT_STORAGE_SPACE:
'There is insufficient storage space available to perform that request.',
NOT_EXTENDED:
'This server does not support the a mandatory extension requested.'
}
class DefaultErrorElement(Element):
"""
An L{ErrorElement} is an L{Element} that renders some HTML for the default
rendering of an error page.
"""
loader = XMLString("""
<html xmlns:t="http://twistedmatrix.com/ns/twisted.web.template/0.1"
t:render="error">
<head>
<title><t:slot name="code"/> <t:slot name="title"/></title>
</head>
<body>
<h1><t:slot name="title" /></h1>
<t:slot name="message" />
</body>
</html>
""")
def __init__(self, request, response):
super(DefaultErrorElement, self).__init__()
self.request = request
self.response = response
@renderer
def error(self, request, tag):
"""
Top-level renderer for page.
"""
return tag.fillSlots(
code=str(self.response.code),
title=RESPONSES.get(self.response.code),
message=self.loadMessage(self.response.code).fillSlots(
uri=self.request.uri,
location=self.response.headers.getHeader('location'),
method=self.request.method,
)
)
def loadMessage(self, code):
tag = XMLString(('<t:transparent xmlns:t="http://twistedmatrix.com/'
'ns/twisted.web.template/0.1">') +
ERROR_MESSAGES.get(code, "") +
'</t:transparent>').load()[0]
return tag
def defaultErrorHandler(request, response):
"""
Handle errors which do not have any stream (i.e. output) associated with
them, so that users will see a nice message in their browser.
This is used as a response filter in L{twext.web2.server.Request}.
"""
if response.stream is not None:
# Already got an error message
return response
if response.code < 300:
# We only do error messages
return response
message = ERROR_MESSAGES.get(response.code, None)
if message is None:
# No message specified for that code
return response
message = message % {
'uri': request.uri,
'location': response.headers.getHeader('location'),
'method': request.method,
}
data = []
error = []
(flattenString(request, DefaultErrorElement(request, response))
.addCallbacks(data.append, error.append))
# No deferreds from our renderers above, so this has always already fired.
if data:
subtype = 'html'
body = data[0]
else:
subtype = 'error'
body = 'Error in default error handler:\n' + error[0].getTraceback()
ctype = http_headers.MimeType('text', subtype, {'charset':'utf-8'})
response.headers.setHeader("content-type", ctype)
response.stream = stream.MemoryStream(body)
return response
defaultErrorHandler.handleErrors = True
__all__ = ['defaultErrorHandler',]
|