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=global_settings.applications_parent)
return new copy of default parameters
source code
 
log_rewrite(string)
Log rewrite activity under control of routes.py
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=global_settings.applications_parent)
called from main.wsgibase to rewrite the http response.
source code
 
try_redirect_on_error(http_object, request, ticket=global_settings.applications_parent)
called from main.wsgibase to rewrite the http response
source code
 
load(routes='routes.py', app=global_settings.applications_parent, data=global_settings.applications_parent, rdict=global_settings.applications_parent)
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=global_settings.applications_parent)
filter incoming URI against a list of regexes
source code
 
regex_select(env=global_settings.applications_parent, app=global_settings.applications_parent, request=global_settings.applications_parent)
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=global_settings.applications_parent)
regex rewrite outgoing URL
source code
 
filter_url(url, method='get', remote='0.0.0.0', out=True, app=True, lang=global_settings.applications_parent, domain=(None, None), env=True, scheme=global_settings.applications_parent, host=global_settings.applications_parent, port=global_settings.applications_parent)
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': [], 'logging': 'off', 'name':...
  routers = <web2py.gluon.custom_import._Web2pyImporter object a...
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=global_settings.applications_parent, data=global_settings.applications_parent, rdict=global_settings.applications_parent)

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': [], 'logging': 'off', 'name': 'BASE', 'routes_\
apps_raw': [], 'error_message': '<html><body><h1>%s</h1></body></html>\
', 'default_function': 'index', 'default_controller': 'default', 'rout\
es_app': [], 'routes_in': [], 'default_application': 'init', 'error_me\
ssage_ticket': '<html><body><h1>Internal error</h1>Ticket issued: <a h\
ref="/admin/default/ticket/%(ticket)s" target="_blank">%(ticket)s</a><\
/body><!-- this is junk text else IE does not display the page: xxxxxx\
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\
...

routers

PyMySQL: A pure-Python drop-in replacement for MySQLdb.

Copyright (c) 2010 PyMySQL contributors

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Value:
global_settings.applications_parent

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>.*))?)?)?)?/?$)')