o k`&@sddlmZmZddlZddlZddlmZmZddlm Z ddl Z zddlm Z Wn e y5dZ YnwdZdZdd ZGd d d eZGd d d eZGdddeedZGdddeZGdddeZGdddeZdS))ABCMetaabstractmethodN) HTTPServerBaseHTTPRequestHandler)ThreadingMixIn) ForkingMixIniE'iF'cCs|jtjtjttdfvS)zrDetermines whether the exception was caused by a disconnecting client. :type e: IOError :rtype: bool N)errnoEPIPE ECONNRESET_WSAECONNABORTED_WSAECONNRESET)er//usr/lib/python3/dist-packages/trac/web/wsgi.pyis_client_disconnect_exception#s rc@s,eZdZddZddZddZddZd S) _ErrorsWrappercCs ||_dSNlogfunc)selfrrrr__init__0s z_ErrorsWrapper.__init__cCsdSrrrrrrflush3z_ErrorsWrapper.flushcCs||dSrr)rmsgrrrwrite6sz_ErrorsWrapper.writecCs|D]}||qdSr)r)rseqitemrrr writelines9s z_ErrorsWrapper.writelinesN)__name__ __module__ __qualname__rrrrrrrrr.s  rc@s.eZdZdZd ddZddZddZeZdS) _FileWrapperz'Wrapper for sending a file as response.NcCs0||_||_|jj|_t|dr|j|_dSdS)Nclose)fileobj blocksizereadhasattrr#)rr$r%rrrrAs    z_FileWrapper.__init__cCs|Srrrrrr__iter__Hrz_FileWrapper.__iter__cCs|j|j}|s t|Sr)r$r&r% StopIterationrdatarrr__next__Ksz_FileWrapper.__next__r)rr r!__doc__rr(r,nextrrrrr">s  r"c@sTeZdZdZdZdZdZdZeZ e j e j fddZ ddZdd d Zed d Zd S) WSGIGatewayz1Abstract base class for WSGI servers or gateways.)rTFcCs|j|d<d|d<|dddvrd|d<n|dddkr&d|d<||d <||d <|j|d <|j|d <|j|d <|jdurG|j|d<||_g|_g|_ d|_ dS)zInitialize the gateway object.z wsgi.versionhttpzwsgi.url_schemeHTTPS)yeson1httpsHTTP_X_FORWARDED_PROTOz wsgi.inputz wsgi.errorszwsgi.multithreadzwsgi.multiprocessz wsgi.run_onceNzwsgi.file_wrapperF) wsgi_versiongetlowerwsgi_multithreadwsgi_multiprocess wsgi_run_oncewsgi_file_wrapperenviron headers_set headers_sent use_chunked)rr@stdinstderrrrrr]s"        zWSGIGateway.__init__c Cs||j|j}z<|jdurt||jrt|dr||jn|D] }|r*||q!|jr1|j r6|dWt|drB| dSdSt|drN| ww)z2Start the gateway with the given WSGI application.N _sendfiler#) r@_start_responser? isinstancer'rFr$_writerBrCr#)r applicationresponsechunkrrrrunrs(        zWSGIGateway.runNcCs@|rz |jr |dWd}n d}w|jrJd||g|_|jS)z&Callback for starting a HTTP response.r0NzResponse already started)rBrArJ)rstatusheadersexc_inforrrrHs  zWSGIGateway._start_responsecCsdS)zcCallback for writing data to the response. Concrete subclasses must implement this method.Nrr*rrrrJszWSGIGateway._writer)rr r!r-r9r<r=r>r"r?sysrDrErrNrHrrJrrrrr/Ts r/) metaclassc@s$eZdZddZddZddZdS)WSGIRequestHandlercCsP|j|_|jjs|jr|sd|_dS|jj}|j |d<|j |d<d|j vr5|j dd\}}n|j d}}t j|d|d<||d<|}||jd krW||d <|jd |d <|jd |d <|jd}|rr||d<|jD].\}}|dd}|}||vrqwd||vr|d|d|7<qw||d|<qw|S)Nr0SERVER_PROTOCOLREQUEST_METHOD?r3z iso-8859-1 PATH_INFO QUERY_STRINGr REMOTE_HOST REMOTE_ADDRz content-type CONTENT_TYPEcontent-lengthCONTENT_LENGTH-_HTTP_,)rfilereadlineraw_requestlineclosed parse_requestclose_connectionserverr@copyrequest_versioncommandpathspliturllibparseunquoteaddress_stringclient_addressrPr:itemsreplaceupperstrip)rr@ path_info query_stringhostlengthnamevaluerrr setup_environsB        z WSGIRequestHandler.setup_environc Cspz|}Wnty#}zd}t|rd|_nWYd}~nd}~ww|r6|j||}||jjdSdS)Nr0)r~IOErrorrrhrigatewayrNrK)rr@r rrrrhandle_one_requests  z%WSGIRequestHandler.handle_one_requestc Csrz1zt|Wnty}z t|sWYd}~nd}~wwWd|_d|_dSWd|_d|_dSd|_d|_w)z/We need to help the garbage collector a little.N)rfinishrrwfilerc)rr rrrrs    zWSGIRequestHandler.finishN)rr r!r~rrrrrrrTs- rTc@seZdZddZddZdS)WSGIServerGatewayc s(t||jtfdd|_dS)Ncs d|S)Nz%s) log_error)xhandlerrrs z,WSGIServerGateway.__init__..)r/rrcrr)rrr@rrrrs  zWSGIServerGateway.__init__c CsL|jsJd|jjjrdSzz|jsl|j\}}|_tdd|Dr'd|_n|jddko3|jjdk|_|jr>| dn| d|j t |dd |D]\}}t |t r_t|d }|j||qQ|j|jr|jjd t||fWdS|jj|WdSty}zt|rd |j_nWYd}~dSd}~ww) NzResponse not startedcss |] \}}|dkVqdS)r]N)r;).0nvrrr sz+WSGIServerGateway._write..FrUzHTTP/1.1)zTransfer-Encodingchunked) Connectionr#zutf-8s%x %s r0)rArrrfrBanyrCr@protocol_versionappend send_responseintrIbytesstr send_header end_headersrlenrrrh)rr+rOrPr|r}r rrrrJs<          zWSGIServerGateway._writeN)rr r!rrJrrrrrs rc@seZdZeefddZdS) WSGIServercCsTt|||||_t|t|_ttot|t|_||_ |j t |j dd|_ dS)Nr3) SERVER_NAME SERVER_PORT SCRIPT_NAME)rrrKrIrr<boolrr=r server_namer server_portr@)rserver_addressrKrrequest_handlerrrrrs  zWSGIServer.__init__N)rr r!rrTrrrrrrsr)abcrrrrR http.serverrr socketserverr urllib.parseror ImportErrorr r robjectrr"r/rTrrrrrrs(   FJ,