a
    eÝìh.R  ã                   @   s‚  d Z ddlmZmZmZmZmZmZmZm	Z	m
Z
mZmZmZmZmZ ddlZddlZddlZddlZddlZddlZddlZddlmZmZ eeeeeeehƒZze W n ey¾   i ZY n0 dd„ ZG dd	„ d	eƒZ e e!e"fZ#d
d„ Z$dd„ Z%dd„ Z&dd„ Z'd(dd„Z(d)dd„Z)e)Z*d*dd„Z+dd„ Z,G dd„ dƒZ-G dd „ d e-ƒZ.d+d!d"„Z/ej0d#kr~G d$d%„ d%ƒZ1G d&d'„ d'e-ƒZ2dS ),aË  Basic infrastructure for asynchronous socket service clients and servers.

There are only two ways to have a program on a single processor do "more
than one thing at a time".  Multi-threaded programming is the simplest and
most popular way to do it, but there is another very different technique,
that lets you have nearly all the advantages of multi-threading, without
actually using multiple threads. it's really only practical if your program
is largely I/O bound. If your program is CPU bound, then pre-emptive
scheduled threads are probably what you really need. Network servers are
rarely CPU-bound, however.

If your operating system supports the select() system call in its I/O
library (and nearly all do), then you can use it to juggle multiple
communication channels at once; doing other work while your I/O is taking
place in the "background."  Although this strategy can seem strange and
complex, especially at first, it is in many ways easier to understand and
control than multi-threaded programming. The module documented here solves
many of the difficult problems for you, making the task of building
sophisticated high-performance network servers and clients a snap.

NB: this is a fork of asyncore from the stdlib that we've (the waitress
developers) named 'wasyncore' to ensure forward compatibility, as asyncore
in the stdlib will be dropped soon.  It is neither a copy of the 2.7 asyncore
nor the 3.X asyncore; it is a version compatible with either 2.7 or 3.X.
é    )ÚEAGAINÚEALREADYÚEBADFÚECONNABORTEDÚ
ECONNRESETÚEINPROGRESSÚEINTRÚEINVALÚEISCONNÚENOTCONNÚEPIPEÚ	ESHUTDOWNÚEWOULDBLOCKÚ	errorcodeNé   )ÚcompatÚ	utilitiesc                 C   s4   zt  | ¡W S  ttttfy.   d|   Y S 0 d S )NzUnknown error %s)ÚosÚstrerrorÚ	TypeErrorÚ
ValueErrorÚOverflowErrorÚ	NameError)Úerr© r   úN/var/www/html/requester/venv/lib/python3.9/site-packages/waitress/wasyncore.pyÚ	_strerrorX   s    r   c                   @   s   e Zd ZdS )ÚExitNowN)Ú__name__Ú
__module__Ú__qualname__r   r   r   r   r   _   s   r   c                 C   s8   z|   ¡  W n& ty    ‚ Y n   |  ¡  Y n0 d S ©N)Úhandle_read_eventÚ_reraised_exceptionsÚhandle_error©Úobjr   r   r   Úreadf   s    r'   c                 C   s8   z|   ¡  W n& ty    ‚ Y n   |  ¡  Y n0 d S r!   )Úhandle_write_eventr#   r$   r%   r   r   r   Úwriteo   s    r)   c                 C   s8   z|   ¡  W n& ty    ‚ Y n   |  ¡  Y n0 d S r!   )Úhandle_expt_eventr#   r$   r%   r   r   r   Ú
_exceptionx   s    r+   c              
   C   sÆ   zX|t j@ r|  ¡  |t j@ r&|  ¡  |t j@ r8|  ¡  |t jt jB t j	B @ rV|  
¡  W nh tyœ } z,|jd tvr€|  ¡  n|  
¡  W Y d }~n.d }~0  ty®   ‚ Y n   |  ¡  Y n0 d S ©Nr   )ÚselectÚPOLLINr"   ÚPOLLOUTr(   ÚPOLLPRIr*   ÚPOLLHUPÚPOLLERRÚPOLLNVALÚhandle_closeÚOSErrorÚargsÚ_DISCONNECTEDr$   r#   )r&   ÚflagsÚer   r   r   Ú	readwrite   s"    



