diff options
author | Remi Delon <rdelon@cherrypy.org> | 2005-01-10 18:01:18 +0000 |
---|---|---|
committer | Remi Delon <rdelon@cherrypy.org> | 2005-01-10 18:01:18 +0000 |
commit | 9c9ab1399b4804bf725bd714ffc28b93d9440bd3 (patch) | |
tree | 22a4f83f55fa97ec6c1caf03431d7b7b3b620b4d | |
parent | 22c119b39a6d03cd84a63843eb76ff262134f827 (diff) | |
download | cherrypy-git-rdelon-experimental.tar.gz |
Refactored the httptools.redirect coderdelon-experimental
-rw-r--r-- | cherrypy/_cphttptools.py | 19 | ||||
-rw-r--r-- | cherrypy/lib/httptools.py | 33 |
2 files changed, 29 insertions, 23 deletions
diff --git a/cherrypy/_cphttptools.py b/cherrypy/_cphttptools.py index 4b4f6503..64403805 100644 --- a/cherrypy/_cphttptools.py +++ b/cherrypy/_cphttptools.py @@ -520,19 +520,20 @@ def mapPathToObject(path = None): raise IndexRedirect(newUrl) return candidate, objectPathList, virtualPathList - -def canonicalizeUrl(newUrl): - if not newUrl.startswith('http://') and not newUrl.startswith('https://'): - # If newUrl is not canonical, we must make it canonical - if newUrl[0] == '/': + +def canonicalizeUrl(url): + """ Canonicalize a URL. The URL might be relative, absolute or canonical """ + if not url.startswith('http://') and not url.startswith('https://'): + # If url is not canonical, we must make it canonical + if url[0] == '/': # URL was absolute: we just add the request.base in front of it - newUrl = cpg.request.base + newUrl + url = cpg.request.base + url else: # URL was relative if cpg.request.browserUrl == cpg.request.base: # browserUrl is request.base - newUrl = cpg.request.base + '/' + newUrl + url = cpg.request.base + '/' + url else: i = cpg.request.browserUrl.rfind('/') - newUrl = cpg.request.browserUrl[:i+1] + newUrl - return newUrl + url = cpg.request.browserUrl[:i+1] + url + return url diff --git a/cherrypy/lib/httptools.py b/cherrypy/lib/httptools.py index 1617b8d5..66324c34 100644 --- a/cherrypy/lib/httptools.py +++ b/cherrypy/lib/httptools.py @@ -32,20 +32,25 @@ Just a few convenient functions from cherrypy import cpg -def redirect(newUrl): - """ Sends a redirect to the browser """ - - if not newUrl.startswith('http://') and not newUrl.startswith('https://'): - # If newUrl is not canonical, we must make it canonical - if newUrl.startswith('/'): - # newUrl was absolute: - # we just add request.base in front of it - newUrl = cpg.request.base + newUrl +def canonicalizeUrl(url): + """ Canonicalize a URL. The URL might be relative, absolute or canonical """ + if not url.startswith('http://') and not url.startswith('https://'): + # If url is not canonical, we must make it canonical + if url[0] == '/': + # URL was absolute: we just add the request.base in front of it + url = cpg.request.base + url else: - # newUrl was relative: - # we remove the last bit from browserUrl and add newUrl to it - i = cpg.request.browserUrl.rfind('/') - newUrl = cpg.request.browserUrl[:i+1] + newUrl + # URL was relative + if cpg.request.browserUrl == cpg.request.base: + # browserUrl is request.base + url = cpg.request.base + '/' + url + else: + i = cpg.request.browserUrl.rfind('/') + url = cpg.request.browserUrl[:i+1] + url + return url + +def redirect(url): + """ Sends a redirect to the browser (after canonicalizing the URL) """ cpg.response.headerMap['Status'] = 302 - cpg.response.headerMap['Location'] = newUrl + cpg.response.headerMap['Location'] = canonicalizeUrl(url) return "" |