Skip to content

Commit db9de68

Browse files
committed
gh-135056: Rename custom headers to extra_response_headers.
1 parent 8d1286a commit db9de68

4 files changed

Lines changed: 26 additions & 27 deletions

File tree

Doc/library/http.server.rst

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,7 @@ instantiation, of which this module provides three different variants:
362362
delays, it now always returns the IP address.
363363

364364

365-
.. class:: SimpleHTTPRequestHandler(request, client_address, server, directory=None, response_headers=None)
365+
.. class:: SimpleHTTPRequestHandler(request, client_address, server, directory=None, extra_response_headers=None)
366366

367367
This class serves files from the directory *directory* and below,
368368
or the current directory if *directory* is not provided, directly
@@ -399,9 +399,9 @@ instantiation, of which this module provides three different variants:
399399
This dictionary is no longer filled with the default system mappings,
400400
but only contains overrides.
401401

402-
.. attribute:: response_headers
402+
.. attribute:: extra_response_headers
403403

404-
A sequence of ``(name, value)`` pairs containing user specified custom
404+
A sequence of ``(name, value)`` pairs containing user specified extra
405405
HTTP response headers to add to each successful HTTP status 200 response.
406406
All other status code responses will not include these headers.
407407

@@ -438,8 +438,8 @@ instantiation, of which this module provides three different variants:
438438
followed by a ``'Content-Length:'`` header with the file's size and a
439439
``'Last-Modified:'`` header with the file's modification time.
440440

441-
The instance attribute ``response_headers`` is a sequence of
442-
``(name, value)`` pairs containing user specified custom response headers.
441+
The instance attribute ``extra_response_headers`` is a sequence of
442+
``(name, value)`` pairs containing user specified extra response headers.
443443

444444
Then follows a blank line signifying the end of the headers, and then the
445445
contents of the file are output.
@@ -558,7 +558,7 @@ The following options are accepted:
558558

559559
.. option:: -H, --header <header> <value>
560560

561-
Specify an additional custom HTTP Response Header to send on successful HTTP
561+
Specify an additional extra HTTP Response Header to send on successful HTTP
562562
200 responses. Can be used multiple times to send additional custom response
563563
headers.
564564

Doc/whatsnew/3.15.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ difflib
112112
http.server
113113
-----------
114114

115-
* Added a new ``response_headers`` keyword argument to
115+
* Added a new ``extra_response_headers`` keyword argument to
116116
:class:`SimpleHTTPRequestHandler` to support custom headers in HTTP responses.
117117
(Contributed by Anton I. Sipos in :gh:`135057`.)
118118

Lib/http/server.py

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -692,11 +692,11 @@ class SimpleHTTPRequestHandler(BaseHTTPRequestHandler):
692692
'.xz': 'application/x-xz',
693693
}
694694

695-
def __init__(self, *args, directory=None, response_headers=None, **kwargs):
695+
def __init__(self, *args, directory=None, extra_response_headers=None, **kwargs):
696696
if directory is None:
697697
directory = os.getcwd()
698698
self.directory = os.fspath(directory)
699-
self.response_headers = response_headers
699+
self.extra_response_headers = extra_response_headers
700700
super().__init__(*args, **kwargs)
701701

702702
def do_GET(self):
@@ -714,11 +714,10 @@ def do_HEAD(self):
714714
if f:
715715
f.close()
716716

717-
def send_custom_response_headers(self):
718-
"""Send the headers stored in self.response_headers"""
719-
# User specified response_headers
720-
if self.response_headers is not None:
721-
for header, value in self.response_headers:
717+
def _send_extra_response_headers(self):
718+
"""Send the headers stored in self.extra_response_headers"""
719+
if self.extra_response_headers is not None:
720+
for header, value in self.extra_response_headers:
722721
self.send_header(header, value)
723722

