3 f@sdZddddgZddlZddlZddlZddlZddlZddl Z ddl Z ddl Z ddl Z ddl Z ddlZddlZddlZddlZddlZddlZddlmZdZd ZGd ddejZGd ddejZGd ddeZd dZdaddZddZ GdddeZ!eedddfddZ"e#dkrej$Z%e%j&dddde%j&dddd d!d"e%j&d#d$de'd%d&d'e%j(Z)e)j*rze!Z+neZ+e"e+e)j,e)j-d(dS))z0.6 HTTPServerBaseHTTPRequestHandlerSimpleHTTPRequestHandlerCGIHTTPRequestHandlerN) HTTPStatusa Error response

Error response

Error code: %(code)d

Message: %(message)s.

Error code explanation: %(code)s - %(explain)s.

ztext/html;charset=utf-8c@seZdZdZddZdS)rcCs4tjj||jdd\}}tj||_||_dS)N) socketserver TCPServer server_bindserver_addresssocketZgetfqdn server_name server_port)selfhostportr#/usr/lib64/python3.6/http/server.pyr s  zHTTPServer.server_bindN)__name__ __module__ __qualname__Zallow_reuse_addressr rrrrrsc @seZdZdejjdZdeZe Z e Z dZ ddZddZd d Zd d Zd?ddZd@ddZdAddZddZddZddZdBddZddZdd Zd!d"ZdCd#d$Zd%d&Zd'd(d)d*d+d,d-gZd d.d/d0d1d2d3d4d5d6d7d8d9g Zd:d;Z de&j'j(DZ)d S)DrzPython/rz BaseHTTP/zHTTP/0.9c Csd|_|j|_}d|_t|jd}|jd}||_|j}t |dkr|\}}}yZ|dddkrjt |jddd}|jd }t |d krt t |d t |df}Wn*t t fk r|j tjd |d SX|dkr|jdkrd |_|dkrr|j tjd|d Sn^t |d krR|\}}d|_|dkrr|j tjd|d Sn |s\d S|j tjd|d S||||_|_|_|jjdrd|jjd|_ytjj|j|jd|_Wnrtjjk r}z|j tjdt|d Sd}~Xn:tjjk r4}z|j tjdt|d Sd}~XnX|jjdd} | jdkrZd|_n | jdkrz|jdkrzd |_|jjdd} | jdkr|jdkr|jdkr|j sd SdS)NTz iso-8859-1z zHTTP//r.rrzBad request version (%r)FzHTTP/1.1zInvalid HTTP version (%s)ZGETzBad HTTP/0.9 request type (%r)zBad request syntax (%r)z//)Z_classz Line too longzToo many headers Connectionclosez keep-aliveZExpectz 100-continue)rr)rr)!commanddefault_request_versionrequest_versionclose_connectionstrraw_requestlinerstrip requestlinesplitlen ValueErrorint IndexError send_errorrZ BAD_REQUESTprotocol_versionZHTTP_VERSION_NOT_SUPPORTEDpath startswithlstriphttpclientZ parse_headersrfile MessageClassheadersZ LineTooLongZREQUEST_HEADER_FIELDS_TOO_LARGEZ HTTPExceptiongetlowerhandle_expect_100) rversionr&wordsrr.Zbase_version_numberZversion_numbererrZconntypeZexpectrrr parse_requests                    z$BaseHTTPRequestHandler.parse_requestcCs|jtj|jdS)NT)send_response_onlyrZCONTINUE end_headers)rrrrr8ns z(BaseHTTPRequestHandler.handle_expect_100cCsy|jjd|_t|jdkr@d|_d|_d|_|jtj dS|jsPd|_ dS|j s\dSd|j}t ||s|jtj d|jdSt||}||jjWn4tjk r}z|jd|d|_ dSd}~XnXdS)NiirTZdo_zUnsupported method (%r)zRequest timed out: %r)r3readliner$r(r&r!rr,rZREQUEST_URI_TOO_LONGr"r<hasattrNOT_IMPLEMENTEDgetattrwfileflushr Ztimeout log_error)rZmnamemethoderrrhandle_one_requests4      z)BaseHTTPRequestHandler.handle_one_requestcCs&d|_|jx|js |jqWdS)NT)r"rH)rrrrhandleszBaseHTTPRequestHandler.handleNcCs y|j|\}}Wntk r.d\}}YnX|dkr<|}|dkrH|}|jd|||j|||jddd}|dkr|tjtjtjfkr|j |t j |ddt j |ddd}|j d d }|jd |j |jd tt||j|jd ko|r|jj|dS)N???zcode %d, message %srrF)quote)codemessageexplainzUTF-8replacez Content-TypezContent-LengthZHEAD)rJrJ) responsesKeyErrorrE send_response send_headerrZ NO_CONTENTZ RESET_CONTENTZ NOT_MODIFIEDerror_message_formathtmlescapeencodeerror_content_typer#r(r>rrCwrite)rrMrNrOZshortmsgZlongmsgZbodyZcontentrrrr,s4     z!BaseHTTPRequestHandler.send_errorcCs:|j||j|||jd|j|jd|jdS)NZServerZDate) log_requestr=rTversion_stringdate_time_string)rrMrNrrrrSs  z$BaseHTTPRequestHandler.send_responsecCsd|jdkr`|dkr0||jkr,|j|d}nd}t|ds@g|_|jjd|j||fjdddS)NzHTTP/0.9rr_headers_bufferz %s %d %s zlatin-1strict)r!rQr@r^appendr-rX)rrMrNrrrr=s   z)BaseHTTPRequestHandler.send_response_onlycCsl|jdkr6t|dsg|_|jjd||fjdd|jdkrh|jdkrVd|_n|jd krhd |_dS) NzHTTP/0.9r^z%s: %s zlatin-1r_Z connectionrTz keep-aliveF)r!r@r^r`rXr7r")rkeywordvaluerrrrTs     z"BaseHTTPRequestHandler.send_headercCs"|jdkr|jjd|jdS)NzHTTP/0.9s )r!r^r` flush_headers)rrrrr> s  z"BaseHTTPRequestHandler.end_headerscCs(t|dr$|jjdj|jg|_dS)Nr^)r@rCrZjoinr^)rrrrrcs z$BaseHTTPRequestHandler.flush_headers-cCs.t|tr|j}|jd|jt|t|dS)Nz "%s" %s %s) isinstancerrb log_messager&r#)rrMsizerrrr[s z"BaseHTTPRequestHandler.log_requestcGs|j|f|dS)N)rh)rformatargsrrrrE!s z BaseHTTPRequestHandler.log_errorcGs&tjjd|j|j||fdS)Nz%s - - [%s] %s )sysstderrrZaddress_stringlog_date_time_string)rrjrkrrrrh/sz"BaseHTTPRequestHandler.log_messagecCs|jd|jS)N )server_version sys_version)rrrrr\Esz%BaseHTTPRequestHandler.version_stringcCs |dkrtj}tjj|ddS)NT)Zusegmt)timeemailZutilsZ formatdate)rZ timestamprrrr]Isz'BaseHTTPRequestHandler.date_time_stringc CsBtj}tj|\ }}}}}}}} } d||j|||||f} | S)Nz%02d/%3s/%04d %02d:%02d:%02d)rsZ localtime monthname) rZnowZyearZmonthZdayZhhZmmZssxyzsrrrroOs z+BaseHTTPRequestHandler.log_date_time_stringZMonZTueZWedZThuZFriZSatZSunZJanZFebZMarZAprZMayZJunZJulZAugZSepZOctZNovZDeccCs |jdS)Nr)client_address)rrrrrn]sz%BaseHTTPRequestHandler.address_stringzHTTP/1.0cCsi|]}|j|jf|qSr)phraseZ description).0vrrr lsz!BaseHTTPRequestHandler.)NN)N)N)rfrf)N)*rrrrlr9r'rr __version__rqDEFAULT_ERROR_MESSAGErUDEFAULT_ERROR_CONTENT_TYPErYr r<r8rHrIr,rSr=rTr>rcr[rErhr\r]roZ weekdaynamerurnr-r1r2Z HTTPMessager4r __members__valuesrQrrrrrs<ig% 5     c@sxeZdZdeZddZddZddZdd Zd d Z d d Z ddZ e j sVe je jjZejddddddS)rz SimpleHTTP/c Cs.|j}|r*z|j||jWd|jXdS)N) send_headcopyfilerCr)rfrrrdo_GETs zSimpleHTTPRequestHandler.do_GETcCs|j}|r|jdS)N)rr)rrrrrdo_HEADsz SimpleHTTPRequestHandler.do_HEADc Csx|j|j}d}tjj|rtjj|j}|jjds|jt j |d|d|dd|d|df}tjj |}|j d||j dSx6dD]$}tjj||}tjj|r|}PqW|j|S|j|}yt|d }Wn$tk r|jt jd dSXyZ|jt j|j d |tj|j}|j d t|d|j d|j|j|j |S|jYnXdS)NrrrrrZLocation index.html index.htmrbzFile not foundz Content-typezContent-Lengthz Last-Modified)rr)translate_pathr.osisdirurllibparseZurlsplitendswithrSrZMOVED_PERMANENTLYZ urlunsplitrTr>reexistslist_directory guess_typeopenOSErrorr, NOT_FOUNDOKfstatfilenor#r]st_mtimer) rr.rpartsZ new_partsZnew_urlindexZctypeZfsrrrrsF            z"SimpleHTTPRequestHandler.send_headc Csytj|}Wn"tk r0|jtjddSX|jdddg}ytjj |j dd}Wn t k r|tjj |}YnXt j |dd}tj}d |}|jd |jd |jd ||jd ||jd||jdx~|D]v}tj j||}|} } tj j|r"|d} |d} tj j|r8|d} |jdtjj| ddt j | ddfqW|jddj|j|d} tj} | j| | jd|jtj|jdd||jdtt| |j| S)NzNo permission to list directorycSs|jS)N)r7)arrrsz9SimpleHTTPRequestHandler.list_directory..)key surrogatepass)errorsF)rLzDirectory listing for %szZz z@z%s z