r:   ç        c           
   
   C   s’  |d u rt }|rŽg }g }g }t| ¡ ƒD ]L\}}| ¡ }| ¡ }|rP| |¡ |rd|jsd| |¡ |sl|r*| |¡ q*g |  kr–|  kr–|kr¨n nt | ¡ d S zt	 	|||| ¡\}}}W nF t
y }	 z,|	jd tkræ‚ nW Y d }	~	d S W Y d }	~	n
d }	~	0 0 |D ]&}| |¡}|d u r*qt|ƒ q|D ]&}| |¡}|d u rVq:t|ƒ q:|D ]&}| |¡}|d u r‚qft|ƒ qfd S r,   )Ú
socket_mapÚlistÚitemsÚreadableÚwritableÚappendÚ	acceptingÚtimeÚsleepr-   r5   r6   r   Úgetr'   r)   r+   )
ÚtimeoutÚmapÚrÚwr9   Úfdr&   Zis_rZis_wr   r   r   r   Úpoll–   sN    


"
$





rK   c              
   C   sþ   |d u rt }| d ur t| d ƒ} t ¡ }|rút| ¡ ƒD ]L\}}d}| ¡ r\|tjtjB O }| 	¡ rt|j
st|tjO }|r8| ||¡ q8z| | ¡}W n8 tyÌ } z |jd tkr´‚ g }W Y d }~n
d }~0 0 |D ]&\}}| |¡}|d u rîqÒt||ƒ qÒd S )Niè  r   )r<   Úintr-   rK   r=   r>   r?   r.   r0   r@   rB   r/   Úregisterr5   r6   r   rE   r:   )rF   rG   ZpollsterrJ   r&   r8   rH   r   r   r   r   Úpoll2Æ   s2    

rN   ç      >@Fc                 C   sb   |d u rt }|r ttdƒr t}nt}|d u r>|r^|| |ƒ q,n |r^|dkr^|| |ƒ |d }q>d S )NrK   r   r   )r<   Úhasattrr-   rN   rK   )rF   Zuse_pollrG   ÚcountZpoll_funr   r   r   Úloopê   s    
rR   c                  C   s‚   t  ¡ \} }}g }|stdƒ‚|rL| |jjj|jjjt|j	ƒf¡ |j
}q~|d \}}}d dd„ |D ƒ¡}|||f| ||fS )Nztraceback does not existéÿÿÿÿú c                 S   s   g | ]}d | ‘qS )z
[%s|%s|%s]r   )Ú.0Úxr   r   r   Ú
<listcomp>  ó    z%compact_traceback.<locals>.<listcomp>)ÚsysÚexc_infoÚAssertionErrorrA   Útb_frameÚf_codeÚco_filenameÚco_nameÚstrÚ	tb_linenoÚtb_nextÚjoin)ÚtÚvÚtbÚtbinfoÚfileÚfunctionÚlineÚinfor   r   r   Úcompact_tracebacký   s     ýÿrl   c                   @   sF  e Zd ZdZdZdZdZdZdZe	dhƒZ
ejZeeƒZdBdd„Zdd„ ZeZdCdd	„ZdDd
d„Zejejfdd„ZdEdd„Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ ZdFdd „Z d!d"„ Z!d#d$„ Z"d%d&„ Z#dGd(d)„Z$d*d+„ Z%d,d-„ Z&d.d/„ Z'd0d1„ Z(d2d3„ Z)d4d5„ Z*d6d7„ Z+d8d9„ Z,d:d;„ Z-d<d=„ Z.d>d?„ Z/d@dA„ Z0dS )HÚ
dispatcherFNÚwarningc              
   C   s¢   |d u rt | _n|| _d | _|r˜| d¡ |  ||¡ d| _z| ¡ | _W qž ty” } z2|j	d t
tfv rtd| _n|  |¡ ‚ W Y d }~qžd }~0 0 nd | _d S ©Nr   TF)r<   Ú_mapÚ_filenoÚsetblockingÚ
set_socketÚ	connectedÚgetpeernameÚaddrr5   r6   r   r	   Údel_channelÚsocket)ÚselfÚsockrG   r   r   r   r   Ú__init__  s     

