rewrite
Module¶
gluon.rewrite parses incoming URLs and formats outgoing URLs for gluon.html.URL.
In addition, it rewrites both incoming and outgoing URLs based on the (optional) user-supplied routes.py, which also allows for rewriting of certain error messages.
routes.py supports two styles of URL rewriting, depending on whether ‘routers’ is defined. Refer to router.example.py and routes.example.py for additional documentation.
-
class
gluon.rewrite.
MapUrlIn
(request=None, env=None)[source]¶ Bases:
object
Logic for mapping incoming URLs
-
arg0
¶ Returns first arg
-
harg0
¶ Returns first arg with optional hyphen mapping
-
map_root_static
()[source]¶ Handles root-static files (no hyphen mapping)
a root-static file is one whose incoming URL expects it to be at the root, typically robots.txt & favicon.ico
-
-
class
gluon.rewrite.
MapUrlOut
(request, env, application, controller, function, args, other, scheme, host, port, language)[source]¶ Bases:
object
Logic for mapping outgoing URLs
-
gluon.rewrite.
compile_regex
(k, v, env=None)[source]¶ Preprocess and compile the regular expressions in routes_app/in/out The resulting regex will match a pattern of the form:
[remote address]:[protocol]://[host]:[method] [path]
We allow abbreviated regexes on input; here we try to complete them.
-
gluon.rewrite.
filter_err
(status, application='app', ticket='tkt')[source]¶ doctest/unittest interface to routes_onerror
-
gluon.rewrite.
filter_url
(url, method='get', remote='0.0.0.0', out=False, app=False, lang=None, domain=(None, None), env=False, scheme=None, host=None, port=None, language=None)[source]¶ doctest/unittest interface to regex_filter_in() and regex_filter_out()
-
gluon.rewrite.
get_effective_router
(appname)[source]¶ Returns a private copy of the effective router for the specified application
-
gluon.rewrite.
load
(routes='routes.py', app=None, data=None, rdict=None)[source]¶ load: read (if file) and parse routes store results in params (called from main.py at web2py initialization time) If data is present, it’s used instead of the routes.py contents. If rdict is present, it must be a dict to be used for routers (unit test)
-
gluon.rewrite.
map_url_out
(request, env, application, controller, function, args, other, scheme, host, port, language=None)[source]¶ Supply /a/c/f (or /a/lang/c/f) portion of outgoing url
The basic rule is that we can only make transformations that map_url_in can reverse.
Suppose that the incoming arguments are a,c,f,args,lang and that the router defaults are da, dc, df, dl.
We can perform these transformations trivially if args=[] and lang=None or dl:
/da/dc/df => / /a/dc/df => /a /a/c/df => /a/c
We would also like to be able to strip the default application or application/controller from URLs with function/args present, thus:
/da/c/f/args => /c/f/args /da/dc/f/args => /f/args
We use [applications] and [controllers] and {functions} to suppress ambiguous omissions.
We assume that language names do not collide with a/c/f names.
-
gluon.rewrite.
regex_select
(env=None, app=None, request=None)[source]¶ Selects a set of regex rewrite params for the current request
-
gluon.rewrite.
regex_uri
(e, regexes, tag, default=None)[source]¶ Filters incoming URI against a list of regexes
-
gluon.rewrite.
try_redirect_on_error
(http_object, request, ticket=None)[source]¶ Called from main.wsgibase to rewrite the http response