summaryrefslogtreecommitdiff
path: root/Lib/http
diff options
context:
space:
mode:
authorStéphane Wirtel <stephane@wirtel.be>2017-05-24 09:29:06 +0200
committerVictor Stinner <victor.stinner@gmail.com>2017-05-24 00:29:06 -0700
commita17a2f52c4c3b37414da95a152fc8669978c7c83 (patch)
treeabf6327c00ac9ae647474b4b63a704a396ba5a58 /Lib/http
parent07244a83014fad42da937c17d98474b47a570bf7 (diff)
downloadcpython-git-a17a2f52c4c3b37414da95a152fc8669978c7c83.tar.gz
bpo-28707: Add the directory parameter to http.server.SimpleHTTPRequestHandler and http.server module (#1776)
* bpo-28707: call the constructor of SimpleHTTPRequestHandler in the test with a mock object * bpo-28707: Add the directory parameter to http.server.SimpleHTTPRequestHandler and http.server module
Diffstat (limited to 'Lib/http')
-rw-r--r--Lib/http/server.py15
1 files changed, 13 insertions, 2 deletions
diff --git a/Lib/http/server.py b/Lib/http/server.py
index 7b3e701fb7..b1151a2b65 100644
--- a/Lib/http/server.py
+++ b/Lib/http/server.py
@@ -103,6 +103,7 @@ import socketserver
import sys
import time
import urllib.parse
+from functools import partial
from http import HTTPStatus
@@ -634,6 +635,12 @@ class SimpleHTTPRequestHandler(BaseHTTPRequestHandler):
server_version = "SimpleHTTP/" + __version__
+ def __init__(self, *args, directory=None, **kwargs):
+ if directory is None:
+ directory = os.getcwd()
+ self.directory = directory
+ super().__init__(*args, **kwargs)
+
def do_GET(self):
"""Serve a GET request."""
f = self.send_head()
@@ -806,7 +813,7 @@ class SimpleHTTPRequestHandler(BaseHTTPRequestHandler):
path = posixpath.normpath(path)
words = path.split('/')
words = filter(None, words)
- path = os.getcwd()
+ path = self.directory
for word in words:
if os.path.dirname(word) or word in (os.curdir, os.pardir):
# Ignore components that are not a simple file/directory name
@@ -1234,6 +1241,9 @@ if __name__ == '__main__':
parser.add_argument('--bind', '-b', default='', metavar='ADDRESS',
help='Specify alternate bind address '
'[default: all interfaces]')
+ parser.add_argument('--directory', '-d', default=os.getcwd(),
+ help='Specify alternative directory '
+ '[default:current directory]')
parser.add_argument('port', action='store',
default=8000, type=int,
nargs='?',
@@ -1242,5 +1252,6 @@ if __name__ == '__main__':
if args.cgi:
handler_class = CGIHTTPRequestHandler
else:
- handler_class = SimpleHTTPRequestHandler
+ handler_class = partial(SimpleHTTPRequestHandler,
+ directory=args.directory)
test(HandlerClass=handler_class, port=args.port, bind=args.bind)