Example #1
0
def parse_path_info(path_info, av=False):
    """
    Parse path info formatted like a/c/f where c and f are optional
    and a leading / accepted.
    Return tuple (a, c, f). If invalid path_info a is set to None.
    If c or f are omitted they are set to None.
    If av=True, parse args and vars
    """
    if av:
        vars = None
        if '?' in path_info:
            path_info, query = path_info.split('?', 2)
            vars = Storage()
            for var in query.split('&'):
                (var, val) = var.split('=', 2) if '=' in var else (var, None)
                vars[var] = val
        items = List(path_info.split('/'))
        args = List(items[3:]) if len(items) > 3 else None
        return (items(0), items(1), items(2), args, vars)

    mo = re.match(r'^/?(?P<a>\w+)(/(?P<c>\w+)(/(?P<f>\w+))?)?$', path_info)
    if mo:
        return (mo.group('a'), mo.group('c'), mo.group('f'))
    else:
        return (None, None, None)
Example #2
0
 def __call__(self, c=None, f='index', args=[], vars={},
              extension=None, target=None,ajax=False,ajax_trap=False,
              url=None):
     import globals
     target = target or 'c'+str(random.random())[2:]
     request = self.environment['request']
     if '.' in f:
         f, extension = f.split('.',1)
     if c and not url and not ajax:
         other_environment = copy.copy(self.environment)
         other_request = globals.Request()
         other_request.application = request.application
         other_request.controller = c
         other_request.function = f
         other_request.extension = extension or request.extension
         other_request.args = List(args)
         other_request.folder = request.folder
         other_request.env = request.env
         if not ajax_trap:
             other_request.vars = request.vars
             other_request.get_vars = request.get_vars
             other_request.post_vars = request.post_vars
         else:
             other_request.vars = vars
         other_environment['request'] = other_request
         other_response = globals.Response()
         other_environment['response'] = other_response
         other_response._view_environment = other_environment
         other_request.env.http_web2py_component_location = \
             request.env.path_info
         other_request.env.http_web2py_component_element = target
         other_response.view = '%s/%s.%s' % (c,f, other_request.extension)
         page = run_controller_in(c, f, other_environment)
         if isinstance(page, dict):
             other_response._vars = page
             for key in page:
                 other_response._view_environment[key] = page[key]
             run_view_in(other_response._view_environment)
             page = other_response.body.getvalue()
         script = ''
         if ajax_trap:
             script += "web2py_trap_form('%s','%s');" % \
                 (html.URL(request.application,c,f,
                           args=args,vars=vars,
                           extension=extension),target)
         #for (name,value) in other_response.headers:
         #    if name == 'web2py-component-command':
         #        script += value
         return html.TAG[''](html.DIV(html.XML(page),_id=target),
                             html.SCRIPT(script,_type="text/javascript"))
     else:
         url = url or html.URL(request.application,c,
                               f, args=args,vars=vars,
                               extension=extension)
         return html.TAG[''](html.SCRIPT('web2py_component("%s","%s")' % \
                                             (url,target),
                                         _type="text/javascript"),
                             html.DIV('loading...',_id=target))
Example #3
0
 def map_prefix(self):
     "strip path prefix, if present in its entirety"
     prefix = routers.BASE.path_prefix
     if prefix:
         prefixlen = len(prefix)
         if prefixlen > len(self.args):
             return
         for i in xrange(prefixlen):
             if prefix[i] != self.args[i]:
                 return  # prefix didn't match
         self.args = List(self.args[prefixlen:])  # strip the prefix
Example #4
0
 def test_listcall(self):
     a = List((1, 2, 3))
     self.assertEqual(a(1), 2)
     self.assertEqual(a[1], 2)
     self.assertEqual(a(3), None)
     self.assertEqual(a[3], None)
     self.assertEqual(a(-1), 3)
     self.assertEqual(a(-5), None)
     self.assertEqual(a[-5], None)
     self.assertEqual(a(-5, default='x'), 'x')
     self.assertEqual(a(-3, cast=str), '1')
     a.append('1234')
     self.assertEqual(a(3), '1234')
     self.assertEqual(a(3, cast=int), 1234)
     a.append('x')
     self.assertRaises(HTTP, a, 4, cast=int)
     b = List()
     self.assertEqual(b(0, cast=int, default=None), None)
     self.assertEqual(b(0, cast=int, default=None, otherwise='something'),
                      None)
