a
    db2                  	   @   s  d dl 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mZm	Z
 d dlZddlmZ ddlmZ ddlmZ zd dlZejZW n* eefy   dZG dd	 d	eZY n0 zeZW n" ey   G d
d deZY n0 ddlmZmZmZmZ ddl m!Z!m"Z" ddl#m$Z$m%Z%m&Z&m'Z'm(Z( ddl)m*Z* ddl+m,Z, e-e.Z/dddZ0e1dddZ2G dd de3Z4G dd dee3ZG dd deZ5G dd de5Z6dd Z7ere5Z8e6Z5ne4Z5dS )    )absolute_importN)errortimeout   )six)HTTPConnection)HTTPExceptionc                   @   s   e Zd ZdS )BaseSSLErrorN__name__
__module____qualname__ r   r   c/home/tom/ab/renpy-build/tmp/install.linux-x86_64/lib/python3.9/site-packages/urllib3/connection.pyr	      s   r	   c                   @   s   e Zd ZdS )ConnectionErrorNr
   r   r   r   r   r      s   r   )NewConnectionErrorConnectTimeoutErrorSubjectAltNameWarningSystemTimeWarning)match_hostnameCertificateError)resolve_cert_reqsresolve_ssl_versionassert_fingerprintcreate_urllib3_contextssl_wrap_socket)
