U
    gj                     @  s   d dl mZ d dlZd dlZddlmZ ddlmZmZ ej	ej
eeejeef f  ZejrjddlmZ dd	d
ddZeddfddddddddZddddddZddddZdd	ddd Zed!ZdS )"    )annotationsN   )LocationParseError   )_DEFAULT_TIMEOUT_TYPE_TIMEOUT)BaseHTTPConnectionr   bool)connreturnc                 C  s   | j  S )z
    Returns True if the connection is dropped and should be closed.
    :param conn: :class:`urllib3.connection.HTTPConnection` object.
    )Zis_connected)r
    r   ;/tmp/pip-unpacked-wheel-f4zjg0cl/urllib3/util/connection.pyis_connection_dropped   s    r   ztuple[str, int]r   ztuple[str, int] | Nonez_TYPE_SOCKET_OPTIONS | Nonezsocket.socket)addresstimeoutsource_addresssocket_optionsr   c                 C  s:  | \}}| dr|d}d}t }z|d W n& tk
rZ   td| ddY nX t|||tjD ]}|\}	}
}}}d}zPt|	|
|}t	|| |t
k	r|| |r|| || d}|W   S  tk
r } z|}|dk	 r|  W 5 d}~X Y qnX qn|dk	r.z|W 5 d}X ntddS )ak  Connect to *address* and return the socket object.

    Convenience function.  Connect to *address* (a 2-tuple ``(host,
    port)``) and return the socket object.  Passing the optional
    *timeout* parameter will set the timeout on the socket instance
    before attempting to connect.  If no *timeout* is supplied, the
    global default timeout setting returned by :func:`socket.getdefaulttimeout`
    is used.  If *source_address* is set it must be a tuple of (host, port)
    for the socket to bind as a source address before making the connection.
    An host of '' or port 0 tells the OS to use the default.
    [z[]Nidna'z', label empty or too longz!getaddrinfo returns an empty list)
startswithstripallowed_gai_familyencodeUnicodeErrorr   socketgetaddrinfoSOCK_STREAM_set_socket_optionsr   
settimeoutbindconnectOSErrorclose)r   r   r   r   hostporterrfamilyresafsocktypeproto	canonnamesasock_r   r   r   create_connection   s>    








r0   None)r.   optionsr   c                 C  s$   |d krd S |D ]}| j |  qd S )N)
setsockopt)r.   r2   optr   r   r   r   ]   s    r   zsocket.AddressFamily)r   c                  C  s   t j} trt j} | S )zThis function is designed to work in the context of
    getaddrinfo, where family=socket.AF_UNSPEC is the default and
    will perform a DNS search for both IPv6 and IPv4 records.)r   AF_INETHAS_IPV6	AF_UNSPEC)r'   r   r   r   r   g   s    r   str)r$   r   c                 C  sV   d}d}t jrFz"t  t j}|| df d}W n tk
rD   Y nX |rR|  |S )z4Returns True if the system can bind an IPv6 address.NFr   T)r   has_ipv6AF_INET6r    	Exceptionr#   )r$   r.   r9   r   r   r   	_has_ipv6r   s    r<   z::1)
__future__r   r   typing
exceptionsr   r   r   r   ListTupleintUnionbytesZ_TYPE_SOCKET_OPTIONSTYPE_CHECKINGZ_base_connectionr   r   r0   r   r   r<   r6   r   r   r   r   <module>   s     B