Example #5
0
 def test_listcall(self):
     a = List((1, 2, 3))
     self.assertEqual(a(1), 2)
     self.assertEqual(a(-1), 3)
     self.assertEqual(a(-5), None)
     self.assertEqual(a(-5, default='x'), 'x')
     self.assertEqual(a(-3, cast=str), '1')
     a.append('1234')
     self.assertEqual(a(3), '1234')
     self.assertEqual(a(3, cast=int), 1234)
     a.append('x')
     self.assertRaises(HTTP, a, 4, cast=int)
Example #6
0
    def __init__(self, request=None, env=None):
        "initialize a map-in object"
        self.request = request
        self.env = env

        self.router = None
        self.application = None
        self.language = None
        self.controller = None
        self.function = None
        self.extension = 'html'

        self.controllers = set()
        self.functions = dict()
        self.languages = set()
        self.default_language = None
        self.map_hyphen = False
        self.exclusive_domain = False

        path = self.env['PATH_INFO']
        self.query = self.env.get('QUERY_STRING', None)
        path = path.lstrip('/')
        self.env['PATH_INFO'] = '/' + path
        self.env['WEB2PY_ORIGINAL_URI'] = self.env['PATH_INFO'] + (
            self.query and ('?' + self.query) or '')

        # to handle empty args, strip exactly one trailing slash, if present
        # .../arg1// represents one trailing empty arg
        #
        if path.endswith('/'):
            path = path[:-1]
        self.args = List(path and path.split('/') or [])

        # see http://www.python.org/dev/peps/pep-3333/#url-reconstruction for URL composition
        self.remote_addr = self.env.get('REMOTE_ADDR', 'localhost')
        self.scheme = self.env.get('wsgi.url_scheme', 'http').lower()
        self.method = self.env.get('REQUEST_METHOD', 'get').lower()
        self.host = self.env.get('HTTP_HOST')
        self.port = None
        if not self.host:
            self.host = self.env.get('SERVER_NAME')
            self.port = self.env.get('SERVER_PORT')
        if not self.host:
            self.host = 'localhost'
            self.port = '80'
        if ':' in self.host:
            (self.host, self.port) = self.host.split(':')
        if not self.port:
            if self.scheme == 'https':
                self.port = '443'
            else:
                self.port = '80'
Example #7
0
 def __init__(self):
     self.wsgi = Storage()  # hooks to environ and start_response
     self.env = Storage()
     self.cookies = Cookie.SimpleCookie()
     self.get_vars = Storage()
     self.post_vars = Storage()
     self.vars = Storage()
     self.folder = None
     self.application = None
     self.function = None
     self.args = List()
     self.extension = None
     self.now = datetime.datetime.today()
Example #8
0
    def __init__(self, input_string, storage='list', basetype=None):
        """
		Creates new graph

		:param input_string: string representation of graph
		:param storage: list or matrix
		:param basetype: type of values stored in nodes
		"""
        if storage == 'list':
            self.storage = List(input_string, basetype)
        elif storage == 'matrix':
            self.storage = Matrix(input_string, basetype)
        self.vertices, self.edges = self.storage.vertices, self.storage.edges
Example #9
0
 def test_listcall(self):
     a = List((1, 2, 3))
     self.assertEqual(a(1), 2)
     self.assertEqual(a(-1), 3)
     self.assertEqual(a(-5), None)
     self.assertEqual(a(-5, default='x'), 'x')
     self.assertEqual(a(-3, cast=str), '1')
     a.append('1234')
     self.assertEqual(a(3), '1234')
     self.assertEqual(a(3, cast=int), 1234)
     a.append('x')
     self.assertRaises(HTTP, a, 4, cast=int)
     b = List()
     # default is always returned when especified
     self.assertEqual(b(0, cast=int, default=None), None)
     self.assertEqual(b(0, cast=int, default=None, otherwise='teste'), None)
     self.assertEqual(b(0, cast=int, default='a', otherwise='teste'), 'a')
     # if don't have value and otherwise is especified it will called
     self.assertEqual(b(0, otherwise=lambda: 'something'), 'something')
     self.assertEqual(b(0, cast=int, otherwise=lambda: 'something'),
                      'something')
     # except if default is especified
     self.assertEqual(b(0, default=0, otherwise=lambda: 'something'), 0)
