Ticket #2625: 2625.2.patch

File 2625.2.patch, 2.6 KB (added by truekonrads, 7 years ago)

Improved version that addresses exarkun's comments

  • twisted/python/urlpath.py

     
    5454                        '/'.join(newpathsegs),
    5555                        query)
    5656
    57     def sibling(self, path, keepQuery=0):
     57    def sibling(self, path, keepQuery=False):
    5858        l = self.pathList()
    5959        l[-1] = path
    6060        return self._pathMod(l, keepQuery)
    6161
    62     def child(self, path, keepQuery=0):
     62    def child(self, path, keepQuery=False):
     63        """
     64        Add a child segment to URL path part.
     65       
     66        @param path: path segment to add. The path argument should not be quoted.
     67        @type path: C{str}
     68        @param keepQuery: if set to True, will keep the query part of URL,
     69                          else (and by default) will strip query part.
     70        @type keepQuery: C{bool}
     71        @return: A new L{URLPath} object with path appended to current object
     72        @rtype: L{URLPath}
     73        """
     74        #Clean up path with urlescaped version
     75        fixedPath = urllib.quote(path,'')
    6376        l = self.pathList()
    6477        if l[-1] == '':
    65             l[-1] = path
     78            l[-1] = fixedPath
    6679        else:
    67             l.append(path)
     80            l.append(fixedPath)
    6881        return self._pathMod(l, keepQuery)
    6982
    70     def parent(self, keepQuery=0):
     83    def parent(self, keepQuery=False):
    7184        l = self.pathList()
    7285        if l[-1] == '':
    7386            del l[-2]
     
    7891            l[-1] = ''
    7992        return self._pathMod(l, keepQuery)
    8093
    81     def here(self, keepQuery=0):
     94    def here(self, keepQuery=False):
    8295        l = self.pathList()
    8396        if l[-1] != '':
    8497            l[-1] = ''
  • twisted/test/test_paths.py

     
    831831        # here should be equivalent to '.'
    832832        self.assertEquals(str(self.path.here()), 'http://example.com/foo/')
    833833        self.assertEquals(str(self.path.child('').here()), 'http://example.com/foo/bar/')
     834       
     835    def test_childQuotes(self):
     836        """
     837        This test checks if child path with spaces and slashes is handled in least surprising way:
     838        properly escaped and result in single segment path amendment.
     839        """
     840        #child() with quotes (",') and slashes  should be properly escaped
     841        self.assertEquals(str(self.path.child("hello/ there")), 'http://example.com/foo/bar/hello%2F%20there')
     842
    834843