zdispatcher.__init__c                 C   s–   | j jd | j j g}| jr.| jr.| d¡ n| jr>| d¡ | jd ur€z| d| j ¡ W n" ty~   | t| jƒ¡ Y n0 d 	d 
|¡t| ƒ¡S )NÚ.Z	listeningrt   z%s:%dz<{} at {:#x}>rT   )Ú	__class__r   r    rB   rv   rA   rt   r   ÚreprÚformatrc   Úid)ry   Ústatusr   r   r   Ú__repr__?  s    

zdispatcher.__repr__c                 C   s   |d u r| j }| || j< d S r!   )rp   rq   )ry   rG   r   r   r   Úadd_channelN  s    zdispatcher.add_channelc                 C   s,   | j }|d u r| j}||v r"||= d | _ d S r!   )rq   rp   )ry   rG   rJ   r   r   r   rw   T  s    zdispatcher.del_channelc                 C   s.   ||f| _ t ||¡}| d¡ |  |¡ d S r,   )Zfamily_and_typerx   rr   rs   )ry   ÚfamilyÚtyperz   r   r   r   Úcreate_socket]  s    

zdispatcher.create_socketc                 C   s   || _ | ¡ | _|  |¡ d S r!   )rx   Úfilenorq   rƒ   ©ry   rz   rG   r   r   r   rs   c  s    
zdispatcher.set_socketc                 C   sB   z*| j  t jt j| j  t jt j¡dB ¡ W n ty<   Y n0 d S )Nr   )rx   Ú
setsockoptÚ
SOL_SOCKETÚSO_REUSEADDRÚ
getsockoptr5   ©ry   r   r   r   Úset_reuse_addrh  s    ýzdispatcher.set_reuse_addrc                 C   s   dS ©NTr   r   r   r   r   r?   y  s    zdispatcher.readablec                 C   s   dS r   r   r   r   r   r   r@   |  s    zdispatcher.writablec                 C   s(   d| _ tjdkr|dkrd}| j |¡S )NTÚnté   )rB   r   Únamerx   Úlisten)ry   Únumr   r   r   r“   ƒ  s    zdispatcher.listenc                 C   s   || _ | j |¡S r!   )rv   rx   Úbind)ry   rv   r   r   r   r•   ‰  s    zdispatcher.bindc                 C   sp   d| _ d| _| j |¡}|tttfv s8|tkrBtj	dkrB|| _
d S |dtfv r^|| _
|  ¡  nt|t| ƒ‚d S )NFTr   r   )rt   Ú
connectingrx   Ú
connect_exr   r   r   r	   r   r’   rv   r
   Úhandle_connect_eventr5   r   )ry   Úaddressr   r   r   r   Úconnect  s    ÿþý
zdispatcher.connectc              
   C   sz   z| j  ¡ \}}W nZ ty&   Y d S  tyl } z0|jd tttfv rVW Y d }~d S ‚ W Y d }~nd }~0 0 ||fS d S r,   )rx   Úacceptr   r5   r6   r   r   r   )ry   Úconnrv   Úwhyr   r   r   r›   ž  s    zdispatcher.acceptTc              
   C   s‚   z| j  |¡}|W S  ty| } zR|jd tkr>W Y d }~dS |jd tv rf|rX|  ¡  W Y d }~dS ‚ W Y d }~n
d }~0 0 d S r,   )rx   Úsendr5   r6   r   r7   r4   )ry   ÚdataZdo_closeÚresultr   r   r   r   rž   ¬  s    zdispatcher.sendc              
   C   sx   z(| j  |¡}|s |  ¡  W dS |W S W nJ tyr } z2|jd tv r\|  ¡  W Y d }~dS ‚ W Y d }~n
d }~0 0 d S )NrX   r   )rx   Úrecvr4   r5   r6   r7   )ry   Úbuffer_sizerŸ   r   r   r   r   r¡   º  s    
zdispatcher.recvc              
   C   sp   d| _ d| _d| _|  ¡  | jd urlz| j ¡  W n8 tyj } z |jd tt	fvrV‚ W Y d }~n