Example #10
0
 def __init__(self):
     Storage.__init__(self)
     self.env = Storage()
     self.cookies = Cookie.SimpleCookie()
     self.get_vars = Storage()
     self.post_vars = Storage()
     self.vars = Storage()
     self.folder = None
     self.application = None
     self.function = None
     self.args = List()
     self.extension = 'html'
     self.now = datetime.datetime.now()
     self.utcnow = datetime.datetime.utcnow()
     self.is_restful = False
     self.is_https = False
     self.is_local = False
     self.global_settings = settings.global_settings
Example #11
0
 def __init__(self, env):
     Storage.__init__(self)
     self.env = Storage(env)
     self.env.web2py_path = global_settings.applications_parent
     self.env.update(global_settings)
     self.cookies = Cookie.SimpleCookie()
     self._get_vars = None
     self._post_vars = None
     self._vars = None
     self.folder = None
     self.application = None
     self.function = None
     self.args = List()
     self.extension = 'html'
     self.now = datetime.datetime.now()
     self.utcnow = datetime.datetime.utcnow()
     self.is_restful = False
     self.is_https = False
     self.is_local = False
     self.global_settings = settings.global_settings
Example #12
0
    def __call__(self, c=None, f='index', args=None, vars=None,
                 extension=None, target=None,ajax=False,ajax_trap=False,
                 url=None,user_signature=False, content='loading...',**attr):
        if args is None: args = []
        vars = Storage(vars or {})
        import globals
        target = target or 'c'+str(random.random())[2:]
        attr['_id']=target
        request = self.environment['request']
        if '.' in f:
            f, extension = f.rsplit('.',1)
        if url or ajax:
            url = url or html.URL(request.application, c, f, r=request,
                                  args=args, vars=vars, extension=extension,
                                  user_signature=user_signature)
            script = html.SCRIPT('web2py_component("%s","%s")' % (url, target),
                                 _type="text/javascript")
            return html.TAG[''](script, html.DIV(content,**attr))
        else:
            if not isinstance(args,(list,tuple)):
                args = [args]
            c = c or request.controller

            other_request = Storage(request)
            other_request['env'] = Storage(request.env)
            other_request.controller = c
            other_request.function = f
            other_request.extension = extension or request.extension
            other_request.args = List(args)
            other_request.vars = vars
            other_request.get_vars = vars
            other_request.post_vars = Storage()
            other_response = globals.Response()
            other_request.env.path_info = '/' + \
                '/'.join([request.application,c,f] + \
                             map(str, other_request.args))
            other_request.env.query_string = \
                vars and html.URL(vars=vars).split('?')[1] or ''
            other_request.env.http_web2py_component_location = \
                request.env.path_info
            other_request.cid = target
            other_request.env.http_web2py_component_element = target
            other_response.view = '%s/%s.%s' % (c,f, other_request.extension)
            other_environment = copy.copy(self.environment)
            other_response._view_environment = other_environment
            other_response.generic_patterns = \
                copy.copy(current.response.generic_patterns)
            other_environment['request'] = other_request
            other_environment['response'] = other_response

            ## some magic here because current are thread-locals

            original_request, current.request = current.request, other_request
            original_response, current.response = current.response, other_response
            page = run_controller_in(c, f, other_environment)
            if isinstance(page, dict):
                other_response._vars = page
                other_response._view_environment.update(page)
                run_view_in(other_response._view_environment)
                page = other_response.body.getvalue()
            current.request, current.response = original_request, original_response
            js = None
            if ajax_trap:
                link = html.URL(request.application, c, f, r=request,
                                args=args, vars=vars, extension=extension,
                                user_signature=user_signature)
                js = "web2py_trap_form('%s','%s');" % (link, target)
            script = js and html.SCRIPT(js,_type="text/javascript") or ''
            return html.TAG[''](html.DIV(html.XML(page),**attr),script)
