Skip to content

Commit 2defce3

Browse files
committed
addresses some of @encukou's observations. still need to add tests.
1 parent 819491f commit 2defce3

File tree

2 files changed

+26
-24
lines changed

2 files changed

+26
-24
lines changed

Doc/library/http.server.rst

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,13 @@ provides three different variants:
167167
header (using :meth:`send_header`) in all of its responses to clients.
168168
For backwards compatibility, the setting defaults to ``'HTTP/1.0'``.
169169

170+
.. attribute:: default_content_type
171+
172+
Specifies the content-type header value sent when the mime-type cannot
173+
be guessed from the file extension of the requested URL.
174+
For backwards compatibility, the setting defaults to
175+
``'application/octet-stream'``.
176+
170177
.. attribute:: MessageClass
171178

172179
Specifies an :class:`email.message.Message`\ -like class to parse HTTP
@@ -462,6 +469,16 @@ following command runs an HTTP/1.1 conformant server::
462469
.. versionchanged:: 3.11
463470
Added the ``--protocol`` option.
464471

472+
By default, the server uses the MIME type ``'application/octet-stream'`` for
473+
the ``content-type`` header when the content type cannot be guessed from the
474+
URL's extension (if any). The option ``--default-content-type`` overrides
475+
this default with one of the user's choosing::
476+
477+
python -m http.server --default-content-type text/html
478+
479+
.. versionchanged:: 3.13
480+
Added the ``--default-content-type`` option.
481+
465482
.. class:: CGIHTTPRequestHandler(request, client_address, server)
466483

467484
This class is used to serve either files or output of CGI scripts from the

Lib/http/server.py

Lines changed: 9 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -891,32 +891,17 @@ def guess_type(self, path):
891891
as a default; however it would be permissible (if
892892
slow) to look inside the data to make a better guess.
893893
894-
>>> testserver = type('', (), {
895-
... 'makefile': lambda mode, bufsize: open(os.devnull, mode)
896-
... })
897-
>>> testhandler = SimpleHTTPRequestHandler(testserver, None, None)
898-
>>> testhandler.guess_type('/foo.bar.GZ') # tests ext.lower()
899-
'application/gzip'
900-
>>> testhandler.default_content_type = 'nonesuch/nonesuch'
901-
>>> testhandler.guess_type('/this/should/give/default')
902-
'nonesuch/nonesuch'
903-
>>> # check short-circuiting works using mock mimetypes.guess_type
904-
>>> sys.modules[__name__].mimetypes = type('', (), {
905-
... 'guess_type': lambda x: (print('foo'), print('bar'))
906-
... })
907-
>>> testhandler.guess_type('/should/show/foo/bar/then/default')
908-
foo
909-
bar
910-
'nonesuch/nonesuch'
911-
>>> testhandler.guess_type('/should/not/print/foo.Z')
912-
'application/octet-stream'
913-
914894
"""
915895
base, ext = posixpath.splitext(path)
916-
return self.extensions_map.get(ext) or \
917-
self.extensions_map.get(ext.lower()) or \
918-
mimetypes.guess_type(path)[0] or \
919-
self.default_content_type
896+
if ext in self.extensions_map:
897+
return self.extensions_map[ext]
898+
ext = ext.lower()
899+
if ext in self.extensions_map:
900+
return self.extensions_map[ext]
901+
guess, _ = mimetypes.guess_type(path)
902+
if guess:
903+
return guess
904+
return self.default_content_type
920905

921906

922907
# Utilities for CGIHTTPRequestHandler

0 commit comments

Comments
 (0)