connection)HTTPHeaderDictP   i  )httphttpsi  c                   @   s   e Zd ZdZdS )DummyConnectionz-Used to detect a failed ConnectionCls import.N)r   r   r   __doc__r   r   r   r   r!   A   s   r!   c                   @   sV   e Zd ZdZed ZejejdfgZ	dZ
dd Zdd Zd	d
 Zdd ZdddZdS )r   a{  
    Based on httplib.HTTPConnection but provides an extra constructor
    backwards-compatibility layer between older and newer Pythons.

    Additional keyword parameters are used to configure attributes of the connection.
    Accepted parameters include:

      - ``strict``: See the documentation on :class:`urllib3.connectionpool.HTTPConnectionPool`
      - ``source_address``: Set the source address for the current connection.

        .. note:: This is ignored for Python 2.6. It is only applied for 2.7 and 3.x

      - ``socket_options``: Set specific options on the underlying socket. If not specified, then
        defaults are loaded from ``HTTPConnection.default_socket_options`` which includes disabling
        Nagle's algorithm (sets TCP_NODELAY to 1) unless the connection is behind a proxy.

        For example, if you wish to enable TCP Keep Alive in addition to the defaults,
        you might pass::

            HTTPConnection.default_socket_options + [
                (socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1),
            ]

        Or you may want to disable the defaults by passing an empty list (e.g., ``[]``).
    r   r   Fc                 O   s`   t jr|dd  |d| _tjdk r4|dd  |d| j| _t	j
| g|R i | d S )Nstrictsource_address)      socket_options)r   PY3popgetr$   sysversion_infodefault_socket_optionsr'   _HTTPConnection__init__)selfargskwr   r   r   r/   j   s    
zHTTPConnection.__init__c              
   C   s   i }| j r| j |d< | jr$| j|d< z"tj| j| jf| jfi |}W nj ty } z"t| d| j| jf W Y d}~n:d}~0  t	y } zt
| d| W Y d}~n
d}~0 0 |S )zp Establish a socket connection and set nodelay settings on it.

        :return: New socket connection.
        r$   r'   z0Connection to %s timed out. (connect timeout=%s)Nz(Failed to establish a new connection: %s)r$   r'   r   create_connectionhostportr   SocketTimeoutr   SocketErrorr   )r0   extra_kwconner   r   r   	_new_conn   s,    



zHTTPConnection._new_connc                 C   s$   || _ t| dd r |   d| _d S )N_tunnel_hostr   )sockgetattr_tunnel	auto_openr0   r9   r   r   r   _prepare_conn   s    zHTTPConnection._prepare_connc                 C   s   |   }| | d S )N)r;   rB   rA   r   r   r   connect   s    zHTTPConnection.connectNc                 C   s  t |dur|ni }d|v }d|v }| j||||d | D ]\}}| || q>d|vrh| dd |   |durtjtjf }	t||	r|f}|D ]b}
|
sqt|
tjs|
	d}
t
t|
d	d }| |	d
 | d | |
 | d q| d dS )z
        Alternative to the common request method, which sends the
        body with chunked encoding and not as one block
        Nzaccept-encodingr4   )skip_accept_encoding	skip_hostztransfer-encodingzTransfer-Encodingchunkedutf8r%   zutf-8s   
s   0

)r   
putrequestitems	putheader
endheadersr   string_typesbinary_type
isinstanceencodehexlensend)r0   methodurlbodyheadersrD   rE   headervaluestringish_typeschunklen_strr   r   r   request_chunked   s:    



zHTTPConnection.request_chunked)NN)r   r   r   r"   port_by_schemedefault_portsocketIPPROTO_TCPTCP_NODELAYr-   is_verifiedr/   r;   rB   rC   r\   r   r   r   r   r   F   s   r   c                   @   s8   e Zd Zed ZdZddddejdfddZdd Z	dS )HTTPSConnectionr    Nc           	      K   s8   t j| ||f||d| || _|| _|| _d| _d S )N)r#   r   r    )r   r/   key_file	cert_filessl_context	_protocol)	r0   r4   r5   rd   re   r#   r   rf   r2   r   r   r   r/      s    zHTTPSConnection.__init__c                 C   sN   |   }| | | jd u r2ttd td d| _t|| j| j| jd| _	d S )Nssl_version	cert_reqs)r=   keyfilecertfilerf   )
r;   rB   rf   r   r   r   r   rd   re   r=   rA   r   r   r   rC      s    

zHTTPSConnection.connect)
r   r   r   r]   r^   ri   r_   _GLOBAL_DEFAULT_TIMEOUTr/   rC   r   r   r   r   rc      s   
rc   c                   @   s6   e Zd ZdZdZdZdZdZdZdddZ	dd Z
dS )VerifiedHTTPSConnectionz[
    Based on httplib.HTTPSConnection but wraps the socket with
    SSL certification.
    Nc                 C   sn   |du r(|s|rd}n| j dur(| j j}|| _|| _|| _|| _|| _|oTtj	|| _
|oftj	|| _dS )zX
        This method should only be called once, before the connection is used.
        NCERT_REQUIRED)rf   verify_moderd   re   rj   assert_hostnamer   ospath
expanduserca_certsca_cert_dir)r0   rd   re   rj   ru   rq   r   rv   r   r   r   set_cert  s    
z VerifiedHTTPSConnection.set_certc              	   C   sN  |   }| j}t| dd r4|| _|   d| _| j}tj	 t
k }|rXtdt
t | jd u r|tt| jt| jd| _| j}t| j|_t|| j| j| j| j||d| _| jrt| jjdd| j nd|jtjkr0t|dd	s0| jd	ur0| j }| d
dstd|t! t"|| jp,| |jtj#kpF| jd u| _$d S )Nr<   r   zWSystem time is way off (before {0}). This will probably lead to SSL verification errorsrh   )r=   rk   rl   ru   rv   server_hostnamerf   T)binary_formcheck_hostnameFsubjectAltNamer   zCertificate for {0} has no `subjectAltName`, falling back to check for a `commonName` for now. This feature is being removed by major browsers and deprecated by RFC 2818. (See https://github.com/shazow/urllib3/issues/497 for details.))%r;   r4   r>   r=   r?   r@   r<   datetimedatetodayRECENT_DATEwarningswarnformatr   rf   r   r   ri   r   rj   rp   r   rd   re   ru   rv   r   getpeercertssl	CERT_NONErq   r*   r   _match_hostnamero   rb   )r0   r9   hostnameis_time_offcontextcertr   r   r   rC     sj    
	

zVerifiedHTTPSConnection.connect)NNNNNNN)r   r   r   r"   rj   ru   rv   ri   r   rw   rC   r   r   r   r   rn      s       
rn   c              
   C   sN   zt | | W n: tyH } z"td||  | |_ W Y d }~n
d }~0 0 d S )Nz@Certificate did not match expected hostname: %s. Certificate: %s)r   r   logr   
_peer_cert)r   asserted_hostnamer:   r   r   r   r   b  s    r   )9
__future__r   r|   loggingrr   r+   r_   r   r7   r   r6   r   packagesr   Zpackages.six.moves.http_clientr   r.   r   r   SSLErrorr	   ImportErrorAttributeErrorBaseExceptionr   	NameError	Exception
exceptionsr   r   r   r   Zpackages.ssl_match_hostnamer   r   Z	util.ssl_r   r   r   r   r   utilr   _collectionsr   	getLoggerr   r   r]   r}   r   objectr!   rc   rn   r   ZUnverifiedHTTPSConnectionr   r   r   r   <module>   sP   
	
 &l