U
    Ýá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k
rÀ   i ZY nX 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   s6   zt  | ¡W S  ttttfk
r0   d|   Y S X d S )NzUnknown error %s)ÚosÚstrerrorÚ	TypeErrorÚ
ValueErrorÚOverflowErrorÚ	NameError)Úerr© r   úD/var/www/html/venv/lib/python3.8/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   s:   z|   ¡  W n( tk
r"   ‚ Y n   |  ¡  Y nX d S ©N)Úhandle_read_eventÚ_reraised_exceptionsÚhandle_error©Úobjr   r   r   Úreadf   s    r'   c                 C   s:   z|   ¡  W n( tk
r"   ‚ Y n   |  ¡  Y nX d S r!   )Úhandle_write_eventr#   r$   r%   r   r   r   Úwriteo   s    r)   c                 C   s:   z|   ¡  W n( tk
r"   ‚ Y n   |  ¡  Y nX 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k
rš } z$|jd tkr‚|  ¡  n|  
¡  W 5 d }~X Y n( tk
r®   ‚ Y n   |  ¡  Y nX 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 k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 n@ t
k
r }	 z |	jd tkrè‚ n
W Y ¢d S W 5 d }	~	X Y nX |D ]&}| |¡}|d kr$qt|ƒ q|D ]&}| |¡}|d krPq4t|ƒ q4|D ]&}| |¡}|d kr|q`t|ƒ q`d 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 krt }| d k	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 n6 tk
rÊ } z|jd tkr¶‚ g }W 5 d }~X Y nX |D ]&\}}| |¡}|d k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 krt }|r ttdƒr t}nt}|d k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>  s     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     ýÿrk   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 krt | _n|| _d | _|r–| d¡ |  ||¡ d| _z| ¡ | _W qœ tk
r’ } z*|j	d t
tfkrvd| _n|  |¡ ‚ W 5 d }~X Y qœX 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 k	r‚z| d| j ¡ W n$ tk
r€   | t| jƒ¡ Y nX d 	d 
|¡t| ƒ¡S )NÚ.Z	listeningrs   z%s:%dz<{} at {:#x}>rT   )Ú	__class__r   r    rB   ru   rA   rs   r   ÚreprÚformatrb   Úid)rx   Ústatusr   r   r   Ú__repr__?  s    

zdispatcher.__repr__c                 C   s   |d kr| j }| || j< d S r!   )ro   rp   )rx   rG   r   r   r   Úadd_channelN  s    zdispatcher.add_channelc                 C   s,   | j }|d kr| j}||kr"||= d | _ d S r!   )rp   ro   )rx   rG   rJ   r   r   r   rv   T  s    zdispatcher.del_channelc                 C   s.   ||f| _ t ||¡}| d¡ |  |¡ d S r,   )Zfamily_and_typerw   rq   rr   )rx   ÚfamilyÚtypery   r   r   r   Úcreate_socket]  s    

zdispatcher.create_socketc                 C   s   || _ | ¡ | _|  |¡ d S r!   )rw   Úfilenorp   r‚   ©rx   ry   rG   r   r   r   rr   c  s    
zdispatcher.set_socketc                 C   sD   z*| j  t jt j| j  t jt j¡dB ¡ W n tk
r>   Y nX d S )Nr   )rw   Ú
setsockoptÚ
SOL_SOCKETÚSO_REUSEADDRÚ
getsockoptr5   ©rx   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   Únamerw   Úlisten)rx   Únumr   r   r   r’   ƒ  s    zdispatcher.listenc                 C   s   || _ | j |¡S r!   )ru   rw   Úbind)rx   ru   r   r   r   r”   ‰  s    zdispatcher.bindc                 C   sp   d| _ d| _| j |¡}|tttfks8|tkrBtj	dkrB|| _
d S |dtfkr^|| _
|  ¡  nt|t| ƒ‚d S )NFTr   r   )rs   Ú
connectingrw   Ú
connect_exr   r   r   r	   r   r‘   ru   r
   Úhandle_connect_eventr5   r   )rx   Úaddressr   r   r   r   Úconnect  s    ÿþý
zdispatcher.connectc              
   C   sv   z| j  ¡ \}}W nV tk
r(   Y d S  tk
rh } z$|jd tttfkrVW Y ¢
d S ‚ W 5 d }~X Y n
X ||fS d S r,   )rw   Úacceptr   r5   r6   r   r   r   )rx   Úconnru   Úwhyr   r   r   rš   ž  s    
zdispatcher.acceptTc              
   C   sx   z| j  |¡}|W S  tk