724723
def send_head(self):
@@ -803,7 +802,7 @@ def send_head(self):
803802
self.send_header("Content-Length", str(fs[6]))
804803
self.send_header("Last-Modified",
805804
self.date_time_string(fs.st_mtime))
806-
self.send_custom_response_headers()
805+
self._send_extra_response_headers()
807806
self.end_headers()
808807
return f
809808
except:
@@ -868,7 +867,7 @@ def list_directory(self, path):
868867
self.send_response(HTTPStatus.OK)
869868
self.send_header("Content-type", "text/html; charset=%s" % enc)
870869
self.send_header("Content-Length", str(len(encoded)))
871-
self.send_custom_response_headers()
870+
self._send_extra_response_headers()
872871
self.end_headers()
873872
return f
874873

@@ -1076,7 +1075,7 @@ def server_bind(self):
10761075
def finish_request(self, request, client_address):
10771076
self.RequestHandlerClass(request, client_address, self,
10781077
directory=args.directory,
1079-
response_headers=args.header)
1078+
extra_response_headers=args.header)
10801079

10811080
class HTTPDualStackServer(DualStackServerMixin, ThreadingHTTPServer):
10821081
pass

Lib/test/test_httpservers.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -466,9 +466,9 @@ def test_err(self):
466466

467467

468468
class CustomHeaderSimpleHTTPRequestHandler(SimpleHTTPRequestHandler):
469-
custom_headers = None
470-
def __init__(self, *args, directory=None, response_headers=None, **kwargs):
471-
super().__init__(*args, response_headers=self.custom_headers, **kwargs)
469+
extra_response_headers = None
470+
def __init__(self, *args, directory=None, extra_response_headers=None, **kwargs):
471+
super().__init__(*args, extra_response_headers=self.extra_response_headers, **kwargs)
472472

473473

474474
class SimpleHTTPServerTestCase(BaseTestCase):
@@ -829,17 +829,17 @@ def test_path_without_leading_slash(self):
829829
self.assertEqual(response.getheader("Location"),
830830
self.tempdir_name + "/?hi=1")
831831

832-
def test_custom_headers_list_dir(self):
833-
with mock.patch.object(self.request_handler, 'custom_headers', new=[
832+
def test_extra_headers_list_dir(self):
833+
with mock.patch.object(self.request_handler, 'extra_response_headers', new=[
834834
('X-Test1', 'test1'),
835835
('X-Test2', 'test2'),
836836
]):
837837
response = self.request(self.base_url + '/')
838838
self.assertEqual(response.getheader("X-Test1"), 'test1')
839839
self.assertEqual(response.getheader("X-Test2"), 'test2')
840840

841-
def test_custom_headers_get_file(self):
842-
with mock.patch.object(self.request_handler, 'custom_headers', new=[
841+
def test_extra_response_headers_get_file(self):
842+
with mock.patch.object(self.request_handler, 'extra_response_headers', new=[
843843
('Set-Cookie', 'test1=value1'),
844844
('Set-Cookie', 'test2=value2'),
845845
('X-Test1', 'value3'),
@@ -1491,7 +1491,7 @@ def test_unknown_flag(self, _):
14911491

14921492
@mock.patch('http.server._make_server', wraps=server._make_server)
14931493
@mock.patch.object(HTTPServer, 'serve_forever')
1494-
def test_response_headers_arg(self, _, mock_make_server):
1494+
def test_extra_response_headers_arg(self, _, mock_make_server):
14951495
server._main(
14961496
['-H', 'Set-Cookie', 'k=v', '-H', 'Set-Cookie', 'k2=v2', '8080']
14971497
)
@@ -1508,11 +1508,11 @@ def test_response_headers_arg(self, _, mock_make_server):
15081508
) as mock_handler_init:
15091509
mock_handler_init.return_value = None
15101510
# finish_request instantiates a request handler class,
1511-
# ensure response_headers are passed to it
1511+
# ensure extra_response_headers are passed to it
15121512
httpd.finish_request(mock.Mock(), '127.0.0.1')
15131513
mock_handler_init.assert_called_once_with(
15141514
mock.ANY, mock.ANY, mock.ANY, directory=mock.ANY,
1515-
response_headers=[
1515+
extra_response_headers=[
15161516
['Set-Cookie', 'k=v'], ['Set-Cookie', 'k2=v2']
15171517
]
15181518
)

0 commit comments

Comments
 (0)