%s

z
    r@z
  • %s
  • z

 surrogateescaperz Content-typeztext/html; charset=%szContent-Length) rlistdirrr,rrsortrrunquoter.UnicodeDecodeErrorrVrWrlgetfilesystemencodingr`rerislinkrLrXioBytesIOrZseekrSrrTr#r(r>) rr.listrZ displaypathenctitlenamefullnameZ displaynameZlinknameZencodedrrrrrs\          z'SimpleHTTPRequestHandler.list_directoryc Cs|jddd}|jddd}|jjd}ytjj|dd}Wn tk rbtjj|}YnXtj|}|jd}t d|}t j }x8|D]0}t j j |s|t jt jfkrqt j j||}qW|r|d7}|S)N?rr#rr)r)r'r%rrrrr posixpathnormpathfilterrgetcwdr.dirnamecurdirpardirre)rr.Ztrailing_slashr:Zwordrrrrs$     z'SimpleHTTPRequestHandler.translate_pathcCstj||dS)N)shutilZ copyfileobj)rsourceZ outputfilerrrrsz!SimpleHTTPRequestHandler.copyfilecCsLtj|\}}||jkr"|j|S|j}||jkr>|j|S|jdSdS)Nr)rsplitextextensions_mapr7)rr.baseZextrrrr)s    z#SimpleHTTPRequestHandler.guess_typezapplication/octet-streamz text/plain)rz.pyz.cz.hN)rrrrrqrrrrrrr mimetypesZinitedZinitZ types_mapcopyrupdaterrrrrrs   1: c Cs|jd\}}}tjj|}|jd}g}x<|ddD],}|dkrN|jq8|r8|dkr8|j|q8W|r|j}|r|dkr|jd}q|dkrd}nd}|rdj||f}ddj||f}dj|}|S)Nrrrz..rr) partitionrrrr'popr`re) r._query path_partsZ head_partspartZ tail_partZ splitpathcollapsed_pathrrr_url_collapse_pathNs.     rcCsptrtSy ddl}Wntk r(dSXy|jddaWn.tk rjdtdd|jDaYnXtS)Nrrnobodyrcss|]}|dVqdS)rNr)r|rvrrr sznobody_uid..r)rpwd ImportErrorgetpwnamrRmaxZgetpwall)rrrr nobody_uids  rcCstj|tjS)N)raccessX_OK)r.rrr executablesrc@sReZdZeedZdZddZddZddZ d d gZ d d Z d dZ ddZ dS)rforkrcCs$|jr|jn|jtjddS)NzCan only POST to CGI scripts)is_cgirun_cgir,rrA)rrrrdo_POSTs  zCGIHTTPRequestHandler.do_POSTcCs|jr|jStj|SdS)N)rrrr)rrrrrszCGIHTTPRequestHandler.send_headcCsPt|j}|jdd}|d|||dd}}||jkrL||f|_dSdS)NrrTF)rr.findcgi_directoriescgi_info)rrZdir_sepheadtailrrrrs    zCGIHTTPRequestHandler.is_cgiz/cgi-binz/htbincCst|S)N)r)rr.rrr is_executablesz#CGIHTTPRequestHandler.is_executablecCstjj|\}}|jdkS)N.py.pyw)rr)rr.rr7)rr.rrrrr is_pythonszCGIHTTPRequestHandler.is_pythonc)Cs|j\}}|d|}|jdt|d}x`|dkr|d|}||dd}|j|}tjj|r||}}|jdt|d}q,Pq,W|jd\}}} |jd}|dkr|d|||d} }n |d} }|d| } |j| } tjj| s|j t j d| dStjj | s2|j t j d| dS|j| } |jsL| rn|j| sn|j t j d| dStjtj}|j|d <|jj|d <d |d <|j|d <t|jj|d<|j|d<tjj|}||d<|j||d<| |d<| r| |d<|jd|d<|jj d}|r|j!}t|dkrddl"}ddl#}|d|d<|dj$dkry"|dj%d}|j&|j'd}Wn|j(t)fk rYn&X|j!d}t|dkr|d|d<|jj ddkr|jj*|d<n|jd|d<|jj d}|r||d<|jj d }|r"||d!<g}xN|jj+d"D]>}|ddd#krZ|j,|j-n||d$dj!d%}q4Wd%j.||d&<|jj d'}|r||d(<t/d|jj0d)g}d*j.|}|r||d+<xdd1|dSy\ytj?|Wnt@k rYnXtjA|jd}"|"d6g|!}!d0| kr|!j,| |jKd7| jL|!y tM|}#WntNtOfk rd}#YnX| jP|!| jQ| jQ| jQ|d8}$|jj$d9kr|#dkr|jd:|'|$jVjW|$jXjW|$jY}(|(r|j>d1|(n |jKd;dS)?NrrrrrzNo such CGI script (%r)z#CGI script is not a plain file (%r)z!CGI script is not executable (%r)ZSERVER_SOFTWAREZ SERVER_NAMEzCGI/1.1ZGATEWAY_INTERFACEZSERVER_PROTOCOLZ SERVER_PORTZREQUEST_METHODZ PATH_INFOZPATH_TRANSLATEDZ SCRIPT_NAME QUERY_STRINGZ REMOTE_ADDR authorizationrZ AUTH_TYPEZbasicascii:Z REMOTE_USERz content-typeZ CONTENT_TYPEzcontent-lengthCONTENT_LENGTHreferer HTTP_REFERERacceptz ,Z HTTP_ACCEPTz user-agentHTTP_USER_AGENTZcookiez, HTTP_COOKIE REMOTE_HOSTzScript output follows+rp=zCGI script exit status %#xzw.exerrz-uz command: %s)stdinstdoutrmenvZpostz%szCGI script exited OK)rrrrrr)Zrrr(rrr.rrrr,rrisfileZ FORBIDDENr have_forkrrdeepcopyenvironr\Zserverrr-r#rrrrrrzr5r6r'base64binasciir7rXZ decodebytesdecodeError UnicodeErrorZget_content_typeZgetallmatchingheadersr`striprerZget_all setdefaultrSrrcrPrrCrDrwaitpidselectr3readrEsetuidrdup2rexecveZ handle_errorZrequest_exit subprocessrlrrrhZ list2cmdliner* TypeErrorr)PopenPIPEZ_sockZrecvZ communicaterZrmrr returncode))rdirrestr.iZnextdirZnextrestZ scriptdirrrZscriptZ scriptnameZ scriptfileZispyrZuqrestrrrZlengthrrlineZuacoZ cookie_strkZ decoded_queryrkrpidstsrZcmdlineZinterpnbytespdatarrmZstatusrrrrs4                                                zCGIHTTPRequestHandler.run_cgiN)rrrr@rrZrbufsizerrrrrrrrrrrrs zHTTP/1.0i@rc Cs||f}||_|||b}|jj}d}t|j|d|ddy |jWn&tk rttdtjdYnXWdQRXdS)Nz>Serving HTTP on {host} port {port} (http://{host}:{port}/) ...rr)rrz& Keyboard interrupt received, exiting.) r-r Z getsocknameprintrjZ serve_foreverKeyboardInterruptrlexit) HandlerClassZ ServerClassZprotocolrbindr ZhttpdZsaZ serve_messagerrrtests   r$__main__z--cgi store_truezRun as CGI Server)actionhelpz--bindz-bZADDRESSz8Specify alternate bind address [default: all interfaces])defaultmetavarr(rZstorerz&Specify alternate port [default: 8000])r'r)typenargsr()r"rr#).r__all__Z email.utilsrtrVZ http.clientr1rrrrr rr r rlrsZ urllib.parserrargparserrrr rZStreamRequestHandlerrrrrrrrr$rArgumentParserparser add_argumentr* parse_argsrkZcgiZ handler_classrr#rrrrSsh  g]0