Package web2py :: Package gluon :: Module rewrite
[hide private]
[frames] | no frames]

Module rewrite

source code

This file is part of the web2py Web Framework Copyrighted by Massimo Di Pierro <mdipierro@cs.depaul.edu> License: LGPLv3 (http://www.gnu.org/licenses/lgpl.html)

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.

Classes [hide private]
  MapUrlIn
logic for mapping incoming URLs
  MapUrlOut
logic for mapping outgoing URLs
Functions [hide private]
 
_router_default()
return new copy of default base router
source code
 
_params_default(app=1)
return new copy of default parameters
source code
 
url_in(request, environ)
parse and rewrite incoming URL
source code
 
url_out(request, env, application, controller, function, args, other, scheme, host, port)
assemble and rewrite outgoing URL
source code
 
try_rewrite_on_error(http_response, request, environ, ticket=1)
called from main.wsgibase to rewrite the http response.
source code
 
try_redirect_on_error(http_object, request, ticket=1)
called from main.wsgibase to rewrite the http response
source code
 
load(routes='routes.py', app=1, data=1, rdict=1)
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.
source code
 
compile_regex(k, v)
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.
source code
 
load_routers(all_apps)
load-time post-processing of routers
source code
 
regex_uri(e, regexes, tag, default=1)
filter incoming URI against a list of regexes
source code
 
regex_select(env=1, app=1, request=1)
select a set of regex rewrite params for the current request
source code
 
regex_filter_in(e)
regex rewrite incoming URL
source code
 
regex_url_in(request, environ)
rewrite and parse incoming URL
source code
 
regex_filter_out(url, e=1)
regex rewrite outgoing URL
source code
 
filter_url(url, method='get', remote='0.0.0.0', out=True, app=True, lang=1, domain=(None, None), env=True, scheme=1, host=1, port=1)
doctest/unittest interface to regex_filter_in() and regex_filter_out()
source code
 
filter_err(status, application='app', ticket='tkt')
doctest/unittest interface to routes_onerror
source code
 
map_url_in(request, env, app=True)
route incoming URL
source code
 
map_url_out(request, env, application, controller, function, args, other, scheme, host, port)
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.
source code
 
get_effective_router(appname)
return a private copy of the effective router for the specified application
source code
Variables [hide private]
  logger = logging.getLogger('web2py.rewrite')
  thread = threading.local()
  params_apps = {}
  params = <Storage {'routes_out': [], 'name': 'BASE', 'routes_a...
  routers = 1
PyMySQL: A pure-Python drop-in replacement for MySQLdb.
  ROUTER_KEYS = set(['acfe_match', 'applications', 'args_match',...
  ROUTER_BASE_KEYS = set(['applications', 'default_application',...
  regex_at = re.compile(r'(?<!\\)\$[a-zA-Z]\w*')
  regex_anything = re.compile(r'(?<!\\)\$anything')
  regex_space = re.compile(r'(\+|\s|%20)+')
  regex_static = re.compile(r'(?x)(^/(?P<b>\w+)/static/(?P<x>(\w...
  regex_url = re.compile(r'(?x)(^(/(?P<a>[\w\s\+]+)(/(?P<c>[\w\s...
  regex_args = re.compile(r'(?x)(^(?P<s>([\w@/-][=\.]?)*)?/?$)')
Function Details [hide private]

load(routes='routes.py', app=1, data=1, rdict=1)

source code 
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)

map_url_out(request, env, application, controller, function, args, other, scheme, host, port)

source code 

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.


Variables Details [hide private]

params

Value:
<Storage {'routes_out': [], 'name': 'BASE', 'routes_apps_raw': [], 'er\
ror_message': '<html><body><h1>%s</h1></body></html>', 'default_functi\
on': 'index', 'default_controller': 'default', 'routes_app': [], 'rout\
es_in': [], 'default_application': 'init', 'error_message_ticket': '<h\
tml><body><h1>Internal error</h1>Ticket issued: <a href="/admin/defaul\
t/ticket/%(ticket)s" target="_blank">%(ticket)s</a></body><!-- this is\
 junk text else IE does not display the page: xxxxxxxxxxxxxxxxxxxxxxxx\
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\
...

ROUTER_KEYS

Value:
set(['acfe_match',
     'applications',
     'args_match',
     'controllers',
     'default_application',
     'default_controller',
     'default_function',
     'default_language',
...

ROUTER_BASE_KEYS

Value:
set(['applications', 'default_application', 'domains', 'path_prefix'])

regex_static

Value:
re.compile(r'(?x)(^/(?P<b>\w+)/static/(?P<x>(\w[-=\./]?)*)$)')

regex_url

Value:
re.compile(r'(?x)(^(/(?P<a>[\w\s\+]+)(/(?P<c>[\w\s\+]+)(/(?P<f>[\w\s\+\
]+)(\.(?P<e>[\w\s\+]+))?(/(?P<r>.*))?)?)?)?/?$)')