def static_file(filename, root, mimetype='<STR_LIT>', download=False):
root = os.path.abspath(root) + os.sep<EOL>filename = os.path.abspath(os.path.join(root, filename.strip('<STR_LIT>')))<EOL>header = dict()<EOL>if not filename.startswith(root):<EOL><INDENT>return HTTPError(<NUM_LIT>, "<STR_LIT>")<EOL><DEDENT>if not os.path.exists(filename) or not os.path.isfile(filename):<EOL><INDENT>return HTTPError(<NUM_LIT>, "<STR_LIT>")<EOL><DEDENT>if not os.access(filename, os.R_OK):<EOL><INDENT>return HTTPError(<NUM_LIT>, "<STR_LIT>")<EOL><DEDENT>if mimetype == '<STR_LIT>':<EOL><INDENT>mimetype, encoding = mimetypes.guess_type(filename)<EOL>if mimetype: header['<STR_LIT:Content-Type>'] = mimetype<EOL>if encoding: header['<STR_LIT>'] = encoding<EOL><DEDENT>elif mimetype:<EOL><INDENT>header['<STR_LIT:Content-Type>'] = mimetype<EOL><DEDENT>if download:<EOL><INDENT>download = os.path.basename(filename if download == True else download)<EOL>header['<STR_LIT>'] = '<STR_LIT>' % download<EOL><DEDENT>stats = os.stat(filename)<EOL>header['<STR_LIT>'] = stats.st_size<EOL>lm = time.strftime("<STR_LIT>", time.gmtime(stats.st_mtime))<EOL>header['<STR_LIT>'] = lm<EOL>ims = request.environ.get('<STR_LIT>')<EOL>if ims:<EOL><INDENT>ims = parse_date(ims.split("<STR_LIT:;>")[<NUM_LIT:0>].strip())<EOL><DEDENT>if ims is not None and ims >= int(stats.st_mtime):<EOL><INDENT>header['<STR_LIT>'] = time.strftime("<STR_LIT>", time.gmtime())<EOL>return HTTPResponse(status=<NUM_LIT>, header=header)<EOL><DEDENT>body = '<STR_LIT>' if request.method == '<STR_LIT>' else open(filename, '<STR_LIT:rb>')<EOL>return HTTPResponse(body, header=header)<EOL>
Open a file in a safe way and return :exc:`HTTPResponse` with status
        code 200, 305, 401 or 404. Set Content-Type, Content-Encoding,
        Content-Length and Last-Modified header. Obey If-Modified-Since header
        and HEAD requests.