work around Apache's mod_proxy limitation with special chars in URLs by not using Rest urls when such a case is detected stable
authorAlexandre Fayolle <alexandre.fayolle@logilab.fr>
Wed, 11 Aug 2010 15:54:44 +0200
branchstable
changeset 6099 b22a4a3895df
parent 6098 a0fde9334dd0
child 6100 3cd1e6a74830
work around Apache's mod_proxy limitation with special chars in URLs by not using Rest urls when such a case is detected
entity.py
test/unittest_entity.py
--- a/entity.py	Wed Aug 11 16:17:05 2010 +0200
+++ b/entity.py	Wed Aug 11 15:54:44 2010 +0200
@@ -51,6 +51,18 @@
                 return card
     return '1'
 
+def can_use_rest_path(value):
+    """return True if value can be used at the end of a Rest URL path"""
+    if value is None:
+        return False
+    value = unicode(value)
+    # the check for ?, /, & are to prevent problems when running
+    # behind Apache mod_proxy
+    if value == u'' or u'?' in value or u'/' in value or u'&' in value:
+        return False
+    return True
+
+
 
 class Entity(AppObject):
     """an entity instance has e_schema automagically set on
@@ -502,7 +514,7 @@
         path = etype.lower()
         if mainattr != 'eid':
             value = getattr(self, mainattr)
-            if value is None or unicode(value) == u'':
+            if not can_use_rest_path(value):
                 mainattr = 'eid'
                 path += '/eid'
             elif needcheck:
--- a/test/unittest_entity.py	Wed Aug 11 16:17:05 2010 +0200
+++ b/test/unittest_entity.py	Wed Aug 11 15:54:44 2010 +0200
@@ -492,8 +492,14 @@
         # unique attr with None value (wikiid in this case)
         card1 = req.create_entity('Card', title=u'hop')
         self.assertEquals(card1.rest_path(), 'card/eid/%s' % card1.eid)
-        card2 = req.create_entity('Card', title=u'pod', wikiid=u'zob/i')
-        self.assertEquals(card2.rest_path(), 'card/zob%2Fi')
+        # don't use rest if we have /, ? or & in the path (breaks mod_proxy)
+        card2 = req.create_entity('Card', title=u'pod', wikiid=u'zo/bi')
+        self.assertEquals(card2.rest_path(), 'card/eid/%d' % card2.eid)
+        card3 = req.create_entity('Card', title=u'pod', wikiid=u'zo&bi')
+        self.assertEquals(card3.rest_path(), 'card/eid/%d' % card3.eid)
+        card4 = req.create_entity('Card', title=u'pod', wikiid=u'zo?bi')
+        self.assertEquals(card4.rest_path(), 'card/eid/%d' % card4.eid)
+        
 
     def test_set_attributes(self):
         req = self.request()