
    il
                     B    d dl mZ d dlmZ d dlmZ  G d d      Zd Zy)    )urlparse)WSGIRequestHandler)settingsc                       e Zd ZdZd Zd Zy)ReverseProxyPathFixag  Wrap the application in this middleware and configure the
    front-end server to add these headers, to let you quietly bind
    this to a URL other than / and to an HTTP scheme that is
    different than what is used locally.

    http://flask.pocoo.org/snippets/35/

    In nginx:
    location /myprefix {
        proxy_pass http://127.0.0.1:8000;
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header X-Script-Name /myprefix;
        }

    :param wsgi_app: the WSGI application
    c                 D   || _         d | _        d | _        d | _        t        d   d   rxt        t        d   d         }|j                  | _        | j                  j                  d      r| j                  d d | _        |j                  | _        |j                  | _        y y )Nserverbase_url/)	wsgi_appscript_nameschemer	   r   r   pathendswithnetloc)selfr   r
   s      /root/searxng/searx/flaskfix.py__init__zReverseProxyPathFix.__init__!   s     Hj)
   2: >?H'}}D((- $(#3#3CR#8 "//DK"//DK *    c                    | j                   xs |j                  dd      }|r,||d<   |d   }|j                  |      r|t        |      d  |d<   | j                  xs$ |j                  d      xs |j                  d      }|r||d<   | j
                  xs |j                  dd      }|r||d	<   | j                  ||      S )
NHTTP_X_SCRIPT_NAME SCRIPT_NAME	PATH_INFOHTTP_X_SCHEMEHTTP_X_FORWARDED_PROTOzwsgi.url_schemeHTTP_X_FORWARDED_HOST	HTTP_HOST)r   get
startswithlenr   r	   r   )r   environstart_responser   	path_infor   r	   s          r   __call__zReverseProxyPathFix.__call__6   s    &&O'++6JB*O%0GM",I##K0'0[1A1C'D$eO <eLd@e)/G%&H,CR H#)GK }}Wn55r   N)__name__
__module____qualname____doc__r   r&    r   r   r   r      s    ***6r   r   c                     dj                  t        d   d         t        _        t	        | j
                        | _        y )NzHTTP/{}r	   http_protocol_version)formatr   r   protocol_versionr   r   )apps    r   patch_applicationr1   H   s1    *3*:*:8H;MNe;f*g'&s||4CLr   N)urllib.parser   werkzeug.servingr   searxr   r   r1   r+   r   r   <module>r5      s     " / :6 :6z5r   