o ?c@sFddlZddlZddlZddlmZddlmZdZGdddZdS)N)ProxySchemeUnsupported)sixi@c@seZdZdZeddZ d:ddZdd Zd d Zd d Z d;ddZ dddZ d>ddZ d?ddZddZdd Zd@d"d#Zd$d%Zd&d'Zd(d)Zd*d+Zd,d-Zd.d/Zd0d1Zd2d3Zd4d5ZdAd6d7Zd8d9ZdS)B SSLTransportaL The SSLTransport wraps an existing socket and establishes an SSL connection. Contrary to Python's implementation of SSLSocket, it allows you to chain multiple TLS connections together. It's particularly useful if you need to implement TLS within TLS. The class supports most of the socket API operations. cCs$t|dstjr tdtddS)z Raises a ProxySchemeUnsupported if the provided ssl_context can't be used for TLS in TLS. The only requirement is that the ssl_context provides the 'wrap_bio' methods. wrap_biozKTLS in TLS requires SSLContext.wrap_bio() which isn't supported on Python 2zXTLS in TLS requires SSLContext.wrap_bio() which isn't available on non-native SSLContextN)hasattrrPY2r) ssl_contextr t/private/var/folders/cw/wlscbxl13mj6wd668h7l9g9sllkg5j/T/pip-target-b31awkwq/lib/python/urllib3/util/ssltransport.py$_validate_ssl_context_for_tls_in_tlss z1SSLTransport._validate_ssl_context_for_tls_in_tlsNTcCsHt|_t|_||_||_|j|j|j|d|_||jj dS)zV Create an SSLTransport around socket using the provided ssl_context. )server_hostnameN) ssl MemoryBIOincomingoutgoingsuppress_ragged_eofssocketrsslobj _ssl_io_loop do_handshake)selfrr r rr r r __init__,s   zSSLTransport.__init__cCs|SNr rr r r __enter__?szSSLTransport.__enter__cGs |dSr)close)r_r r r __exit__B zSSLTransport.__exit__cC |jSr)rfilenorr r r r!E zSSLTransport.filenocCs |||Sr)_wrap_ssl_read)rlenbufferr r r readHrzSSLTransport.readrcCs|dkrtd||S)Nrz+non-zero flags not allowed in calls to recv) ValueErrorr$)rr%flagsr r r recvKs zSSLTransport.recvcCs>|dkrtd|r|durt|}n|durd}|||S)Nrz0non-zero flags not allowed in calls to recv_intor#)r(r%r')rr&nbytesr)r r r recv_intoPs   zSSLTransport.recv_intoc Cs|dkrtdd}t|?}|d!}t|}||kr.|||d}||7}||ksWdn1s8wYWddSWddS1sPwYdS)Nrz.non-zero flags not allowed in calls to sendallB)r( memoryviewcastr%send)rdatar)countview byte_viewamountvr r r sendallYsPzSSLTransport.sendallcCs$|dkrtd||jj|}|S)Nrz+non-zero flags not allowed in calls to send)r(rrwrite)rr1r)responser r r r0cszSSLTransport.sendrc Cst|hdkstd|fd|v}d|vp| }|s |s Jd|v}d} |r,| d7} |r2| d7} t|| } |jjd7_|durFd }|d krMtj}|d krY|sWtd | S|re|ret| | |} n|rnt| |} n |srJt | |} |r|| St | |||} || _ | S) a Python's httpclient uses makefile and buffered io when reading HTTP messages and we need to support it. This is unfortunately a copy and paste of socket.py makefile with small changes to point to the socket directly. >r:wbz&invalid mode %r (only r, w, b allowed)r;r:r<Nrz!unbuffered streams must be binary) setr(rSocketIO_io_refsioDEFAULT_BUFFER_SIZEBufferedRWPairBufferedReaderBufferedWriter TextIOWrappermode) rrI bufferingencodingerrorsnewlinewritingreadingbinaryrawmoderawr&textr r r makefileis@    zSSLTransport.makefilecCs||jjdSr)rrunwraprr r r rUszSSLTransport.unwrapcC|jdSr)rrrr r r rzSSLTransport.closeFcCs |j|Sr)r getpeercert)r binary_formr r r rXrzSSLTransport.getpeercertcCr r)rversionrr r r rZr"zSSLTransport.versioncCr r)rcipherrr r r r[r"zSSLTransport.ciphercCr r)rselected_alpn_protocolrr r r r\r"z#SSLTransport.selected_alpn_protocolcCr r)rselected_npn_protocolrr r r r]r"z"SSLTransport.selected_npn_protocolcCr r)rshared_ciphersrr r r r^r"zSSLTransport.shared_cipherscCr r)r compressionrr r r r_r"zSSLTransport.compressioncCs|j|dSr)r settimeout)rvaluer r r r`szSSLTransport.settimeoutcCr r)r gettimeoutrr r r rbr"zSSLTransport.gettimeoutcCrVr)r_decref_socketiosrr r r rcrWzSSLTransport._decref_socketiosc CsRz ||jj||WStjy(}z|jtjkr#|jr#WYd}~dSd}~wwNr)rrr'rSSLErrorerrno SSL_ERROR_EOFr)rr%r&er r r r$szSSLTransport._wrap_ssl_readc Gsd}d}|r]d}z||}Wn!tjy/}z|jtjtjfvr"||j}WYd}~nd}~ww|j}|j||durBd}n|tjkr[|j t }|rV|j |n|j |s|S)z>Performs an I/O loop between incoming/outgoing and the socket.TNF)rrerfSSL_ERROR_WANT_READSSL_ERROR_WANT_WRITErr'rr7r* SSL_BLOCKSIZErr8 write_eof)rfuncargs should_loopretrfrhbufr r r rs.      zSSLTransport._ssl_io_loop)NT)r#N)r#rrd)r)r:NNNN)Fr)__name__ __module__ __qualname____doc__ staticmethodr rrrr!r'r*r,r7r0rTrUrrXrZr[r\r]r^r_r`rbrcr$rr r r r r s<       -   r) rCrr exceptionsrpackagesrrkrr r r r s