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

Changeset 124

Show
Ignore:
Timestamp:
01/10/05 12:01:18
Author:
rdelon
Message:

Refactored the httptools.redirect code

Files:

Legend:

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

    r122 r124  
    521521 
    522522    return candidate, objectPathList, virtualPathList 
    523      
    524 def canonicalizeUrl(newUrl): 
    525     if not newUrl.startswith('http://') and not newUrl.startswith('https://'): 
    526         # If newUrl is not canonical, we must make it canonical 
    527         if newUrl[0] == '/': 
     523  
     524def canonicalizeUrl(url): 
     525    """ Canonicalize a URL. The URL might be relative, absolute or canonical """ 
     526    if not url.startswith('http://') and not url.startswith('https://'): 
     527        # If url is not canonical, we must make it canonical 
     528        if url[0] == '/': 
    528529            # URL was absolute: we just add the request.base in front of it 
    529             newUrl = cpg.request.base + newUrl 
     530            url = cpg.request.base + url 
    530531        else: 
    531532            # URL was relative 
    532533            if cpg.request.browserUrl == cpg.request.base: 
    533534                # browserUrl is request.base 
    534                 newUrl = cpg.request.base + '/' + newUrl 
     535                url = cpg.request.base + '/' + url 
    535536            else: 
    536537                i = cpg.request.browserUrl.rfind('/') 
    537                 newUrl = cpg.request.browserUrl[:i+1] + newUrl 
    538     return newUrl 
     538                url = cpg.request.browserUrl[:i+1] + url 
     539    return url 
  • trunk/cherrypy/lib/httptools.py

    r119 r124  
    3333from cherrypy import cpg 
    3434 
    35 def redirect(newUrl): 
    36     """ Sends a redirect to the browser """ 
    37      
    38     if not newUrl.startswith('http://') and not newUrl.startswith('https://'): 
    39         # If newUrl is not canonical, we must make it canonical 
    40         if newUrl.startswith('/'): 
    41             # newUrl was absolute: 
    42             # we just add request.base in front of it 
    43             newUrl = cpg.request.base + newUrl 
     35def canonicalizeUrl(url): 
     36    """ Canonicalize a URL. The URL might be relative, absolute or canonical """ 
     37    if not url.startswith('http://') and not url.startswith('https://'): 
     38        # If url is not canonical, we must make it canonical 
     39        if url[0] == '/': 
     40            # URL was absolute: we just add the request.base in front of it 
     41            url = cpg.request.base + url 
    4442        else: 
    45             # newUrl was relative: 
    46             # we remove the last bit from browserUrl and add newUrl to it 
    47             i = cpg.request.browserUrl.rfind('/') 
    48             newUrl = cpg.request.browserUrl[:i+1] + newUrl 
     43            # URL was relative 
     44            if cpg.request.browserUrl == cpg.request.base: 
     45                # browserUrl is request.base 
     46                url = cpg.request.base + '/' + url 
     47            else: 
     48                i = cpg.request.browserUrl.rfind('/') 
     49                url = cpg.request.browserUrl[:i+1] + url 
     50    return url 
     51 
     52def redirect(url): 
     53    """ Sends a redirect to the browser (after canonicalizing the URL) """ 
    4954    cpg.response.headerMap['Status'] = 302 
    50     cpg.response.headerMap['Location'] = newUrl 
     55    cpg.response.headerMap['Location'] = canonicalizeUrl(url) 
    5156    return "" 

Hosted by WebFaction

Log in as guest/cpguest to create tickets