
    qiH                        S r SSKrSSKrSSKrSSKrSSKrSSKrSSK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rSSKrSSKrSSKJr  SSKJrJrJrJrJrJr  SSKJrJrJr  SSKJ r J!r!J"r"J#r#  SS	K$J%r%  \=(       d    \r&\RN                  SS S
:*  r(\RR                  RU                  \+" \S5      (       + SS9r,\RR                  RU                  \SS9r-\R\                  " SS9S 5       r/\R\                  S 5       r0\R\                  S 5       r1S r2S r3\RR                  Ri                  S\5\6\7\845      S 5       r9S r:\RR                  Ri                  S\ \!45      S 5       r;\,S 5       r<\,S 5       r=Sr>Sr? " S S \5      r@\R\                  S! 5       rA\,\-\RR                  R                  S"SS#9S$ 5       5       5       rC\RR                  RU                  \(       + S%S9\,\-S& 5       5       5       rD\RR                  RU                  \S'S9\RR                  Ri                  S(S)S*S+9\RR                  R                  S,5      S- 5       5       5       rF\RR                  RU                  \+" \S.5      (       + S/S9\RR                  Ri                  S\ \!45      S0 5       5       rG\R\                  S1 5       rH\R\                  S2 5       rI\R\                  S3 5       rJ\RR                  Ri                  S4S5S6\K" S75      4S5S8\K" S75      4S9S:S5\K" S75      \K" S75      4SS6\K" S75      4SS8\K" S75      4S;S\K" S75      \K" S75      44	5      S< 5       rL\RR                  Ri                  S=S>5      S? 5       rMS@ rNSA rOSB rPg)CzTests for the HTTP server.    N)
HTTPStatus)	DefaultGc   )IS_LINUXIS_MACOS
IS_WINDOWSSYS_PLATFORMbtonntob)IS_UID_GID_RESOLVABLEGateway
HTTPServer)ANY_INTERFACE_IPV4ANY_INTERFACE_IPV6EPHEMERAL_PORTSUCCESSFUL_SUBPROCESS_EXIT)
ThreadPool)      AF_UNIXz:UNIX domain sockets are only available under UNIX-based OSreasonz9Peercreds lookup does not work under macOS/BSD currently.)abstractfile)paramsc                     [         (       a  [        R                  " S5        SR                  " S0 [	        5       D6nU R                  U5      $ )9Check that bound UNIX socket address is stored in server.z1`requests-unixsocket` is defunct under Python 3.8zunix_{request.param}_sock )PY38_OR_LOWERpytestskipformatlocalsgetfixturevalue)requestnames     M/home/kali/flask_env/lib/python3.13/site-packages/cheroot/test/test_server.pyunix_sock_filer(   1   s>     } 	GH&--99D""4((    c            	          [         (       d  [        R                  " [         S35        SR	                  S[        [        [        R                  " 5       5      5      45      R                  5       $ )z'Return an abstract UNIX socket address.z. does not support an abstract socket namespacer)   s    cheroot-test-socket)
r   r    r!   r	   joinr   struuiduuid4decoder   r)   r'   unix_abstract_sockr0   =   sW     8nJK	
 88&TZZ\"#	

 fhr)   c               #      #    [         R                  " 5       u  pUv   [        R                  " U 5        [        R                  " U5        g7f)zYield a UNIX file socket.N)tempfilemkstemposcloseunlink)tmp_sock_fhtmp_sock_fnames     r'   unix_file_sockr9   L   s6      #+"2"2"4K
HH[IIns   A	Ac                     [        [        [        4[        S9n U R                  (       a   eU R
                  R                  (       a   eU R                  5         U R                  (       d   eU R
                  R                  (       d   eU R
                  R                   H  nUR                  (       a  M   e   U R                  5         U R
                  R                  (       a   eU R                  (       a   eg)zBCheck that prepare() makes the server ready, and stop() clears it.	bind_addrgatewayN)	r   r   r   r   readyrequests_threadspreparestop)
httpserverthrs     r'   test_prepare_makes_server_readyrE   W   s    %~6J
 ""++++''''""++yyyy , OO""++++r)   c                  p   [        [        [        4[        S9n U R	                  5         [
        R                  " U R                  S9nUR                  5         UR                  S5        UR                  5       (       d   eU R                  5         UR                  S5        UR                  5       (       a   eg)0Check that stop() interrupts running of serve().r;   target      ?N)r   r   r   r   rA   	threadingThreadservestartr+   is_aliverB   )rC   serve_threads     r'   test_stop_interrupts_serverQ   n   s    %~6J
 ##:+;+;<Lc  """"OOc$$&&&&&r)   exc_clsc                   ^ ^^^^ S[         R                  " 5       < 3m[        5       m[        [        [
        4[        S9m[        R                  " 5       mU UUUU4S jnTR                  5         [        R                  " US9nUR                  5         UR                  S5        UR                  5       (       d   eT " T5      Tl        UR                  S5        UR                  5       (       a   eTR!                  5       TL d   eg)z0Check that assigning interrupt stops the server.zshould catch r;   c                     >  TR                  5         g ! T a/  n [        U 5      T:X  a  TR                  T5         S n A g  S n A g S n A ff = fN)rM   r,   put)erR   rC   interrupt_msgraise_marker_sentinelresult_qs    r'   rP   +test_server_interrupt.<locals>.serve_thread   sE    	4 	41v&233 '	4s    A	 AA	rH   rJ   N)r-   r.   objectr   r   r   r   queueQueuerA   rK   rL   rN   r+   rO   	interrupt
get_nowait)rR   rP   rC   rX   rY   rZ   s   ` @@@@r'   test_server_interruptra      s     $DJJL#34M"H%~6J
 {{}H4 4 ##<8Lc  """" #=1Jc$$&&&& $9999r)   c                     [        [        [        4[        S9n U R	                  5         S nXR
                  R                  l        [        R                  " U R                  S9nUR                  5         UR                  U R                  [        (       a  SOS-  5        UR                  5       (       a   eU R
                  R                   (       a   eU R#                  5         g)rG   r;   c                      [         erU   )KeyboardInterrupt)argskwargss     r'   raise_keyboard_interruptTtest_serving_is_false_and_stop_returns_after_ctrlc.<locals>.raise_keyboard_interrupt   s    r)   rH      r   N)r   r   r   r   rA   _connections	_selectorselectrK   rL   rM   rN   r+   expiration_intervalIS_SLOW_ENVrO   _servingrB   )rC   rg   rP   s      r'   2test_serving_is_false_and_stop_returns_after_ctrlcrp      s    %~6J
   0H%%,##:+;+;<L &&{{!B $$&&&&&&////OOr)   ip_addrc                     U R                  U[        45      nUR                  S   U:X  d   eUR                  S   [        :w  d   eg)z0Check that bound IP address is stored in server.r      N)sendr   r<   )http_serverrq   rC   s      r'   test_bind_addr_inetrv      sK     !!7N";<J"g---"n444r)   c                 J    U R                  U5      nUR                  U:X  d   eg)r   Nrt   r<   ru   r(   rC   s      r'   test_bind_addr_unixrz      s(     !!.1J>111r)   c                 J    U R                  U5      nUR                  U:X  d   eg)zBCheck that bound UNIX abstract socket address is stored in server.Nrx   )ru   r0   rC   s      r'   test_bind_addr_unix_abstractr|      s*     !!"45J#5555r)   z/peer_creds/idsz/peer_creds/textsc                   .   ^  \ rS rSrU 4S jrS rSrU =r$ )_TestGateway   c                   > U R                   nUR                  n[        UR                  5      nU[        :X  aR  UR
                  UR                  UR                  4nU R                  SR                  [        [        U5      5      5        g U[        :X  a7  U R                  SR                  UR                  UR                  45      5        g [        [         U ]G  5       $ )N|!)reqconnr
   uriPEERCRED_IDS_URIpeer_pidpeer_uidpeer_gidsend_payloadr+   mapr,   PEERCRED_TEXTS_URI	peer_user
peer_groupsuperr~   respond)selfr   r   req_uri
peer_creds	__class__s        r'   r   _TestGateway.respond   s    hhxxsww-&&t}}DJchhs3
';<=((chh'HIJ\4022r)   c                 ~    U R                   nSUl        UR                  5         UR                  [	        U5      5        g )Ns   200 OK)r   statusensure_headers_sentwriter   )r   payloadr   s      r'   r   _TestGateway.send_payload   s.    hh
!		$w- r)   r   )__name__
__module____qualname____firstlineno__r   r   __static_attributes____classcell__)r   s   @r'   r~   r~      s    3! !r)   r~   c                 L    U R                  U5      n[        Ul        SUl        U$ )z3Construct a test server with ``peercreds_enabled``.T)rt   r~   r=   peercreds_enabledry   s      r'   peercreds_enabled_serverr     s*     !!.1J%J#'J r)   r   )rerunsreruns_delayc                    UnUR                   n[        U[        5      (       a  UR                  5       n[        R
                  R                  USS9nSR                  " S0 [        5       D6n[        R                  " 5       [        R                  " 5       [        R                  " 5       4nSR                  [        [        U5      5      n[         R"                  " 5          [$        R&                  " U[(        -   U S9nUR+                  5         UR,                  U:X  d   e[$        R&                  " U[.        -   U S9nUR0                  S:X  d   e SSS5        g! , (       d  f       g= f)	z2Check that ``PEERCRED`` lookup works when enabled. safehttp+unix://{quoted}r   timeouti  Nr   )r<   
isinstancebytesr/   urllibparsequoter"   r#   r4   getpidgetuidgetgidr+   r   r,   requests_unixsocketmonkeypatchr?   getr   raise_for_statustextr   status_code)	http_request_timeoutr   rC   r<   quotedunix_base_uriexpected_peercredspeercreds_resppeercreds_text_resps	            r'   test_peercreds_unix_sockr     s   
 *J$$I)U##$$&	 \\	3F*11=FH=Mbiik299;>#c+=">?		(	(	*!,,(
 	'')""&8888&ll..(
 #..#555 
+	*	*s   A-E
E#zDModules `grp` and `pwd` are not available under the current platformc                    UnSUl         UR                  n[        U[        5      (       a  UR	                  5       n[
        R                  R                  USS9nSR                  " S	0 [        5       D6nSSK
nSSKnUR                  [        R                  " 5       5      R                  UR!                  [        R"                  " 5       5      R$                  4nSR'                  [)        [*        U5      5      n[,        R.                  " 5          [0        R2                  " U[4        -   U S9n	U	R7                  5         U	R8                  U:X  d   e SSS5        g! , (       d  f       g= f)
z6Check that ``PEERCRED`` resolution works when enabled.Tr   r   r   r   Nr   r   r   )peercreds_resolve_enabledr<   r   r   r/   r   r   r   r"   r#   grppwdgetpwuidr4   r   pw_namegetgrgidr   gr_namer+   r   r,   r   r   r?   r   r   r   r   )
r   r   rC   r<   r   r   r   r   expected_textcredsr   s
             r'   $test_peercreds_unix_sock_with_lookupr   -  s    *J+/J($$I)U##$$&	 \\	3F*11=FH=M 	RYY[!))RYY[!)) #c+=">?		(	(	*&ll..(
 	,,."''+==== 
+	*	*s   ?E
E"z\This regression test is for a Linux bug, and the resource module is not available on Windowsresource_limit)i   i   )r   )indirectmany_open_socketsc                 (  ^^^ U R                   R                  mUU4S jm[        5       Tl        TU R                   l        U R	                  S5        [        TR                  5      S:  d   e[        U4S jTR                   5       5      (       d   eg)a*  Test the server does not crash with a high file-descriptor value.

This test shouldn't cause a server crash when trying to access
file-descriptor higher than 1024.

The earlier implementation used to rely on ``select()`` syscall that
doesn't support file descriptors with numbers higher than 1024.
c                 z   > TR                   R                  U R                  R                  5       5        T" U 5      $ rU   )filenosaddsocketfileno)r   _old_process_connnative_process_conns    r'   r   Atest_high_number_of_file_descriptors.<locals>.native_process_conns  s/    ##''(:(:(<= &&r)   /r   c              3   ,   >#    U  H	  oT:  v   M     g 7frU   r   ).0fnr   s     r'   	<genexpr>7test_high_number_of_file_descriptors.<locals>.<genexpr>  s     J.I^#.Is   N)server_instanceprocess_connsetr   connectlenany)native_server_clientr   r   r   s    `@@r'   $test_high_number_of_file_descriptorsr   V  s    6 -<<II' #&%8K((5   % "**+a/// J.A.I.IJJJJJr)   SO_REUSEPORTz5socket.SO_REUSEPORT is not supported on this platformc                    [         R                  " U[        5      S   S   n[         R                   " U5      nUR                  [         R                  [         R
                  S5        UR                  U[        45        [        UR                  5       SS [        SS9nUR                  US5      nUR                  5         UR                  5         UR                  5         UR                  b   eg)z5Check that port initialized externally can be reused.r   rs   Nr   T)r<   r=   
reuse_portrA   )r   getaddrinfor   
setsockopt
SOL_SOCKETr   bindr   getsocknamer   spyrA   rB   r5   spy_exception)ru   rq   mockerfamilysserverr   s          r'   test_reuse_portr     s     8;A>FfALL""F$7$7;FFG^$%--/"1%F
 **VY
'C
NN
KKMGGI$$$r)   c               #   x   #    [        5       R                  5          Sv   SSS5        g! , (       d  f       g= f7f)z7Disable garbage collection when this fixture is in use.N)r   nogcr   r)   r'   _garbage_binr     s#      
			 
		s   :)	:
7:c              #   b  #    [         R                  " SSS9nUR                  UR                  5      u  p#UR	                  UR                  U R
                  S-  U45         U R
                  v   UR	                  UR                  X#45        g! UR	                  UR                  X#45        f = f7f)z7Set the resource limit two times bigger then requested.resourcez&The "resource" module is Unix-specificr   r   N)r    importorskip	getrlimitRLIMIT_NOFILE	setrlimitparam)r%   r   
soft_limit
hard_limits       r'   r   r     s      ""7H &//0F0FGJ
 		J'
Mmm 	811J3KL811J3KLs   AB/!B /B/B,,B/c              #   z  #    U R                  S5        / n [        U5       H?  n[        R                  " 5       nUR                  U5        UR	                  5       U:  d  M?    O   US   R	                  5       nXQ:  d   eUv   U H  nUR                  5         M     g! U H  nUR                  5         M     f = f7f)z<Allocate a lot of file descriptors by opening dummy sockets.r   N)r$   ranger   appendr   r5   )r%   r   test_sockets_sockthe_highest_filenotest_sockets          r'   r   r     s      N+ L ~&A==?D%{{}. ' *"-446!333   (K (<K (s#   B;AB !"B B;B88B;)
minthreads
maxthreadsinited_maxthreadsrs   infr  )rs   rs   rs   )rs   r   r   )r   r   r   c                 b    [        SU US9nUR                  U :X  d   eUR                  U:X  d   eg)zTest setting the number of threads in a ThreadPool.

The ThreadPool should properly set the min+max number of the threads to use
in the pool if those limits are valid.
Nr   minmax)r   r  r  )r  r  r  tps       r'   test_threadpool_threadrange_setr    s>    4 

B
 66Z66&&&&r)   r  r  error))r  r  min=-1 must be > 0)r  r   r  )r  rs   r  )r  r   r  )r   r  min=0 must be > 0)r   r   r  )r   rs   r  )r   r   r  )rs   r   KExpected an integer or the infinity value for the `max` argument but got 0.)rs   rJ   zMExpected an integer or the infinity value for the `max` argument but got 0.5.)r   r   r  )r   1zMExpected an integer or the infinity value for the `max` argument but got '1'.)r   rs   zmax=1 must be > min=2c                     [         R                  " [        [        4US9   [	        SU US9  SSS5        g! , (       d  f       g= f)zTest that a ThreadPool rejects invalid min/max values.

The ThreadPool should raise an error with the proper message when
initialized with an invalid min+max number of threads.
matchNr  )r    raises
ValueError	TypeErrorr   r  s      r'   #test_threadpool_invalid_threadranger"    s5    P 

I.e	<	
 
=	<	<s	   5
Ac                     U R                  [        SS 5        [        SS9nUR                  5         [        R                  " [
        SS9   UR                  5         SSS5        g! , (       d  f       g= f)zTest for ThreadPool multi-start behavior.

Tests that when calling start() on a ThreadPool multiple times raises a
:exc:`RuntimeError`
_spawn_workerc                 *    [         R                  " SS9$ )NT)r>   )typesSimpleNamespace)r  s    r'   <lambda>7test_threadpool_multistart_validation.<locals>.<lambda>>  s    %''d3r)   N)r   z%Threadpools can only be started once.r  )setattrr   rN   r    r  RuntimeError)r   r  s     r'   %test_threadpool_multistart_validationr,  3  s]     3 
4	 BHHJ	5
 	
	
 
 
s   A%%
A3c                    Sn[        U[        4[        S9n[        SSSSUS9Ul        UR                  5         [        R                  " UR                  S9nUR                  5         U R                  UR                  5        UR                  R                  5         UR                  u  pEUR                  R                  R                  S5        [        R                  " SU S	U 3S
S9nUR                   ["        R$                  :X  d   eg)zCA server that can't keep up with requests returns a 503 HTTP error.z	127.0.0.1r;   rs   r   )r  r  accepted_queue_sizeaccepted_queue_timeoutr   rH   Nzhttp://:   r   )r   r   r   r   r?   rA   rK   rL   rM   rN   addfinalizerrB   r<   _queuerV   r   r   r   SERVICE_UNAVAILABLE)r%   	localhostrC   rP   _hostportresponses          r'   ,test_overload_results_in_suitable_http_errorr9  J  s    In-J
 % J ##:+;+;<L)&&KE ""4(||gi[$8"EH:#A#AAAAr)   c                      [         R                  " [        5      R                  S-  n [        R
                  " [        R                  U /SS9nUR                  [        :X  d   eg)zGOn shutdown the overload thread exits.

This is a test for issue #769.
zthreadleakcheck.pyF)checkN)
pathlibPath__file__parent
subprocessrunsys
executable
returncoder   )pathprocesss     r'   "test_overload_thread_does_not_leakrG  k  sN    
 <<!((+??Dnncnnd35AG !;;;;r)   )Q__doc__r4   r<  r]   r   r@  rB  r2   rK   r&  urllib.parser   r-   httpr   r    r?   r   pypytools.gc.customr   _compatr   r   r   r	   r
   r   r   r   r   r   testingr   r   r   r   workers.threadpoolr   rn   version_infor   markskipifhasattrunix_only_sock_testnon_macos_sock_testfixturer(   r0   r9   rE   rQ   parametrizeIOErrorrd   OSErrorr+  ra   rp   rv   rz   r|   r   r   r~   r   flakyr   r   usefixturesr   r   r   r   r   floatr  r"  r,  r9  rG  r   r)   r'   <module>r\     sZ     	     
          ) N N ? ?  , $*  !$. kk((	""G )   kk((F )   +,) -)     .'( 	!:!:H8 55 2 2 6 6 % ( !7 !*   !!,6 -  6< !  
 >  >D :  
  !   ,-K .KB ''B   %	%$   M M2    8 5
 %L	
 
Be	
E%L%,'	
Be	
Be	
E%L%,'(')(' )!D
E!D
.BB	<r)   