]> granicus.if.org Git - python/commitdiff
Fix http.server's request handling case on trailing '/'.
authorSenthil Kumaran <senthil@uthcode.com>
Fri, 13 Sep 2013 07:21:18 +0000 (00:21 -0700)
committerSenthil Kumaran <senthil@uthcode.com>
Fri, 13 Sep 2013 07:21:18 +0000 (00:21 -0700)
Patch contributed by Vajrasky Kok. Addresses Issue #17324

Lib/http/server.py
Lib/test/test_httpservers.py
Misc/NEWS

index c4ac703d2daf0f6cc4ef1b945219ed7c614492d6..195f9ee36b1463fae15347980472c8ea6a8e0042 100644 (file)
@@ -780,6 +780,8 @@ class SimpleHTTPRequestHandler(BaseHTTPRequestHandler):
         # 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 @@ class SimpleHTTPRequestHandler(BaseHTTPRequestHandler):
             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):
index 03c0776ce565891848090a85ace1b5dd06a2fe86..b8bbcb673485b41523b0bd75cd9b2510c895ad94 100644 (file)
@@ -249,6 +249,9 @@ class SimpleHTTPServerTestCase(BaseTestCase):
         #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)
index 3d9f24d71848c9199b94efd909463c38c9b989de..1655238181d6a31487a43e139c299beb0746240c 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -68,6 +68,10 @@ Core and Builtins
 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.