Ticket #4: #2-#4b.patch
-
Test/test.py
old new 92 92 # display a list of links to random, made-up users. In a real 93 93 # application, this could be generated from a database result set. 94 94 return renderTemplate(locals(), globals(), ''' 95 <a href="<py-eval="cpg.request. myPath">/remi">Remi Delon</a><br/>96 <a href="<py-eval="cpg.request. myPath">/hendrik">Hendrik Mans</a><br/>97 <a href="<py-eval="cpg.request. myPath">/lorenzo">Lorenzo Lamas</a><br/>95 <a href="<py-eval="cpg.request.objectPath">remi">Remi Delon</a><br/> 96 <a href="<py-eval="cpg.request.objectPath">hendrik">Hendrik Mans</a><br/> 97 <a href="<py-eval="cpg.request.objectPath">lorenzo">Lorenzo Lamas</a><br/> 98 98 ''') 99 99 100 100 index.exposed = True 101 101 102 102 103 def default(self ):103 def default(self, person): 104 104 # Here we react depending on the virtualPath -- the part of the 105 105 # path that could not be mapped to an object method. In a real 106 106 # application, we would probably do some database lookups here 107 107 # instead of the silly if/elif/else construct. 108 if cpg.request.virtualPath== 'remi':108 if person == 'remi': 109 109 out = "Remi Delon, CherryPy lead developer" 110 elif cpg.request.virtualPath== 'hendrik':110 elif person == 'hendrik': 111 111 out = "Hendrik Mans, CherryPy fanboi" 112 elif cpg.request.virtualPath== 'lorenzo':112 elif person == 'lorenzo': 113 113 out = "Lorenzo Lamas, famous actor and singer!" 114 114 else: 115 115 out = "Unknown user. :-(" 116 116 117 return '%s (<a href="%s">back</a>)' % (out, cpg.request. myPath)117 return '%s (<a href="%s">back</a>)' % (out, cpg.request.objectPath) 118 118 119 119 default.exposed = True 120 120 -
Tutorial/tutorial07.py
old new 32 32 index = cpg.expose(index) 33 33 34 34 35 def default(self ):35 def default(self, person): 36 36 # Here we react depending on the virtualPath -- the part of the 37 37 # path that could not be mapped to an object method. In a real 38 38 # application, we would probably do some database lookups here 39 39 # instead of the silly if/elif/else construct. 40 if cpg.request.virtualPath== 'remi':40 if person == 'remi': 41 41 out = "Remi Delon, CherryPy lead developer" 42 elif cpg.request.virtualPath== 'hendrik':42 elif person == 'hendrik': 43 43 out = "Hendrik Mans, CherryPy co-developer & crazy German" 44 elif cpg.request.virtualPath== 'lorenzo':44 elif person == 'lorenzo': 45 45 out = "Lorenzo Lamas, famous actor and singer!" 46 46 else: 47 47 out = "Unknown user. :-(" -
CherryPy/_cpHTTPTools.py
old new 11 11 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 12 12 """ 13 13 14 import cpg, urllib, sys, time, traceback, types, StringIO, cgi, os 14 import cpg, urllib, sys, time, traceback, types, StringIO, cgi, os, inspect 15 15 import mimetypes, sha, whrandom, string, _cpUtil, cpError 16 16 17 17 """ … … 65 65 cpg.request.paramMap[key] = value 66 66 cpg.request.path = cpg.request.path[:i] 67 67 68 cpg.request.trailingSlash = False 68 69 if cpg.request.path and cpg.request.path[-1] == '/': 69 70 cpg.request.path = cpg.request.path[:-1] # Remove trailing '/' if any 71 cpg.request.trailingSlash = True 70 72 71 73 def parsePostData(rfile): 72 74 # Read request body and put it in data … … 353 355 searchedPathList = [] 354 356 myPath = '' 355 357 bestKnownDefaultMethod = None 358 trailingSlash = True 356 359 # Successively get objects from the path: 'root', then 'a' then 'b' 357 360 for pathItem in pathList: 358 361 previousObj = obj … … 366 369 367 370 # if found object has a default method, remember it for later 368 371 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): 371 373 bestKnownDefaultMethod = default 372 374 bestKnownDefaultMethodMyPath = '/'.join(searchedPathList[1:]) 373 375 … … 375 377 break 376 378 377 379 # TODO: the following code could probably be KISSed somewhat. 378 380 381 # root_a_b exists 382 root_a_b = objList[-1] 383 379 384 if len(objList) == len(pathList): 380 # root_a_b exists381 root_a_b = objList[-1]382 383 385 # Try root.a.b.index() 384 386 root_a_b_dot_index = getattr(root_a_b, 'index', None) 385 387 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:]) 387 389 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 399 397 400 398 if func == None: 401 399 # None of these exist: use the default method we found earlier … … 415 413 myPath = '/' + myPath 416 414 417 415 cpg.request.objectPath = myPath 418 cpg.request.virtualPath = cpg.request.path[len(myPath)-1:] 419 cpg.response.body = func(**(cpg.request.paramMap)) 416 virtualParameters = pathList[len(searchedPathList):] 417 cpg.request.virtualPath = "/".join(virtualParameters) 418 419 if cpg.request.virtualPath: 420 # Add a trailing slash if the list of virtual parameters is partial 421 trailingSlash = len(virtualParameters) < len(inspect.getargspec(func)[0])-1 422 423 # Redirect to canonical URL 424 if cpg.request.path and (trailingSlash != cpg.request.trailingSlash): 425 if trailingSlash: 426 redirect(wfile, "/" + cpg.request.path + "/") 427 else: 428 redirect(wfile, "/" + cpg.request.path) 429 430 cpg.response.body = func(*virtualParameters, **(cpg.request.paramMap)) 420 431 421 432 _cpUtil.getSpecialFunction('_cpInitResponse')() 422 433 … … 448 459 449 460 if cpg.response.sendResponse: sendResponse(wfile) 450 461 462 def redirect(wfile, url): 463 cpg.response.headerMap["Status"] = "301 Moved Permanently" 464 cpg.response.headerMap["Location"] = url 465 cpg.response.body = '''<html><body>Moved <a href="%s">here</a>.</body></html>''' % url 466 sendResponse(wfile) 451 467 452 468 def generateSessionId(): 453 469 s = ''

