MCPcopy Index your code
hub / github.com/python/cpython / send_head

Method send_head

Lib/http/server.py:721–807  ·  view source on GitHub ↗

Common code for GET and HEAD commands. This sends the response code and MIME headers. Return value is either a file object (which has to be copied to the outputfile by the caller unless the command was HEAD, and must be closed by the caller under all circumstances),

(self)

Source from the content-addressed store, hash-verified

719 f.close()
720
721 def send_head(self):
722 """Common code for GET and HEAD commands.
723
724 This sends the response code and MIME headers.
725
726 Return value is either a file object (which has to be copied
727 to the outputfile by the caller unless the command was HEAD,
728 and must be closed by the caller under all circumstances), or
729 None, in which case the caller has nothing further to do.
730
731 """
732 path = self.translate_path(self.path)
733 f = None
734 if os.path.isdir(path):
735 parts = urllib.parse.urlsplit(self.path)
736 if not parts.path.endswith(('/', '%2f', '%2F')):
737 # redirect browser - doing basically what apache does
738 self.send_response(HTTPStatus.MOVED_PERMANENTLY)
739 new_parts = (parts[0], parts[1], parts[2] + '/',
740 parts[3], parts[4])
741 new_url = urllib.parse.urlunsplit(new_parts)
742 self.send_header("Location", new_url)
743 self.send_header("Content-Length", "0")
744 self.end_headers()
745 return None
746 for index in self.index_pages:
747 index = os.path.join(path, index)
748 if os.path.isfile(index):
749 path = index
750 break
751 else:
752 return self.list_directory(path)
753 ctype = self.guess_type(path)
754 # check for trailing "/" which should return 404. See Issue17324
755 # The test for this was added in test_httpserver.py
756 # However, some OS platforms accept a trailingSlash as a filename
757 # See discussion on python-dev and Issue34711 regarding
758 # parsing and rejection of filenames with a trailing slash
759 if path.endswith("/"):
760 self.send_error(HTTPStatus.NOT_FOUND, "File not found")
761 return None
762 try:
763 f = open(path, 'rb')
764 except OSError:
765 self.send_error(HTTPStatus.NOT_FOUND, "File not found")
766 return None
767
768 try:
769 fs = os.fstat(f.fileno())
770 # Use browser cache if possible
771 if ("If-Modified-Since" in self.headers
772 and "If-None-Match" not in self.headers):
773 # compare If-Modified-Since and time of last file modification
774 try:
775 ims = email.utils.parsedate_to_datetime(
776 self.headers["If-Modified-Since"])
777 except (TypeError, IndexError, OverflowError, ValueError):
778 # ignore ill-formed values

Callers 2

do_GETMethod · 0.95
do_HEADMethod · 0.95

Calls 15

translate_pathMethod · 0.95
list_directoryMethod · 0.95
guess_typeMethod · 0.95
strFunction · 0.85
send_responseMethod · 0.80
send_headerMethod · 0.80
send_errorMethod · 0.80
date_time_stringMethod · 0.80
openFunction · 0.50
isdirMethod · 0.45
endswithMethod · 0.45
end_headersMethod · 0.45

Tested by

no test coverage detected