a t~`@sFddlZddlZddlZddlmZddlmZdZGdddZdS)N)ProxySchemeUnsupported)sixi@c@seZdZdZeddZd:ddZdd Zd d Zd d Z d;ddZ dddZ d?ddZd@ddZddZdd ZdAd"d#Zd$d%Zd&d'Zd(d)Zd*d+Zd,d-Zd.d/Zd0d1Zd2d3Zd4d5ZdBd6d7Zd8d9ZdS)C 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|ds"tjrtdntddS)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)hasattrrZPY2r) ssl_contextr=/usr/lib/python3.9/site-packages/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) sslZ MemoryBIOincomingoutgoingsuppress_ragged_eofssocketrsslobj _ssl_io_loopZ do_handshake)selfrrr rrrr __init__,s   zSSLTransport.__init__cCs|SNrrrrr __enter__?szSSLTransport.__enter__cGs |dSr)close)r_rrr __exit__BszSSLTransport.__exit__cCs |jSr)rfilenorrrr rEszSSLTransport.filenocCs |||Sr)_wrap_ssl_read)rlenbufferrrr readHszSSLTransport.readrcCs|dkrtd||S)Nrz+non-zero flags not allowed in calls to recv) ValueErrorr)rrflagsrrr recvKszSSLTransport.recvcCs>|dkrtd|r&|dur&t|}n |dur2d}|||S)Nrz0non-zero flags not allowed in calls to recv_intor)r!rr )rrnbytesr"rrr recv_intoPs  zSSLTransport.recv_intoc Cs|dkrtdd}t|f}|d<}t|}||krV|||d}||7}q2Wdn1sj0YWdn1s0YdS)Nrz.non-zero flags not allowed in calls to sendallB)r! memoryviewcastrsend)rdatar"countview byte_viewamountvrrr sendallYszSSLTransport.sendallcCs$|dkrtd||jj|}|S)Nrz+non-zero flags not allowed in calls to send)r!rrwrite)rr*r"Zresponserrr r)cszSSLTransport.sendrc Cst|hdkstd|fd|v}d|vp2| }d|v}d} |rL| d7} |rX| d7} t|| } |jjd7_|durd }|d krtj}|d kr|std | S|r|rt| | |} n|rt| |} n t | |} |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. >r2bwz&invalid mode %r (only r, w, b allowed)r4r2r3Nrz!unbuffered streams must be binary) setr!rZSocketIOZ_io_refsioDEFAULT_BUFFER_SIZEBufferedRWPairBufferedReaderBufferedWriter TextIOWrappermode) rr? bufferingencodingerrorsnewlineZwritingZreadingZbinaryZrawmoderawrtextrrr makefileis<   zSSLTransport.makefilecCs||jjdSr)rrunwraprrrr rGszSSLTransport.unwrapcCs|jdSr)rrrrrr rszSSLTransport.closeFcCs |j|Sr)r getpeercert)rZ binary_formrrr rHszSSLTransport.getpeercertcCs |jSr)rversionrrrr rIszSSLTransport.versioncCs |jSr)rcipherrrrr rJszSSLTransport.ciphercCs |jSr)rselected_alpn_protocolrrrr rKsz#SSLTransport.selected_alpn_protocolcCs |jSr)rselected_npn_protocolrrrr rLsz"SSLTransport.selected_npn_protocolcCs |jSr)rshared_ciphersrrrr rMszSSLTransport.shared_cipherscCs |jSr)r compressionrrrr rNszSSLTransport.compressioncCs|j|dSr)r settimeout)rvaluerrr rOszSSLTransport.settimeoutcCs |jSr)r gettimeoutrrrr rQszSSLTransport.gettimeoutcCs|jdSr)r_decref_socketiosrrrr rRszSSLTransport._decref_socketiosc Csbz||jj||WStjy\}z.|jtjkrF|jrFWYd}~dSWYd}~n d}~00dS)Nr)rrr r SSLErrorerrnoZ SSL_ERROR_EOFr)rrrerrr rs zSSLTransport._wrap_ssl_readc Gsd}d}|rd}z ||}WnBtjy^}z(|jtjtjfvrD||j}WYd}~n d}~00|j}|j||durd}q|tjkr|j t }|r|j |q|j q|S)z>Performs an I/O loop between incoming/outgoing and the socket.TNF)r rSrTZSSL_ERROR_WANT_READZSSL_ERROR_WANT_WRITErr rr0r# SSL_BLOCKSIZEr r1Z write_eof)rfuncargsZ should_loopretrTrUbufrrr rs(      zSSLTransport._ssl_io_loop)NT)rN)rr)Nr)r)r)r2NNNN)F)N)__name__ __module__ __qualname____doc__ staticmethodr rrrrr r#r%r0r)rFrGrrHrIrJrKrLrMrNrOrQrRrrrrrr r s:       -  r) r9rr Zurllib3.exceptionsrZurllib3.packagesrrVrrrrr s