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)
| 747 | f.close() |
| 748 | |
| 749 | def send_head(self): |
| 750 | """Common code for GET and HEAD commands. |
| 751 | |
| 752 | This sends the response code and MIME headers. |
| 753 | |
| 754 | Return value is either a file object (which has to be copied |
| 755 | to the outputfile by the caller unless the command was HEAD, |
| 756 | and must be closed by the caller under all circumstances), or |
| 757 | None, in which case the caller has nothing further to do. |
| 758 | |
| 759 | """ |
| 760 | path = self.translate_path(self.path) |
| 761 | f = None |
| 762 | if os.path.isdir(path): |
| 763 | parts = urllib.parse.urlsplit(self.path) |
| 764 | if not parts.path.endswith(('/', '%2f', '%2F')): |
| 765 | # redirect browser - doing basically what apache does |
| 766 | self.send_response(HTTPStatus.MOVED_PERMANENTLY) |
| 767 | new_parts = (parts[0], parts[1], parts[2] + '/', |
| 768 | parts[3], parts[4]) |
| 769 | new_url = urllib.parse.urlunsplit(new_parts) |
| 770 | self.send_header("Location", new_url) |
| 771 | self.send_header("Content-Length", "0") |
| 772 | self.end_headers() |
| 773 | return None |
| 774 | for index in self.index_pages: |
| 775 | index = os.path.join(path, index) |
| 776 | if os.path.isfile(index): |
| 777 | path = index |
| 778 | break |
| 779 | else: |
| 780 | return self.list_directory(path) |
| 781 | ctype = self.guess_type(path) |
| 782 | # check for trailing "/" which should return 404. See Issue17324 |
| 783 | # The test for this was added in test_httpserver.py |
| 784 | # However, some OS platforms accept a trailingSlash as a filename |
| 785 | # See discussion on python-dev and Issue34711 regarding |
| 786 | # parsing and rejection of filenames with a trailing slash |
| 787 | if path.endswith("/"): |
| 788 | self.send_error(HTTPStatus.NOT_FOUND, "File not found") |
| 789 | return None |
| 790 | try: |
| 791 | f = open(path, 'rb') |
| 792 | except OSError: |
| 793 | self.send_error(HTTPStatus.NOT_FOUND, "File not found") |
| 794 | return None |
| 795 | |
| 796 | try: |
| 797 | fs = os.fstat(f.fileno()) |
| 798 | # Use browser cache if possible |
| 799 | if ("If-Modified-Since" in self.headers |
| 800 | and "If-None-Match" not in self.headers): |
| 801 | # compare If-Modified-Since and time of last file modification |
| 802 | try: |
| 803 | ims = email.utils.parsedate_to_datetime( |
| 804 | self.headers["If-Modified-Since"]) |
| 805 | except (TypeError, IndexError, OverflowError, ValueError): |
| 806 | # ignore ill-formed values |
no test coverage detected