mfc@sdZdZdgZddlZddlZddlZddlZddlZddlZddl Z ddl Z yddl m Z Wn!e k rddl m Z nXdejfdYZeejdZedkrendS( sSimple HTTP Server. This module builds on BaseHTTPServer by implementing the standard GET and HEAD requests in a fairly straightforward manner. s0.6tSimpleHTTPRequestHandleriN(tStringIOcBseZdZdeZdZdZdZdZdZ dZ dZ e j ske jne jjZejid d 6d d 6d d 6d d6RS(sWSimple HTTP request handler with GET and HEAD commands. This serves files from the current directory and any of its subdirectories. The MIME type for files is determined by calling the .guess_type() method. The GET and HEAD requests are identical except that the HEAD request omits the actual contents of the file. s SimpleHTTP/cCs6|j}|r2|j||j|jndS(sServe a GET request.N(t send_headtcopyfiletwfiletclose(tselftf((s(/usr/lib64/python2.7/SimpleHTTPServer.pytdo_GET*s cCs#|j}|r|jndS(sServe a HEAD request.N(RR(RR((s(/usr/lib64/python2.7/SimpleHTTPServer.pytdo_HEAD1s cCs}|j|j}d}tjj|r|jjdsn|jd|jd|jd|jdSxOdD]7}tjj ||}tjj |ru|}PququW|j |Sn|j |}yt |d}Wn"tk r|jdddSX|jd |jd |tj|j}|jd t|d |jd |j|j|j|S(s{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), or None, in which case the caller has nothing further to do. t/i-tLocations index.htmls index.htmtrbisFile not foundis Content-typesContent-Lengthis Last-ModifiedN(s index.htmls index.htm(ttranslate_pathtpathtNonetostisdirtendswitht send_responset send_headert end_headerstjointexiststlist_directoryt guess_typetopentIOErrort send_errortfstattfilenotstrtdate_time_stringtst_mtime(RRRtindextctypetfs((s(/usr/lib64/python2.7/SimpleHTTPServer.pyR7s6       c Csytj|}Wn%tjk r:|jdddSX|jddt}tjt j |j }|j d|j d||j d||j dx|D]}tj j ||}|}}tj j|r|d }|d }ntj j|r"|d }n|j d t j|tj|fqW|j d |j} |jd |jdtj} |jdd| |jdt| |j|S(sHelper to produce a directory listing (absent index.html). Return value is either a file object, or None (indicating an error). In either case, the headers are sent, making the interface the same as for send_head(). isNo permission to list directorytkeycSs |jS(N(tlower(ta((s(/usr/lib64/python2.7/SimpleHTTPServer.pytpss7s/ Directory listing for %s s)

Directory listing for %s

s

iis Content-typestext/html; charset=%ssContent-LengthN(RtlistdirterrorRRtsortRtcgitescapeturllibtunquoteRtwriteRRtislinktquotettelltseekRtsystgetfilesystemencodingRRR( RRtlistRt displaypathtnametfullnamet displaynametlinknametlengthtencoding((s(/usr/lib64/python2.7/SimpleHTTPServer.pyRcs>         $      cCs|jddd}|jddd}tjtj|}|jd}td|}tj}xq|D]i}tj j |\}}tj j|\}}|tj tj fkrquntj j ||}quW|S(sTranslate a /-separated PATH to the local filename syntax. Components that mean special things to the local file system (e.g. drive or directory names) are ignored. (XXX They should probably be diagnosed.) t?iit#R N(tsplitt posixpathtnormpathR/R0tfilterRRtgetcwdRt splitdrivetcurdirtpardirR(RRtwordstwordtdrivethead((s(/usr/lib64/python2.7/SimpleHTTPServer.pyR s   cCstj||dS(sCopy all data between two file objects. The SOURCE argument is a file object open for reading (or anything with a read() method) and the DESTINATION argument is a file object open for writing (or anything with a write() method). The only reason for overriding this would be to change the block size or perhaps to replace newlines by CRLF -- note however that this the default server uses this to copy binary data as well. N(tshutilt copyfileobj(Rtsourcet outputfile((s(/usr/lib64/python2.7/SimpleHTTPServer.pyRscCsdtj|\}}||jkr/|j|S|j}||jkrU|j|S|jdSdS(sGuess the type of a file. Argument is a PATH (a filename). Return value is a string of the form type/subtype, usable for a MIME Content-type header. The default implementation looks the file's extension up in the table self.extensions_map, using application/octet-stream as a default; however it would be permissible (if slow) to look inside the data to make a better guess. tN(RCtsplitexttextensions_mapR&(RRtbasetext((s(/usr/lib64/python2.7/SimpleHTTPServer.pyRs   sapplication/octet-streamRRs text/plains.pys.cs.h(t__name__t __module__t__doc__t __version__tserver_versionRR RRR RRt mimetypestinitedtinitt types_maptcopyRTtupdate(((s(/usr/lib64/python2.7/SimpleHTTPServer.pyRs"     , *      cCstj||dS(N(tBaseHTTPServerttest(t HandlerClasst ServerClass((s(/usr/lib64/python2.7/SimpleHTTPServer.pyRcst__main__(RYRZt__all__RRCRbR/R-R6RNR\t cStringIORt ImportErrortBaseHTTPRequestHandlerRt HTTPServerRcRW(((s(/usr/lib64/python2.7/SimpleHTTPServer.pyts&