B
    `                 @   s   d dl Z d dlZd dlZyd dlZW n  ek
rD   d dlmZ Y nX d dlmZ d dl	Z	d dl
mZ d dlmZ ddgZG dd dZe Ze jfZee dree jf7 ZdS )	    N)	url_quote)exc)PY2send_request_appSendRequestc               @   sH   e Zd ZdZejejfddZdd ZdZ	e
dZdd	 Zd
d ZdS )r   ax  
    Sends the request, as described by the environ, over actual HTTP.
    All controls about how it is sent are contained in the request
    environ itself.

    This connects to the server given in SERVER_NAME:SERVER_PORT, and
    sends the Host header in HTTP_HOST -- they do not have to match.
    You can send requests to servers despite what DNS says.

    Set ``environ['webob.client.timeout'] = 10`` to set the timeout on
    the request (to, for example, 10 seconds).

    Does not add X-Forwarded-For or other standard headers

    If you use ``send_request_app`` then simple ``httplib``
    connections will be used.
    c             C   s   || _ || _d S )N)HTTPConnectionHTTPSConnection)selfr   r    r
   T/home/kop/projects/devel/pgwui/test_venv/lib/python3.7/site-packages/webob/client.py__init__"   s    zSendRequest.__init__c          
   C   s  |d }|dkr| j }n|dkr(| j}ntd| d|kr|d}|sRtdd|krl|dd	\}}n|dkrzd
}nd}||d< ||d< i }d|kr| |r|d |d< |d| f|}i }	x>| D ]2\}
}|
dr|
dd  dd	 }
||	|
< qW t
|ddt
|dd }|dr@|d|d  7 }yt|dd}W n tk
rn   d}Y nX |r|d |}nd}||	d< |dr|d |	d< |d sd | }y ||d! |||	 | }W n tjk
r   t }|||S  tjtjfk
r } zt|tjr<|jd d"ksZt|tjrv|jd d#krvtd$|d  }|||S |jd tkrtd%}|||S  W d d }~X Y nX | |j}d&|j|jf }||| |d'}|d k	r|t|}n| }|  |gS )(Nzwsgi.url_schemehttphttpszUnknown scheme: %rZSERVER_NAMEZ	HTTP_HOSTz2environ contains neither SERVER_NAME nor HTTP_HOST:   80443ZSERVER_PORTzwebob.client.timeouttimeoutz%(SERVER_NAME)s:%(SERVER_PORT)sZHTTP_   _-ZSCRIPT_NAME Z	PATH_INFOQUERY_STRING?CONTENT_LENGTH0r   z
wsgi.inputzContent-LengthCONTENT_TYPEzContent-Type/REQUEST_METHOD   z/Name or service not known (bad domain name: %s)zConnection refusedz%s %szcontent-length)r   r   
ValueErrorgetsplit_timeout_supporteditems
startswithreplacetitler   intreadrequestgetresponsesocketr   r   ZHTTPGatewayTimeouterrorgaierror
isinstanceargsZHTTPBadGateway
_e_refusedparse_headersmsgstatusreason	getheaderclose)r	   environZstart_responsescheme	ConnClasshostportkwconnheaderskeyvaluepathcontent_lengthbodyresrespeheaders_outr5   lengthr
   r
   r   __call__'   s    













zSendRequest.__call__)ztransfer-encodingz\r?\n\s*c       	      C   s   g }t s|j}n|j}x|D ]}|s&q|d  rn|sBtd| | \}}|d |  }|||f qt|t	r|\}}n0y|
dd\}}W n   td|f Y nX | }d|ksd|kr| jd|}| | jkr|||f qW |S )	zJ
        Turn a Message object into a list of WSGI-style headers.
        r   z%First header starts with a space (%r)z, r   r   zInvalid header: %r
z
)r   _headersr@   isspacer!   popstripappendr0   tupler#   MULTILINE_REsublowerfiltered_headers)	r	   messagerI   r@   Zfull_headerZlast_header
last_valuerB   headerr
   r
   r   r3      s6    



zSendRequest.parse_headersc             C   s"   t jdk r|tjtjfkrdS dS )N)      FT)sysversion_infohttplibr   r   )r	   r;   r
   r
   r   r$      s    zSendRequest._timeout_supportedN)__name__
__module____qualname____doc__r^   r   r   r   rK   rV   recompilerS   r3   r$   r
   r
   r
   r   r      s   W
&ENODATA)errnor\   rc   r^   ImportErrorhttp.clientclientZwebob.compatr   r-   Zwebobr   r   __all__r   r   ZECONNREFUSEDr2   hasattrre   r
   r
   r
   r   <module>   s"    "
