From 7848730b46bcd6802ec5cc7310732065eeb9688c Mon Sep 17 00:00:00 2001 From: John Comeau Date: Mon, 25 Dec 2023 14:57:32 +0000 Subject: [PATCH 01/20] changeable default --- Lib/http/server.py | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/Lib/http/server.py b/Lib/http/server.py index ee7a9b6aa55b88..16a0e8ab6d8fb1 100644 --- a/Lib/http/server.py +++ b/Lib/http/server.py @@ -263,6 +263,7 @@ class BaseHTTPRequestHandler(socketserver.StreamRequestHandler): # the client gets back when sending a malformed request line. # Most web servers default to HTTP 0.9, i.e. don't send a status line. default_request_version = "HTTP/0.9" + default_content_type = "application/octet-stream" def parse_request(self): """Parse a request (internal). @@ -718,7 +719,7 @@ def send_head(self): break else: return self.list_directory(path) - ctype = self.guess_type(path) + ctype = self.guess_type(path, default_type) # check for trailing "/" which should return 404. See Issue17324 # The test for this was added in test_httpserver.py # However, some OS platforms accept a trailingSlash as a filename @@ -877,7 +878,7 @@ def copyfile(self, source, outputfile): """ shutil.copyfileobj(source, outputfile) - def guess_type(self, path): + def guess_type(self, path, default=self.default_content_type): """Guess the type of a file. Argument is a PATH (a filename). @@ -892,15 +893,10 @@ def guess_type(self, path): """ base, ext = posixpath.splitext(path) - if ext in self.extensions_map: - return self.extensions_map[ext] - ext = ext.lower() - if ext in self.extensions_map: - return self.extensions_map[ext] - guess, _ = mimetypes.guess_type(path) - if guess: - return guess - return 'application/octet-stream' + guess = self.extensions_map.get(ext, + self.extensions_map.get(ext.lower(), + mimetypes.guess_type(path))) + return guess or default # Utilities for CGIHTTPRequestHandler From 19cde5481ba3d86a772e5e44b7cbd5c82d39b498 Mon Sep 17 00:00:00 2001 From: John Comeau Date: Mon, 25 Dec 2023 16:15:28 +0000 Subject: [PATCH 02/20] ready to test --- Lib/http/server.py | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/Lib/http/server.py b/Lib/http/server.py index 16a0e8ab6d8fb1..46017a4652de99 100644 --- a/Lib/http/server.py +++ b/Lib/http/server.py @@ -719,7 +719,7 @@ def send_head(self): break else: return self.list_directory(path) - ctype = self.guess_type(path, default_type) + ctype = self.guess_type(path) # check for trailing "/" which should return 404. See Issue17324 # The test for this was added in test_httpserver.py # However, some OS platforms accept a trailingSlash as a filename @@ -878,7 +878,7 @@ def copyfile(self, source, outputfile): """ shutil.copyfileobj(source, outputfile) - def guess_type(self, path, default=self.default_content_type): + def guess_type(self, path): """Guess the type of a file. Argument is a PATH (a filename). @@ -896,7 +896,7 @@ def guess_type(self, path, default=self.default_content_type): guess = self.extensions_map.get(ext, self.extensions_map.get(ext.lower(), mimetypes.guess_type(path))) - return guess or default + return guess or self.default_content_type # Utilities for CGIHTTPRequestHandler @@ -1252,7 +1252,8 @@ def _get_best_family(*address): def test(HandlerClass=BaseHTTPRequestHandler, ServerClass=ThreadingHTTPServer, - protocol="HTTP/1.0", port=8000, bind=None): + protocol="HTTP/1.0", port=8000, bind=None, + content_type=BaseHTTPRequestHandler.default_content_type): """Test the HTTP request handler class. This runs an HTTP server on port 8000 (or the port argument). @@ -1260,6 +1261,7 @@ def test(HandlerClass=BaseHTTPRequestHandler, """ ServerClass.address_family, addr = _get_best_family(bind, port) HandlerClass.protocol_version = protocol + HandlerClass.default_content_type = content_type with ServerClass(addr, HandlerClass) as httpd: host, port = httpd.socket.getsockname()[:2] url_host = f'[{host}]' if ':' in host else host @@ -1290,6 +1292,9 @@ def test(HandlerClass=BaseHTTPRequestHandler, default='HTTP/1.0', help='conform to this HTTP version ' '(default: %(default)s)') + parser.add_argument('-c', '--content-type', # parsed into content_type + default='application/octet-stream', + help='sets default content type for unknown extensions') parser.add_argument('port', default=8000, type=int, nargs='?', help='bind to this port ' '(default: %(default)s)') @@ -1319,4 +1324,5 @@ def finish_request(self, request, client_address): port=args.port, bind=args.bind, protocol=args.protocol, + content_type=args.content_type ) From e7860c32590586bea5e7d7b3eb7e204bb4b8686b Mon Sep 17 00:00:00 2001 From: John Comeau Date: Mon, 25 Dec 2023 16:36:47 +0000 Subject: [PATCH 03/20] passed test this time --- Lib/http/server.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/http/server.py b/Lib/http/server.py index 46017a4652de99..82e69954af73fd 100644 --- a/Lib/http/server.py +++ b/Lib/http/server.py @@ -895,7 +895,7 @@ def guess_type(self, path): base, ext = posixpath.splitext(path) guess = self.extensions_map.get(ext, self.extensions_map.get(ext.lower(), - mimetypes.guess_type(path))) + mimetypes.guess_type(path)[0])) return guess or self.default_content_type From b945ceded69e38d42cd6e5d90dd195ca9e0c802f Mon Sep 17 00:00:00 2001 From: John Comeau Date: Mon, 25 Dec 2023 17:14:25 +0000 Subject: [PATCH 04/20] DRY fix for #113471 --- Lib/http/server.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/http/server.py b/Lib/http/server.py index 82e69954af73fd..2dbe1619498fc7 100644 --- a/Lib/http/server.py +++ b/Lib/http/server.py @@ -1293,7 +1293,7 @@ def test(HandlerClass=BaseHTTPRequestHandler, help='conform to this HTTP version ' '(default: %(default)s)') parser.add_argument('-c', '--content-type', # parsed into content_type - default='application/octet-stream', + default=BaseHTTPRequestHandler.default_content_type, help='sets default content type for unknown extensions') parser.add_argument('port', default=8000, type=int, nargs='?', help='bind to this port ' From c2da505bd55e07f5a6923baa11afb795b024d696 Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Mon, 25 Dec 2023 19:14:08 +0000 Subject: [PATCH 05/20] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20b?= =?UTF-8?q?lurb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../next/Library/2023-12-25-19-14-07.gh-issue-113471.ZQMpbI.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Library/2023-12-25-19-14-07.gh-issue-113471.ZQMpbI.rst diff --git a/Misc/NEWS.d/next/Library/2023-12-25-19-14-07.gh-issue-113471.ZQMpbI.rst b/Misc/NEWS.d/next/Library/2023-12-25-19-14-07.gh-issue-113471.ZQMpbI.rst new file mode 100644 index 00000000000000..25d82240cfae30 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2023-12-25-19-14-07.gh-issue-113471.ZQMpbI.rst @@ -0,0 +1 @@ +Adds ability to set default content-type for no extension/unrecognized extension on http.server module. From 4a6370b3c94e379701afae2a254a821867d4e909 Mon Sep 17 00:00:00 2001 From: John Comeau Date: Sat, 27 Jan 2024 04:40:01 -0800 Subject: [PATCH 06/20] Update Misc/NEWS.d/next/Library/2023-12-25-19-14-07.gh-issue-113471.ZQMpbI.rst Co-authored-by: Serhiy Storchaka --- .../next/Library/2023-12-25-19-14-07.gh-issue-113471.ZQMpbI.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Library/2023-12-25-19-14-07.gh-issue-113471.ZQMpbI.rst b/Misc/NEWS.d/next/Library/2023-12-25-19-14-07.gh-issue-113471.ZQMpbI.rst index 25d82240cfae30..38f3396b94d0f4 100644 --- a/Misc/NEWS.d/next/Library/2023-12-25-19-14-07.gh-issue-113471.ZQMpbI.rst +++ b/Misc/NEWS.d/next/Library/2023-12-25-19-14-07.gh-issue-113471.ZQMpbI.rst @@ -1 +1 @@ -Adds ability to set default content-type for no extension/unrecognized extension on http.server module. +Adds ability to set default content-type for no extension/unrecognized extension on :mod:`http.server` module. From 7ae00eadfe3808794c5a5d55cbeb2b44829d2238 Mon Sep 17 00:00:00 2001 From: John Comeau Date: Sat, 27 Jan 2024 05:01:38 -0800 Subject: [PATCH 07/20] removed short option and inefficiency pointed out by @serhiy-storchaka --- Lib/http/server.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/Lib/http/server.py b/Lib/http/server.py index 2dbe1619498fc7..377c0c27fb5e1b 100644 --- a/Lib/http/server.py +++ b/Lib/http/server.py @@ -893,10 +893,10 @@ def guess_type(self, path): """ base, ext = posixpath.splitext(path) - guess = self.extensions_map.get(ext, - self.extensions_map.get(ext.lower(), - mimetypes.guess_type(path)[0])) - return guess or self.default_content_type + return self.extensions_map.get(ext) or \ + self.extensions_map.get(ext.lower()) or \ + mimetypes.guess_type(path)[0] or \ + self.default_content_type # Utilities for CGIHTTPRequestHandler @@ -1292,7 +1292,7 @@ def test(HandlerClass=BaseHTTPRequestHandler, default='HTTP/1.0', help='conform to this HTTP version ' '(default: %(default)s)') - parser.add_argument('-c', '--content-type', # parsed into content_type + parser.add_argument('--content-type', # parsed into content_type default=BaseHTTPRequestHandler.default_content_type, help='sets default content type for unknown extensions') parser.add_argument('port', default=8000, type=int, nargs='?', From 2ba2aeea781270aaa517110a7fb0bd8053e95f42 Mon Sep 17 00:00:00 2001 From: John Comeau Date: Mon, 29 Jan 2024 10:36:47 -0800 Subject: [PATCH 08/20] adding tests per @serhiy_storchaka's suggestion, not all pass yet --- Lib/http/server.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/Lib/http/server.py b/Lib/http/server.py index 377c0c27fb5e1b..5178254e7fb049 100644 --- a/Lib/http/server.py +++ b/Lib/http/server.py @@ -891,6 +891,26 @@ def guess_type(self, path): as a default; however it would be permissible (if slow) to look inside the data to make a better guess. + >>> testserver = type('', (), { + ... 'makefile': lambda mode, bufsize: open(os.devnull, mode) + ... }) + >>> testhandler = SimpleHTTPRequestHandler(testserver, None, None) + >>> testhandler.guess_type('/foo.bar.GZ') # tests ext.lower() + 'application/gzip' + >>> testhandler.default_content_type = 'nonesuch/nonesuch' + >>> testhandler.guess_type('/this/should/give/default') + 'nonesuch/nonesuch' + >>> # check short-circuiting works + >>> mimetypes = type('', (), { + ... 'guess_type': lambda x: (print('foo'), print('bar')) + ... }) + >>> testhandler.guess_type('/should/show/foo/bar/then/default') + foo + bar + 'nonesuch/nonesuch' + >>> testhandler.guess_type('/should/not/print/foo.Z') + 'application/octet-stream' + """ base, ext = posixpath.splitext(path) return self.extensions_map.get(ext) or \ From 819491fe1a2845ade57d7b5e095b7d2212250d88 Mon Sep 17 00:00:00 2001 From: John Comeau Date: Mon, 29 Jan 2024 11:07:19 -0800 Subject: [PATCH 09/20] tests all pass --- Lib/http/server.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Lib/http/server.py b/Lib/http/server.py index 5178254e7fb049..78590fd7797d5e 100644 --- a/Lib/http/server.py +++ b/Lib/http/server.py @@ -900,8 +900,8 @@ def guess_type(self, path): >>> testhandler.default_content_type = 'nonesuch/nonesuch' >>> testhandler.guess_type('/this/should/give/default') 'nonesuch/nonesuch' - >>> # check short-circuiting works - >>> mimetypes = type('', (), { + >>> # check short-circuiting works using mock mimetypes.guess_type + >>> sys.modules[__name__].mimetypes = type('', (), { ... 'guess_type': lambda x: (print('foo'), print('bar')) ... }) >>> testhandler.guess_type('/should/show/foo/bar/then/default') From 2defce38cc76601c03d0fed28faed03b6438ac3a Mon Sep 17 00:00:00 2001 From: John Comeau Date: Fri, 2 Feb 2024 18:21:24 -0800 Subject: [PATCH 10/20] addresses some of @encukou's observations. still need to add tests. --- Doc/library/http.server.rst | 17 +++++++++++++++++ Lib/http/server.py | 33 +++++++++------------------------ 2 files changed, 26 insertions(+), 24 deletions(-) diff --git a/Doc/library/http.server.rst b/Doc/library/http.server.rst index bc59d3d17912fd..38a3b563787604 100644 --- a/Doc/library/http.server.rst +++ b/Doc/library/http.server.rst @@ -167,6 +167,13 @@ provides three different variants: header (using :meth:`send_header`) in all of its responses to clients. For backwards compatibility, the setting defaults to ``'HTTP/1.0'``. + .. attribute:: default_content_type + + Specifies the content-type header value sent when the mime-type cannot + be guessed from the file extension of the requested URL. + For backwards compatibility, the setting defaults to + ``'application/octet-stream'``. + .. attribute:: MessageClass Specifies an :class:`email.message.Message`\ -like class to parse HTTP @@ -462,6 +469,16 @@ following command runs an HTTP/1.1 conformant server:: .. versionchanged:: 3.11 Added the ``--protocol`` option. +By default, the server uses the MIME type ``'application/octet-stream'`` for +the ``content-type`` header when the content type cannot be guessed from the +URL's extension (if any). The option ``--default-content-type`` overrides +this default with one of the user's choosing:: + + python -m http.server --default-content-type text/html + +.. versionchanged:: 3.13 + Added the ``--default-content-type`` option. + .. class:: CGIHTTPRequestHandler(request, client_address, server) This class is used to serve either files or output of CGI scripts from the diff --git a/Lib/http/server.py b/Lib/http/server.py index 78590fd7797d5e..5f8a4fe4030bca 100644 --- a/Lib/http/server.py +++ b/Lib/http/server.py @@ -891,32 +891,17 @@ def guess_type(self, path): as a default; however it would be permissible (if slow) to look inside the data to make a better guess. - >>> testserver = type('', (), { - ... 'makefile': lambda mode, bufsize: open(os.devnull, mode) - ... }) - >>> testhandler = SimpleHTTPRequestHandler(testserver, None, None) - >>> testhandler.guess_type('/foo.bar.GZ') # tests ext.lower() - 'application/gzip' - >>> testhandler.default_content_type = 'nonesuch/nonesuch' - >>> testhandler.guess_type('/this/should/give/default') - 'nonesuch/nonesuch' - >>> # check short-circuiting works using mock mimetypes.guess_type - >>> sys.modules[__name__].mimetypes = type('', (), { - ... 'guess_type': lambda x: (print('foo'), print('bar')) - ... }) - >>> testhandler.guess_type('/should/show/foo/bar/then/default') - foo - bar - 'nonesuch/nonesuch' - >>> testhandler.guess_type('/should/not/print/foo.Z') - 'application/octet-stream' - """ base, ext = posixpath.splitext(path) - return self.extensions_map.get(ext) or \ - self.extensions_map.get(ext.lower()) or \ - mimetypes.guess_type(path)[0] or \ - self.default_content_type + if ext in self.extensions_map: + return self.extensions_map[ext] + ext = ext.lower() + if ext in self.extensions_map: + return self.extensions_map[ext] + guess, _ = mimetypes.guess_type(path) + if guess: + return guess + return self.default_content_type # Utilities for CGIHTTPRequestHandler From 69800b96b602c1c5173937cec92ccf122de65784 Mon Sep 17 00:00:00 2001 From: John Comeau Date: Sat, 8 Mar 2025 23:44:54 -0700 Subject: [PATCH 11/20] donBarbos's suggestion --- Doc/library/http.server.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/library/http.server.rst b/Doc/library/http.server.rst index 37ac7069cab107..f8c5ba880cb165 100644 --- a/Doc/library/http.server.rst +++ b/Doc/library/http.server.rst @@ -476,7 +476,7 @@ this default with one of the user's choosing:: python -m http.server --default-content-type text/html -.. versionchanged:: 3.13 +.. versionchanged:: next Added the ``--default-content-type`` option. .. class:: CGIHTTPRequestHandler(request, client_address, server) From 4778206c1aa1cc68429d19df6bcc5b4dc67df513 Mon Sep 17 00:00:00 2001 From: John Comeau Date: Sun, 16 Mar 2025 04:57:15 -0700 Subject: [PATCH 12/20] Update Doc/library/http.server.rst MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com> --- Doc/library/http.server.rst | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Doc/library/http.server.rst b/Doc/library/http.server.rst index 9f630dea72115e..2593727b1ea04e 100644 --- a/Doc/library/http.server.rst +++ b/Doc/library/http.server.rst @@ -169,11 +169,11 @@ provides three different variants: .. attribute:: default_content_type - Specifies the content-type header value sent when the mime-type cannot - be guessed from the file extension of the requested URL. - For backwards compatibility, the setting defaults to - ``'application/octet-stream'``. + Specifies the content-type header value sent when the MIME type + cannot be guessed from the file extension of the requested URL. + By default, it is set to ``'application/octet-stream'``. + .. versionadded:: next .. attribute:: MessageClass Specifies an :class:`email.message.Message`\ -like class to parse HTTP From 8903147d77f7c57023126928da020eeea6539662 Mon Sep 17 00:00:00 2001 From: John Comeau Date: Sun, 16 Mar 2025 04:58:02 -0700 Subject: [PATCH 13/20] Update Misc/NEWS.d/next/Library/2023-12-25-19-14-07.gh-issue-113471.ZQMpbI.rst MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com> --- .../Library/2023-12-25-19-14-07.gh-issue-113471.ZQMpbI.rst | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Library/2023-12-25-19-14-07.gh-issue-113471.ZQMpbI.rst b/Misc/NEWS.d/next/Library/2023-12-25-19-14-07.gh-issue-113471.ZQMpbI.rst index 38f3396b94d0f4..99ba9bd1820fc1 100644 --- a/Misc/NEWS.d/next/Library/2023-12-25-19-14-07.gh-issue-113471.ZQMpbI.rst +++ b/Misc/NEWS.d/next/Library/2023-12-25-19-14-07.gh-issue-113471.ZQMpbI.rst @@ -1 +1,2 @@ -Adds ability to set default content-type for no extension/unrecognized extension on :mod:`http.server` module. +Allow :mod:`http.server` to set a default content-type when serving +files with an unknown or missing extension. From dfa30d9639387f5d97c8bc8f933f3bf94e6f0099 Mon Sep 17 00:00:00 2001 From: John Comeau Date: Sun, 16 Mar 2025 04:59:31 -0700 Subject: [PATCH 14/20] Update Lib/http/server.py MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com> --- Lib/http/server.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/http/server.py b/Lib/http/server.py index c75d0b76f5d1dd..ebb8ea1cffbde3 100644 --- a/Lib/http/server.py +++ b/Lib/http/server.py @@ -1304,7 +1304,7 @@ def test(HandlerClass=BaseHTTPRequestHandler, default='HTTP/1.0', help='conform to this HTTP version ' '(default: %(default)s)') - parser.add_argument('--content-type', # parsed into content_type + parser.add_argument('--content-type', default=BaseHTTPRequestHandler.default_content_type, help='sets default content type for unknown extensions') parser.add_argument('port', default=8000, type=int, nargs='?', From 3b8450d9197832ab89b21c4368c28f5e3470b9b5 Mon Sep 17 00:00:00 2001 From: John Comeau Date: Sun, 16 Mar 2025 04:59:58 -0700 Subject: [PATCH 15/20] Update Lib/http/server.py MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com> --- Lib/http/server.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Lib/http/server.py b/Lib/http/server.py index ebb8ea1cffbde3..feebc6e9dbea8e 100644 --- a/Lib/http/server.py +++ b/Lib/http/server.py @@ -1306,7 +1306,8 @@ def test(HandlerClass=BaseHTTPRequestHandler, '(default: %(default)s)') parser.add_argument('--content-type', default=BaseHTTPRequestHandler.default_content_type, - help='sets default content type for unknown extensions') + help='default content type for unknown extensions' + '(default: %(default)s)') parser.add_argument('port', default=8000, type=int, nargs='?', help='bind to this port ' '(default: %(default)s)') From 59f152a135049165bf1ef484192938c85474211e Mon Sep 17 00:00:00 2001 From: John Comeau Date: Sun, 16 Mar 2025 05:00:19 -0700 Subject: [PATCH 16/20] Update Doc/library/http.server.rst MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com> --- Doc/library/http.server.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/library/http.server.rst b/Doc/library/http.server.rst index 2593727b1ea04e..95208050d3f193 100644 --- a/Doc/library/http.server.rst +++ b/Doc/library/http.server.rst @@ -427,7 +427,7 @@ such as using different index file names by overriding the class attribute By default, the server uses the MIME type ``'application/octet-stream'`` for -the ``content-type`` header when the content type cannot be guessed from the +the ``Content-Type`` header when the content type cannot be guessed from the URL's extension (if any). The option ``--default-content-type`` overrides this default with one of the user's choosing:: From 259e9b53b0c357084979db3fb87c4bc4ad56e532 Mon Sep 17 00:00:00 2001 From: John Comeau Date: Sun, 16 Mar 2025 05:00:53 -0700 Subject: [PATCH 17/20] Update Lib/http/server.py MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com> --- Lib/http/server.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/http/server.py b/Lib/http/server.py index feebc6e9dbea8e..287712c42c2b48 100644 --- a/Lib/http/server.py +++ b/Lib/http/server.py @@ -1337,5 +1337,5 @@ def finish_request(self, request, client_address): port=args.port, bind=args.bind, protocol=args.protocol, - content_type=args.content_type + content_type=args.content_type, ) From 16626670e10ba19886f8a99d0e4a8f3e69755ca3 Mon Sep 17 00:00:00 2001 From: John Comeau Date: Sun, 16 Mar 2025 06:04:29 -0700 Subject: [PATCH 18/20] Update Doc/library/http.server.rst Co-authored-by: donBarbos --- Doc/library/http.server.rst | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/Doc/library/http.server.rst b/Doc/library/http.server.rst index 95208050d3f193..83d1d624840195 100644 --- a/Doc/library/http.server.rst +++ b/Doc/library/http.server.rst @@ -426,15 +426,15 @@ such as using different index file names by overriding the class attribute :attr:`index_pages`. -By default, the server uses the MIME type ``'application/octet-stream'`` for -the ``Content-Type`` header when the content type cannot be guessed from the -URL's extension (if any). The option ``--default-content-type`` overrides -this default with one of the user's choosing:: +.. option:: -default-content-type + + Specifies the Content-Type HTTP header. By default, the server uses the + MIME type ``'application/octet-stream'`` for the ``Content-Type`` header + when the content type cannot be guessed from the URL's extension (if any):: python -m http.server --default-content-type text/html -.. versionchanged:: next - Added the ``--default-content-type`` option. + .. versionadded:: next .. class:: CGIHTTPRequestHandler(request, client_address, server) From 6fa0a6d65f36fd978c4021681e251f760be7cdab Mon Sep 17 00:00:00 2001 From: John Comeau Date: Sun, 16 Mar 2025 07:09:04 -0700 Subject: [PATCH 19/20] corrected double hyphen --- Doc/library/http.server.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/library/http.server.rst b/Doc/library/http.server.rst index 83d1d624840195..fc034ae21d355e 100644 --- a/Doc/library/http.server.rst +++ b/Doc/library/http.server.rst @@ -426,7 +426,7 @@ such as using different index file names by overriding the class attribute :attr:`index_pages`. -.. option:: -default-content-type +.. option:: --default-content-type Specifies the Content-Type HTTP header. By default, the server uses the MIME type ``'application/octet-stream'`` for the ``Content-Type`` header From afe8ebbb50e5d4c2fdd1a82710dcf4a5d0836e4f Mon Sep 17 00:00:00 2001 From: John Comeau Date: Sun, 16 Mar 2025 08:07:47 -0700 Subject: [PATCH 20/20] moved to options section --- Doc/library/http.server.rst | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/Doc/library/http.server.rst b/Doc/library/http.server.rst index fc034ae21d355e..b32a75ae589e53 100644 --- a/Doc/library/http.server.rst +++ b/Doc/library/http.server.rst @@ -426,16 +426,6 @@ such as using different index file names by overriding the class attribute :attr:`index_pages`. -.. option:: --default-content-type - - Specifies the Content-Type HTTP header. By default, the server uses the - MIME type ``'application/octet-stream'`` for the ``Content-Type`` header - when the content type cannot be guessed from the URL's extension (if any):: - - python -m http.server --default-content-type text/html - - .. versionadded:: next - .. class:: CGIHTTPRequestHandler(request, client_address, server) This class is used to serve either files or output of CGI scripts from the @@ -541,6 +531,16 @@ The following options are accepted: .. versionadded:: 3.11 +.. option:: --default-content-type + + Specifies the Content-Type HTTP header. By default, the server uses the + MIME type ``'application/octet-stream'`` for the ``Content-Type`` header + when the content type cannot be guessed from the URL's extension (if any):: + + python -m http.server --default-content-type text/html + + .. versionadded:: next + .. option:: --cgi :class:`CGIHTTPRequestHandler` can be enabled in the command line by passing