Commits

Remi Delon  committed 0430658

Fixed ticket #123; now using urlparse.urljoin for concatenating urls; added a test for httptools.redirect()

  • Participants
  • Parent commits 3e2fbea
  • Branches cherrypy

Comments (0)

Files changed (4)

File _cphttptools.py

 """
 
 import cpg, urllib, sys, time, traceback, types, StringIO, cgi, os
-import mimetypes, sha, random, string, _cputil, cperror, Cookie
+import mimetypes, sha, random, string, _cputil, cperror, Cookie, urlparse
 from lib.filter import basefilter
 
 """
     except IndexRedirect, inst:
         # For an IndexRedirect, we don't go through the regular
         #   mechanism: we return the redirect immediately
-        newUrl = canonicalizeUrl(inst.args[0])
+        newUrl = urlparse.urljoin(cpg.request.base, inst.args[0])
         wfile.write('%s 302\r\n' % (cpg.response.headerMap['protocolVersion']))
         cpg.response.headerMap['Location'] = newUrl
         for key, valueList in cpg.response.headerMap.items():
             raise IndexRedirect(newUrl)
 
     return candidate, objectPathList, virtualPathList
- 
-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:
-            # 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

File lib/httptools.py

 """
 
 from cherrypy import cpg
+import urlparse
 
 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:
-            # 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
+    return urlparse.urljoin(cpg.request.base, url)
 
 def redirect(url):
     """ Sends a redirect to the browser (after canonicalizing the URL) """

File test/test.py

 
     print "**** THE ABOVE TESTS FAILED"
     print
-    print "**** Some errors occured: please add a ticket in our Trac system (http://trac.cherrypy.org/cgi-bin/trac.cgi/newticket) with the output of this test script"
+    print "**** Some errors occured: please add a ticket in our Trac system (http://trac.cherrypy.org/newticket) with the output of this test script"
 
 else:
     print

File test/testObjectMapping.py

 
 code = """
 from cherrypy import cpg
+from cherrypy.lib import httptools
 class Root:
     def index(self, name="world"):
         return name
     def extra(self, *p):
         return repr(p)
     extra.exposed = True
+    def redirect(self):
+        return httptools.redirect('dir1/')
+    redirect.exposed = True
     def notExposed(self):
         return "not exposed"
 class Dir1:
     ("/dir1/dir2", "cpg.response.headerMap['Status'] == 302" +
         " and cpg.response.headerMap['Location'] == 'http://127.0.0.1/dir1/dir2/'"),
     ("/dir1/dir2/dir3/dir4/index", '''cpg.response.body == "default for dir1, param is:('dir2', 'dir3', 'dir4', 'index')"'''),
+    ("/redirect", "cpg.response.headerMap['Status'] == 302" +
+        " and cpg.response.headerMap['Location'] == 'http://127.0.0.1/dir1/'"),
 ]
 
 def test(infoMap, failedList, skippedList):