PyXWF.Context – One HTTP transaction context

class PyXWF.Context.Context[source]

The context of a request. It is passed around when retrieving the Document from the Node tree and can be used to store custom data. All the properties of the framework are named with a capital first letter or prefixed with an underscore, so you’re safe to use all other names.

Note

Do not instanciate this class directly. The web backend will do it for you.

If you are going to write a web backend, we suggest you have a look at the PyXWF.WebBackends.WSGI module as a reference. There is a whole bunch of internal attributes which need to be set up to get the Context work properly.

Accept None[source]

Return the contents of the HTTP Accept header as AcceptPreferenceList.

Cachable None[source]

Set whether the response is cachable. This may be force-set to False by the Context if POST data was accessed. Otherwise it is writable by the application to define whether the response may be cached by the client.

CacheControl None[source]

The current contents of the Cache-Control header values.

CanUseXHTML None[source]

Whether XHTML can be interpreted by the client. This is by default False and shall be identified by the request headers sent by the requesting entity.

If this is False, the application handling the request represented by this Context, must not send XHTML responses.

Cookies None[source]

Return a dictionary mapping cookie names to Cookie instances.

FullURI None[source]

The URL path for the request. This is everything behind the host name and the port number.

HTML5Support None[source]

Return whether the User-Agent is supposed to support HTML5. This is used by the site to determine whether to apply a to-html4 backtransform.

HostName None[source]

Host name the request was sent to.

IfModifiedSince None[source]

Access to the requests If-Modified-Since value. Can be None if not supplied or malformed.

IsMobileClient None[source]

Return whether the user agent is a mobile phone or similar. This can be overriden to disable mobile detection and force it to a static value. This is for example useful to decide about mobile-suited responses depending on the host name used in the request.

LastModified None[source]

Current Last-Modified value based on the used resources (see use_resource()). This will return None if no resources are used or one of them exposes a None value as LastModified (which implies uncachability).

Method None[source]

The request method (i.e. GET, POST , HEAD, ...)

PageNode None[source]

The PyXWF node responsible for serving the page.

Path None

The URL path for the request, relative to the applications (not the servers) root.

PostData None[source]

Access to the POST data of the request. Accessing this property will disable caching of the response.

PrefixedXHTMLSupport None[source]

Return whether the User-Agent is positively known to support XHTML with namespace prefixes.

QueryData None[source]

Access to the GET query data of the request as a dict. Accessing this property will add the query string to the cache path.

RemainingPath None[source]

Path suffix which was not interpreted during resolution of the path inside the node tree. This may be useful for redirects.

RequestPath None[source]

The URL path for the request, relative to the applications (not the servers) root.

URLScheme None[source]

URL scheme used for the request (this is either http or https).

_determine_html_content_type()[source]

Use the AcceptPreferenceList instance to figure out whether the client properly supports XHTML.

Will set CanUseXHTML to True if the best match for all HTML content types is the XHTML content type.

_require_cookies()[source]

Extract the cookie information from the request and make it available at Cookies.

Note

This must be overriden when implementing a Context for a specific web backend. It is expected that this method sets the _cookies attribute to a dict mapping cookie names to Cookie instances.

_require_post()[source]

Extract the post data from the request and make it available at PostData. This disables caching of the response altogether.

Note

This must be overridden when implementing a Context for a specific web backend.

_require_query()[source]

Extract query string from the web frameworks transaction and make it accessible. Also make a note that the query string was used in this request and is thus needs to be appended to the cache path.

Note

This must be overridden when implementing a Context for a specific web backend.

_set_cache_status(no_last_modified=False)[source]

Use the values of Cachable and LastModified to set up the response headers which relate to caching. This may change the value of the Last-Modified header and will add a cache control token.

If no_last_modified is True (default is False), the Last-Modified header will not be set. This is required per RFC 2616 for 304 Not Modified responses.

_set_property_headers()[source]

