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

Ticket #4: #2-#4.patch

  • _cpHTTPTools.py

    old new  
    6565                        cpg.request.paramMap[key] = value 
    6666        cpg.request.path = cpg.request.path[:i] 
    6767 
     68    cpg.request.trailingSlash = False 
    6869    if cpg.request.path and cpg.request.path[-1] == '/': 
    6970        cpg.request.path = cpg.request.path[:-1] # Remove trailing '/' if any 
     71        cpg.request.trailingSlash = True 
    7072 
    7173def parsePostData(rfile): 
    7274    # Read request body and put it in data 
     
    353355    searchedPathList = [] 
    354356    myPath = '' 
    355357    bestKnownDefaultMethod = None 
     358    trailingSlash = True 
    356359    # Successively get objects from the path: 'root', then 'a' then 'b' 
    357360    for pathItem in pathList: 
    358361        previousObj = obj 
     
    366369 
    367370            # if found object has a default method, remember it for later 
    368371            default = getattr(obj, 'default', None) 
    369             if default: 
    370                 # TODO: check if default is callable? 
     372            if default and callable(default) and getattr(default, 'exposed', None): 
    371373                bestKnownDefaultMethod = default 
    372374                bestKnownDefaultMethodMyPath = '/'.join(searchedPathList[1:]) 
    373375 
     
    375377            break 
    376378 
    377379    # TODO: the following code could probably be KISSed somewhat. 
    378  
     380     
     381    # root_a_b exists 
     382    root_a_b = objList[-1] 
     383     
    379384    if len(objList) == len(pathList): 
    380         # root_a_b exists 
    381         root_a_b = objList[-1] 
    382  
    383385        # Try root.a.b.index() 
    384386        root_a_b_dot_index = getattr(root_a_b, 'index', None) 
    385387        if root_a_b_dot_index and getattr(root_a_b_dot_index, 'exposed', None): 
    386             myPath = '/'.join(pathList[1:]) 
     388            myPath = '/'.join(searchedPathList[1:]) 
    387389            func = root_a_b_dot_index 
    388         else: 
    389             # Try root.a.b.default() 
    390             root_a_b_dot_default = getattr(root_a_b, 'default', None) 
    391             if root_a_b_dot_default and getattr(root_a_b_dot_default, 'exposed', None): 
    392                 # XXX: I don't think this ever gets called? 
    393                 myPath = 'root_a_b_dot_default' 
    394                 func = root_a_b_dot_default 
    395             elif callable(root_a_b) and getattr(root_a_b, 'exposed', None): 
    396                 # We use root.a.b() 
    397                 myPath = '/'.join(pathList[1:-1]) 
    398                 func = root_a_b 
     390             
     391    if func == None: 
     392        # Try root.a.b() 
     393        if callable(root_a_b) and getattr(root_a_b, 'exposed', None): 
     394            myPath = '/'.join(searchedPathList[1:-1]) 
     395            func = root_a_b 
     396            trailingSlash = False 
    399397 
    400398    if func == None: 
    401399        # None of these exist: use the default method we found earlier 
     
    415413            myPath = '/' + myPath 
    416414 
    417415        cpg.request.objectPath = myPath 
    418         cpg.request.virtualPath = cpg.request.path[len(myPath)-1:] 
    419         cpg.response.body = func(**(cpg.request.paramMap)) 
     416        cpg.request.virtualPath = "/".join(pathList[len(searchedPathList):]) 
     417             
     418        if cpg.request.virtualPath: trailingSlash = False 
     419         
     420        # Redirect to canonical URL 
     421        print cpg.request.path, trailingSlash, cpg.request.trailingSlash 
     422        if cpg.request.path and (trailingSlash != cpg.request.trailingSlash): 
     423            if trailingSlash: 
     424                redirect(wfile, "/" + cpg.request.path + "/") 
     425            else: 
     426                redirect(wfile, "/" + cpg.request.path) 
     427             
     428        cpg.response.body = func(*(pathList[len(searchedPathList):]), **(cpg.request.paramMap)) 
    420429 
    421430    _cpUtil.getSpecialFunction('_cpInitResponse')() 
    422431 
     
    448457 
    449458    if cpg.response.sendResponse: sendResponse(wfile) 
    450459 
     460def redirect(wfile, url): 
     461    cpg.response.headerMap["Status"] = "301 Moved Permanently" 
     462    cpg.response.headerMap["Location"] = url 
     463    cpg.response.body = '''Moved <a href="%s">here</a>.''' % url 
     464    sendResponse(wfile) 
    451465 
    452466def generateSessionId(): 
    453467    s = '' 

Hosted by WebFaction

Log in as guest/cpguest to create tickets