a „oe�ã@sªdZddlZddlZddlZddlZddlmZddlmZddl m Z m Z m Z m Z ddlmZddlZddlmZmZdZd ZGd d „d ejƒZGd d „d ejjƒZdS)z>An ISAPI extension base class implemented using a thread-pool.éN)Ú OVERLAPPED)ÚINFINITE)Ú CloseHandleÚCreateIoCompletionPortÚGetQueuedCompletionStatusÚPostQueuedCompletionStatus)ÚSetThreadToken)ÚExtensionErrorÚisapiconééc@s$eZdZdd„Zdd„Zdd„ZdS)Ú WorkerThreadcCs,d|_||_||_tj |¡| d¡dS)NFT)ÚrunningÚ io_req_portÚ extensionÚ threadingÚThreadÚ__init__Ú setDaemon)Úselfrr©rú9C:\Program Files\Certbot\pkgs\isapi\threaded_extension.pyrs  zWorkerThread.__init__cCsjd|_|jrft|jtƒ\}}}}|tkr2|dur2qf|jj |¡}|durVtd|fƒ‚|||||ƒqdS)NTzBad request '%s') rrrrÚISAPI_SHUTDOWNrÚ dispatch_mapÚgetÚ RuntimeError)rÚerrCodeÚbytesÚkeyÚ overlappedÚ dispatcherrrrÚrun$sÿ zWorkerThread.runcCs|j |¡dS©N)rÚDispatch)rZcblockrrrÚ call_handler4szWorkerThread.call_handlerN)Ú__name__Ú __module__Ú __qualname__rr!r$rrrrr s r c@sPeZdZdZdZdZdd„Zdd„Zdd „Zd d „Z d d „Z dd„Z dd„Z dS)ÚThreadPoolExtensionzlóz:ThreadPoolExtension.TerminateExtension..) r*rrrrÚtimeÚworker_shutdown_waitÚsleeprr)rÚstatusr8Zend_timeÚaliverrrÚTerminateExtension^s    z&ThreadPoolExtension.TerminateExtensionc Cs^|j}| ¡}td|ƒz4z| |¡Wn| |¡Yn0Wtddƒn tddƒ0dSr")r9ZGetImpersonationTokenrr#ÚHandleDispatchError)rrrrrr:Z hRequestTokenrrrr,ss z&ThreadPoolExtension.DispatchConnectioncCs tdƒ‚dS)aœOverridden by the sub-class to handle connection requests. This class creates a thread-pool using a Windows completion port, and dispatches requests via this port. Sub-classes can generally implement each connection request using blocking reads and writes, and the thread-pool will still provide decent response to the end user. The sub-class can set a max_workers attribute (default is 20). Note that this generally does *not* mean 20 threads will all be concurrently running, via the magic of Windows completion ports. There is no default implementation - sub-classes must implement this. z$sub-classes should override DispatchN)ÚNotImplementedError)rÚecbrrrr#�szThreadPoolExtension.Dispatchc Csütj|_t ¡\}}}d}zÎzzddl}| ddd¡t|d�td|d�t  ||¡t  ||¡}td|  d   |dd …¡¡|  |d ¡f|d�WnBt y¨Yn2td ƒt ¡td ƒt |||¡Yn0Wd}| ¡nd}| ¡0dS) zÜHandles errors in the Dispatch method. When a Dispatch method call fails, this method is called to handle the exception. The default implementation formats the traceback in the browser. Nrz200 OKzContent-type: text/html F)Úfilez+

Traceback (most recent call last):

z
%s%s
Úr-z#FAILED to render the error message!zORIGINAL extension error:)r ZHSE_STATUS_ERRORZHttpStatusCodeÚsysÚexc_infoÚcgiZSendResponseHeadersÚprintÚ tracebackÚ format_tbÚformat_exception_onlyÚescapeÚjoinr Ú print_excÚprint_exceptionZDoneWithSession)rrIZexc_typZexc_valZexc_tbÚlimitrNÚlistrrrrG‘sFÿ  ÿ þ þÿú   ÿz'ThreadPoolExtension.HandleDispatchErrorN) r%r&r'Ú__doc__r3rBrr1r;rFr,r#rGrrrrr(<s  r()rYrLrrArPZ pywintypesrZ win32eventrZ win32filerrrrZ win32securityrZ isapi.simpler.r r r+rrr r/r0r(rrrrÚs   "