[Python-checkins] cpython (3.3): Fix http.server's request handling case on trailing '/'.

senthil.kumaran python-checkins at python.org
Fri Sep 13 09:22:55 CEST 2013


http://hg.python.org/cpython/rev/1fcccbbe15e2
changeset:   85671:1fcccbbe15e2
branch:      3.3
parent:      85667:7aaba721ebc0
user:        Senthil Kumaran <senthil at uthcode.com>
date:        Fri Sep 13 00:21:18 2013 -0700
summary:
  Fix http.server's request handling case on trailing '/'.

Patch contributed by Vajrasky Kok. Addresses Issue #17324

files:
  Lib/http/server.py           |  4 ++++
  Lib/test/test_httpservers.py |  3 +++
  Misc/NEWS                    |  4 ++++
  3 files changed, 11 insertions(+), 0 deletions(-)


diff --git a/Lib/http/server.py b/Lib/http/server.py
--- a/Lib/http/server.py
+++ b/Lib/http/server.py
@@ -780,6 +780,8 @@
         # abandon query parameters
         path = path.split('?',1)[0]
         path = path.split('#',1)[0]
+        # Don't forget explicit trailing slash when normalizing. Issue17324
+        trailing_slash = True if path.rstrip().endswith('/') else False
         path = posixpath.normpath(urllib.parse.unquote(path))
         words = path.split('/')
         words = filter(None, words)
@@ -789,6 +791,8 @@
             head, word = os.path.split(word)
             if word in (os.curdir, os.pardir): continue
             path = os.path.join(path, word)
+        if trailing_slash:
+            path += '/'
         return path
 
     def copyfile(self, source, outputfile):
diff --git a/Lib/test/test_httpservers.py b/Lib/test/test_httpservers.py
--- a/Lib/test/test_httpservers.py
+++ b/Lib/test/test_httpservers.py
@@ -249,6 +249,9 @@
         #constructs the path relative to the root directory of the HTTPServer
         response = self.request(self.tempdir_name + '/test')
         self.check_status_and_reason(response, 200, data=self.data)
+        # check for trailing "/" which should return 404. See Issue17324
+        response = self.request(self.tempdir_name + '/test/')
+        self.check_status_and_reason(response, 404)
         response = self.request(self.tempdir_name + '/')
         self.check_status_and_reason(response, 200)
         response = self.request(self.tempdir_name)
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -68,6 +68,10 @@
 Library
 -------
 
+
+- Issue #17324: Fix http.server's request handling case on trailing '/'. Patch
+  contributed by Vajrasky Kok.
+
 - Issue #18784: The uuid module no more attempts to load libc via ctypes.CDLL,
   if all necessary functions are already found in libuuid.
   Patch by Evgeny Sologubov.

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list