U
    Â½„b%  ã                   @   s†   d dl mZ d dlZddlmZ ddlmZ ddlmZ ddl	m
Z
mZ d	d
„ Zejddfdd„Zdd„ Zdd„ Zdd„ ZedƒZdS )é    )Úabsolute_importNé   )Ú_appengine_environ)ÚLocationParseError)Úsixé   )ÚNoWayToWaitForSocketErrorÚwait_for_readc                 C   sN   t | ddƒ}|dkrdS |dkr$dS zt|dd�W S  tk
rH   Y dS X dS )a$  
    Returns True if the connection is dropped and should be closed.

    :param conn:
        :class:`http.client.HTTPConnection` object.

    Note: For platforms like AppEngine, this will always return ``False`` to
    let the platform handle connection recycling transparently for us.
    ÚsockFNTg        )Útimeout)Úgetattrr	   r   )Úconnr
   © r   ú;/tmp/pip-unpacked-wheel-_z7g_w1e/urllib3/util/connection.pyÚis_connection_dropped   s    
r   c                 C   s4  | \}}|  d¡r| d¡}d}tƒ }z| d¡ W n( tk
r\   t td| ƒd¡ Y S X t 	|||tj
¡D ]¦}|\}	}
}}}d}zNt |	|
|¡}t||ƒ |tjk	r´| |¡ |rÂ| |¡ | |¡ |W   S  tjk
�r } z|}|dk	�r| ¡  d}W 5 d}~X Y qpX qp|dk	�r&|‚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Úidnaz'%s', label empty or too longz!getaddrinfo returns an empty list)Ú
startswithÚstripÚallowed_gai_familyÚencodeÚUnicodeErrorr   Ú
raise_fromr   ÚsocketÚgetaddrinfoÚSOCK_STREAMÚ_set_socket_optionsÚ_GLOBAL_DEFAULT_TIMEOUTÚ
settimeoutÚbindÚconnectÚerrorÚclose)Úaddressr   Úsource_addressÚsocket_optionsÚhostÚportÚerrÚfamilyÚresÚafÚsocktypeÚprotoÚ	canonnameÚsar
   Úer   r   r   Úcreate_connection%   s@    


 ÿ








r1   c                 C   s$   |d krd S |D ]}| j |Ž  qd S )N)Ú
setsockopt)r
   ÚoptionsÚoptr   r   r   r   d   s    r   c                  C   s   t j} trt j} | S )z³This 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   l   s    r   c                 C   sb   d}d}t  ¡ rdS tjrRz"t tj¡}| | df¡ d}W n tk
rP   Y nX |r^| ¡  |S )z4Returns True if the system can bind an IPv6 address.NFr   T)r   Úis_appengine_sandboxr   Úhas_ipv6ÚAF_INET6r   Ú	Exceptionr"   )r&   r
   r9   r   r   r   Ú	_has_ipv6w   s    r<   z::1)Ú
__future__r   r   Úcontribr   Ú
exceptionsr   Úpackagesr   Úwaitr   r	   r   r   r1   r   r   r<   r6   r   r   r   r   Ú<module>   s   ü
?