Module rhino.response

Classes

class Response

Represents an HTTP response.

Class attributes:

default_encoding
When finalizing the response and the response body is a unicode string, it is encoded using default_encoding (default: 'utf-8')
default_content_type
When finalizing the response and the response body is not empty this is used as the default value for the Content-Type header, if none is provided (default: 'text/plain; charset=utf-8')

__init__(code, headers=None, body='')

Create a new HTTP response.

Parameters:

code
an integer status code.
headers
a list of response headers as (name, value) tuples.
body

a value for the response body. The body is only validated when the request is finalized (in __call__).

Valid values for the response body are:

The empty string ('')
This indicates an empty response body.
A str or unicode object
Sent as-is, after encoding unicode using default_encoding.
An iterator that yields str or unicode objects
The response is streamed to the client using chunked transfer-encoding (when implemented by the WSGI server).
A callable that returns a single str or unicode object
This is only useful in combination with an 'ETag' or 'Last-Modified' header, to delay construction of the response body until after conditional request handling has taken place, and no "304 Not Modified" response has been sent.
An Entity instance
The response body (which must be of one of the types listed above) will be taken from the entity instance, and any entity headers will be added to the response headers, with existing headers of the same name taking precedence.

add_callback(fn)

Add a callback to be executed when the response is closed.

body

The response body.

code

The HTTP status code as an integer.

conditional_to(request)

Return a response that is conditional to a given request.

Returns the Response instance unchanged, or a new Response instance with a "304 Not Modified" status code.

headers

A wsgiref.headers.Headers instance.

status

The status line as a string (status code + reason)

class Entity

Represents a response body with entity headers.

__init__(body, **kw)

Functions

response(code, body='', etag=None, last_modified=None, expires=None, **kw)

Helper to build an HTTP response.

Parameters:

code
An integer status code.
body
The response body. See Response.__init__ for details.
etag
A value for the ETag header. Double quotes will be added unless the string starts and ends with a double quote. If the value is callable, it will be called with body as argument and should return a string (quotes will be added to the returned string as described above).
last_modified
A value for the Last-Modified header as a datetime.datetime instance or Unix timestamp.
expires
A value for the Expires header as number of seconds, datetime.timedelta or datetime.datetime instance. Note: a value of type int or float is interpreted as a number of seconds in the future, not as Unix timestamp.
**kw
All other keyword arguments are interpreted as response headers. The names will be converted to header names by replacing underscores with hyphens and converting to title case (e.g. x_powered_by => X-Powered-By).

ok(body='', code=200, **kw)

Shortcut for response(200, ...).

The status code must be in the 2xx range.

created(body='', **kw)

Shortcut for response(201, ...).

no_content(**kw)

Shortcut for response(204, body='', ...).

redirect(location, code=302, **kw)

Shortcut for response(302, location=location, ...)

The status code must be in the 3xx range.