Table Of Contents

Previous topic

trac.versioncontrol.svn_fs – Subversion backend for Trac

Next topic

trac.web.auth – Trac Authentication

This Page

trac.web.api – Trac Web Request Handling

Primary interface for handling web requests.

Interfaces

The following interfaces allow components to interact at various stages of the web requests processing pipeline.

class trac.web.api.IRequestHandler

Decide which trac.core.Component handles which Request, and how.

See also trac.web.api.IRequestHandler extension point

match_request(req)

Return whether the handler wants to process the given request.

process_request(req)

Process the request.

Return a (template_name, data, content_type) tuple, where data is a dictionary of substitutions for the Genshi template.

“text/html” is assumed if content_type is None.

Note that if template processing should not occur, this method can simply send the response itself and not return anything.

Since 1.0:Clearsilver templates are no longer supported.
class trac.web.api.IRequestFilter

Enable components to interfere with the processing done by the main handler, either before and/or after it enters in action.

See also trac.web.api.IRequestFilter extension point

post_process_request(req, template, data, content_type)

Do any post-processing the request might need; typically adding values to the template data dictionary, or changing the Genshi template or mime type.

data may be updated in place.

Always returns a tuple of (template, data, content_type), even if unchanged.

Note that template, data, content_type will be None if:
  • called when processing an error page
  • the default request handler did not return any result
Since 0.11:there’s a data argument for supporting Genshi templates; this introduced a difference in arity which made it possible to distinguish between the IRequestFilter components still targeted at ClearSilver templates and the newer ones targeted at Genshi templates.
Since 1.0:Clearsilver templates are no longer supported.
pre_process_request(req, handler)

Called after initial handler selection, and can be used to change the selected handler or redirect request.

Always returns the request handler, even if unchanged.

For how the main content itself can be generated, see trac.web.chrome.

class trac.web.api.ITemplateStreamFilter

Transform the generated content by filtering the Genshi event stream generated by the template, prior to its serialization.

See also trac.web.api.ITemplateStreamFilter extension point

filter_stream(req, method, filename, stream, data)

Return a filtered Genshi event stream, or the original unfiltered stream if no match.

req is the current request object, method is the Genshi render method (xml, xhtml or text), filename is the filename of the template to be rendered, stream is the event stream and data is the data for the current template.

See the Genshi documentation for more information.

class trac.web.api.IAuthenticator

Extension point interface for components that can provide the name of the remote user.

See also trac.web.api.IAuthenticator extension point

authenticate(req)

Return the name of the remote user, or None if the identity of the user is unknown.

Classes

class trac.web.api.Request(environ, start_response)

Represents a HTTP request/response pair.

This class provides a convenience API over WSGI.

Create the request wrapper.

Parameters:
  • environ – The WSGI environment dict
  • start_response – The WSGI callback for starting the response
  • callbacks – A dictionary of functions that are used to lazily evaluate attribute lookups
authname

The name associated with the user after authentification or 'anonymous' if no authentification took place.

This corresponds to the remote_user when the request is targeted to an area requiring authentication, otherwise the authname is retrieved from the trac_auth cookie.

href

An Href instance for generating relative URLs pointing to resources within the current Trac environment.

abs_href

An Href instance for generating absolute URLs pointing to resources within the current Trac environment.

add_redirect_listener(listener)

Add a callable to be called prior to executing a redirect.

The callable is passed the arguments to the redirect() call.

base_path

The root path of the application

check_modified(datetime, extra='')

Check the request “If-None-Match” header against an entity tag.

The entity tag is generated from the specified last modified time (datetime), optionally appending an extra string to indicate variants of the requested resource.

That extra parameter can also be a list, in which case the MD5 sum of the list content will be used.

If the generated tag matches the “If-None-Match” header of the request, this method sends a “304 Not Modified” response to the client. Otherwise, it adds the entity tag as an “ETag” header to the response so that consecutive requests can be cached.

end_headers()

Must be called after all headers have been sent and before the actual content is written.

get_header(name)

Return the value of the specified HTTP header, or None if there’s no such header in the request.

method

The HTTP method of the request

path_info

Path inside the application

query_string

Query part of the request

read(size=None)

Read the specified number of bytes from the request body.

redirect(url, permanent=False)

Send a redirect to the client, forwarding to the specified URL.

The url may be relative or absolute, relative URLs will be translated appropriately.

remote_addr

IP address of the remote user

remote_user

Name of the remote user.

Will be None if the user has not logged in using HTTP authentication.

scheme

The scheme of the request URL

send_file(path, mimetype=None)

Send a local file to the browser.

This method includes the “Last-Modified”, “Content-Type” and “Content-Length” headers in the response, corresponding to the file attributes. It also checks the last modification time of the local file against the “If-Modified-Since” provided by the user agent, and sends a “304 Not Modified” response if it matches.

send_header(name, value)

Send the response header with the specified name and value.

value must either be an unicode string or can be converted to one (e.g. numbers, ...)

send_response(code=200)

Set the status code of the response.

server_name

Name of the server

server_port

Port number the server is bound to

write(data)

Write the given data to the response body.

data must be a str string or an iterable instance which iterates str strings, encoded with the charset which has been specified in the 'Content-Type' header or UTF-8 otherwise.

Note that when the 'Content-Length' header is specified, its value either corresponds to the length of data, or, if there are multiple calls to write, to the cumulative length of the data arguments.

class trac.web.api.RequestDone(iterable=None)

Marker exception that indicates whether request processing has completed and a response was sent.

Helper Functions

trac.web.api.arg_list_to_args(arg_list)

Convert a list of (name, value) tuples into into a _RequestArgs.

trac.web.api.parse_arg_list(query_string)

Parse a query string into a list of (name, value) tuples.