-
-
Notifications
You must be signed in to change notification settings - Fork 34.5k
gh-135056: Add a --header CLI argument to http.server #135057
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 50 commits
0d02fbe
1838da7
a3256fd
77b5fff
5f89c97
a3243fe
b1026d2
6f88c13
7a793f2
9450b86
5a30d91
d317cc2
5f1fb94
c376a71
89a89f0
9653710
f3ae904
44efbed
d47c5a7
8d1286a
db9de68
e149708
c16f4c9
777b5b6
eac5c6a
c9c8083
c2d6bb3
3377cf7
06a9977
fae21f9
be78515
53965ff
c280ed8
64122df
f0d1bac
e99780e
2e829bb
8baa875
7856d27
303ab5b
ed0b0b3
79c577b
526e499
46c1c91
c1fee3b
3a4fed6
9f0ed01
1036a91
49ffc92
511d902
44205e0
280cea3
d188977
9f7ac47
9851622
c1ab0ce
69e59e9
c40f8b1
597b973
347b968
a249eea
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -466,6 +466,7 @@ def handle_one_request(self): | |
| def handle(self): | ||
| """Handle multiple requests if necessary.""" | ||
| self.close_connection = True | ||
| self._default_response_headers = [] | ||
|
|
||
| self.handle_one_request() | ||
| while not self.close_connection: | ||
|
|
@@ -551,13 +552,15 @@ def send_response_only(self, code, message=None): | |
| (self.protocol_version, code, message)).encode( | ||
| 'latin-1', 'strict')) | ||
|
|
||
| def send_header(self, keyword, value): | ||
| def send_header(self, keyword, value, _is_extra=False): | ||
|
picnixz marked this conversation as resolved.
Outdated
|
||
| """Send a MIME header to the headers buffer.""" | ||
| if self.request_version != 'HTTP/0.9': | ||
| if not hasattr(self, '_headers_buffer'): | ||
| self._headers_buffer = [] | ||
| self._headers_buffer.append( | ||
| ("%s: %s\r\n" % (keyword, value)).encode('latin-1', 'strict')) | ||
| if not _is_extra and hasattr(self, '_default_response_headers'): | ||
| self._default_response_headers.append((keyword, value)) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Did we consider using
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I actually didn't look at the pattern used for |
||
|
|
||
| if keyword.lower() == 'connection': | ||
| if value.lower() == 'close': | ||
|
|
@@ -735,10 +738,11 @@ class SimpleHTTPRequestHandler(BaseHTTPRequestHandler): | |
| '.xz': 'application/x-xz', | ||
| } | ||
|
|
||
| def __init__(self, *args, directory=None, **kwargs): | ||
| def __init__(self, *args, directory=None, extra_response_headers=None, **kwargs): | ||
|
picnixz marked this conversation as resolved.
|
||
| if directory is None: | ||
| directory = os.getcwd() | ||
| self.directory = os.fspath(directory) | ||
| self.extra_response_headers = extra_response_headers | ||
| super().__init__(*args, **kwargs) | ||
|
|
||
| def do_GET(self): | ||
|
|
@@ -756,6 +760,16 @@ def do_HEAD(self): | |
| if f: | ||
| f.close() | ||
|
|
||
| def _send_extra_response_headers(self): | ||
| """Send the headers stored in self.extra_response_headers.""" | ||
| if self.extra_response_headers is not None: | ||
| default_headers = {h.lower() for h, _ in self._default_response_headers} | ||
| for header, value in self.extra_response_headers: | ||
| # Don't send the header if it's already sent | ||
| # as part of the default response headers | ||
| if header not in default_headers: | ||
|
picnixz marked this conversation as resolved.
Outdated
|
||
| self.send_header(header, value, _is_extra=True) | ||
|
|
||
| def send_head(self): | ||
| """Common code for GET and HEAD commands. | ||
|
|
||
|
|
@@ -838,6 +852,7 @@ def send_head(self): | |
| self.send_header("Content-Length", str(fs[6])) | ||
| self.send_header("Last-Modified", | ||
| self.date_time_string(fs.st_mtime)) | ||
| self._send_extra_response_headers() | ||
| self.end_headers() | ||
| return f | ||
| except: | ||
|
|
@@ -902,6 +917,7 @@ def list_directory(self, path): | |
| self.send_response(HTTPStatus.OK) | ||
| self.send_header("Content-type", "text/html; charset=%s" % enc) | ||
| self.send_header("Content-Length", str(len(encoded))) | ||
| self._send_extra_response_headers() | ||
| self.end_headers() | ||
| return f | ||
|
|
||
|
|
@@ -1010,25 +1026,33 @@ def _get_best_family(*address): | |
| return family, sockaddr | ||
|
|
||
|
|
||
| def _make_server(HandlerClass=BaseHTTPRequestHandler, | ||
|
picnixz marked this conversation as resolved.
|
||
| ServerClass=ThreadingHTTPServer, | ||
| protocol="HTTP/1.0", port=8000, bind=None, | ||
| tls_cert=None, tls_key=None, tls_password=None): | ||
| ServerClass.address_family, addr = _get_best_family(bind, port) | ||
| HandlerClass.protocol_version = protocol | ||
|
|
||
| if tls_cert: | ||
| return ServerClass(addr, HandlerClass, certfile=tls_cert, | ||
| keyfile=tls_key, password=tls_password) | ||
| else: | ||
| return ServerClass(addr, HandlerClass) | ||
|
|
||
|
|
||
| def test(HandlerClass=BaseHTTPRequestHandler, | ||
| ServerClass=ThreadingHTTPServer, | ||
| protocol="HTTP/1.0", port=8000, bind=None, | ||
| tls_cert=None, tls_key=None, tls_password=None): | ||
| """Test the HTTP request handler class. | ||
|
|
||
| This runs an HTTP server on port 8000 (or the port argument). | ||
|
|
||
| """ | ||
| ServerClass.address_family, addr = _get_best_family(bind, port) | ||
| HandlerClass.protocol_version = protocol | ||
|
|
||
| if tls_cert: | ||
| server = ServerClass(addr, HandlerClass, certfile=tls_cert, | ||
| keyfile=tls_key, password=tls_password) | ||
| else: | ||
| server = ServerClass(addr, HandlerClass) | ||
|
|
||
| with server as httpd: | ||
| with _make_server( | ||
| HandlerClass=HandlerClass, ServerClass=ServerClass, | ||
| protocol=protocol, port=port, bind=bind, | ||
| tls_cert=tls_cert, tls_key=tls_key, tls_password=tls_password | ||
| ) as httpd: | ||
| host, port = httpd.socket.getsockname()[:2] | ||
| url_host = f'[{host}]' if ':' in host else host | ||
| protocol = 'HTTPS' if tls_cert else 'HTTP' | ||
|
|
@@ -1069,6 +1093,10 @@ def _main(args=None): | |
| parser.add_argument('port', default=8000, type=int, nargs='?', | ||
| help='bind to this port ' | ||
| '(default: %(default)s)') | ||
| parser.add_argument('-H', '--header', nargs=2, action='append', | ||
| metavar=('HEADER', 'VALUE'), | ||
| help='Add a custom response header ' | ||
| '(can be specified multiple times)') | ||
| args = parser.parse_args(args) | ||
|
|
||
| if not args.tls_cert and args.tls_key: | ||
|
|
@@ -1097,7 +1125,8 @@ def server_bind(self): | |
|
|
||
| def finish_request(self, request, client_address): | ||
| self.RequestHandlerClass(request, client_address, self, | ||
| directory=args.directory) | ||
| directory=args.directory, | ||
| extra_response_headers=args.header) | ||
|
|
||
| class HTTPDualStackServer(DualStackServerMixin, ThreadingHTTPServer): | ||
| pass | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| Add a ``-H`` or ``--header`` CLI option to :program:`python -m http.server`. Contributed by | ||
| Anton I. Sipos. |
Uh oh!
There was an error while loading. Please reload this page.