<s> """<STR_LIT>""" <EOL> import mimetypes <EOL> import os <EOL> import posixpath <EOL> import re <EOL> import stat <EOL> import urllib <EOL> from email . Utils import parsedate_tz , mktime_tz <EOL> from django . template import loader <EOL> from django . http import Http404 , HttpResponse , HttpResponseRedirect , HttpResponseNotModified <EOL> from django . template import Template , Context , TemplateDoesNotExist <EOL> from django . utils . http import http_date <EOL> def serve ( request , path , document_root = None , show_indexes = False ) : <EOL> """<STR_LIT>""" <EOL> path = posixpath . normpath ( urllib . unquote ( path ) ) <EOL> path = path . lstrip ( '<STR_LIT:/>' ) <EOL> newpath = '<STR_LIT>' <EOL> for part in path . split ( '<STR_LIT:/>' ) : <EOL> if not part : <EOL> continue <EOL> drive , part = os . path . splitdrive ( part ) <EOL> head , part = os . path . split ( part ) <EOL> if part in ( os . curdir , os . pardir ) : <EOL> continue <EOL> newpath = os . path . join ( newpath , part ) . replace ( '<STR_LIT:\\>' , '<STR_LIT:/>' ) <EOL> if newpath and path != newpath : <EOL> return HttpResponseRedirect ( newpath ) <EOL> fullpath = os . path . join ( document_root , newpath ) <EOL> if os . path . isdir ( fullpath ) : <EOL> if show_indexes : <EOL> return directory_index ( newpath , fullpath ) <EOL> raise Http404 ( "<STR_LIT>" ) <EOL> if not os . path . exists ( fullpath ) : <EOL> raise Http404 ( '<STR_LIT>' % fullpath ) <EOL> statobj = os . stat ( fullpath ) <EOL> mimetype , encoding = mimetypes . guess_type ( fullpath ) <EOL> mimetype = mimetype or '<STR_LIT>' <EOL> if not was_modified_since ( request . META . get ( '<STR_LIT>' ) , <EOL> statobj [ stat . ST_MTIME ] , statobj [ stat . ST_SIZE ] ) : <EOL> return HttpResponseNotModified ( mimetype = mimetype ) <EOL> contents = open ( fullpath , '<STR_LIT:rb>' ) . read ( ) <EOL> response = HttpResponse ( contents , mimetype = mimetype ) <EOL> response [ "<STR_LIT>" ] = http_date ( statobj [ stat . ST_MTIME ] ) <EOL> response [ "<STR_LIT>" ] = len ( contents ) <EOL> if encoding : <EOL> response [ "<STR_LIT>" ] = encoding <EOL> return response <EOL> DEFAULT_DIRECTORY_INDEX_TEMPLATE = """<STR_LIT>""" <EOL> def directory_index ( path , fullpath ) : <EOL> try : <EOL> t = loader . select_template ( [ '<STR_LIT>' , <EOL> '<STR_LIT>' ] ) <EOL> except TemplateDoesNotExist : <EOL> t = Template ( DEFAULT_DIRECTORY_INDEX_TEMPLATE , name = '<STR_LIT>' ) <EOL> files = [ ] <EOL> for f in os . listdir ( fullpath ) : <EOL> if not f . startswith ( '<STR_LIT:.>' ) : <EOL> if os . path . isdir ( os . path . join ( fullpath , f ) ) : <EOL> f += '<STR_LIT:/>' <EOL> files . append ( f ) <EOL> c = Context ( { <EOL> '<STR_LIT>' : path + '<STR_LIT:/>' , <EOL> '<STR_LIT>' : files , <EOL> } ) <EOL> return HttpResponse ( t . render ( c ) ) <EOL> def was_modified_since ( header = None , mtime = <NUM_LIT:0> , size = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> if header is None : <EOL> raise ValueError <EOL> matches = re . match ( r"<STR_LIT>" , header , <EOL> re . IGNORECASE ) <EOL> header_date = parsedate_tz ( matches . group ( <NUM_LIT:1> ) ) <EOL> if header_date is None : <EOL> raise ValueError <EOL> header_mtime = mktime_tz ( header_date ) <EOL> header_len = matches . group ( <NUM_LIT:3> ) <EOL> if header_len and int ( header_len ) != size : <EOL> raise ValueError <EOL> if mtime > header_mtime : <EOL> raise ValueError <EOL> except ( AttributeError , ValueError , OverflowError ) : <EOL> return True <EOL> return False </s>