Convert Vary and CacheControl into HTTP headers and add them to the response headers.

add_cache_control(token)[source]

Add token to the set of Cache-Control HTTP tokens. Token must be a valid Cache-Control response value according to HTTP/1.1 (this is not enforced though (yet)).

add_vary(field_name)[source]

Add a HTTP header field name to the Vary HTTP response. field_name must be a valid HTTP/1.1 header name and will be lower-cased.

check_acceptable(content_type)[source]

Check whether the given content_type (which must be either a basestring or a Preference instance) is acceptable by the client.

Raise NotAcceptable if not.

check_not_modified()[source]

Check whether the current Last-Modified value (based on the used resources, see use_resource()) is older or equal to the If-Modified-Since value.

If so, and if caching is not disabled, a NotModified is thrown.

clear_response_header(header)[source]

Clear the value from the response header header, if any.

get_encoded_body(message)[source]

Try to get the best encoded version of the Message instance message.

Use the contents of _accept_charset (the parsed Accept-Charset HTTP header) to figure out which charsets the client prefers. Then mix in what charsets _we_ like to deliver and get the best match, giving priority to the clients wishes.

If no matching encoding can be found, NotAcceptable is raised.

get_reconstructed_uri(urlroot, update_query={})[source]

Return the full URI to reconstruct the request as stored in the Context currently. This takes into account any changes to the QueryData for example.

iter_resources()[source]

Returns an iterator over the resources used to build the response.

parse_accept(header_value)[source]

Parse header_value as value of an HTTP Accept header and return the resulting AcceptPreferenceList instance.

parse_accept_charset(header_value)[source]

Parse header_value as value of an HTTP Accept-Charset header and return the resulting CharsetPreferenceList instance.

send_empty_response(status)[source]

Send an empty response with the HTTP status status. status may be either a HTTPStatusBase descendant class or instance.

send_response(message)[source]

Send the Message object referred to by message as response. This will render the filelike behind Out invalid for use for writing. This must be implemented by a derived class.

Add the given Cookie cookie to the list of cookies to be sent with the response.

set_response_content_type(mimetype, charset)[source]

Set the content type of the response according to mimetype and charset. Charset may be None or the empty string if it should be omitted.

set_response_header(header, value)[source]

Set the value of the HTTP/1.1 response header header to value. Both are forced into non-unicode strings as per wsgi specification.

use_resource(resource)[source]

Mark the use of a given resource (which is expected to be a Resource instance) to build the response. This will later be regarded when calculating the Last-Modified value of the response, and thus whether the full response needs to be created.

The resource is also asked to recheck its Last-Modified value and reload if neccessary, so this is a possible costy operation. However, a resource will never be asked twice during the same request.

use_resources(resources)[source]

Marks multiple resources for use. This requires resources to be an iterable of Resources.

useragent_support(useragent, version)[source]

Set the attributes backing :prop:`HTML5Support` and :prop:`PrefixedXHTMLSupport` based on the useragent and its version.

classmethod useragent_supports_html5(useragent, version)[source]

Guess whether the user agent useragent with version version (as obtained for example from PyXWF.utils.guess_useragent()) can deal with HTML5. This is only more or less accurate for the popular browsers. Everyone else will just be served HTML5.

classmethod useragent_supports_prefixed_xhtml(useragent, version)[source]

Make a negative guess on the prefixed XHTML support. Some user agents (mainly firefox, even with version 16.0) are unable to deal with XHTML if it has XML namespace prefixes. Running javascript will fail then. Compare <http://stackoverflow.com/q/13591707/1248008> for more info.

Return whether the current user agent is known to support prefixed XHTML. If the UA is unknown, False will be returned for maximum compatibility. Please note that the transform removing the prefixes has to be enabled explicitly in the config ( <py:compatibility remove-xhtml-prefixes="true" />).

Previous topic

PyXWF core services

Next topic

PyXWF.Site — A whole Website

This Page