Example #13
0
def LOAD(c=None, f='index', args=None, vars=None,
         extension=None, target=None,ajax=False,ajax_trap=False,
         url=None,user_signature=False, timeout=None, times=1,
         content='loading...',**attr):
    """  LOAD a component into the action's document

    Timing options:
    -times: An integer or string ("infinity"/"continuous")
    specifies how many times the component is requested
    -timeout (milliseconds): specifies the time to wait before
    starting the request or the frequency if times is greater than
    1 or "infinity".
    Timing options default to the normal behavior. The component
    is added on page loading without delay.
    """
    from html import TAG, DIV, URL, SCRIPT, XML
    if args is None: args = []
    vars = Storage(vars or {})
    target = target or 'c'+str(random.random())[2:]
    attr['_id']=target
    request = current.request
    if '.' in f:
        f, extension = f.rsplit('.',1)
    if url or ajax:
        url = url or URL(request.application, c, f, r=request,
                         args=args, vars=vars, extension=extension,
                         user_signature=user_signature)
        # timing options
        if isinstance(times, basestring):
            if times.upper() in ("INFINITY", "CONTINUOUS"):
                times = "Infinity"
            else:
                raise TypeError("Unsupported times argument %s" % times)
        elif isinstance(times, int):
            if times <= 0:
                raise ValueError("Times argument must be greater than zero, 'Infinity' or None")
        else:
            raise TypeError("Unsupported times argument type %s" % type(times))
        if timeout is not None:
            if not isinstance(timeout, (int, long)):
                raise ValueError("Timeout argument must be an integer or None")
            elif timeout <= 0:
                raise ValueError("Timeout argument must be greater than zero or None")
            statement = "web2py_component('%s','%s', %s, %s);" \
            % (url, target, timeout, times)
        else:
            statement = "web2py_component('%s','%s');" % (url, target)
        script = SCRIPT(statement, _type="text/javascript")
        if not content is None:
            return TAG[''](script, DIV(content,**attr))
        else:
            return TAG[''](script)

    else:
        if not isinstance(args,(list,tuple)):
            args = [args]
        c = c or request.controller
        other_request = Storage(request)
        other_request['env'] = Storage(request.env)
        other_request.controller = c
        other_request.function = f
        other_request.extension = extension or request.extension
        other_request.args = List(args)
        other_request.vars = vars
        other_request.get_vars = vars
        other_request.post_vars = Storage()
        other_response = Response()
        other_request.env.path_info = '/' + \
            '/'.join([request.application,c,f] + \
                         map(str, other_request.args))
        other_request.env.query_string = \
            vars and URL(vars=vars).split('?')[1] or ''
        other_request.env.http_web2py_component_location = \
            request.env.path_info
        other_request.cid = target
        other_request.env.http_web2py_component_element = target
        other_response.view = '%s/%s.%s' % (c,f, other_request.extension)

        other_environment = copy.copy(current.globalenv) ### NASTY

        other_response._view_environment = other_environment
        other_response.generic_patterns = \
            copy.copy(current.response.generic_patterns)
        other_environment['request'] = other_request
        other_environment['response'] = other_response

        ## some magic here because current are thread-locals

        original_request, current.request = current.request, other_request
        original_response, current.response = current.response, other_response
        page = run_controller_in(c, f, other_environment)
        if isinstance(page, dict):
            other_response._vars = page
            other_response._view_environment.update(page)
            run_view_in(other_response._view_environment)
            page = other_response.body.getvalue()
        current.request, current.response = original_request, original_response
        js = None
        if ajax_trap:
            link = URL(request.application, c, f, r=request,
                            args=args, vars=vars, extension=extension,
                            user_signature=user_signature)
            js = "web2py_trap_form('%s','%s');" % (link, target)
        script = js and SCRIPT(js,_type="text/javascript") or ''
        return TAG[''](DIV(XML(page),**attr),script)
