
    qi78                     $   S r SSKrSSKrSSKJr  SSKJrJr  SSKJ	r	   " S S\R                  5      r " S	 S
\R                  5      r " S S\5      r " S S\5      r\R                  5       r " S S5      r\" 5       R'                  \\\\\S9  g)a  This class holds Cheroot WSGI server implementation.

Simplest example on how to use this server::

    from cheroot import wsgi

    def my_crazy_app(environ, start_response):
        status = '200 OK'
        response_headers = [('Content-type','text/plain')]
        start_response(status, response_headers)
        return [b'Hello world!']

    addr = '0.0.0.0', 8070
    server = wsgi.Server(addr, my_crazy_app)
    server.start()

The Cheroot WSGI server can serve as many WSGI applications
as you want in one instance by using a PathInfoDispatcher::

    path_map = {
        '/': my_crazy_app,
        '/blog': my_blog_app,
    }
    d = wsgi.PathInfoDispatcher(path_map)
    server = wsgi.Server(addr, d)
    N   )server)btonntob)
threadpoolc                      ^  \ rS rSrSrSr            SU 4S jjr\S 5       r\R                  S 5       rSr
U =r$ )	Server$   z8A subclass of HTTPServer which calls a WSGI application.r   r   c           	         > [         [        U ]  U[        U R                     UUUUS9  X l        X`l        Xpl        Xl        [        R                  " U U=(       d    SUU	U
S9U l        g)a	  Initialize WSGI Server instance.

Args:
    bind_addr (tuple): network interface to listen to
    wsgi_app (callable): WSGI application callable
    numthreads (int): number of threads for WSGI thread pool
    server_name (str): web server name to be advertised via
        Server HTTP header
    max (int): maximum number of worker threads
    request_queue_size (int): the 'backlog' arg to
        socket.listen(); max queued connections
    timeout (int): the timeout in seconds for accepted connections
    shutdown_timeout (int): the total time, in seconds, to
        wait for worker threads to cleanly exit
    accepted_queue_size (int): maximum number of active
        requests in queue
    accepted_queue_timeout (int): timeout for putting request
        into queue
)gatewayserver_namepeercreds_enabledpeercreds_resolve_enabled
reuse_portr   )minmaxaccepted_queue_sizeaccepted_queue_timeoutN)superr	   __init__wsgi_gatewayswsgi_versionwsgi_apprequest_queue_sizetimeoutshutdown_timeoutr   
ThreadPoolrequests)self	bind_addrr   
numthreadsr   r   r   r   r   r   r   r   r   r   	__class__s                 A/home/kali/flask_env/lib/python3.13/site-packages/cheroot/wsgi.pyr   Server.__init__*   st    F 	fd$!$"3"34#/&?! 	% 	
 !"4 0"--a 3#9
    c                 .    U R                   R                  $ )zSet minimum number of threads.r   r   r    s    r$   r"   Server.numthreadsa   s     }}   r&   c                 $    XR                   l        g Nr(   )r    values     r$   r"   r*   f   s    !r&   )r   r   r   r   r   )
   N   r.   r0   r/   r.   FFF)__name__
__module____qualname____firstlineno____doc__r   r   propertyr"   setter__static_attributes____classcell__r#   s   @r$   r	   r	   $   se    BL) !"'5
n ! ! " "r&   r	   c                   h   ^  \ rS rSrSrU 4S jr\S 5       rS rS r	SS jr
\S 5       rS	 rS
rU =r$ )Gatewayk   z/A base class to interface HTTPServer with WSGI.c                 r   > [         [        U ]  U5        SU l        U R	                  5       U l        SU l        g)zbInitialize WSGI Gateway instance with request.

Args:
    req (HTTPRequest): current HTTP request
FN)r   r<   r   started_responseget_environenvremaining_bytes_out)r    reqr#   s     r$   r   Gateway.__init__n   s4     	gt%c* %##%#' r&   c                 b    U R                  5        Vs0 s H  oR                  U_M     sn$ s  snf )zCreate a mapping of gateways and their versions.

Returns:
    dict[tuple[int,int],class]: map of gateway version and
        corresponding class

)__subclasses__version)clsgws     r$   gateway_mapGateway.gateway_mapy   s-     *-););)=>)=2

B)=>>>s   ,c                     [         e);Return a new environ dict targeting the given wsgi.version.)NotImplementedErrorr)   s    r$   r@   Gateway.get_environ   s    !!r&   c                    U R                   R                  R                  U R                  U R                  5      n [        SU5       H4  n[        U[        5      (       d  [        S5      eU R                  U5        M6     U R                   R                  5         [        US5      (       a  UR                  5         gg! U R                   R                  5         [        US5      (       a  UR                  5         f f = f)az  Process the current request.

From :pep:`333`:

    The start_response callable must not actually transmit
    the response headers. Instead, it must store them for the
    server or gateway to transmit only after the first
    iteration of the application return value that yields
    a NON-EMPTY string, or upon the application's first
    invocation of the write() callable.
Nz"WSGI Applications must yield bytesclose)rC   r   r   rA   start_responsefilter
isinstancebytes
ValueErrorwriteensure_headers_senthasattrrQ   )r    responsechunks      r$   respondGateway.respond   s     88??++DHHd6I6IJ		!h/!%//$%IJJ

5! 0 HH((*x))  * HH((*x))  *s   AB= =>C;c                 R   U R                   (       a  U(       d  [        S5      eSU l         U R                  R                  (       a  US   nUeU R	                  U5      U R                  l        U H  u  pV[        U[        5      (       d  [        SU-  5      e[        U[        5      (       d  [        SU-  5      eUR                  5       S:X  a  [        U5      U l        [        U5      [        U5      4nU R                  R                  R                  U5        M     U R                  $ )z)WSGI callable to begin the HTTP response.z:WSGI start_response called a second time with no exc_info.Tr   z/WSGI response header key %r is not of type str.z1WSGI response header value %r is not of type str.zcontent-length)r?   RuntimeErrorrC   sent_headers_encode_statusstatusrT   str	TypeErrorlowerintrB   r   
outheadersappendrW   )r    rb   headersexc_infor-   kv
out_headers           r$   rR   Gateway.start_response   s      L  !%
 88  QKEK--f5DAa%%EI  a%%G!K  wwy,,+.q6(a$q')JHH&&z2  zzr&   c                 d    [        U [        5      (       d  [        S5      eU R                  S5      $ )a  Cast status to bytes representation of current Python version.

According to :pep:`3333`, when using Python 3, the response status
and headers must be bytes masquerading as Unicode; that is, they
must be of type "str" but are restricted to code points in the
"Latin-1" set.
z(WSGI response status is not of type str.
ISO-8859-1)rT   rc   rd   encode)rb   s    r$   ra   Gateway._encode_status   s,     &#&&FGG}}\**r&   c                    U R                   (       d  [        S5      e[        U5      nU R                  nUbB  X#:  a=  U R                  R
                  (       d  U R                  R                  SS5        OUSU nU R                  R                  5         U R                  R                  U5        Ub  X2-  nUS:  a  [        S5      egg)zWSGI callable to write unbuffered data to the client.

This method is also used internally by start_response (to write
data from the iterable returned by the WSGI application).
z(WSGI write called before start_response.Nz500 Internal Server ErrorzLThe requested resource returned more bytes than the declared Content-Length.r   z2Response body exceeds the declared Content-Length.)
r?   r_   lenrB   rC   r`   simple_responserX   rW   rV   )r    r[   chunklenrbos       r$   rW   Gateway.write   s     $$IJJu:&&?x~88((((// ds$$&u?OCQw H   r&   )rA   rB   r?   r,   )r1   r2   r3   r4   r5   r   classmethodrJ   r@   r\   rR   staticmethodra   rW   r8   r9   r:   s   @r$   r<   r<   k   sM    9	( ? ?"!0!F 
+ 
+! !r&   r<   c                   "    \ rS rSrSrSrS rSrg)
Gateway_10   z8A Gateway class to interface HTTPServer with WSGI 1.0.x.r   c                    U R                   nUR                  n0 SUR                  R                  _S[	        UR
                  5      _S[	        UR                  5      _SUR                  =(       d    S_S[        UR                  =(       d    S5      _S[	        UR                  5      _S[	        UR                  5      _S	S_S
UR                  R                  _S[	        UR                  5      _SUR                  R                  _S[        R                   _SUR"                  _S[%        UR&                  5      _SS_SS_SS_[	        UR(                  5      U R*                  S.En[-        UR                  R.                  [        5      (       a  SUS'    [        UR0                  5      US'   [        UR2                  5      US'   [        UR4                  5      US'   [        UR6                  5      US'   [        UR8                  5      US'   US   US'   O%[        UR                  R.                  S   5      US'   UR=                  S UR>                  RA                  5        5       5        URC                  SS 5      nUb  XCS!'   URC                  S"S 5      nUb  XSS#'   UR                  RD                  (       a%  UR=                  UR                  RD                  5        U$ ! [:         a     Nf = f)$rM   ACTUAL_SERVER_PROTOCOL	PATH_INFOQUERY_STRINGREMOTE_ADDR REMOTE_PORTREQUEST_METHODREQUEST_URISCRIPT_NAMESERVER_NAMESERVER_PROTOCOLSERVER_SOFTWAREzwsgi.errorsz
wsgi.inputzwsgi.input_terminatedzwsgi.multiprocessFzwsgi.multithreadTzwsgi.run_once)zwsgi.url_schemezwsgi.versionSERVER_PORTX_REMOTE_PIDX_REMOTE_UIDX_REMOTE_GIDX_REMOTE_USERX_REMOTE_GROUPREMOTE_USERr   c              3      #    U  HH  u  pS R                  [        U5      R                  5       R                  SS5      S9[        U5      4v   MJ     g7f)zHTTP_{header_name!s}-_)header_nameN)formatr   upperreplace).0rk   rl   s      r$   	<genexpr>)Gateway_10.get_environ.<locals>.<genexpr>.  sU      
 . '-- $Q 7 7S A .  Q	 .s   AAHTTP_CONTENT_TYPENCONTENT_TYPEHTTP_CONTENT_LENGTHCONTENT_LENGTH)#rC   connr   protocolr   pathqsremote_addrrc   remote_portmethodurir   request_protocolsoftwaresysstderrrfileboolchunked_readschemerG   rT   r!   peer_pidpeer_uidpeer_gid	peer_user
peer_groupr_   update	inheadersitemspopssl_env)r    rC   req_connrA   ctcls         r$   r@   Gateway_10.get_environ   s   hh88
 %cjj&9&9	

 chh
 DL
 8//52
 3x339r:
 d3::.
 4=
 2
 3::11
 tC$8$89
 szz22
  3::!
" #))#
$ $T#*:*:%;%
&  '
( )
* U+
,  $CJJ/ LL/
4 cjj**C00 "$C&)(*;*;&<N#&)(*;*;&<N#&)(*;*;&<N#'*8+=+='>O$(+H,?,?(@$%%(%9M" "%SZZ%9%9!%<!=C 	

 
 ++-
 	
 WW($/>"$WW*D1>$& !88JJsxx''(
?   s   B K 
K+*K+ N)r1   r2   r3   r4   r5   rG   r@   r8   r   r&   r$   r|   r|      s    BGJr&   r|   c                   0   ^  \ rS rSrSrSrU 4S jrSrU =r$ )
Gateway_u0iF  zA Gateway class to interface HTTPServer with WSGI u.0.

WSGI u.0 is an experimental protocol, which uses Unicode for keys
and values in both Python 2 and Python 3.
)ur   c                   > U R                   n[        [        U ]  5       n[	        UR                  5       5      nUR                  SS5      n UR                  R                  U5      US'   UR                  R                  U5      US'   UR                  UR                  5       5        U$ ! [         a    SUS'   US   US'   US   US'    NBf = f)rM   zwsgi.url_encodingzutf-8r   r   rp   )rC   r   r   r@   dictr   
setdefaultr   decoder   UnicodeDecodeErrorr   )r    rC   env_10rA   encr#   s        r$   r@   Gateway_u0.get_environO  s    hhz4466<<>" nn0':	9"xxs3C"%&&--"4C 	

399;
 " 	9'3C#$%k2C"("8C		9s   <B) )C
Cr   )	r1   r2   r3   r4   r5   rG   r@   r8   r9   r:   s   @r$   r   r   F  s     G r&   r   c                   $    \ rS rSrSrS rS rSrg)PathInfoDispatcherih  z6A WSGI dispatcher for dispatch based on the PATH_INFO.c                 *   [         R                  " [        5         [        UR	                  5       5      nSSS5        S nUR                  USS9  U VVs/ s H  u  p4UR                  S5      U4PM     snnU l        g! , (       d  f       NO= fs  snnf )zInitialize path info WSGI app dispatcher.

Args:
    apps (dict[str,object]|list[tuple[str,object]]): URI prefix
        and WSGI app pairs
Nc                     [        U S   5      $ )Nr   )rt   )apps    r$   by_path_len0PathInfoDispatcher.__init__.<locals>.by_path_lenv  s    s1v;r&   T)keyreverse/)
contextlibsuppressAttributeErrorlistr   sortrstripapps)r    r   r   pas        r$   r   PathInfoDispatcher.__init__k  sr       0

%D 1	 			k4	0 599DDAahhsmQ'D9	 10 :s   A>B>
Bc                 (   US   =(       d    SnU R                    Hg  u  pEUR                  U< S35      (       d  X4:X  d  M&  UR                  5       nUR                  SS5      U-   US'   U[	        U5      S US'   U" X5      s  $    U" SSS/5        S/$ )	a7  Process incoming WSGI request.

Ref: :pep:`3333`

Args:
    environ (Mapping): a dict containing WSGI environment variables
    start_response (callable): function, which sets response
        status and headers

Returns:
    list[bytes]: iterable containing bytes to be returned in
        HTTP response body

r   r   r   r   Nz404 Not Found)zContent-Typez
text/plain)zContent-Length0)r   
startswithcopygetrt   )r    environrR   r   r   r   s         r$   __call__PathInfoDispatcher.__call__  s     {#*siiFA!ay))TY!,,.)0]B)G!)K&'+CFH~$733   	.'	
 tr&   )r   N)r1   r2   r3   r4   r5   r   r   r8   r   r&   r$   r   r   h  s    @:(r&   r   )
WSGIServerWSGIGatewayWSGIGateway_u0WSGIGateway_10WSGIPathInfoDispatcher)r5   r   r   r   r   _compatr   r   workersr   
HTTPServerr	   r<   r|   r   rJ   r   r   globalsr   r   r&   r$   <module>r      s   6  
   D"V D"NFfnn FRO Od > ##%6 6t 	  -  r&   