d }~0 0 d S )NFr   )
rt   rB   r–   rw   rx   Úcloser5   r6   r   r   )ry   r   r   r   r   r£   Ì  s    
zdispatcher.closec                 C   s   | j  tj|¡ d S r!   )ÚloggerÚlogÚloggingÚDEBUG)ry   Úmessager   r   r   r¥   Ü  s    zdispatcher.logrk   c                 C   s.   t jt jt jdœ}| j | |t j¡|¡ d S )N)rk   rn   Úerror)r¦   ÚINFOÚWARNÚERRORr¤   r¥   rE   )ry   r¨   r…   Zseverityr   r   r   Úlog_infoß  s
    ýzdispatcher.log_infoc                 C   s:   | j r|  ¡  n&| js.| jr$|  ¡  |  ¡  n|  ¡  d S r!   )rB   Úhandle_acceptrt   r–   r˜   Úhandle_readr   r   r   r   r"   ç  s    

zdispatcher.handle_read_eventc                 C   s@   | j  t jt j¡}|dkr(t|t|ƒƒ‚|  ¡  d| _d| _d S ro   )	rx   rŒ   rŠ   ÚSO_ERRORr5   r   Úhandle_connectrt   r–   ©ry   r   r   r   r   r˜   ó  s    zdispatcher.handle_connect_eventc                 C   s*   | j r
d S | js| jr|  ¡  |  ¡  d S r!   )rB   rt   r–   r˜   Úhandle_writer   r   r   r   r(   û  s    zdispatcher.handle_write_eventc                 C   s0   | j  t jt j¡}|dkr$|  ¡  n|  ¡  d S r,   )rx   rŒ   rŠ   r°   r4   Úhandle_exptr²   r   r   r   r*     s    
zdispatcher.handle_expt_eventc                 C   sZ   |   ¡ \}}}}zt| ƒ}W n   dt| ƒ }Y n0 |  d||||f d¡ |  ¡  d S )Nz)<__repr__(self) failed for object at %0x>z:uncaptured python exception, closing channel %s (%s:%s %s)r©   )rl   r~   r€   r­   r4   )ry   Únilrd   re   rg   Z	self_reprr   r   r   r$     s    
ÿýzdispatcher.handle_errorc                 C   s   |   dd¡ d S )Nz!unhandled incoming priority eventrn   ©r­   r   r   r   r   r´   %  s    zdispatcher.handle_exptc                 C   s   |   dd¡ d S )Nzunhandled read eventrn   r¶   r   r   r   r   r¯   (  s    zdispatcher.handle_readc                 C   s   |   dd¡ d S )Nzunhandled write eventrn   r¶   r   r   r   r   r³   +  s    zdispatcher.handle_writec                 C   s   |   dd¡ d S )Nzunhandled connect eventrn   r¶   r   r   r   r   r±   .  s    zdispatcher.handle_connectc                 C   s   |   ¡ }|d ur| j|Ž  d S r!   )r›   Úhandle_accepted)ry   Úpairr   r   r   r®   1  s    zdispatcher.handle_acceptc                 C   s   |  ¡  |  dd¡ d S )Nzunhandled accepted eventrn   )r£   r­   )ry   rz   rv   r   r   r   r·   6  s    zdispatcher.handle_acceptedc                 C   s   |   dd¡ |  ¡  d S )Nzunhandled close eventrn   )r­   r£   r   r   r   r   r4   :  s    zdispatcher.handle_close)NN)N)N)N)T)rk   )1r   r   r    Údebugrt   rB   r–   Úclosingrv   Ú	frozensetZignore_log_typesr   r¤   Ústaticmethodrl   r{   r‚   Ú__str__rƒ   rw   rx   ÚAF_INETÚSOCK_STREAMr†   rs   rŽ   r?   r@   r“   r•   rš   r›   rž   r¡   r£   r¥   r­   r"   r˜   r(   r*   r$   r´   r¯   r³   r±   r®   r·   r4   r   r   r   r   rm     sP   

 

	


rm   c                   @   s2   e Zd Zd
dd„Zdd„ ZeZdd„ Zdd	„ ZdS )Údispatcher_with_sendNc                 C   s   t  | ||¡ d| _d S )NrX   )rm   r{   Ú
out_bufferrˆ   r   r   r   r{   F  s    zdispatcher_with_send.__init__c                 C   s.   d}t  | | jd d… ¡}| j|d … | _d S )Nr   i   )rm   rž   rÁ   )ry   Znum_sentr   r   r   Úinitiate_sendJ  s    z"dispatcher_with_send.initiate_sendc                 C   s   | j  pt| jƒS r!   )rt   ÚlenrÁ   r   r   r   r   r@   Q  s    zdispatcher_with_send.writablec                 C   s0   | j r|  dt|ƒ ¡ | j| | _|  ¡  d S )Nz
sending %s)r¹   r­   r~   rÁ   rÂ   )ry   rŸ   r   r   r   rž   T  s    zdispatcher_with_send.send)NN)r   r   r    r{   rÂ   r³   r@   rž   r   r   r   r   rÀ   E  s
   
rÀ   c                 C   s”   | d u rt } t|  ¡ ƒD ]n}z| ¡  W q tyb } z"|jd tkrHn|sN‚ W Y d }~qd }~0  tyt   ‚ Y q   |s€‚ Y q0 q|  ¡  d S r,   )	r<   r=   Úvaluesr£   r5   r6   r   r#   Úclear)rG   Z
ignore_allrV   r   r   r   Ú	close_all[  s     
rÆ   Úposixc                   @   sN   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zdd
d„ZeZeZ	dd„ Z
dd„ Zd	S )Úfile_wrapperc                 C   s   t  |¡| _d S r!   )r   ÚduprJ   ©ry   rJ   r   r   r   r{   ‚  s    zfile_wrapper.__init__c                 C   s&   | j dkrt d|  t¡ |  ¡  d S )Nr   zunclosed file %r)rJ   ÚwarningsÚwarnÚResourceWarningr£   r   r   r   r   Ú__del__…  s    
zfile_wrapper.__del__c                 G   s   t j| jg|¢R Ž S r!   )r   r'   rJ   ©ry   r6   r   r   r   r¡   Š  s    zfile_wrapper.recvc                 G   s   t j| jg|¢R Ž S r!   )r   r)   rJ   rÏ   r   r   r   rž     s    zfile_wrapper.sendNc                 C   s(   |t jkr|t jkr|sdS tdƒ‚d S )Nr   z-Only asyncore specific behaviour implemented.)rx   rŠ   r°   ÚNotImplementedError)ry   ÚlevelÚoptnameÚbuflenr   r   r   rŒ     s
    ÿzfile_wrapper.getsockoptc                 C   s(   | j dk rd S | j }d| _ t |¡ d S )Nr   rS   )rJ   r   r£   rÊ   r   r   r   r£   š  s
    
