ó ô^‚Rc@sòdZdZdgZddlZddlZddlZddlZddlZddlZddl Z ddl Z yddl m Z Wn!e k r¬ddl m Z nXdejfd„ƒYZeejd„ZedkrîeƒndS( s’Simple HTTP Server. This module builds on BaseHTTPServer by implementing the standard GET and HEAD requests in a fairly straightforward manner. s0.6tSimpleHTTPRequestHandleriÿÿÿÿN(tStringIOcBs¥eZdZdeZd„Zd„Zd„Zd„Zd„Z d„Z d„Z e j ske jƒne jjƒZejid d 6d d 6d d 6d d6ƒRS(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ƒ|jƒndS(sServe a GET request.N(t send_headtcopyfiletwfiletclose(tselftf((s&/usr/lib/python2.7/SimpleHTTPServer.pytdo_GET*s cCs#|jƒ}|r|jƒndS(sServe a HEAD request.N(RR(RR((s&/usr/lib/python2.7/SimpleHTTPServer.pytdo_HEAD1s cCs}|j|jƒ}d}tjj|ƒrÀ|jjdƒsn|jdƒ|jd|jdƒ|jƒdSxOdD]7}tjj ||ƒ}tjj |ƒru|}PququW|j |ƒSn|j |ƒ}yt |dƒ}Wn"tk r|jddƒdSX|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.htmtrbi”sFile not foundiÈs 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/lib/python2.7/SimpleHTTPServer.pyR7s6       c CsÆytj|ƒ}Wn%tjk r:|jddƒdSX|jdd„ƒtƒ}tjt j |j ƒƒ}|j dƒ|j d|ƒ|j d|ƒ|j dƒxž|D]–}tj j ||ƒ}|}}tj j|ƒr|d }|d }ntj j|ƒr"|d }n|j d t j|ƒtj|ƒfƒqµW|j d ƒ|jƒ} |jd ƒ|jdƒtjƒ} |jdd| ƒ|jdt| ƒƒ|jƒ|S(sûHelper 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(). i”sNo permission to list directorytkeycSs |jƒS(N(tlower(ta((s&/usr/lib/python2.7/SimpleHTTPServer.pytpss7s/ Directory listing for %s s)

Directory listing for %s

s

iiÈs Content-typestext/html; charset=%ssContent-LengthN(RtlistdirterrorRRtsortRtcgitescapeturllibtunquoteRtwriteRRtislinktquotettelltseekRtsystgetfilesystemencodingRRR( RRtlistRt displaypathtnametfullnamet displaynametlinknametlengthtencoding((s&/usr/lib/python2.7/SimpleHTTPServer.pyRcs>         $      cCsæ|jddƒd}|jddƒd}tjtj|ƒƒ}|jdƒ}td|ƒ}tjƒ}xq|D]i}tj j |ƒ\}}tj j|ƒ\}}|tj tj fkrÉquntj j ||ƒ}quW|S(sñTranslate 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/lib/python2.7/SimpleHTTPServer.pyR s   cCstj||ƒdS(sñCopy 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/lib/python2.7/SimpleHTTPServer.pyR£scCsdtj|ƒ\}}||jkr/|j|S|jƒ}||jkrU|j|S|jdSdS(s»Guess 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/lib/python2.7/SimpleHTTPServer.pyR³s   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/lib/python2.7/SimpleHTTPServer.pyRs"     , *      cCstj||ƒdS(N(tBaseHTTPServerttest(t HandlerClasst ServerClass((s&/usr/lib/python2.7/SimpleHTTPServer.pyRcÖst__main__(RYRZt__all__RRCRbR/R-R6RNR\t cStringIORt ImportErrortBaseHTTPRequestHandlerRt HTTPServerRcRW(((s&/usr/lib/python2.7/SimpleHTTPServer.pyts&          »