Example #14
0
def wsgibase(environ, responder):
    """
    this is the gluon wsgi application. the first function called when a page
    is requested (static or dynamic). it can be called by paste.httpserver
    or by apache mod_wsgi.

      - fills request with info
      - the environment variables, replacing '.' with '_'
      - adds web2py path and version info
      - compensates for fcgi missing path_info and query_string
      - validates the path in url

    The url path must be either:

    1. for static pages:

      - /<application>/static/<file>

    2. for dynamic pages:

      - /<application>[/<controller>[/<function>[/<sub>]]][.<extension>]
      - (sub may go several levels deep, currently 3 levels are supported:
         sub1/sub2/sub3)

    The naming conventions are:

      - application, controller, function and extension may only contain
        [a-zA-Z0-9_]
      - file and sub may also contain '-', '=', '.' and '/'
    """

    if rewrite.params.routes_in:
        environ = rewrite.filter_in(environ)

    request = Request()
    response = Response()
    session = Session()
    try:
        try:

            # ##################################################
            # parse the environment variables
            # ##################################################

            for (key, value) in environ.items():
                request.env[key.lower().replace('.', '_')] = value
            request.env.web2py_path = web2py_path
            request.env.web2py_version = web2py_version
            request.env.update(settings)

            # ##################################################
            # validate the path in url
            # ##################################################

            if not request.env.path_info and request.env.request_uri:
                # for fcgi, decode path_info and query_string
                items = request.env.request_uri.split('?')
                request.env.path_info = items[0]
                if len(items) > 1:
                    request.env.query_string = items[1]
                else:
                    request.env.query_string = ''
            path = request.env.path_info.replace('\\', '/')

            # ##################################################
            # serve if a static file
            # ##################################################

            match = regex_static.match(regex_space.sub('_', path))
            if match and match.group('x'):
                static_file = os.path.join(request.env.web2py_path,
                                           'applications', match.group('b'),
                                           'static', match.group('x'))
                if request.env.get('query_string', '')[:10] == 'attachment':
                    response.headers['Content-Disposition'] = 'attachment'
                response.stream(static_file, request=request)

            # ##################################################
            # parse application, controller and function
            # ##################################################

            path = re.sub('%20', ' ', path)
            match = regex_url.match(path)
            if not match or match.group('c') == 'static':
                raise HTTP(400,
                           rewrite.params.error_message,
                           web2py_error='invalid path')

            request.application = \
                regex_space.sub('_', match.group('a') or 'init')
            request.controller = \
                regex_space.sub('_', match.group('c') or 'default')
            request.function = \
                regex_space.sub('_', match.group('f') or 'index')
            group_e = match.group('e')
            raw_extension = group_e and regex_space.sub('_', group_e) or None
            request.extension = raw_extension or 'html'
            request.raw_args = match.group('r')
            request.args = List([])
            if request.application in rewrite.params.routes_apps_raw:
                # application is responsible for parsing args
                request.args = None
            elif request.raw_args:
                match = regex_args.match(request.raw_args)
                if match:
                    group_s = match.group('s')
                    request.args = \
                        List((group_s and group_s.split('/')) or [])
                else:
                    raise HTTP(400,
                               rewrite.params.error_message,
                               web2py_error='invalid path')
            request.client = get_client(request.env)
            request.folder = os.path.join(request.env.web2py_path,
                                          'applications',
                                          request.application) + '/'

            # ##################################################
            # access the requested application
            # ##################################################

            if not os.path.exists(request.folder):
                if request.application == 'init':
                    request.application = 'welcome'
                    redirect(URL(r=request))
                elif rewrite.params.error_handler:
                    redirect(
                        URL(rewrite.params.error_handler['application'],
                            rewrite.params.error_handler['controller'],
                            rewrite.params.error_handler['function'],
                            args=request.application))
                else:
                    raise HTTP(400,
                               rewrite.params.error_message,
                               web2py_error='invalid application')
            request.url = URL(r=request,
                              args=request.args,
                              extension=raw_extension)

            # ##################################################
            # build missing folder
            # ##################################################

            if not request.env.web2py_runtime_gae:
                for subfolder in [
                        'models', 'views', 'controllers', 'databases',
                        'modules', 'cron', 'errors', 'sessions', 'languages',
                        'static', 'private', 'uploads'
                ]:
                    path = os.path.join(request.folder, subfolder)
                    if not os.path.exists(path):
                        os.mkdir(path)

            # ##################################################
            # get the GET and POST data
            # ##################################################

            parse_get_post_vars(request, environ)

            # ##################################################
            # expose wsgi hooks for convenience
            # ##################################################

            request.wsgi.environ = environ_aux(environ, request)
            request.wsgi.start_response = lambda status='200', headers=[], \
                exec_info=None, response=response: \
                start_response_aux(status, headers, exec_info, response)
            request.wsgi.middleware = lambda *a: middleware_aux(
                request, response, *a)

            # ##################################################
            # load cookies
            # ##################################################

            if request.env.http_cookie:
                try:
                    request.cookies.load(request.env.http_cookie)
                except Cookie.CookieError, e:
                    pass  # invalid cookies

            # ##################################################
            # try load session or create new session file
            # ##################################################

            session.connect(request, response)

            # ##################################################
            # set no-cache headers
            # ##################################################

            response.headers['Content-Type'] = contenttype('.' +
                                                           request.extension)
            response.headers['Cache-Control'] = \
                'no-store, no-cache, must-revalidate, post-check=0, pre-check=0'
            response.headers['Expires'] = \
                time.strftime('%a, %d %b %Y %H:%M:%S GMT', time.gmtime())
            response.headers['Pragma'] = 'no-cache'

            # ##################################################
            # run controller
            # ##################################################

            serve_controller(request, response, session)

        except HTTP, http_response:

            if request.body:
                request.body.close()

            # ##################################################
            # on success, try store session in database
            # ##################################################
            session._try_store_in_db(request, response)

            # ##################################################
            # on success, commit database
            # ##################################################

            if response._custom_commit:
                response._custom_commit()
            else:
                BaseAdapter.close_all_instances(BaseAdapter.commit)

            # ##################################################
            # if session not in db try store session on filesystem
            # this must be done after trying to commit database!
            # ##################################################

            session._try_store_on_disk(request, response)

            # ##################################################
            # store cookies in headers
            # ##################################################

            if session._forget:
                del response.cookies[response.session_id_name]
            elif session._secure:
                response.cookies[response.session_id_name]['secure'] = True
            if len(response.cookies) > 0:
                http_response.headers['Set-Cookie'] = \
                    [str(cookie)[11:] for cookie in response.cookies.values()]
            ticket = None
Example #15
0
 def test_listgetitem(self):
     '''Mantains list behaviour.'''
     a = List((1, 2, 3))
     self.assertEqual(a[0], 1)
     self.assertEqual(a[::-1], [3, 2, 1])
Example #16
0
    def __call__(self, c=None, f='index', args=[], vars={},
                 extension=None, target=None,ajax=False,ajax_trap=False,
                 url=None,user_signature=False, content='loading...',**attr):
        import globals
        target = target or 'c'+str(random.random())[2:]
        attr['_id']=target        
        request = self.environment['request']
        if '.' in f:
            f, extension = f.split('.',1)
        if url or ajax:
            url = url or html.URL(request.application, c, f, r=request,
                                  args=args, vars=vars, extension=extension,
                                  user_signature=user_signature)
            script = html.SCRIPT('web2py_component("%s","%s")' % (url, target),
                                 _type="text/javascript")
            return html.TAG[''](script, html.DIV(content,**attr))
        else:
            if not isinstance(args,(list,tuple)):
                args = [args]
            c = c or request.controller
            other_request = globals.Request()
            other_request.application = request.application
            other_request.controller = c
            other_request.function = f
            other_request.extension = extension or request.extension
            other_request.args = List(args)
            other_request.folder = request.folder
            other_request.env = request.env
            other_request.vars = request.vars
            other_request.get_vars = request.get_vars
            other_request.post_vars = request.post_vars
            other_response = globals.Response()
            other_request.env.http_web2py_component_location = \
                request.env.path_info
            other_request.env.http_web2py_component_element = target
            other_response.view = '%s/%s.%s' % (c,f, other_request.extension)

            other_environment = copy.copy(self.environment)
            other_response._view_environment = other_environment
            other_environment['request'] = other_request
            other_environment['response'] = other_response
            page = run_controller_in(c, f, other_environment)

            if isinstance(page, dict):
                other_response._vars = page
                for key in page:
                    other_response._view_environment[key] = page[key]
                run_view_in(other_response._view_environment)
                page = other_response.body.getvalue()
            js = None
            if ajax_trap:
                link = html.URL(request.application, c, f, r=request,
                                args=args, vars=vars, extension=extension,
                                user_signature=user_signature)
                js = "web2py_trap_form('%s','%s');" % (link, target)
            # not sure about this
            # for (name,value) in other_response.headers:
            #     if name == 'web2py-component-command':
            #         js += value
            script = js and html.SCRIPT(js,_type="text/javascript") or ''
            return html.TAG[''](html.DIV(html.XML(page),**attr),script)
