Skip to content

Commit 7848730

Browse files
committed
changeable default
1 parent f7c5a7a commit 7848730

1 file changed

Lines changed: 7 additions & 11 deletions

File tree

Lib/http/server.py

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,7 @@ class BaseHTTPRequestHandler(socketserver.StreamRequestHandler):
263263
# the client gets back when sending a malformed request line.
264264
# Most web servers default to HTTP 0.9, i.e. don't send a status line.
265265
default_request_version = "HTTP/0.9"
266+
default_content_type = "application/octet-stream"
266267

267268
def parse_request(self):
268269
"""Parse a request (internal).
@@ -718,7 +719,7 @@ def send_head(self):
718719
break
719720
else:
720721
return self.list_directory(path)
721-
ctype = self.guess_type(path)
722+
ctype = self.guess_type(path, default_type)
722723
# check for trailing "/" which should return 404. See Issue17324
723724
# The test for this was added in test_httpserver.py
724725
# However, some OS platforms accept a trailingSlash as a filename
@@ -877,7 +878,7 @@ def copyfile(self, source, outputfile):
877878
"""
878879
shutil.copyfileobj(source, outputfile)
879880

880-
def guess_type(self, path):
881+
def guess_type(self, path, default=self.default_content_type):
881882
"""Guess the type of a file.
882883
883884
Argument is a PATH (a filename).
@@ -892,15 +893,10 @@ def guess_type(self, path):
892893
893894
"""
894895
base, ext = posixpath.splitext(path)
895-
if ext in self.extensions_map:
896-
return self.extensions_map[ext]
897-
ext = ext.lower()
898-
if ext in self.extensions_map:
899-
return self.extensions_map[ext]
900-
guess, _ = mimetypes.guess_type(path)
901-
if guess:
902-
return guess
903-
return 'application/octet-stream'
896+
guess = self.extensions_map.get(ext,
897+
self.extensions_map.get(ext.lower(),
898+
mimetypes.guess_type(path)))
899+
return guess or default
904900

905901

906902
# Utilities for CGIHTTPRequestHandler

0 commit comments

Comments
 (0)