zfile_wrapper.closec                 C   s   | j S r!   )rJ   r   r   r   r   r‡   ¡  s    zfile_wrapper.fileno)N)r   r   r    r{   rÎ   r¡   rž   rŒ   r'   r)   r£   r‡   r   r   r   r   rÈ   }  s   
rÈ   c                   @   s   e Zd Zddd„Zdd„ ZdS )Úfile_dispatcherNc                 C   sN   t  | d |¡ d| _z| ¡ }W n ty2   Y n0 |  |¡ t |d¡ d S )NTF)rm   r{   rt   r‡   ÚAttributeErrorÚset_filer   Úset_blocking)ry   rJ   rG   r   r   r   r{   ¥  s    
zfile_dispatcher.__init__c                 C   s"   t |ƒ| _| j ¡ | _|  ¡  d S r!   )rÈ   rx   r‡   rq   rƒ   rÊ   r   r   r   rÖ   °  s    
zfile_dispatcher.set_file)N)r   r   r    r{   rÖ   r   r   r   r   rÔ   ¤  s   
rÔ   )r;   N)r;   N)rO   FNN)NF)3Ú__doc__Úerrnor   r   r   r   r   r   r   r	   r
   r   r   r   r   r   r¦   r   r-   rx   rY   rC   rË   Ú r   r   r»   r7   r<   r   r   Ú	Exceptionr   ÚKeyboardInterruptÚ
SystemExitr#   r'   r)   r+   r:   rK   rN   Zpoll3rR   rl   rm   rÀ   rÆ   r’   rÈ   rÔ   r   r   r   r   Ú<module>   sD   @

			
0
!
  3
 '