Download Install Tutorial Docs FAQ Tools WikiLicense Team IRC Planet Involvement Shop Book

Changeset 1080

Show
Ignore:
Timestamp:
04/26/06 18:54:11
Author:
fumanchu
Message:

Moved dispatch, find_handler into _cputil.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/cherrypy/_cprequest.py

    r1078 r1080  
    8787            # right away. 
    8888            self.processRequestLine() 
    89             self.dispatch = cherrypy.config.get("dispatch") or dispatch 
     89            self.dispatch = cherrypy.config.get("dispatch") or _cputil.dispatch 
    9090            self.hooks.setup() 
    9191             
     
    227227 
    228228 
    229 def dispatch(path): 
    230     """Find and run the appropriate page handler.""" 
    231     request = cherrypy.request 
    232     handler, opath, vpath = find_handler(path) 
    233      
    234     # Remove "root" from opath and join it to get found_object_path 
    235     # There are no consumers of this info right now, so this block 
    236     # may disappear soon. 
    237     if opath and opath[0] == "root": 
    238         opath.pop(0) 
    239     request.found_object_path = '/' + '/'.join(opath) 
    240      
    241     # Decode any leftover %2F in the virtual_path atoms. 
    242     vpath = [x.replace("%2F", "/") for x in vpath] 
    243     cherrypy.response.body = handler(*vpath, **request.params) 
    244  
    245 def find_handler(objectpath): 
    246     """Find the appropriate page handler for the given path.""" 
    247     objectTrail = _cputil.get_object_trail(objectpath) 
    248     names = [name for name, candidate in objectTrail] 
    249      
    250     # Try successive objects (reverse order) 
    251     mounted_app_roots = cherrypy.tree.mount_points.values() 
    252     for i in xrange(len(objectTrail) - 1, -1, -1): 
    253          
    254         name, candidate = objectTrail[i] 
    255          
    256         # Try a "default" method on the current leaf. 
    257         defhandler = getattr(candidate, "default", None) 
    258         if callable(defhandler) and getattr(defhandler, 'exposed', False): 
    259             return defhandler, names[:i+1] + ["default"], names[i+1:-1] 
    260          
    261         # Uncomment the next line to restrict positional params to "default". 
    262         # if i < len(objectTrail) - 2: continue 
    263          
    264         # Try the current leaf. 
    265         if callable(candidate) and getattr(candidate, 'exposed', False): 
    266             if i == len(objectTrail) - 1: 
    267                 # We found the extra ".index". Check if the original path 
    268                 # had a trailing slash (otherwise, do a redirect). 
    269                 if not objectpath.endswith('/'): 
    270                     atoms = cherrypy.request.browser_url.split("?", 1) 
    271                     newUrl = atoms.pop(0) + '/' 
    272                     if atoms: 
    273                         newUrl += "?" + atoms[0] 
    274                     raise cherrypy.HTTPRedirect(newUrl) 
    275             return candidate, names[:i+1], names[i+1:-1] 
    276          
    277         if candidate in mounted_app_roots: 
    278             break 
    279      
    280     # We didn't find anything 
    281     raise cherrypy.NotFound(objectpath) 
    282  
    283  
    284229class Body(object): 
    285230    """The body of the HTTP response (the response entity).""" 
  • trunk/cherrypy/_cputil.py

    r1078 r1080  
    5959     
    6060    return objectTrail 
     61 
     62def dispatch(path): 
     63    """Find and run the appropriate page handler.""" 
     64    request = cherrypy.request 
     65    handler, opath, vpath = find_handler(path) 
     66     
     67    # Remove "root" from opath and join it to get found_object_path 
     68    # There are no consumers of this info right now, so this block 
     69    # may disappear soon. 
     70    if opath and opath[0] == "root": 
     71        opath.pop(0) 
     72    request.found_object_path = '/' + '/'.join(opath) 
     73     
     74    # Decode any leftover %2F in the virtual_path atoms. 
     75    vpath = [x.replace("%2F", "/") for x in vpath] 
     76    cherrypy.response.body = handler(*vpath, **request.params) 
     77 
     78def find_handler(objectpath): 
     79    """Find the appropriate page handler for the given path.""" 
     80    objectTrail = get_object_trail(objectpath) 
     81    names = [name for name, candidate in objectTrail] 
     82     
     83    # Try successive objects (reverse order) 
     84    mounted_app_roots = cherrypy.tree.mount_points.values() 
     85    for i in xrange(len(objectTrail) - 1, -1, -1): 
     86         
     87        name, candidate = objectTrail[i] 
     88         
     89        # Try a "default" method on the current leaf. 
     90        defhandler = getattr(candidate, "default", None) 
     91        if callable(defhandler) and getattr(defhandler, 'exposed', False): 
     92            return defhandler, names[:i+1] + ["default"], names[i+1:-1] 
     93         
     94        # Uncomment the next line to restrict positional params to "default". 
     95        # if i < len(objectTrail) - 2: continue 
     96         
     97        # Try the current leaf. 
     98        if callable(candidate) and getattr(candidate, 'exposed', False): 
     99            if i == len(objectTrail) - 1: 
     100                # We found the extra ".index". Check if the original path 
     101                # had a trailing slash (otherwise, do a redirect). 
     102                if not objectpath.endswith('/'): 
     103                    atoms = cherrypy.request.browser_url.split("?", 1) 
     104                    newUrl = atoms.pop(0) + '/' 
     105                    if atoms: 
     106                        newUrl += "?" + atoms[0] 
     107                    raise cherrypy.HTTPRedirect(newUrl) 
     108            return candidate, names[:i+1], names[i+1:-1] 
     109         
     110        if candidate in mounted_app_roots: 
     111            break 
     112     
     113    # We didn't find anything 
     114    raise cherrypy.NotFound(objectpath) 
     115 
    61116 
    62117def get_special_attribute(name): 

Hosted by WebFaction

Log in as guest/cpguest to create tickets