
    	h                         S r SSKrSSKrSSKrSSKrSSKJr  SSKJr  SSK	J
r
JrJrJr  SSKJr  SSKrSSKJrJr  SrS	r " S
 S\R,                  5      r " S S\R0                  R2                  5      rg)z>An ISAPI extension base class implemented using a thread-pool.    N)
OVERLAPPED)INFINITE)CloseHandleCreateIoCompletionPortGetQueuedCompletionStatusPostQueuedCompletionStatus)SetThreadToken)ExtensionErrorisapicon      c                   &    \ rS rSrS rS rS rSrg)WorkerThread   c                 f    SU l         X l        Xl        [        R                  R                  U SS9  g )NFT)daemon)runningio_req_port	extension	threadingThread__init__)selfr   r   s      kC:\Users\julio\OneDrive\Documentos\Trabajo\Ideas Frescas\venv\Lib\site-packages\isapi/threaded_extension.pyr   WorkerThread.__init__   s5    &"!! 	 	" 	
    c                 2   SU l         U R                   (       a  [        U R                  [        5      u  pp4U[        :X  a  Uc  g U R
                  R                  R                  U5      nUc  [        SU S35      eU" XX45        U R                   (       a  M~  g g )NTzBad request '')	r   r   r   r   ISAPI_SHUTDOWNr   dispatch_mapgetRuntimeError)r   errCodebyteskey
overlapped
dispatchers         r   runWorkerThread.run'   s    ll.G  (/+GC n$); 4488=J!"]3%q#9::ws7 lllr   c                 :    U R                   R                  U5        g N)r   Dispatch)r   cblocks     r   call_handlerWorkerThread.call_handler7   s    'r   )r   r   r   N)__name__
__module____qualname____firstlineno__r   r(   r.   __static_attributes__ r   r   r   r      s    	
8 (r   r   c                   J    \ rS rSrSrSrSrS rS rS r	S r
S	 rS
 rS rSrg)ThreadPoolExtension?   z<Base class for an ISAPI extension based around a thread-pool   i:  c                 @    / U l         [        U R                  0U l        g r+   )workersISAPI_REQUESTDispatchConnectionr    )r   s    r   r   ThreadPoolExtension.__init__E   s      422
r   c                 B   [         R                  R                  R                  X5        [	        SS SS5      U l        / U l        [        U R                  5       HC  n[        X R
                  5      nUR                  5         U R                  R                  U5        ME     g )Nr   )isapisimpleSimpleExtensionGetExtensionVersionr   r   r;   rangemax_workersr   startappend)r   viiworkers       r   rD   'ThreadPoolExtension.GetExtensionVersionO   sv    $$88B 2"dAqAt''(A!$(8(89FLLNLL' )r   c                 z    [        5       nXl        [        U R                  S[        U5        [
        R                  $ )Nr   )r   objectr   r   r<   r   HSE_STATUS_PENDING)r   control_blockr&   s      r   HttpExtensionProc%ThreadPoolExtension.HttpExtensionProc\   s/    \
)"4#3#3QzR***r   c                 
   U R                    H
  nSUl        M     U R                    H  n[        U R                  S[        S 5        M!     [
        R
                  " 5       U R                  S-  -   nU R                   nU(       aa  [
        R
                  " 5       U:  a  OG[
        R                  " S5        U Vs/ s H  oUR                  5       (       d  M  UPM     nnU(       a  Ma  0 U l	        [        U R                  5        g s  snf )NFr   i  g?)r;   r   r   r   r   timeworker_shutdown_waitsleepis_aliver    r   )r   statusrK   end_timealivews         r   TerminateExtension&ThreadPoolExtension.TerminateExtensionb   s    llF"FN #llF&t'7'7NDQ # 99;!:!:T!AAyy{X%JJsO %61QE6 e D$$% 7s   6D D c                     UR                   nUR                  5       n[        S U5          U R                  U5        [        S S 5        g !   U R	                  U5         N"= f! [        S S 5        f = fr+   )rN   GetImpersonationTokenr	   r,   HandleDispatchError)r   r#   r$   r%   r&   rP   hRequestTokens          r   r=   &ThreadPoolExtension.DispatchConnectionw   si    "))%;;=t]+	'8m,
 4&	8((7 4&s   A	 	AA! !A/c                     [        S5      e)aT  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 Dispatch)NotImplementedError)r   ecbs     r   r,   ThreadPoolExtension.Dispatch   s     ""HIIr   c           	         [         R                  Ul        [        R                  " 5       u  p#nSn  SSKnUR                  SSS5        [        US9  [        SUS9  [        R                  " XE5      [        R                  " X#5      -   nUR                  5       n[        SR                  UR                  S	R                  U5      5      UR                  U5      5      US9  SnUR%                  5         g! [         a     N  [        S
5        [        R                   " 5         [        S5        [        R"                  " X#U5         Nd= f! SnUR%                  5         f = f)zHandles 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.
Nr   z200 OKzContent-type: text/html

F)filez+<H3>Traceback (most recent call last):</H3>z<PRE>{}<B>{}</B></PRE> z#FAILED to render the error message!zORIGINAL extension error:)r   HSE_STATUS_ERRORHttpStatusCodesysexc_infohtmlSendResponseHeadersprint	traceback	format_tbformat_exception_onlypopformatescapejoinr
   	print_excprint_exceptionDoneWithSession)	r   re   exc_typexc_valexc_tblimitrn   listbolds	            r   r`   'ThreadPoolExtension.HandleDispatchError   s:    &66#&<<> &!	"D''? 3C#N **33GEF xxz,33BGGDM2D) & F! "  D;<##%12))'FC F!s%   B.C3 3
E=E ?AEE E)r    r   r;   N)r0   r1   r2   r3   __doc__rF   rU   r   rD   rQ   r\   r=   r,   r`   r4   r5   r   r   r7   r7   ?   s4    BK 
(+&*'J ,"r   r7   )r   rl   r   rT   rq   
pywintypesr   
win32eventr   	win32filer   r   r   r   win32securityr	   isapi.simplerA   r
   r   r<   r   r   r   rB   rC   r7   r5   r   r   <module>r      sf    D     !   )  *(9## (HB"%,,66 B"r   