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()
Returns new copy of default base router
source code
 
_params_default(app=None)
Returns a new copy of default parameters
source code
 
log_rewrite(string)
Log rewrite activity under control of routes.py
source code
 
fixup_missing_path_info(environ) source code
 
url_in(request, environ)
Parses and rewrites incoming URL
source code
 
url_out(request, environ, application, controller, function, args, other, scheme, host, port, language=None)
Assembles and rewrites outgoing URL
source code
 
try_rewrite_on_error(http_response, request, environ, ticket=None)
Called from main.wsgibase to rewrite the http response.
source code
 
try_redirect_on_error(http_object, request, ticket=None)
Called from main.wsgibase to rewrite the http response
source code
 
load(routes='routes.py', app=None, data=None, rdict=None)
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, env=None)
Preprocess and compile the regular expressions in routes_app/in/out The resulting regex will match a pattern of the form:
source code
 
load_routers(all_apps)
Load-time post-processing of routers
source code
 
regex_uri(e, regexes, tag, default=None)
Filters incoming URI against a list of regexes
source code
 
regex_select(env=None, app=None, request=None)
Selects a set of regex rewrite params for the current request
source code
 
regex_filter_in(e)
Regex rewrite incoming URL
source code
 
sluggify(key) source code
 
invalid_url(routes) source code
 
regex_url_in(request, environ)
Rewrites and parses incoming URL
source code
 
regex_filter_out(url, e=None)
Regex rewrite outgoing URL
source code
 
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)
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=False)
Routes incoming URL
source code
 
map_url_out(request, env, application, controller, function, args, other, scheme, host, port, language=None)
Supply /a/c/f (or /a/lang/c/f) portion of outgoing url
source code
 
get_effective_router(appname)
Returns a private copy of the effective router for the specified application
source code
Variables [hide private]
  logger = logging.getLogger('web2py.rewrite')
  THREAD_LOCAL = threading.local()
  regex_at = re.compile(r'(?<!\\)\$[a-zA-Z]\w*')
  regex_anything = re.compile(r'(?<!\\)\$anything')
  regex_redirect = re.compile(r'(\d+)->(.*)')
  regex_full_url = re.compile(r'^(?P<scheme>http|https|HTTP|HTTP...
  regex_version = re.compile(r'^(_\d+\.\d+\.\d+)$')
  regex_url = re.compile(r'^/((?P<a>\w+)(/(?P<c>\w+)(/(?P<z>(?P<...
  regex_args = re.compile(r'[^\w/\.@=-]')
  params_apps = {}
  params = <Storage {'routes_out': [], 'logging': 'off', 'name':...
  routers = None
hash(x)
  ROUTER_KEYS = set(['acfe_match', 'applications', 'args_match',...
  ROUTER_BASE_KEYS = set(['applications', 'default_application',...
  __package__ = 'gluon'
Function Details [hide private]

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

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)

compile_regex(k, v, env=None)

source code 

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.

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

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]

regex_full_url

Value:
re.compile(r'^(?P<scheme>http|https|HTTP|HTTPS)://(?P<host>[^/]*)(?P<u\
ri>.*)')

regex_url

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

params

Value:
<Storage {'routes_out': [], 'logging': 'off', 'name': 'BASE', 'routes_\
apps_raw': [], 'routers': None, 'error_message': '<html><body><h1>%s</\
h1></body></html>', 'default_function': 'index', 'default_controller':\
 'default', 'error_handler': None, 'routes_app': [], 'routes_in': [], \
'default_application': 'init', 'error_message_ticket': '<html><body><h\
1>Internal error</h1>Ticket issued: <a href="/admin/default/ticket/%(t\
icket)s" target="_blank">%(ticket)s</a></body><!-- this is junk text e\
lse IE does not display the page: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\
...

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'])