| 658 | | headers = {'Content-type': 'text/plain'} |
| 659 | | output = ['Page not found: %s' % environ['PATH_INFO']] |
| 660 | | start_response(status, headers.items()) |
| 661 | | return output |
| 662 | | if not os.path.exists(file_path): |
| 663 | | status = '404 NOT FOUND' |
| 664 | | headers = {'Content-type': 'text/plain'} |
| 665 | | output = ['Page not found: %s' % environ['PATH_INFO']] |
| 666 | | else: |
| 667 | | try: |
| 668 | | fp = open(file_path, 'rb') |
| 669 | | except IOError: |
| 670 | | status = '401 UNAUTHORIZED' |
| 671 | | headers = {'Content-type': 'text/plain'} |
| 672 | | output = ['Permission denied: %s' % environ['PATH_INFO']] |
| 673 | | else: |
| 674 | | # This is a very simple implementation of conditional GET with |
| 675 | | # the Last-Modified header. It makes media files a bit speedier |
| 676 | | # because the files are only read off disk for the first |
| 677 | | # request (assuming the browser/client supports conditional |
| 678 | | # GET). |
| 679 | | mtime = http_date(os.stat(file_path)[stat.ST_MTIME]) |
| 680 | | headers = {'Last-Modified': mtime} |
| 681 | | if environ.get('HTTP_IF_MODIFIED_SINCE', None) == mtime: |
| 682 | | status = '304 NOT MODIFIED' |
| 683 | | output = [] |
| 684 | | else: |
| 685 | | status = '200 OK' |
| 686 | | mime_type = mimetypes.guess_type(file_path)[0] |
| 687 | | if mime_type: |
| 688 | | headers['Content-Type'] = mime_type |
| 689 | | output = [fp.read()] |
| 690 | | fp.close() |
| 691 | | start_response(status, headers.items()) |
| 692 | | return output |
| | 660 | start_response(status, {'Content-type': 'text/plain'}.items()) |
| | 661 | return [str('Page not found: %s' % environ['PATH_INFO'])] |
| | 662 | status_text = STATUS_CODE_TEXT[response.status_code] |
| | 663 | status = '%s %s' % (response.status_code, status_text) |
| | 664 | response_headers = [(str(k), str(v)) for k, v in response.items()] |
| | 665 | for c in response.cookies.values(): |
| | 666 | response_headers.append(('Set-Cookie', str(c.output(header='')))) |
| | 667 | start_response(status, response_headers) |
| | 668 | return response |
| | 669 | |
| | 670 | |