Base HTTP request handler.
A webtools.request.Request instance.
A webtools.response.Response instance.
A webtools.wsgi.WSGIApplication instance.
Validates if a request has permission to view the requested resource. Authorize should be set to a callable that accepts one parameter, a webtools.handlers.RequestHandler subclass instance. The callable should return True if the request is allowed to proceed and False otherwise. If False, a 401 Not Authorized error will be raised:
def my_authorizer(handler):
# Maybe do something with the request object attached to the handler
if request_is_authorized:
return True
else:
return False
class MyHandler(RequestHandler):
authorize = my_authorizer
def get(self):
self.response.write('Only authorized requests can see this')
Lists the HTTP methods that authorize() should validate. By default, authorize() validates all methods. If set, authorize() will validate only those HTTP methods specified and ignore any others:
class MyHandler(RequestHandler):
authorize = my_authorizer
authorize_methods = ['post']
def get(self):
self.response.write('Anyone can see a GET request')
def post(self):
self.response.write('Only those authorized can POST')
Sets the HTTP Cache-Control header in the response for GET requests. Defaults to the value specified by default_cache_control if blank. Care should be taken when setting this attribute.
Sets the default value for the HTTP Cache-Control header in the response. This applies to all HTTP request methods (i.e. GET, POST, etc..). This attibute should not be modified in most use casses.
Initializes this request handler with the given WSGI application, Request and Response.
Parameters: |
|
---|
Dispatches the request.
This will first check if there’s a handler_method defined in the matched route, and if not it’ll use the method correspondent to the request method (get(), post() etc).
Clears the response and sets the given HTTP status code.
This doesn’t stop code execution; for this, use abort().
Parameters: | code – HTTP status error code (e.g., 501). |
---|
Raises an HTTPException.
This stops code execution, leaving the HTTP exception to be handled by an exception handler.
Parameters: |
|
---|
Issues an HTTP redirect to the given relative URI.
The arguments are described in redirect().
Convenience method mixing redirect() and uri_for().
The arguments are described in redirect() and uri_for().
Returns a URI for a named Route.
See also
Called if this handler throws an exception during execution.
The default behavior is to re-raise the exception to be handled by WSGIApplication.handle_exception().
Parameters: |
|
---|
Redirects to the given URI for all GET requests.
This is intended to be used when defining URI routes. You must provide at least the keyword argument url in the route default values. Example:
def get_redirect_url(handler, *args, **kwargs):
return handler.uri_for('new-route-name')
app = WSGIApplication([
Route('/old-url', RedirectHandler, defaults={'_uri': '/new-url'}),
Route('/other-old-url', RedirectHandler, defaults={'_uri': get_redirect_url}),
])
Performs a redirect.
Two keyword arguments can be passed through the URI route: