3 \•eã @sÎddlZddlZy ddlZWnek r4dZYnXddlmZddlmZddlmZddlmZddl m Z dd „Z d d „Z d Z d ZdZdZGdd„deƒZGdd„dejejƒZGdd„dejƒZdS)éNé)Ú base_events)Úcompat)Ú protocols)Ú transports)ÚloggercCsj|r tdƒ‚ttdƒr*tjƒ}|sfd|_n|j dkr˜tj |_ |j tj tj tj fkr°‚|j tj k|_WYdd}~XnX|jjrè|j|jjƒƒ|t|ƒksú|jrDPqDW||fS)NFZPROTOCOL_IS_SHUTDOWN)rrÚlenÚ memoryviewr rr7r r;Úreasonr>r6r?r@rrAr:r8)r#rBÚoffsetr0ZviewrDrrrÚ feed_appdataós2       z_SSLPipe.feed_appdatai)N)N)N)F)r)Ú__name__Ú __module__Ú __qualname__r9r%Úpropertyr$r&r'r)r2r4r5r.rIrrrrr0s       Jrc@s†eZdZdd„Zddd„Zdd„Zdd „Zd d „Zd d „Ze j rHdd„Z dd„Z dd„Z ddd„Zdd„Zdd„Zdd„Zdd„ZdS) Ú_SSLProtocolTransportcCs||_||_d|_dS)NF)Ú_loopÚ _ssl_protocolÚ_closed)r#ÚloopZ ssl_protocolrrrr%)sz_SSLProtocolTransport.__init__NcCs|jj||ƒS)N)rPÚ_get_extra_info)r#ÚnameÚdefaultrrrÚget_extra_info/sz$_SSLProtocolTransport.get_extra_infocCs ||j_dS)N)rPÚ _app_protocol)r#ÚprotocolrrrÚ set_protocol3sz"_SSLProtocolTransport.set_protocolcCs|jjS)N)rPrW)r#rrrÚ get_protocol6sz"_SSLProtocolTransport.get_protocolcCs|jS)N)rQ)r#rrrÚ is_closing9sz _SSLProtocolTransport.is_closingcCsd|_|jjƒdS)NT)rQrPÚ_start_shutdown)r#rrrÚclose<sz_SSLProtocolTransport.closecCs&|js"tjd|t|d|jƒdS)Nzunclosed transport %r)Úsource)rQÚwarningsÚwarnÚResourceWarningr])r#rrrÚ__del__Ks z_SSLProtocolTransport.__del__cCs|jjjƒdS)N)rPÚ _transportÚ pause_reading)r#rrrrdQsz#_SSLProtocolTransport.pause_readingcCs|jjjƒdS)N)rPrcÚresume_reading)r#rrrreYsz$_SSLProtocolTransport.resume_readingcCs|jjj||ƒdS)N)rPrcÚset_write_buffer_limits)r#ZhighZlowrrrrfasz-_SSLProtocolTransport.set_write_buffer_limitscCs |jjjƒS)N)rPrcÚget_write_buffer_size)r#rrrrgvsz+_SSLProtocolTransport.get_write_buffer_sizecCs<t|tttfƒs$tdjt|ƒjƒƒ‚|s,dS|jj |ƒdS)Nz/data: expecting a bytes-like instance, got {!r}) Ú isinstanceÚbytesÚ bytearrayrFÚ TypeErrorÚformatÚtyperJrPÚ_write_appdata)r#rBrrrr7zs z_SSLProtocolTransport.writecCsdS)NFr)r#rrrÚ can_write_eof‡sz#_SSLProtocolTransport.can_write_eofcCs|jjƒdS)N)rPÚ_abort)r#rrrÚabort‹sz_SSLProtocolTransport.abort)N)NN)rJrKrLr%rVrYrZr[r]rZPY34rbrdrerfrgr7rorqrrrrrN&s   rNc@sœeZdZd'dd„Zd(dd„Zdd „Zd d „Zd d „Zdd„Zdd„Z dd„Z d)dd„Z dd„Z dd„Z dd„Zdd„Zdd„Zd*d!d"„Zd#d$„Zd%d&„ZdS)+Ú SSLProtocolFNTcCs¦tdkrtdƒ‚|st||ƒ}||_|r6| r6||_nd|_||_t|d|_tj ƒ|_ d|_ ||_ ||_ ||_t|j |ƒ|_d|_d|_d|_d|_d|_||_dS)Nzstdlib ssl module not available)rrF)r r,rrrÚ _sslcontextÚdictÚ_extraÚ collectionsÚdequeÚ_write_backlogÚ_write_buffer_sizeÚ_waiterrOrWrNÚ_app_transportÚ_sslpipeÚ_session_establishedÚ _in_handshakeÚ _in_shutdownrcÚ_call_connection_made)r#rRZ app_protocolrZwaiterrrZcall_connection_maderrrr%œs,    zSSLProtocol.__init__cCsD|jdkrdS|jjƒs:|dk r.|jj|ƒn |jjdƒd|_dS)N)rzZ cancelledZ set_exceptionZ set_result)r#rDrrrÚ_wakeup_waiterÀs   zSSLProtocol._wakeup_waitercCs&||_t|j|j|jƒ|_|jƒdS)N)rcrrsrrr|Ú_start_handshake)r#Ú transportrrrÚconnection_madeÊs  zSSLProtocol.connection_madecCs8|jrd|_|jj|jj|ƒd|_d|_|j|ƒdS)NF)r}rOÚ call_soonrWÚconnection_lostrcr{r)r#rDrrrr†Õs zSSLProtocol.connection_lostcCs|jjƒdS)N)rWÚ pause_writing)r#rrrr‡ãszSSLProtocol.pause_writingcCs|jjƒdS)N)rWÚresume_writing)r#rrrrˆészSSLProtocol.resume_writingcCs´|jdkrdSy|jj|ƒ\}}WnHtjk rj}z*|jjƒrTtjd||j|j ƒ|j ƒdSd}~XnXx|D]}|j j |ƒqrWx(|D] }|r¢|j j|ƒqŒ|jƒPqŒWdS)Nz%r: SSL error %s (reason %s))r|r.r r;rOÚ get_debugrÚwarningr6rGrprcr7rWÚ data_receivedr\)r#rBr0r1ÚerCrrrr‹ïs"    zSSLProtocol.data_receivedc CsTzB|jjƒrtjd|ƒ|jtƒ|js@|jjƒ}|r@tj dƒWd|j j ƒXdS)Nz%r received EOFz?returning true from eof_received() has no effect when using ssl) rOr‰rÚdebugrÚConnectionResetErrorr~rWÚ eof_receivedrŠrcr])r#Z keep_openrrrr s    zSSLProtocol.eof_receivedcCs4||jkr|j|S|jdk r,|jj||ƒS|SdS)N)rurcrV)r#rTrUrrrrS!s    zSSLProtocol._get_extra_infocCs.|jr dS|jr|jƒnd|_|jdƒdS)NTr*)rr~rprn)r#rrrr\)s  zSSLProtocol._start_shutdowncCs.|jj|dfƒ|jt|ƒ7_|jƒdS)Nr)rxr:ryrEÚ_process_write_backlog)r#rBrrrrn2szSSLProtocol._write_appdatacCsH|jjƒr$tjd|ƒ|jjƒ|_nd|_d|_|jjdƒ|j ƒdS)Nz%r starts SSL handshakeTr*r)r*r) rOr‰rrÚtimeÚ_handshake_start_timer~rxr:r)r#rrrr‚7s   zSSLProtocol._start_handshakecCsTd|_|jj}yF|dk r|‚|jƒ}t|jdƒsR|jrR|jjtj krRtj ||jƒWn~t k rÒ}zb|j j ƒržt|tjƒrŽtjd|ddntjd|dd|jjƒt|tƒrÀ|j|ƒdS‚WYdd}~XnX|j j ƒr|j jƒ|j}tjd||dƒ|jj||jƒ|jƒ|d |jr4|jj|j ƒ|jƒd|_!|j j"|j#ƒdS) NFr z5%r: SSL handshake failed on verifying the certificateT)Úexc_infoz%r: SSL handshake failedz%r: SSL handshake took %.1f msg@@)ÚpeercertÚcipherÚ compressionr&)$r~r|r&Z getpeercertr rsrr r Z CERT_NONEZmatch_hostnameÚ BaseExceptionrOr‰rhr<rrŠrcr]Ú Exceptionrr‘r’rruÚupdater•r–r€rWr„r{r}r…r)r#Z handshake_excZsslobjr”rDZdtrrrÚ_on_handshake_completeCsD         z"SSLProtocol._on_handshake_completecCs>|jdks|jdkrdSyÎxÈtt|jƒƒD]¶}|jd\}}|rT|jj||ƒ\}}n*|rl|jj|jƒ}d}n|jj|j ƒ}d}x|D]}|jj |ƒq„W|t|ƒkrÆ||f|jd<|jj rÄ|jj ƒP|jd=|j t|ƒ8_ q*WWnRtk r8}z4|jr|j|ƒn |j|dƒt|tƒs(‚WYdd}~XnXdS)NrrzFatal error on SSL transport)rcr|ÚrangerErxrIr2ršr4Ú _finalizer7Z_pausedreryr—r~Ú _fatal_errorrhr˜)r#ÚirBrHr0rCrDrrrrws8      z"SSLProtocol._process_write_backlogúFatal error on transportcCsXt|tjƒr*|jjƒrBtjd||ddn|jj|||j|dœƒ|jrT|jj |ƒdS)Nz%r: %sT)r“)ÚmessageZ exceptionrƒrX) rhrZ_FATAL_ERROR_IGNORErOr‰rrZcall_exception_handlerrcZ _force_close)r#rDr rrrr£s   zSSLProtocol._fatal_errorcCsd|_|jdk r|jjƒdS)N)r|rcr])r#rrrrœ²s zSSLProtocol._finalizec Cs(z|jdk r|jjƒWd|jƒXdS)N)rcrqrœ)r#rrrrp¸s zSSLProtocol._abort)FNT)N)N)rŸ)rJrKrLr%rr„r†r‡rˆr‹rrSr\rnr‚ršrrrœrprrrrrr•s$ "     4, rr)rvr_r Ú ImportErrorÚrrrrÚlogrrrrr-r(r3ÚobjectrZ_FlowControlMixinZ TransportrNZProtocolrrrrrrÚs*       wn