| 679 | | headers = {'Content-type': 'text/plain'} |
| 680 | | output = ['Page not found: %s' % environ['PATH_INFO']] |
| 681 | | start_response(status, headers.items()) |
| 682 | | return output |
| 683 | | if not os.path.exists(file_path): |
| 684 | | status = '404 NOT FOUND' |
| 685 | | headers = {'Content-type': 'text/plain'} |
| 686 | | output = ['Page not found: %s' % environ['PATH_INFO']] |
| 687 | | else: |
| 688 | | try: |
| 689 | | fp = open(file_path, 'rb') |
| 690 | | except IOError: |
| 691 | | status = '401 UNAUTHORIZED' |
| 692 | | headers = {'Content-type': 'text/plain'} |
| 693 | | output = ['Permission denied: %s' % environ['PATH_INFO']] |
| 694 | | else: |
| 695 | | # This is a very simple implementation of conditional GET with |
| 696 | | # the Last-Modified header. It makes media files a bit speedier |
| 697 | | # because the files are only read off disk for the first |
| 698 | | # request (assuming the browser/client supports conditional |
| 699 | | # GET). |
| 700 | | mtime = http_date(os.stat(file_path)[stat.ST_MTIME]) |
| 701 | | headers = {'Last-Modified': mtime} |
| 702 | | if environ.get('HTTP_IF_MODIFIED_SINCE', None) == mtime: |
| 703 | | status = '304 NOT MODIFIED' |
| 704 | | output = [] |
| 705 | | else: |
| 706 | | status = '200 OK' |
| 707 | | mime_type = mimetypes.guess_type(file_path)[0] |
| 708 | | if mime_type: |
| 709 | | headers['Content-Type'] = mime_type |
| 710 | | output = [fp.read()] |
| 711 | | fp.close() |
| 712 | | start_response(status, headers.items()) |
| 713 | | return output |
| | 681 | start_response(status, {'Content-type': 'text/plain'}.items()) |
| | 682 | return [str('Page not found: %s' % environ['PATH_INFO'])] |
| | 683 | status_text = STATUS_CODE_TEXT[response.status_code] |
| | 684 | status = '%s %s' % (response.status_code, status_text) |
| | 685 | response_headers = [(str(k), str(v)) for k, v in response.items()] |
| | 686 | for c in response.cookies.values(): |
| | 687 | response_headers.append(('Set-Cookie', str(c.output(header='')))) |
| | 688 | start_response(status, response_headers) |
| | 689 | return response |
| | 690 | |
| | 691 | |