Example #1
0
def get_path_components(path):
    """returns (script_name, path_info)

    determines what part of the path belongs to cp (script_name)
    and what part belongs to the wsgi application (path_info)
    """
    no_parts = ['']
    object_trail = get_object_trail(path)
    root = object_trail.pop(0)
    if not path.endswith('/index'):
        object_trail.pop()
    script_name_parts = [""]
    path_info_parts = [""]
    for (pc,obj) in object_trail:
        if obj:
            script_name_parts.append(pc)
        else:
            path_info_parts.append(pc)
    script_name = "/".join(script_name_parts)
    path_info = "/".join(path_info_parts)
    if len(script_name) > 1 and path.endswith('/'):
        path_info = path_info + '/'
    
    if script_name and not script_name.startswith('/'):
        script_name = '/' + script_name
    if path_info and not path_info.startswith('/'):
        path_info = '/' + path_info
    
    return script_name, path_info
Example #2
0
 def mapPathToObject(self, objectpath):
     """For path, return the corresponding exposed callable (or raise NotFound).
     
     path should be a "relative" URL path, like "/app/a/b/c". Leading and
     trailing slashes are ignored.
     
     Traverse path:
     for /a/b?arg=val, we'll try:
       root.a.b.index -> redirect to /a/b/?arg=val
       root.a.b.default(arg='val') -> redirect to /a/b/?arg=val
       root.a.b(arg='val')
       root.a.default('b', arg='val')
       root.default('a', 'b', arg='val')
     
     The target method must have an ".exposed = True" attribute.
     """
     
     objectTrail = _cputil.get_object_trail(objectpath)
     names = [name for name, candidate in objectTrail]
     
     # Try successive objects (reverse order)
     mounted_app_roots = cherrypy.tree.mount_points.values()
     for i in xrange(len(objectTrail) - 1, -1, -1):
         
         name, candidate = objectTrail[i]
         
         # Try a "default" method on the current leaf.
         defhandler = getattr(candidate, "default", None)
         if callable(defhandler) and getattr(defhandler, 'exposed', False):
             return defhandler, names[:i+1] + ["default"], names[i+1:-1]
         
         # Uncomment the next line to restrict positional params to "default".
         # if i < len(objectTrail) - 2: continue
         
         # Try the current leaf.
         if callable(candidate) and getattr(candidate, 'exposed', False):
             if i == len(objectTrail) - 1:
                 # We found the extra ".index". Check if the original path
                 # had a trailing slash (otherwise, do a redirect).
                 if not objectpath.endswith('/'):
                     atoms = self.browser_url.split("?", 1)
                     newUrl = atoms.pop(0) + '/'
                     if atoms:
                         newUrl += "?" + atoms[0]
                     raise cherrypy.HTTPRedirect(newUrl)
             return candidate, names[:i+1], names[i+1:-1]
         
         if candidate in mounted_app_roots:
             break
     
     # We didn't find anything
     raise cherrypy.NotFound(objectpath)