rr } zB|jd tkr<W Y ¢.dS |jd tkr`|rV|  ¡  W Y ¢
dS ‚ W 5 d }~X Y nX d S r,   )rw   Úsendr5   r6   r   r7   r4   )rx   ÚdataZdo_closeÚresultrœ   r   r   r   r   ¬  s    

zdispatcher.sendc              
   C   sr   z(| j  |¡}|s |  ¡  W dS |W S W nD tk
rl } z&|jd tkrZ|  ¡  W Y ¢
dS ‚ W 5 d }~X Y nX d S )Nó    r   )rw   Úrecvr4   r5   r6   r7   )rx   Úbuffer_sizerž   rœ   r   r   r   r¡   º  s    

zdispatcher.recvc              
   C   sn   d| _ d| _d| _|  ¡  | jd k	rjz| j ¡  W n6 tk
rh } z|jd tt	fkrX‚ W 5 d }~X Y nX d S )NFr   )
rs   rB   r•   rv   rw   Úcloser5   r6   r   r   )rx   rœ   r   r   r   r£   Ì  s    
zdispatcher.closec                 C   s   | j  tj|¡ d S r!   )ÚloggerÚlogÚloggingÚDEBUG)rx   Úmessager   r   r   r¥   Ü  s    zdispatcher.logrj   c                 C   s.   t jt jt jdœ}| j | |t j¡|¡ d S )N)rj   rm   Úerror)r¦   ÚINFOÚWARNÚERRORr¤   r¥   rE   )rx   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_acceptrs   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 rn   )	rw   r‹   r‰   ÚSO_ERRORr5   r   Úhandle_connectrs   r•   ©rx   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   rs   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,   )rw   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X |  d||||f d¡ |  ¡  d S )Nz)<__repr__(self) failed for object at %0x>z:uncaptured python exception, closing channel %s (%s:%s %s)r©   )rk   r}   r   r­   r4   )rx   Únilrc   rd   rf   Z	self_reprr   r   r   r$     s    
ÿýzdispatcher.handle_errorc                 C   s   |   dd¡ d S )Nz!unhandled incoming priority eventrm   ©r­   rŒ   r   r   r   r´   %  s    zdispatcher.handle_exptc                 C   s   |   dd¡ d S )Nzunhandled read eventrm   r¶   rŒ   r   r   r   r¯   (  s    zdispatcher.handle_readc                 C   s   |   dd¡ d S )Nzunhandled write eventrm   r¶   rŒ   r   r   r   r³   +  s    zdispatcher.handle_writec                 C   s   |   dd¡ d S )Nzunhandled connect eventrm   r¶   rŒ   r   r   r   r±   .  s    zdispatcher.handle_connectc                 C   s   |   ¡ }|d k	r| j|Ž  d S r!   )rš   Úhandle_accepted)rx   Úpairr   r   r   r®   1  s    zdispatcher.handle_acceptc                 C   s   |  ¡  |  dd¡ d S )Nzunhandled accepted eventrm   )r£   r­   )rx   ry   ru   r   r   r   r·   6  s    zdispatcher.handle_acceptedc                 C   s   |   dd¡ |  ¡  d S )Nzunhandled close eventrm   )r­   r£   rŒ   r   r   r   r4   :  s    zdispatcher.handle_close)NN)N)N)N)T)rj   )1r   r   r    Údebugrs   rB   r•   Úclosingru   Ú	frozensetZignore_log_typesr   r¤   Ústaticmethodrk   rz   r   Ú__str__r‚   rv   rw   ÚAF_INETÚSOCK_STREAMr…   rr   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   rl     sP   

 

	


rl   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 )Nr    )rl   rz   Ú
out_bufferr‡   r   r   r   rz   F  s    zdispatcher_with_send.__init__c                 C   s.   d}t  | | jd d… ¡}| j|d … | _d S )Nr   i   )rl   r   rÁ   )rx   Znum_sentr   r   r   Úinitiate_sendJ  s    z"dispatcher_with_send.initiate_sendc                 C   s   | j  pt| jƒS r!   )rs   Ú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Â   )rx   rž   r   r   r   r   T  s    zdispatcher_with_send.send)NN)r   r   r    rz   rÂ   r³   r@   r   r   r   r   r   rÀ   E  s
   
rÀ   c                 C   s”   | d krt } t|  ¡ ƒD ]n}z| ¡  W q tk
r` } z|jd tkrJn|sP‚ W 5 d }~X Y q tk
rt   ‚ Y q   |s€‚ Y qX 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   ©rx   rJ   r   r   r   rz   ‚  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f|žŽ S r!   )r   r'   rJ   ©rx   r6   r   r   r   r¡   Š  s    zfile_wrapper.recvc                 G   s   t j| jf|žŽ 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.)rw   r‰   r°   ÚNotImplementedError)rx   Ú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    rz   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   sP   t  | d |¡ d| _z| ¡ }W n tk
r4   Y nX |  |¡ t |d¡ d S )NTF)rl   rz   rs   r†   ÚAttributeErrorÚset_filer   Úset_blocking)rx   rJ   rG   r   r   r   rz   ¥  s    
zfile_dispatcher.__init__c                 C   s"   t |ƒ| _| j ¡ | _|  ¡  d S r!   )rÈ   rw   r†   rp   r‚   rÊ   r   r   r   rÖ   °  s    
zfile_dispatcher.set_file)N)r   r   r    rz   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-   rw   rX   rC   rË   Ú r   r   r»   r7   r<   r   r   Ú	Exceptionr   ÚKeyboardInterruptÚ
SystemExitr#   r'   r)   r+   r:   rK   rN   Zpoll3rR   rk   rl   rÀ   rÆ   r‘   rÈ   rÔ   r   r   r   r   Ú<module>   sD   @

			
0
!
  3
 '