U a@sdZddlZddlmZddlmZddlmZddlm Z ddlm Z dd l m Z ej r~dd lmZdd lmZdd lmZGd ddZdS)zt Basic HTTP Proxy ================ .. autoclass:: ProxyMiddleware :copyright: 2007 Pallets :license: BSD-3-Clause N)client)EnvironHeaders)is_hop_by_hop_header) url_parse) url_quote)get_input_stream) StartResponse)WSGIApplication)WSGIEnvironmentc@sveZdZdZddejeejeejffe e ddddZ ejeejfeedd d d Z d d ej e dddZdS)ProxyMiddlewareaProxy requests under a path to an external server, routing other requests to the app. This middleware can only proxy HTTP requests, as HTTP is the only protocol handled by the WSGI server. Other protocols, such as WebSocket requests, cannot be proxied at this layer. This should only be used for development, in production a real proxy server should be used. The middleware takes a dict mapping a path prefix to a dict describing the host to be proxied to:: app = ProxyMiddleware(app, { "/static/": { "target": "http://127.0.0.1:5001/", } }) Each host has the following options: ``target``: The target URL to dispatch to. This is required. ``remove_prefix``: Whether to remove the prefix from the URL before dispatching it to the target. The default is ``False``. ``host``: ``""`` (default): The host header is automatically rewritten to the URL of the target. ``None``: The host header is unmodified from the client request. Any other value: The host header is overwritten with the value. ``headers``: A dictionary of headers to be sent with the request to the target. The default is ``{}``. ``ssl_context``: A :class:`ssl.SSLContext` defining how to verify requests if the target is HTTPS. The default is ``None``. In the example above, everything under ``"/static/"`` is proxied to the server on port 5001. The host header is rewritten to the target, and the ``"/static/"`` prefix is removed from the URLs. :param app: The WSGI application to wrap. :param targets: Proxy target configurations. See description above. :param chunk_size: Size of chunks to read from input stream and write to target. :param timeout: Seconds before an operation to a target fails. .. versionadded:: 0.14 @ r N)apptargets chunk_sizetimeoutreturncsVtjttjftjttjfddd||_fdd|D|_||_||_dS)N)optsrcSs4|dd|dd|di|dd|S)N remove_prefixFhostheaders ssl_context) setdefault)rr_C:\Users\vtejo\AppData\Local\Temp\pip-unpacked-wheel-1tps7o9u\werkzeug\middleware\http_proxy.py _set_defaultsVs     z/ProxyMiddleware.__init__.._set_defaultscs(i|] \}}d|dd|qS)/)strip.0kvrrr ^sz,ProxyMiddleware.__init__..) tDictstrAnyritemsrrr)selfrrrrrr$r__init__Os( zProxyMiddleware.__init__)rpathprefixrcsDtdttjddtjtdfdd }|S)Ntargetr r environstart_responsercstt|}dd|D|dd<|dddkrL|dfn2ddkrl|d|dfn|ddf|d}d r|tdd }jd d |}| d }d }|d kr|d|fn|dk r|dd}z4j dkr2t j j p&djd}n@j dkr`t jj pNdjdd}ntdj d|t|}|d}|r|d|}|j|d|dd|D](\} } | dkrd} || | q|t|} | j} | sq2|r$|d t| | fn || q|Wn.tk rjd!d"lm} | ||YSX|jd#j d$d!Dt"j#t$d%fd&d' }|S)(NcSs,g|]$\}}t|s|dkr||fqS))zcontent-lengthr)rlowerr rrr ns zAProxyMiddleware.proxy_to..application..) ConnectioncloserrHostZ HTTP_HOSTrrrCONTENT_LENGTHF)NzContent-Length)zTransfer-EncodingchunkedThttpP)rhttpsir)rcontextz-Target scheme must be 'http' or 'https', got . QUERY_STRING?REQUEST_METHOD) skip_host connectionr6s%x %s r) BadGateway cSs$g|]\}}t|s||fqSr)rtitler rrrr4s)rc3s<zj}Wntk r(Yq8YnX|s0q8|VqdS)N)readrOSError)data)respr+rrrHs z;ProxyMiddleware.proxy_to..application..read)%listrr*appendextendlenlstripr-rstripgetschemerHTTPConnectionportrHTTPSConnection RuntimeErrorconnectr putrequestr3 putheader endheadersrrHrsend getresponserI exceptionsrEstatusreason getheadersr&Iteratorbytes)r1r2rZ remote_pathcontent_lengthr:con remote_urlZ querystringr"r#streamrJrErHrrr-r.r+r/)rKr applicationjs                 z-ProxyMiddleware.proxy_to..application)rr&castr(Z ascii_hostIterablerc)r+rr-r.rirrhrproxy_tods ozProxyMiddleware.proxy_tor r r0cCsH|d}|j}|jD]$\}}||r||||}q>q|||S)NZ PATH_INFO)rrr* startswithrl)r+r1r2r-rr.rrrr__call__s zProxyMiddleware.__call__)r r)__name__ __module__ __qualname____doc__r&Mappingr(r'r)intr,rlrkrcrnrrrrr s&9  xr )rrtypingr&r;rZdatastructuresrrurlsrrZwsgir TYPE_CHECKINGZ_typeshed.wsgir r r r rrrrs