B a�N�'�@sxdZddlZddlZddlZddlmZddlmZdZGdd�de �Z dd d �Z Gd d �d e �Z d d�Z ddd�ZdS)a� An application that proxies WSGI requests to a remote server. TODO: * Send ``Via`` header? It's not clear to me this is a Via in the style of a typical proxy. * Other headers or metadata? I put in X-Forwarded-For, but that's it. * Signed data of non-HTTP keys? This would be for things like REMOTE_USER. * Something to indicate what the original URL was? The original host, scheme, and base path. * Rewriting ``Location`` headers? mod_proxy does this. * Rewriting body? (Probably not on this one -- that can be done with a different middleware that wraps this middleware) * Example:: use = egg:Paste#proxy address = http://server3:8680/exist/rest/db/orgs/sch/config/ allowed_request_methods = GET �N)�httpexceptions)�aslist)ztransfer-encodingZ connectionz keep-alivezproxy-authenticatezproxy-authorization�teZtrailers�upgradec@seZdZddd�Zdd�ZdS)�Proxy�cCs^||_t�|�|_|jd��|_|jd|_|jd|_dd�|D�|_dd�|D�|_ dS)Nr��cSsg|]}|r|���qSr)�lower)�.0�xrr�</opt/alt/python37/lib/python3.7/site-packages/paste/proxy.py� <listcomp>>sz"Proxy.__init__.<locals>.<listcomp>cSsg|]}|r|���qSr)r )r r rrr rAs) �address�urlparse�urlsplit�parsedr �scheme�host�path�allowed_request_methods�suppress_http_headers)�selfrrrrrr �__init__6s   zProxy.__init__cCs@|jr(|d��|jkr(t�d�||�S|jdkr:tj}n&|jdkrLtj}ntd|j |jf��||j �}i}xR|� �D]F\}}|� d�rx|dd���� dd �}|d ksx||jkr�qx|||<qxW|j |d <d |kr�|d |d <|�d �r�|d |d<|�d��rX|ddk�r0|d�d�}tt|��|d<n&|d|d<t|d�} |d�| �}nd}t�|d�} |j�r�| } | �r�| ddk�r�| dd�} t�|j| �} n| } |�d��r�| d|d7} |�|d| ||�|��} t| j�}d| j| jf}|||�| �d�} | dk �r*| �t| ��}n| ��}|� �|gS)N�REQUEST_METHODZ Disallowed�http�httpszUnknown scheme for %r: %r�HTTP_��_�-r� REMOTE_ADDRzx-forwarded-for� CONTENT_TYPEz content-type�CONTENT_LENGTHz-1z wsgi.input���zcontent-length�� PATH_INFOr�/r� QUERY_STRING�?z%s %s)!rr rZHTTPBadRequestr�httplib�HTTPConnection�HTTPSConnection� ValueErrorrr�items� startswith�replacer�get�read�str�len�int�urllib�quoterr�urljoin�request� getresponse� parse_headers�msg�status�reason� getheader�close)r�environ�start_response� ConnClass�conn�headers�key�value�body�lengthZ path_infoZ request_pathr�res� headers_outr=rrr �__call__Csh                   zProxy.__call__N)rr)�__name__� __module__� __qualname__rrLrrrr r4s rr%cCst|�}t|�}t|||d�S)a� Make a WSGI application that proxies to another address: ``address`` the full URL ending with a trailing ``/`` ``allowed_request_methods``: a space seperated list of request methods (e.g., ``GET POST``) ``suppress_http_headers`` a space seperated list of http headers (lower case, without the leading ``http_``) that should not be passed on to target host )rr)rr)� global_confrrrrrr � make_proxy�s rQc@s*eZdZdZd dd�Zdd�Zdd �ZdS) �TransparentProxya$ A proxy that sends the request just as it was given, including respecting HTTP_HOST, wsgi.url_scheme, etc. This is a way of translating WSGI requests directly to real HTTP requests. All information goes in the environment; modify it to modify the way the request is made. If you specify ``force_host`` (and optionally ``force_scheme``) then HTTP_HOST won't be used to determine where to connect to; instead a specific host will be connected to, but the ``Host`` header in the request will remain intact. NrcCs||_||_dS)N)� force_host� force_scheme)rrSrTrrr r�szTransparentProxy.__init__cCs d|jjtt|��|j|jfS)Nz%<%s %s force_host=%r force_scheme=%r>)� __class__rM�hex�idrSrT)rrrr �__repr__�s zTransparentProxy.__repr__cCs|d}|jdkr|}n|j}|dkr.tj}n|dkr>tj}n td|��d|krZtd��|d}|jdkrr|}n|j}||�}i} x>|��D]2\} } | �d�r�| dd���� d d �} | | | <q�W|| d <d |kr�d |kr�|d | d<|� d�r�|d| d<|� d��r@t |d�} |d� | �} | dk�r\t t| ��|d<nd|k�rTd} d} nd} d} |� dd�|� dd�}t�|�}d|k�r�|d|d7}|�|d|| | �|��}t|j�}d|j|jf}|||�|�d�} | dk �r�|� t | ��} n|� �} |��| gS)Nzwsgi.url_schemerrzUnknown scheme %rZ HTTP_HOSTz*WSGI environ must contain an HTTP_HOST keyrrrr rr!ZHTTP_X_FORWARDED_FORzx-forwarded-forr"z content-typer#z wsgi.inputr$r%rZ SCRIPT_NAMEr&r(r)rz%s %szcontent-length)rSrTr*r+r,r-r.r/r r0r1r5r2r3r4r6r7r9r:r;r<r=r>r?r@)rrArBrZ conn_schemerCrZ conn_hostrDrErFrGrIrHrrJrKr=rrr rL�sn                     zTransparentProxy.__call__)Nr)rMrNrO�__doc__rrXrLrrrr rR�s  rRcCs�g}x�|jD]�}|sq |d��r^|s2td|��|��\}}|d|��}|�||f�q y|�dd�\}}Wntd|��YnX|��}|��tkr |�||f�q W|S)zB Turn a Message object into a list of WSGI-style headers. rz%First header starts with a space (%r)� �:rzInvalid header: %r) rE�isspacer-�pop�strip�append�splitr �filtered_headers)�messagerKZ full_headerZ last_header� last_valuerG�headerrrr r;�s(     r;rcCs t||d�S)z� Create a proxy that connects to a specific host, but does absolutely no other filtering, including the Host header. )rSrT)rR)rPrSrTrrr �make_transparent_proxysre)r%r%)Nr)rYr*rr6�pasterZpaste.util.convertersrra�objectrrQrRr;rerrrr �<module>s   Q \