MCPcopy
hub / github.com/cherrypy/cherrypy / set_response

Method set_response

cherrypy/_cperror.py:244–307  ·  view source on GitHub ↗

Modify cherrypy.response status, headers, and body to represent self. CherryPy uses this internally, but you can also use it to create an HTTPRedirect object and set its output without *raising* the exception.

(self)

Source from the content-addressed store, hash-verified

242 return status
243
244 def set_response(self):
245 """Modify cherrypy.response status, headers, and body to represent
246 self.
247
248 CherryPy uses this internally, but you can also use it to create
249 an HTTPRedirect object and set its output without *raising* the
250 exception.
251 """
252 response = cherrypy.serving.response
253 response.status = status = self.status
254
255 if status in (300, 301, 302, 303, 307, 308):
256 response.headers['Content-Type'] = 'text/html;charset=utf-8'
257 # "The ... URI SHOULD be given by the Location field
258 # in the response."
259 response.headers['Location'] = self.urls[0]
260
261 # "Unless the request method was HEAD, the entity of the response
262 # SHOULD contain a short hypertext note with a hyperlink to the
263 # new URI(s)."
264 msg = {
265 300: 'This resource can be found at ',
266 301: 'This resource has permanently moved to ',
267 302: 'This resource resides temporarily at ',
268 303: 'This resource can be found at ',
269 307: 'This resource has moved temporarily to ',
270 308: 'This resource has been moved to ',
271 }[status]
272 msg += '<a href=%s>%s</a>.'
273 msgs = [
274 msg % (saxutils.quoteattr(u), html.escape(u, quote=False))
275 for u in self.urls
276 ]
277 response.body = ntob('<br />\n'.join(msgs), 'utf-8')
278 # Previous code may have set C-L, so we have to reset it
279 # (allow finalize to set it).
280 response.headers.pop('Content-Length', None)
281 elif status == 304:
282 # Not Modified.
283 # "The response MUST include the following header fields:
284 # Date, unless its omission is required by section 14.18.1"
285 # The "Date" header should have been set in Response.__init__
286
287 # "...the response SHOULD NOT include other entity-headers."
288 for key in ('Allow', 'Content-Encoding', 'Content-Language',
289 'Content-Length', 'Content-Location', 'Content-MD5',
290 'Content-Range', 'Content-Type', 'Expires',
291 'Last-Modified'):
292 if key in response.headers:
293 del response.headers[key]
294
295 # "The 304 response MUST NOT contain a message-body."
296 response.body = None
297 # Previous code may have set C-L, so we have to reset it.
298 response.headers.pop('Content-Length', None)
299 elif status == 305:
300 # Use Proxy.
301 # self.urls[0] should be the URI of the proxy.

Callers

nothing calls this directly

Calls 3

ntobFunction · 0.90
joinMethod · 0.80
popMethod · 0.80

Tested by

no test coverage detected