
    $
i                         d Z ddlZddlZddlZddlZddlmZ ddlmZ ddl	m
Z
mZmZmZ ddlmZ ddlZddlmZmZ dZd	Z G d
 dej                  Z G d dej        j                  ZdS )z>An ISAPI extension base class implemented using a thread-pool.    N)
OVERLAPPED)INFINITE)CloseHandleCreateIoCompletionPortGetQueuedCompletionStatusPostQueuedCompletionStatus)SetThreadToken)ExtensionErrorisapicon      c                        e Zd Zd Zd Zd ZdS )WorkerThreadc                 r    d| _         || _        || _        t          j                            | d           d S )NFT)daemon)runningio_req_port	extension	threadingThread__init__)selfr   r   s      C:\Users\Dell Inspiron 16\Desktop\tws\AgrotaPowerBi\back-agrota-powerbi\mcp-client-agrota\venv\Lib\site-packages\isapi/threaded_extension.pyr   zWorkerThread.__init__   sJ    &"!! 	 	" 	
 	
 	
 	
 	
    c                    d| _         | j         ryt          | j        t                    \  }}}}|t          k    r|d S | j        j                            |          }|t          d| d           |||||           | j         wd S d S )NTzBad request '')	r   r   r   r   ISAPI_SHUTDOWNr   dispatch_mapgetRuntimeError)r   errCodebyteskey
overlapped
dispatchers         r   runzWorkerThread.run'   s    l 	8.G (/ /+GUC n$$); 488==J!"#93#9#9#9:::JwsJ777 l 	8 	8 	8 	8 	8r   c                 :    | j                             |           d S N)r   Dispatch)r   cblocks     r   call_handlerzWorkerThread.call_handler7   s    '''''r   N)__name__
__module____qualname__r   r&   r+    r   r   r   r      sA        	
 	
 	
8 8 8 ( ( ( ( (r   r   c                   D    e Zd ZdZdZdZd Zd Zd Zd Z	d Z
d	 Zd
 ZdS )ThreadPoolExtensionz<Base class for an ISAPI extension based around a thread-pool   i:  c                 :    g | _         t          | j        i| _        d S r(   )workersISAPI_REQUESTDispatchConnectionr   )r   s    r   r   zThreadPoolExtension.__init__E   s#     42
r   c                 @   t           j        j                            | |           t	          dd dd          | _        g | _        t          | j                  D ]E}t          | | j                  }|
                                 | j                            |           Fd S )Nr   )isapisimpleSimpleExtensionGetExtensionVersionr   r   r4   rangemax_workersr   startappend)r   viiworkers       r   r<   z'ThreadPoolExtension.GetExtensionVersionO   s    $88rBBB 2"dAqAAt'(( 	( 	(A!$(899FLLNNNL''''	( 	(r   c                 |    t                      }||_        t          | j        dt          |           t
          j        S )Nr   )r   objectr   r   r5   r   HSE_STATUS_PENDING)r   control_blockr$   s      r   HttpExtensionProcz%ThreadPoolExtension.HttpExtensionProc\   s3    \\
)
"4#3QzRRR**r   c                 l   | j         D ]	}d|_        
| j         D ]}t          | j        dt          d            t          j                    | j        dz  z   }| j         }|r:t          j                    |k    rn"t          j        d           d |D             }|:i | _        t          | j                   d S )NFr   i  g?c                 :    g | ]}|                                 |S r/   )is_alive).0ws     r   
<listcomp>z:ThreadPoolExtension.TerminateExtension.<locals>.<listcomp>p   s%    66616Q666r   )
r4   r   r   r   r   timeworker_shutdown_waitsleepr   r   )r   statusrC   end_timealives        r   TerminateExtensionz&ThreadPoolExtension.TerminateExtensionb   s    l 	# 	#F"FNNl 	R 	RF&t'7NDQQQQ 9;;!:T!AA 	7y{{X%%JsOOO66666E  	7 D$%%%%%r   c                    |j         }|                                }t          d |           	 	 |                     |           n#  |                     |           Y nxY wt          d d            d S # t          d d            w xY wr(   )rE   GetImpersonationTokenr	   r)   HandleDispatchError)r   r!   r"   r#   r$   rG   hRequestTokens          r   r6   z&ThreadPoolExtension.DispatchConnectionw   s    ")%;;==t]+++	'8m,,,,8((77777 4&&&&&N4&&&&s   A A2 AA2 2Bc                      t          d          )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 Dispatch)NotImplementedError)r   ecbs     r   r)   zThreadPoolExtension.Dispatch   s     ""HIIIr   c           	      &   t           j        |_        t          j                    \  }}}d}	 	 ddl}|                    ddd           t          |           t          d|           t          j	        ||          t          j
        ||          z   }|                                }t          d                    |                    d	                    |                    |                    |                    |           nY# t          $ r Y nM t          d
           t          j                     t          d           t          j        |||           Y nxY wd}|                                 dS # d}|                                 w xY w)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   r\   exc_typexc_valexc_tblimitrd   listbolds	            r   rX   z'ThreadPoolExtension.HandleDispatchError   s    &6#&<>> &!	"D''?   3C#NNNN *E 3GWEEF xxzz,33BGGDMM22D))       "    D;<<<#%%%1222)'7FCCCCC F!!!!! F!!!!s+   CD E8 
EE8 AEE8 8FN)r,   r-   r.   __doc__r>   rP   r   r<   rH   rU   r6   r)   rX   r/   r   r   r1   r1   ?   s        BBK 
 
 
( ( (+ + +& & &*' ' 'J J J ," ," ," ," ,"r   r1   )rw   rb   r   rO   rg   
pywintypesr   
win32eventr   	win32filer   r   r   r   win32securityr	   isapi.simpler9   r
   r   r5   r   r   r   r:   r;   r1   r/   r   r   <module>r}      sJ   D D 


          ! ! ! ! ! !                  ) ( ( ( ( (     * * * * * * * *( ( ( ( (9# ( ( (HB" B" B" B" B"%,6 B" B" B" B" B"r   