U
f" @ s d dl mZ d dlZd dlZd dlZd dlZddlmZ ejrZd dl m
Z
ddlmZm
Z
ejeef ZedZd ZG d
d dZdS ) )annotationsN )ProxySchemeUnsupported)Self )_TYPE_PEER_CERT_RET_TYPE_PEER_CERT_RET_DICT_ReturnValuei @ c @ s2 e Zd ZdZedddddZdkd dd
dddd
dZddddZdddddZddddZ dlddddddZ
dmdddd!d"d#Zdnd$d%dd&d'd(d)Zdod*ddd+d,d-Z
dpd*ddd+d.d/Zdqdddd0d1d%d
d
d
d2d3d4d5Zddd6d7Zddd8d9Zejdrd;dd?Zejd@dAd=dBd?ZdsddDd=dEd?Zd
ddFdGZdHddIdJZd
ddKdLZdMddNdOZd
ddPdQZdRddSdTdUZdRddVdWZdddXdYZdtddZddd[d\Zejd]dd^d_d`Zejdad*ddbdcd`ZejddddZd*dedfd`ZdudgdhdZdidedjd`ZdS )vSSLTransportaL
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.
zssl.SSLContextNone)ssl_contextreturnc C s t | dstddS )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_biozXTLS in TLS requires SSLContext.wrap_bio() which isn't available on non-native SSLContextN)hasattrr )r r J/opt/alt/python38/lib/python3.8/site-packages/urllib3/util/ssltransport.py$_validate_ssl_context_for_tls_in_tls! s
z1SSLTransport._validate_ssl_context_for_tls_in_tlsNTz
socket.socketz
str | Nonebool)socketr server_hostnamesuppress_ragged_eofsr
c C sH t | _t | _|| _|| _|j| j| j|d| _| | jj dS )zV
Create an SSLTransport around socket using the provided ssl_context.
)r N)
ssl MemoryBIOincomingoutgoingr r r sslobj_ssl_io_loopdo_handshake)selfr r r r r r r __init__1 s
zSSLTransport.__init__r )r
c C s | S Nr r r r r __enter__H s zSSLTransport.__enter__z
typing.Any)_r
c G s | d S r )close)r r# r r r __exit__K s zSSLTransport.__exit__intc C s
| j S r )r filenor! r r r r' N s zSSLTransport.fileno ztyping.Any | Nonezint | bytes)lenbufferr
c C s | ||S r )_wrap_ssl_read)r r) r* r r r readQ s zSSLTransport.readr )buflenflagsr
c C s |dkrt d| |S )Nr z+non-zero flags not allowed in calls to recv)
ValueErrorr+ )r r- r. r r r recvT s zSSLTransport.recv_WriteBufferz
int | NonezNone | int | bytes)r* nbytesr. r
c C s, |dkrt d|d kr t|}| ||S )Nr z0non-zero flags not allowed in calls to recv_into)r/ r) r, )r r* r2 r. r r r recv_intoY s
zSSLTransport.recv_intobytes)datar. r
c
C sn |dkrt dd}t|H}|d2}t|}||k rV| ||d }||7 }q2W 5 Q R X W 5 Q R X d S )Nr z.non-zero flags not allowed in calls to sendallB)r/
memoryviewcastr) send)r r5 r. countview byte_viewamountvr r r sendalle s zSSLTransport.sendallc C s |dkrt d| | jj|S )Nr z+non-zero flags not allowed in calls to send)r/ r r write)r r5 r. r r r r9 o s zSSLTransport.send)encodingerrorsnewlinestrz1typing.BinaryIO | typing.TextIO | socket.SocketIO)mode bufferingrA rB rC r
c
C s t |dddhks"td|dd|k}d|kp6| }|sD|sDtd|k}d} |r\| d7 } |rh| d7 } t| | }
| j jd7 _|dkrd }|d
k rtj}|d
kr|std|
S |r|rt|
|
|}n&|rt |
|}n|stt
|
|}|r|S t||||}||_|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.
rwbz
invalid mode z (only r, w, b allowed) r Nr z!unbuffered streams must be binary)
setr/ AssertionErrorr SocketIO_io_refsioDEFAULT_BUFFER_SIZEBufferedRWPairBufferedReaderBufferedWriter
TextIOWrapperrE )
r rE rF rA rB rC writingreadingbinaryrawmoderawr* textr r r makefilet s@ zSSLTransport.makefilec C s | | jj d S r )r r unwrapr! r r r r] s zSSLTransport.unwrapc C s | j d S r )r r$ r! r r r r$ s zSSLTransport.close.ztyping.Literal[False]z_TYPE_PEER_CERT_RET_DICT | None)binary_formr
c C s d S r r r r^ r r r getpeercert s zSSLTransport.getpeercertztyping.Literal[True]zbytes | Nonec C s d S r r r_ r r r r` s Fr c C s | j |S r )r r` r_ r r r r` s c C s
| j S r )r versionr! r r r ra s zSSLTransport.versionztuple[str, str, int] | Nonec C s
| j S r )r cipherr! r r r rb s zSSLTransport.cipherc C s
| j S r )r selected_alpn_protocolr! r r r rc s z#SSLTransport.selected_alpn_protocolz!list[tuple[str, str, int]] | Nonec C s
| j S r )r shared_ciphersr! r r r rd s zSSLTransport.shared_ciphersc C s
| j S r )r compressionr! r r r re s zSSLTransport.compressionzfloat | None)valuer
c C s | j | d S r )r
settimeout)r rf r r r rg s zSSLTransport.settimeoutc C s
| j S r )r
gettimeoutr! r r r rh s zSSLTransport.gettimeoutc C s | j d S r )r _decref_socketiosr! r r r ri s zSSLTransport._decref_socketioszbytearray | Nonec
C s\ z| | jj||W S tjk
rV } z"|jtjkrD| jrDW Y
dS W 5 d }~X Y nX d S )Nr )r r r, r SSLErrorerrno
SSL_ERROR_EOFr )r r) r* er r r r+ s
zSSLTransport._wrap_ssl_readztyping.Callable[[], None])funcr
c C s d S r r )r rn r r r r s zSSLTransport._ssl_io_loopztyping.Callable[[bytes], int])rn arg1r
c C s d S r r )r rn ro r r r r s z/typing.Callable[[int, bytearray | None], bytes])rn ro arg2r
c C s d S r r )r rn ro rp r r r r s z"typing.Callable[..., _ReturnValue]zNone | bytes | intr c
C s d}d}|rd}z8|dkr*|dkr*| }n|dkr<||}n
|||}W n@ t jk
r } z |jt jt jfkrr||j}W 5 d}~X Y nX | j }| j| |dkrd}q|t jkr| j t
}|r| j| q| j
qtt|S )z>Performs an I/O loop between incoming/outgoing and the socket.TNF)r rj rk SSL_ERROR_WANT_READSSL_ERROR_WANT_WRITEr r, r r? r0
SSL_BLOCKSIZEr r@ write_eoftypingr8 r ) r rn ro rp should_loopretrk rm bufr r r r s0
)NT)r( N)r( r )Nr )r )r )N).)F)N)NN)__name__
__module____qualname____doc__staticmethodr r r" r% r' r, r0 r3 r? r9 r\ r] r$ ru overloadr` ra rb rc rd re rg rh ri r+ r r r r r r
s`
4
r
)
__future__r rP r r ru
exceptionsr
TYPE_CHECKINGZtyping_extensionsr ssl_r r Union bytearrayr7 r1 TypeVarr rs r
r r r r s