Example #17
0
def regex_url_in(request, environ):
    "rewrite and parse incoming URL"

    # ##################################################
    # select application
    # rewrite URL if routes_in is defined
    # update request.env
    # ##################################################

    regex_select(env=environ, request=request)

    if thread.routes.routes_in:
        environ = regex_filter_in(environ)

    for (key, value) in environ.items():
        request.env[key.lower().replace('.', '_')] = value

    path = request.env.path_info.replace('\\', '/')

    # ##################################################
    # serve if a static file
    # ##################################################

    match = regex_static.match(regex_space.sub('_', path))
    if match and match.group('x'):
        static_file = os.path.join(request.env.applications_parent,
                                   'applications', match.group('b'), 'static',
                                   match.group('x'))
        return (static_file, environ)

    # ##################################################
    # parse application, controller and function
    # ##################################################

    path = re.sub('%20', ' ', path)
    match = regex_url.match(path)
    if not match or match.group('c') == 'static':
        raise HTTP(400,
                   thread.routes.error_message % 'invalid request',
                   web2py_error='invalid path')

    request.application = \
        regex_space.sub('_', match.group('a') or thread.routes.default_application)
    request.controller = \
        regex_space.sub('_', match.group('c') or thread.routes.default_controller)
    request.function = \
        regex_space.sub('_', match.group('f') or thread.routes.default_function)
    group_e = match.group('e')
    request.raw_extension = group_e and regex_space.sub('_', group_e) or None
    request.extension = request.raw_extension or 'html'
    request.raw_args = match.group('r')
    request.args = List([])
    if request.application in thread.routes.routes_apps_raw:
        # application is responsible for parsing args
        request.args = None
    elif request.raw_args:
        match = regex_args.match(request.raw_args.replace(' ', '_'))
        if match:
            group_s = match.group('s')
            request.args = \
                List((group_s and group_s.split('/')) or [])
            if request.args and request.args[-1] == '':
                request.args.pop()  # adjust for trailing empty arg
        else:
            raise HTTP(400,
                       thread.routes.error_message % 'invalid request',
                       web2py_error='invalid path (args)')
    return (None, environ)
Example #18
0
def parse_url(request, environ):
    "parse and rewrite the incoming URL"

    # ##################################################
    # validate the path in url
    # ##################################################

    if not request.env.path_info and request.env.request_uri:
        # for fcgi, decode path_info and query_string
        items = request.env.request_uri.split('?')
        request.env.path_info = items[0]
        if len(items) > 1:
            request.env.query_string = items[1]
        else:
            request.env.query_string = ''
    path = request.env.path_info.replace('\\', '/')

    # ##################################################
    # serve if a static file
    # ##################################################

    match = regex_static.match(regex_space.sub('_', path))
    if match and match.group('x'):
        static_file = os.path.join(request.env.web2py_path, 'applications',
                                   match.group('b'), 'static',
                                   match.group('x'))
        return static_file

    # ##################################################
    # parse application, controller and function
    # ##################################################

    path = re.sub('%20', ' ', path)
    match = regex_url.match(path)
    if not match or match.group('c') == 'static':
        raise HTTP(400,
                   rewrite.thread.routes.error_message % 'invalid request',
                   web2py_error='invalid path')

    request.application = \
        regex_space.sub('_', match.group('a') or rewrite.thread.routes.default_application)
    request.controller = \
        regex_space.sub('_', match.group('c') or rewrite.thread.routes.default_controller)
    request.function = \
        regex_space.sub('_', match.group('f') or rewrite.thread.routes.default_function)
    group_e = match.group('e')
    raw_extension = group_e and regex_space.sub('_', group_e) or None
    request.extension = raw_extension or 'html'
    request.raw_args = match.group('r')
    request.args = List([])
    if request.application in rewrite.thread.routes.routes_apps_raw:
        # application is responsible for parsing args
        request.args = None
    elif request.raw_args:
        match = regex_args.match(request.raw_args.replace(' ', '_'))
        if match:
            group_s = match.group('s')
            request.args = \
                List((group_s and group_s.split('/')) or [])
        else:
            raise HTTP(400,
                       rewrite.thread.routes.error_message % 'invalid request',
                       web2py_error='invalid path')
    request.client = get_client(request.env)
    request.folder = os.path.join(request.env.web2py_path, 'applications',
                                  request.application) + '/'
    request.ajax = str(
        request.env.http_x_requested_with).lower() == 'xmlhttprequest'
    request.cid = request.env.http_web2py_component_element

    # ##################################################
    # access the requested application
    # ##################################################

    if not os.path.exists(request.folder):
        if request.application == rewrite.thread.routes.default_application:
            request.application = 'welcome'
            redirect(Url(r=request))
        elif rewrite.thread.routes.error_handler:
            redirect(
                Url(rewrite.thread.routes.error_handler['application'],
                    rewrite.thread.routes.error_handler['controller'],
                    rewrite.thread.routes.error_handler['function'],
                    args=request.application))
        else:
            raise HTTP(400,
                       rewrite.thread.routes.error_message % 'invalid request',
                       web2py_error='invalid application')
    request.url = Url(r=request, args=request.args, extension=raw_extension)
    return None