[Checkins] SVN: z3c.rest/trunk/ - Bug/Misfeature: Finally get handling of URL merging working as desired. Also

Stephan Richter srichter at cosmos.phy.tufts.edu
Tue Sep 16 18:33:33 EDT 2008


Log message for revision 91201:
  - Bug/Misfeature: Finally get handling of URL merging working as desired. Also
    added extensive tests to document the behavior.
  
  - Get ready for release.
  

Changed:
  U   z3c.rest/trunk/CHANGES.txt
  U   z3c.rest/trunk/setup.py
  U   z3c.rest/trunk/src/z3c/rest/client.py
  U   z3c.rest/trunk/src/z3c/rest/client.txt

-=-
Modified: z3c.rest/trunk/CHANGES.txt
===================================================================
--- z3c.rest/trunk/CHANGES.txt	2008-09-16 22:01:45 UTC (rev 91200)
+++ z3c.rest/trunk/CHANGES.txt	2008-09-16 22:33:33 UTC (rev 91201)
@@ -2,14 +2,17 @@
 CHANGES
 =======
 
-0.2.5 (unreleased)
+0.2.5 (2008-09-16)
 ------------------
 
+- Bug/Misfeature: Finally get handling of URL merging working as desired. Also
+  added extensive tests to document the behavior.
 
+
 0.2.4 (2008-09-04)
 ------------------
 
-- RESTClient() now correctly interprets https:// URLs.
+- RESTClient() now correctly interprets `https://` URLs.
 
 
 0.2.3 (2008-03-20)

Modified: z3c.rest/trunk/setup.py
===================================================================
--- z3c.rest/trunk/setup.py	2008-09-16 22:01:45 UTC (rev 91200)
+++ z3c.rest/trunk/setup.py	2008-09-16 22:33:33 UTC (rev 91201)
@@ -31,7 +31,7 @@
 
 setup (
     name='z3c.rest',
-    version='0.2.5dev',
+    version='0.2.5',
     author = "Stephan Richter and the Zope Community",
     author_email = "zope3-dev at zope.org",
     description = "A REST Framework for Zope 3 Applications",

Modified: z3c.rest/trunk/src/z3c/rest/client.py
===================================================================
--- z3c.rest/trunk/src/z3c/rest/client.py	2008-09-16 22:01:45 UTC (rev 91200)
+++ z3c.rest/trunk/src/z3c/rest/client.py	2008-09-16 22:33:33 UTC (rev 91201)
@@ -34,21 +34,25 @@
 
 def absoluteURL(base, url):
     """Convertes a URL to an absolute URL given a base."""
-    if isRelativeURL(url):
-        if not base.endswith('/'):
-            base += '/'
-        fullUrl = urlparse.urljoin(base, url)
-    else:
-        fullUrl = url
-    pieces = list(urlparse.urlparse(fullUrl))
+    if not isRelativeURL(url):
+        return url
+
+    pieces = list(urlparse.urlparse(base))
+    urlPieces = list(urlparse.urlparse(url))
+
     if not pieces[2].endswith('/'):
         pieces[2] += '/'
-    newUrl = urlparse.urlunparse(pieces)
-    # Some systems really do not like the trailing /
-    if not url.endswith('/') and newUrl.endswith('/'):
-        newUrl = newUrl[:-1]
-    return newUrl
+    pieces[2] = urlparse.urljoin(pieces[2], urlPieces[2])
 
+    if urlPieces[4]:
+        if pieces[4]:
+            pieces[4] = pieces[4] + '&' + urlPieces[4]
+        else:
+            pieces[4] = urlPieces[4]
+
+    return urlparse.urlunparse(pieces)
+
+
 def getFullPath(pieces, params):
     """Build a full httplib request path, including a query string."""
     query = ''

Modified: z3c.rest/trunk/src/z3c/rest/client.txt
===================================================================
--- z3c.rest/trunk/src/z3c/rest/client.txt	2008-09-16 22:01:45 UTC (rev 91200)
+++ z3c.rest/trunk/src/z3c/rest/client.txt	2008-09-16 22:33:33 UTC (rev 91201)
@@ -547,3 +547,72 @@
   Traceback (most recent call last):
   ...
   ValueError: There is not enough history.
+
+
+Absolute URLs
+-------------
+
+As mentioned above, we allow specifying relative URLs, a call with an absolute
+URL has been made. A function called ``absoluteURL()`` is used to compute the
+new absolute URL.
+
+  >>> from z3c.rest.client import absoluteURL
+
+The basic functionality is simple:
+
+  >>> absoluteURL('http://localhost/folder1/', 'folder11')
+  'http://localhost/folder1/folder11'
+
+It also detects, if the new part of the URL is absolute, in which case it
+replaces the original base URL:
+
+  >>> absoluteURL('http://einstein/folder1', 'http://localhost/folder11')
+  'http://localhost/folder11'
+
+If the base URL does not have a trailing slash, it is added automatically:
+
+  >>> absoluteURL('http://localhost/folder1', 'folder11')
+  'http://localhost/folder1/folder11'
+
+Any slashes at the end of the new URL are preserved:
+
+  >>> absoluteURL('http://localhost/folder1', 'folder11/')
+  'http://localhost/folder1/folder11/'
+
+  >>> absoluteURL('http://einstein/folder1', 'http://localhost/folder11/')
+  'http://localhost/folder11/'
+
+The function also handles more complex URLs containing a query string
+correctly:
+
+  >>> absoluteURL('http://localhost/folder1', 'folder11?max=1')
+  'http://localhost/folder1/folder11?max=1'
+
+  >>> absoluteURL('http://localhost/folder1', 'folder11/?max=1')
+  'http://localhost/folder1/folder11/?max=1'
+
+If the base URL contains a query string, the resulting URL will as well:
+
+  >>> absoluteURL('http://localhost/folder1?max=1', 'folder11/')
+  'http://localhost/folder1/folder11/?max=1'
+
+  >>> absoluteURL('http://localhost/folder1/?max=1', 'folder11/')
+  'http://localhost/folder1/folder11/?max=1'
+
+  >>> absoluteURL('http://localhost/folder1?max=1', 'folder11')
+  'http://localhost/folder1/folder11?max=1'
+
+  >>> absoluteURL('http://localhost/folder1/?max=1', 'folder11')
+  'http://localhost/folder1/folder11?max=1'
+
+If both, the base and relative URL provide query strings, they are merged:
+
+  >>> absoluteURL('http://localhost/folder1/?max=1', 'folder11?min=0')
+  'http://localhost/folder1/folder11?max=1&min=0'
+
+  >>> absoluteURL('http://localhost/folder1/?max=1', 'folder11?min=0&start=0')
+  'http://localhost/folder1/folder11?max=1&min=0&start=0'
+
+  >>> absoluteURL('http://localhost/folder1/?max=1&start=0', 'folder11?min=0')
+  'http://localhost/folder1/folder11?max=1&start=0&min=0'
+



More information about the Checkins mailing list