U
    `'^                     @   sn   d dl mZ d dlZddlmZmZ ddl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   )NoWayToWaitForSocketErrorwait_for_read   )_appengine_environc                 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:`httplib.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   )Zconnr    r   9/usr/lib/python3/dist-packages/urllib3/util/connection.pyis_connection_dropped   s    
r   c                 C   s   | \}}| dr|d}d}t }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 q8X q8|dk	r|tddS )ad  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:`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[]Nz!getaddrinfo returns an empty list)
startswithstripallowed_gai_familysocketZgetaddrinfoZSOCK_STREAM_set_socket_options_GLOBAL_DEFAULT_TIMEOUTZ
settimeoutbindZconnecterrorclose)Zaddressr	   Zsource_addressZsocket_optionshostZporterrfamilyresZafZsocktypeprotoZ	canonnameZsar   er   r   r   create_connection!   s2    







r   c                 C   s$   |d krd S |D ]}| j |  qd S )N)Z
setsockopt)r   ZoptionsZoptr   r   r   r   Y   s    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   ZAF_INETHAS_IPV6Z	AF_UNSPEC)r   r   r   r   r   a   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 )z6 Returns True if the system can bind an IPv6 address. NFr   T)r   Zis_appengine_sandboxr   has_ipv6ZAF_INET6r   	Exceptionr   )r   r   r    r   r   r   	_has_ipv6l   s    r"   z::1)Z
__future__r   r   waitr   r   Zcontribr   r   r   r   r   r   r"   r   r   r   r   r   <module>   s   
8