FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-113471: adds custom default content-type to http.server by donbarbos · Pull Request #131000 · python/cpython · GitHub

Repository navigation

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (1) .rst  (2) All 2 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
17 changes: 17 additions & 0 deletions Doc/library/http.server.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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.
For backwards compatibility, the setting defaults to
``'application/octet-stream'``.

.. attribute:: MessageClass

Specifies an :class:`email.message.Message`\ -like class to parse HTTP
Expand Down Expand Up @@ -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
Expand Down
11 changes: 9 additions & 2 deletions Lib/http/server.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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,9 @@ 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
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 '
'(default: %(default)s)')
Expand Down Expand Up @@ -1330,4 +1336,5 @@ def finish_request(self, request, client_address):
port=args.port,
bind=args.bind,
protocol=args.protocol,
content_type=args.content_type
)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Adds ability to set default content-type for no extension/unrecognized extension on :mod:`http.server` module.

Back | FazBrowse Home | New Git URL