Rer @s2dZddlZddlZddlZddlmZddlmZddlm Z ddl m Z m Z m Z mZmZddlmZmZmZmZdd lmZmZmZdd lmZmZdd lmZdd lmZm Z m!Z!dd l"m#Z#m$Z$m%Z%m&Z&ddl'm(Z(ddl)m*Z*ddlm+Z+m,Z,m-Z-m.Z.m/Z/m0Z0ddl1m2Z2ddlm3Z3ej4dkry ej5Z6Wne7k rej8Z6YnXn ejZ6eddZ9eddZ:Gddde;Z<Gddde<Z=ddZ>dS)z requests.session ~~~~~~~~~~~~~~~~ This module provides a Session object to manage and persist settings across requests (cookies, auth, proxies). N) timedelta) OrderedDict)_basic_auth_str) cookielibis_py3urljoinurlparseMapping)cookiejar_from_dictextract_cookies_to_jarRequestsCookieJar merge_cookies)RequestPreparedRequestDEFAULT_REDIRECT_LIMIT) default_hooks dispatch_hook)to_native_string)to_key_val_listdefault_headers DEFAULT_PORTS)TooManyRedirects InvalidSchemaChunkedEncodingErrorContentDecodingError)CaseInsensitiveDict) HTTPAdapter) requote_uriget_environ_proxiesget_netrc_authshould_bypass_proxiesget_auth_from_url rewind_body)codes)REDIRECT_STATIwin32cCs|dkr|S|dkr |St|to;t|tsB|S|t|}|jt|dd|jD}x|D] }||=qW|S)zDetermines appropriate setting for a given request, taking into account the explicit setting on that request, and the setting in the session. If a setting is a dictionary, they will be merged together using `dict_class` NcSs(g|]\}}|dkr|qS)N).0kvr'r'/builddir/build/BUILDROOT/alt-python35-pip-20.2.4-5.el8.x86_64/opt/alt/python35/lib/python3.5/site-packages/pip/_vendor/requests/sessions.py Js z!merge_setting..) isinstancer rupdateitems)request_settingsession_setting dict_classmerged_setting none_keyskeyr'r'r+ merge_setting2s    r6cCsZ|dks!|jdgkr%|S|dksF|jdgkrJ|St|||S)zProperly merges both requests and session hooks. This is necessary because when request_hooks == {'response': []}, the merge breaks Session hooks entirely. Nresponse)getr6) request_hooks session_hooksr2r'r'r+ merge_hooksQs !!r;c@sjeZdZddZddZdddddddd Zd d Zd d ZddZdS)SessionRedirectMixincCs<|jr8|jd}tr+|jd}t|dSdS)z7Receives a Response. Returns a redirect URI or ``None``locationlatin1utf8N) is_redirectheadersrencoder)selfrespr=r'r'r+get_redirect_targetbs    z(SessionRedirectMixin.get_redirect_targetcCst|}t|}|j|jkr.dS|jdkrn|jdkrn|jdkrn|jd krndS|j|jk}|j|jk}tj|jddf}| r|j|kr|j|krdS|p|S) zFDecide whether Authorization header should be removed when redirectingThttpPNhttpsF)rGN)rIN)r hostnameschemeportrr8)rCold_urlnew_url old_parsed new_parsed changed_portchanged_scheme default_portr'r'r+should_strip_authws  z&SessionRedirectMixin.should_strip_authFNTc ks,g} |j|} t|jj} x| r'|j} | j|| dd|_y |jWn.tt t fk r|j j ddYnXt |j|jkrtdj|jd||j| jdrt|j}djt|j| g} t| }|jd krJ| rJ|jd | }n|jr\|j} |j} |jst|jt| } n t| } t| | _|j| ||jtjtjfkrd}x!|D]}| j j!|dqWd| _"| j }|j!ddt#| j$||j t%| j$|j&| j'| j$|j(| |}|j)| || j*dk od |kpd |k}|rt+| | }|r|Vq*|j,|d|d|d|d|d|dd| }t#|j&| |j |j|} |Vq*WdS)zBReceives a Response. Returns a generator of Responses or Requests.rNdecode_contentFzExceeded {} redirects.r7z//:fragmentContent-Length Content-TypeTransfer-EncodingCookiestreamtimeoutverifycertproxiesallow_redirects)rYrZr[)-rEr urlrXcopyappendhistorycontentrr RuntimeErrorrawreadlen max_redirectsrformatclose startswithjoinrrK_replacegeturlnetlocrrrebuild_method status_coder$temporary_redirectpermanent_redirectrApopbodyr _cookiesrcookiesprepare_cookiesrebuild_proxies rebuild_auth_body_positionr#send)rCrDreqr]r^r_r`rayield_requestsadapter_kwargshistrcprevious_fragmentprepared_request parsed_rurlparsedpurged_headersheaderrA rewindabler'r'r+resolve_redirectssv                z&SessionRedirectMixin.resolve_redirectscCsu|j}|j}d|kr=|j|jj|r=|d=|jrRt|nd}|dk rq|j|dS)zWhen being redirected we may want to strip authentication from the request to avoid leaking credentials. This method intelligently removes and reapplies authentication where possible to avoid credential loss. AuthorizationN)rArcrTrequest trust_envr prepare_auth)rCrr7rArcnew_authr'r'r+r~s  $ z!SessionRedirectMixin.rebuild_authc Cs*|dk r|ni}|j}|j}t|j}|j}|jd}t|d|}|jr| rt|d|} | j|| jd} | r|j || d|kr|d=yt ||\} } Wnt k rd\} } YnX| r&| r&t | | |d<|S)aThis method re-evaluates the proxy configuration by considering the environment variables. If we are redirected to a URL covered by NO_PROXY, we strip the proxy configuration. Otherwise, we set missing proxy keys for this URL (in case they were stripped by a previous redirect). This method also replaces the Proxy-Authorization header where necessary. :rtype: dict Nno_proxyallzProxy-Authorization)NN) rArcr rKrdr8r!rr setdefaultr"KeyErrorr) rCrrarArcrK new_proxiesr bypass_proxyenviron_proxiesproxyusernamepasswordr'r'r+r}s*       z$SessionRedirectMixin.rebuild_proxiescCs|j}|jtjkr-|dkr-d}|jtjkrQ|dkrQd}|jtjkru|dkrud}||_dS)zWhen being redirected we may want to change the method of the request based on certain specs or browser behavior. HEADGETPOSTN)methodrur$ see_otherfoundmoved)rCrr7rr'r'r+rt:s z#SessionRedirectMixin.rebuild_method) __name__ __module__ __qualname__rErTrr~r}rtr'r'r'r+r<`s   m  )r<c@sZeZdZdZdddddddd d d d d g ZddZddZddZddZddddddddddddddddZ ddZ ddZ ddZ ddd d!Z dd"d#Zdd$d%Zd&d'Zd(d)Zd*d+Zd,d-Zd.d/Zd0d1Zd2d3Zd4d5ZdS)6SessionaA Requests session. Provides cookie persistence, connection-pooling, and configuration. Basic Usage:: >>> import requests >>> s = requests.Session() >>> s.get('https://httpbin.org/get') Or as a context manager:: >>> with requests.Session() as s: ... s.get('https://httpbin.org/get') rAr{authrahooksparamsr_r`adaptersr]rrlcCst|_d|_i|_t|_i|_d|_d|_d|_ t |_ d|_ t i|_t|_|jdt|jdtdS)NFTzhttps://zhttp://)rrArrarrrr]r_r`rrlrr r{rrmountr)rCr'r'r+__init__js           zSession.__init__cCs|S)Nr')rCr'r'r+ __enter__szSession.__enter__cGs|jdS)N)rn)rCargsr'r'r+__exit__szSession.__exit__cCs$|jp i}t|tjs-t|}ttt|j|}|j}|jr}| r}|j r}t |j }t }|j d|j jd|j d|jd|jd|jdt|j|jdtdt|j|jd t||jd |d t|j|j |S) aConstructs a :class:`PreparedRequest ` for transmission and returns it. The :class:`PreparedRequest` has settings merged from the :class:`Request ` instance and those of the :class:`Session`. :param request: :class:`Request` instance to prepare with this session's settings. :rtype: requests.PreparedRequest rrcfilesdatajsonrAr2rrr{r)r{r-r CookieJarr rr rrr rcrpreparerupperrrrr6rArrr;r)rCrr{merged_cookiesrpr'r'r+prepare_requests*         zSession.prepare_requestNTcCstd|jd|d|d|d|p-id|d|p?id|d |d | }|j|}| poi} |j|j| | ||}d | d | i}|j||j||}|S) aConstructs a :class:`Request `, prepares it and sends it. Returns :class:`Response ` object. :param method: method for the new :class:`Request` object. :param url: URL for the new :class:`Request` object. :param params: (optional) Dictionary or bytes to be sent in the query string for the :class:`Request`. :param data: (optional) Dictionary, list of tuples, bytes, or file-like object to send in the body of the :class:`Request`. :param json: (optional) json to send in the body of the :class:`Request`. :param headers: (optional) Dictionary of HTTP Headers to send with the :class:`Request`. :param cookies: (optional) Dict or CookieJar object to send with the :class:`Request`. :param files: (optional) Dictionary of ``'filename': file-like-objects`` for multipart encoding upload. :param auth: (optional) Auth tuple or callable to enable Basic/Digest/Custom HTTP Auth. :param timeout: (optional) How long to wait for the server to send data before giving up, as a float, or a :ref:`(connect timeout, read timeout) ` tuple. :type timeout: float or tuple :param allow_redirects: (optional) Set to True by default. :type allow_redirects: bool :param proxies: (optional) Dictionary mapping protocol or protocol and hostname to the URL of the proxy. :param stream: (optional) whether to immediately download the response content. Defaults to ``False``. :param verify: (optional) Either a boolean, in which case it controls whether we verify the server's TLS certificate, or a string, in which case it must be a path to a CA bundle to use. Defaults to ``True``. :param cert: (optional) if String, path to ssl client cert file (.pem). If Tuple, ('cert', 'key') pair. :rtype: requests.Response rrcrArrrrrr{rr^rb)rrrmerge_environment_settingsrcr.r)rCrrcrrrAr{rrr^rbrarr]r_r`rrprepsettings send_kwargsrDr'r'r+rs()       zSession.requestcKs#|jdd|jd||S)zSends a GET request. Returns :class:`Response` object. :param url: URL for the new :class:`Request` object. :param \*\*kwargs: Optional arguments that ``request`` takes. :rtype: requests.Response rbTr)rr)rCrckwargsr'r'r+r8sz Session.getcKs#|jdd|jd||S)zSends a OPTIONS request. Returns :class:`Response` object. :param url: URL for the new :class:`Request` object. :param \*\*kwargs: Optional arguments that ``request`` takes. :rtype: requests.Response rbTOPTIONS)rr)rCrcrr'r'r+options!szSession.optionscKs#|jdd|jd||S)zSends a HEAD request. Returns :class:`Response` object. :param url: URL for the new :class:`Request` object. :param \*\*kwargs: Optional arguments that ``request`` takes. :rtype: requests.Response rbFr)rr)rCrcrr'r'r+head,sz Session.headcKs|jd|d|d||S)aSends a POST request. Returns :class:`Response` object. :param url: URL for the new :class:`Request` object. :param data: (optional) Dictionary, list of tuples, bytes, or file-like object to send in the body of the :class:`Request`. :param json: (optional) json to send in the body of the :class:`Request`. :param \*\*kwargs: Optional arguments that ``request`` takes. :rtype: requests.Response rrr)r)rCrcrrrr'r'r+post7s z Session.postcKs|jd|d||S)auSends a PUT request. Returns :class:`Response` object. :param url: URL for the new :class:`Request` object. :param data: (optional) Dictionary, list of tuples, bytes, or file-like object to send in the body of the :class:`Request`. :param \*\*kwargs: Optional arguments that ``request`` takes. :rtype: requests.Response PUTr)r)rCrcrrr'r'r+putDs z Session.putcKs|jd|d||S)awSends a PATCH request. Returns :class:`Response` object. :param url: URL for the new :class:`Request` object. :param data: (optional) Dictionary, list of tuples, bytes, or file-like object to send in the body of the :class:`Request`. :param \*\*kwargs: Optional arguments that ``request`` takes. :rtype: requests.Response PATCHr)r)rCrcrrr'r'r+patchPs z Session.patchcKs|jd||S)zSends a DELETE request. Returns :class:`Response` object. :param url: URL for the new :class:`Request` object. :param \*\*kwargs: Optional arguments that ``request`` takes. :rtype: requests.Response DELETE)r)rCrcrr'r'r+delete\szSession.deletec Ks|jd|j|jd|j|jd|j|jd|jt|trgtd|jdd}|j d}|j }|j d|j }t }|j||}t |} td | |_td |||}|jr+x*|jD]} t|j| j| jqWt|j||j|rr|j|||} d d | D} ng} | r| jd || j}| |_|sy(t|j||dd||_Wntk rYnX|s|j|S)zISend a given PreparedRequest. :rtype: requests.Response r]r_r`raz#You can only send PreparedRequests.rbTrcsecondsr7cSsg|] }|qSr'r')r(rDr'r'r+r,s z Session.send..rr)rr]r_r`rar-r ValueErrorrxr8r get_adapterrcpreferred_clockrrelapsedrrfr r{rririnsertnext_next StopIterationrg) rCrrrbr]radapterstartrrrDgenrfr'r'r+rfsF       ( z Session.sendc Cs |jr|dk r$|jdnd}t|d|}x*|jD]\}} |j|| qIW|dks|dkrtjjdptjjd}t||j}t||j }t||j }t||j }d|d|d|d |iS) z^ Check the environment and merge it with some settings. :rtype: dict NrTREQUESTS_CA_BUNDLECURL_CA_BUNDLEr_rar]r`) rr8rr/rosenvironr6rar]r_r`) rCrcrar]r_r`r env_proxiesr)r*r'r'r+rs !z"Session.merge_environment_settingscCsXx<|jjD]+\}}|jj|jr|SqWtdj|dS)z~ Returns the appropriate connection adapter for the given URL. :rtype: requests.adapters.BaseAdapter z*No connection adapters were found for {!r}N)rr/lowerrorrm)rCrcprefixrr'r'r+rszSession.get_adaptercCs(x!|jjD]}|jqWdS)z+Closes all adapters and as such the sessionN)rvaluesrn)rCr*r'r'r+rnsz Session.closecsW||j<fdd|jD}x'|D]}|jj||j|.N)rrx)rCrr keys_to_mover5r')rr+rs  z Session.mountcs fddjD}|S)Ncs%i|]}t|d|qS)N)getattr)r(attr)rCr'r+ s z(Session.__getstate__..) __attrs__)rCstater')rCr+ __getstate__szSession.__getstate__cCs1x*|jD]\}}t|||q WdS)N)r/setattr)rCrrvaluer'r'r+ __setstate__szSession.__setstate__)rrr__doc__rrrrrrr8rrrrrrrrrrnrrrr'r'r'r+rQs2    7   ) D    K    rcCstS)aZ Returns a :class:`Session` for context-management. .. deprecated:: 1.0.0 This method has been deprecated since version 1.0.0 and is only kept for backwards compatibility. New code should use :class:`~requests.sessions.Session` to create a session. This may be removed at a future date. :rtype: Session )rr'r'r'r+sessions r)?rrsystimedatetimer collectionsrrrcompatrrrr r r{r r r rmodelsrrrrrr_internal_utilsrutilsrrr exceptionsrrrr structuresrrrrrr r!r"r# status_codesr$r%platform perf_counterrAttributeErrorclockr6r;objectr<rrr'r'r'r+ s<   ("".