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

Lib/SimpleHTTPServer.py
Lib/test/test_httpservers.py
Misc/NEWS

index 3e0334da3b3fb312fe18e00a483ea216095b151d..1996f4d29ba52f49ad870a43116bfd4874152449 100644 (file)
@@ -149,6 +149,8 @@ class SimpleHTTPRequestHandler(BaseHTTPServer.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.unquote(path))
         words = path.split('/')
         words = filter(None, words)
@@ -158,6 +160,8 @@ class SimpleHTTPRequestHandler(BaseHTTPServer.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 5dcedc0fe8bdbf5e0711c975378af6b5312b6386..3f1436004d53266e8b93314978fa2dde7f573512 100644 (file)
@@ -313,6 +313,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 ea74d71bd9a0e7878c70e8be0dcebd284954b377..e025df630d6f3582c66fe0d93a05641f2f23ec09 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -32,6 +32,9 @@ 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.