5.7.8. vlcp.utils.http

Note

This document is generated from the source file.

View Source on GitHub

Created on 2015/11/10

author:hubo
class vlcp.utils.http.Dispatcher(scheduler=None, daemon=False, vhost='')
__init__(scheduler=None, daemon=False, vhost='')

Initialize self. See help(type(self)) for accurate signature.

redirect(path, expand, status=302, host=None, vhost=None, method=[b'GET', b'HEAD'], keepquery=True)

Redirect a request to another location

rewrite(path, expand, newmethod=None, host=None, vhost=None, method=[b'GET', b'HEAD'], keepquery=True)

Rewrite a request to another location

route(path, routinemethod, container=None, host=None, vhost=None, method=[b'GET', b'HEAD'])

Route specified path to a WSGI-styled routine factory :param path: path to match, can be a regular expression :param routinemethod: factory function routinemethod(env), env is an Environment object

see also utils.http.Environment
Parameters:
  • container – routine container
  • host – if specified, only response to request to specified host
  • vhost – if specified, only response to request to specified vhost. If not specified, response to dispatcher default vhost.
  • method – if specified, response to specified methods
routeargs(path, routinemethod, container=None, host=None, vhost=None, method=[b'POST'], tostr=True, matchargs=(), fileargs=(), queryargs=(), cookieargs=(), sessionargs=(), csrfcheck=False, csrfarg='_csrf', formlimit=67108864)

Convenient way to route a processor with arguments. Automatically parse arguments and pass them to the corresponding handler arguments. If required arguments are missing, HttpInputException is thrown which creates a 400 Bad Request response. If optional arguments are missing, they are replaced with default values just as normal Python call does. If handler accepts keyword arguments, extra arguments are sent with kwargs. If not, they are safely ignored. :param path: path to match, can be a regular expression :param routinemethod: factory function routinemethod(env, arga, argb, argc…). env is an Environment

object. form or querystring arguments ‘arga’, ‘argb’, ‘argc’ are passed to arga, argb, argc.
Parameters:
  • container – routine container
  • host – if specified, only response to request to specified host
  • vhost – if specified, only response to request to specified vhost. If not specified, response to dispatcher default vhost.
  • method – methods allowed. With POST method, arguments are extracted from form by default; With GET or HEAD method, arguments are extracted from querystring(args).
  • tostr – In Python3, convert bytes to str before sending arguments to handler.
  • matchargs – Instead of using form or args, extract arguments from path match. matchargs is a sequence of matcher group names. If specified a group name by number, the argument is used as positional arguments; if specified a group name by name(str), the argument is used as a keyword argument.
  • fileargs – Instead of using form or args, extract specified arguments from files.
  • queryargs – Instead of using form, extract specified arguments from args. Notice that when GET is allowed, the arguments are always extracted from args by default.
  • cookieargs – Instead of using form or args, extract specified arguments from cookies.
  • sessionargs – Instead of using form or args, extract specified arguments from session. Notice that if sessionargs is not empty, env.sessionstart() is called, so vlcp.service.utils.session.Session module must be loaded.
  • csrfcheck – If True, check <csrfarg> in input arguments against <csrfarg> in session. Notice that csrfcheck=True cause env.sessionstart() to be called, so vlcp.service.utils.session.Session module must be loaded.
  • csrfarg – argument name to check, default to “_csrf”
  • formlimit – limit on parseform, default to 64MB. None to no limit.

For example, if using: def handler(env, target, arga, argb, argc):

dispatcher.routeargs(b’/do/(.*)’, handler, matchargs=(1,), queryargs=(‘argc’))

And there is a HTTP POST: POST /do/mytarget?argc=1 HTTP/1.1 Host: … …

arga=test&argb=test2

then handler accepts arguments: target=”mytarget”, arga=”test”, argb=”test2”, argc=”1”

routeevent(path, routinemethod, container=None, host=None, vhost=None, method=[b'GET', b'HEAD'])

Route specified path to a routine factory :param path: path to match, can be a regular expression :param routinemethod: factory function routinemethod(event), event is the HttpRequestEvent :param container: routine container. If None, default to self for bound method, or event.connection if not :param host: if specified, only response to request to specified host :param vhost: if specified, only response to request to specified vhost.

If not specified, response to dispatcher default vhost.
Parameters:method – if specified, response to specified methods
exception vlcp.utils.http.HttpExitException
class vlcp.utils.http.HttpHandler(scheduler=None, daemon=False, vhost='')
__init__(scheduler=None, daemon=False, vhost='')

Create the routine container.

Parameters:
  • scheduler – The scheduler. This must be set; if None is used, it must be set with container.bind(scheduler) before using.
  • daemon – If daemon = True, the main routine container.main is set to be a daemon routine. A daemon routine does not stop the scheduler from quitting; if all non-daemon routines are quit, the scheduler stops.
close()

Same as terminate()

static routeargs(path, host=None, vhost=None, method=[b'POST'], **kwargs)

For extra arguments, see Dispatcher.routeargs. They must be specified by keyword arguments

start(asyncStart=False)

Start container.main as the main routine.

Parameters:asyncStart – if True, start the routine in background. By default, the routine starts in foreground, which means it is executed to the first yield statement before returning. If the started routine raises an exception, the exception is re-raised to the caller of start
exception vlcp.utils.http.HttpInputException
exception vlcp.utils.http.HttpRewriteLoopException
vlcp.utils.http.http(container=None)

wrap a WSGI-style class method to a HTTPRequest event handler

vlcp.utils.http.statichttp(container=None)

wrap a WSGI-style function to a HTTPRequest event handler