Skip to content
Open
Show file tree
Hide file tree
Changes from 26 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
7848730
changeable default
jcomeauictx Dec 25, 2023
19cde54
ready to test
jcomeauictx Dec 25, 2023
e7860c3
passed test this time
jcomeauictx Dec 25, 2023
b945ced
DRY fix for #113471
jcomeauictx Dec 25, 2023
c2da505
📜🤖 Added by blurb_it.
blurb-it[bot] Dec 25, 2023
2f900fe
Merge branch 'main' into httpupgrade
jcomeauictx Dec 25, 2023
3486458
Merge branch 'main' into httpupgrade
jcomeauictx Dec 25, 2023
497f11e
Merge branch 'httpupgrade' of github.com:jcomeauictx/cpython into htt…
jcomeauictx Dec 27, 2023
48cdd20
Merge branch 'main' into httpupgrade
jcomeauictx Jan 4, 2024
d189051
Merge branch 'main' into httpupgrade
jcomeauictx Jan 13, 2024
4a6370b
Update Misc/NEWS.d/next/Library/2023-12-25-19-14-07.gh-issue-113471.Z…
jcomeauictx Jan 27, 2024
f7ff834
Merge branch 'httpupgrade' of github.com:jcomeauictx/cpython into htt…
jcomeauictx Jan 27, 2024
7ae00ea
removed short option and inefficiency pointed out by @serhiy-storchaka
jcomeauictx Jan 27, 2024
b9841d0
Merge branch 'main' into httpupgrade
jcomeauictx Jan 29, 2024
2ba2aee
adding tests per @serhiy_storchaka's suggestion, not all pass yet
jcomeauictx Jan 29, 2024
819491f
tests all pass
jcomeauictx Jan 29, 2024
2defce3
addresses some of @encukou's observations. still need to add tests.
jcomeauictx Feb 3, 2024
43e6851
Merge branch 'main' into httpupgrade for donBarbos
jcomeauictx Mar 9, 2025
69800b9
donBarbos's suggestion
jcomeauictx Mar 9, 2025
339b3ee
Merge branch 'main' into httpupgrade
jcomeauictx Mar 16, 2025
4778206
Update Doc/library/http.server.rst
jcomeauictx Mar 16, 2025
8903147
Update Misc/NEWS.d/next/Library/2023-12-25-19-14-07.gh-issue-113471.Z…
jcomeauictx Mar 16, 2025
dfa30d9
Update Lib/http/server.py
jcomeauictx Mar 16, 2025
3b8450d
Update Lib/http/server.py
jcomeauictx Mar 16, 2025
59f152a
Update Doc/library/http.server.rst
jcomeauictx Mar 16, 2025
259e9b5
Update Lib/http/server.py
jcomeauictx Mar 16, 2025
1662667
Update Doc/library/http.server.rst
jcomeauictx Mar 16, 2025
6fa0a6d
corrected double hyphen
jcomeauictx Mar 16, 2025
afe8ebb
moved to options section
jcomeauictx Mar 16, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions Doc/library/http.server.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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.
By default, it is set to ``'application/octet-stream'``.

.. versionadded:: next
.. attribute:: MessageClass

Specifies an :class:`email.message.Message`\ -like class to parse HTTP
Expand Down Expand Up @@ -419,6 +426,16 @@ 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::

python -m http.server --default-content-type text/html

.. versionchanged:: next
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
Expand Down
12 changes: 10 additions & 2 deletions Lib/http/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,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).
Expand Down Expand Up @@ -907,7 +908,7 @@ def guess_type(self, path):
guess, _ = mimetypes.guess_file_type(path)
if guess:
return guess
return 'application/octet-stream'
return self.default_content_type


# Utilities for CGIHTTPRequestHandler
Expand Down Expand Up @@ -1263,14 +1264,16 @@ 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).

"""
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
Expand Down Expand Up @@ -1301,6 +1304,10 @@ def test(HandlerClass=BaseHTTPRequestHandler,
default='HTTP/1.0',
help='conform to this HTTP version '
'(default: %(default)s)')
parser.add_argument('--content-type',
default=BaseHTTPRequestHandler.default_content_type,
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)')
Expand Down Expand Up @@ -1330,4 +1337,5 @@ def finish_request(self, request, client_address):
port=args.port,
bind=args.bind,
protocol=args.protocol,
content_type=args.content_type,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Allow :mod:`http.server` to set a default content-type when serving
files with an unknown or missing extension.
Loading