U f|1@sddlmZddlZddlZddlZddlZddlZddlZddl Zddl Zddl m Z ddl mZddlmZmZddlmZddlmZeZed ZeeZed Zed Zd d dddZd d dddZ Gdddej!eZ"GdddeZ#GdddeZ$dS)) annotationsN) _TYPE_BODY)HTTPHeaderDict)HTTPSConnection_get_default_user_agent)ConnectionError)BaseHTTPResponseTs^[!#$%&'*+\-.^_`|~0-9a-z]+$s*[\0\x00\x0a\x0d\r\n]|^[ \r\n\t]|[ \r\n\t]$bytesbool)namereturncCstt|S)a5 "An implementation that validates fields according to the definitions in Sections 5.1 and 5.5 of [HTTP] only needs an additional check that field names do not include uppercase characters." (https://httpwg.org/specs/rfc9113.html#n-field-validity) `http.client._is_legal_header_name` does not validate the field name according to the HTTP 1.1 spec, so we do that here, in addition to checking for uppercase characters. This does not allow for the `:` character in the header name, so should not be used to validate pseudo-headers. )r RE_IS_LEGAL_HEADER_NAMEmatch)r rI/opt/alt/python38/lib/python3.8/site-packages/urllib3/http2/connection.py_is_legal_header_names r)valuercCstt|S)aK "A field value MUST NOT contain the zero value (ASCII NUL, 0x00), line feed (ASCII LF, 0x0a), or carriage return (ASCII CR, 0x0d) at any position. A field value MUST NOT start or end with an ASCII whitespace character (ASCII SP or HTAB, 0x20 or 0x09)." (https://httpwg.org/specs/rfc9113.html#n-field-validity) )r RE_IS_ILLEGAL_HEADER_VALUEsearch)rrrr_is_illegal_header_value,src@sDeZdZdZdZddddZdddd Zd d d d dddZdS) _LockedObjectaX A wrapper class that hides a specific object behind a lock. The goal here is to provide a simple way to protect access to an object that cannot safely be simultaneously accessed from multiple threads. The intended use of this class is simple: take hold of it with a context manager, which returns the protected object. )lock_objr )objcCst|_||_dSN) threadingRLockrr)selfrrrr__init__Ds z_LockedObject.__init__rcCs|j|jSr)racquirerrrrr __enter__Hs z_LockedObject.__enter__ztype[BaseException] | NonezBaseException | Noneztypes.TracebackType | NoneNone)exc_typeexc_valexc_tbrcCs|jdSr)rrelease)rr&r'r(rrr__exit__Lsz_LockedObject.__exit__N)__name__ __module__ __qualname____doc__ __slots__r r$r*rrrrr6s rc seZdZd-dddddfdd Zd d d d Zdd fd d ZdddddddZddddddZd.dddddZdddddZ d/dddddddd Z d!d d"d#Z d0d$d$d$d%ddd&dd'd'd'ddd( d)d*Z dd fd+d, Z ZS)1HTTP2ConnectionNstrz int | Nonez typing.Anyr%)hostportkwargsrc sV||_d|_g|_d|ks&d|kr.tdtj||f||jdk rRtddS)Nproxy proxy_configz$Proxies aren't supported with HTTP/2z%Tunneling isn't supported with HTTP/2) _new_h2_conn_h2_conn _h2_stream_headersNotImplementedErrorsuperr _tunnel_host)rr2r3r4 __class__rrr Vs  zHTTP2Connection.__init__z)_LockedObject[h2.connection.H2Connection]r!cCs tjjdd}ttjj|dS)NT)Z client_side)config)h2r@ZH2Configurationr connectionZ H2Connection)rr@rrrr7eszHTTP2Connection._new_h2_connc s@t|j&}||}r2|j|W5QRXdSr)r<connectr8Zinitiate_connection data_to_sendsocksendall)rconnrDr>rrrCis   zHTTP2Connection.connect)methodurlr4rc Ksd|krtdd|kr td|p&d|_||d|jkrXd|jd|jpPd }n|jd|jphd }|jd |jd |f|jd |f|jd |f|j}| |_ W5QRXdS)zputrequest This deviates from the HTTPConnection method signature since we never need to override sending accept-encoding headers or the host header. skip_hostz`skip_host` isn't supportedskip_accept_encodingz&`skip_accept_encoding` isn't supported/:[z]:i)s:schemeshttpss:methods :authoritys:pathN) r; _request_url_validate_pathr2r3r:appendencoder8Zget_next_available_stream_idr9)rrHrIr4 authorityrGrrr putrequestps     zHTTP2Connection.putrequestz str | bytes)headervaluesrcGst|tr|n|}|}t|s8tdt||D]D}t|trR|n|}t|rptdt||j||fq|j|q>| |jW5QRdSt |t r| }zzt |tr|j |j|dd|}r|j|n@|D].}|j |j|dd|}r|j|q| |jWn&tk rZtdt|YnXW5QRXdS)zSend data to the server. `data` can be: `str`, `bytes`, an iterable, or file-like objects that support a .read() method. Nr\readF)r_Tz6`data` should be str, bytes, iterable, or file. got %r)r9rr8rDrErFhasattrrc blocksizerWr1rRZ send_datar_r TypeErrortype)rrbrGrDchunkrrrsendsD             zHTTP2Connection.sendhttpztyping.Mapping[str, str] | None)r2r3r^schemercCs tddS)Nz;HTTP/2 does not support setting up a tunnel through a proxy)r;)rr2r3r^rkrrr set_tunnelszHTTP2Connection.set_tunnel HTTP2Responsec Csd}t}|j}d}|s|jd}r||}|D]}t|tjjrt }|j D]6\} } | dkrtt | }qV| | d| dqVq8t|tjjr||j7}||j|jq8t|tjjr8d}q8|} r|j| qW5QRX|dk stt|||jt|dS)NFis:statusasciiT)statusr^ request_urlrb) bytearrayr8rErecvZ receive_datarWrAeventsZResponseReceivedrr^intdecodeaddZ DataReceivedrbZacknowledge_received_dataZflow_controlled_lengthr]Z StreamEndedrDrFAssertionErrorrmrOr ) rrorbrGr_Z received_datarseventr^rUrrDrrr getresponsesD    zHTTP2Connection.getresponseT)preload_contentdecode_contentenforce_content_lengthz_TYPE_BODY | Noner ) rHrIbodyr^rzr{r|r4rc Ksd|kr|jdk r |j|j||||p2i}|D],\} } | dkr\| dkr\qrrr3s  zHTTP2Connection.close)N)N)NNrj)NN)r+r,r-r r7rCrTrZrarirlryrr __classcell__rrr>rr0Us* 0 ."'r0cs\eZdZddddddddfd d Zedd d d Zdd ddZdd ddZZS)rmFrtrr1r r r%)ror^rprbr{rc s*tj||ddd||d||_d|_dS)NzHTTP/2)ror^versionversion_stringreasonr{rpr)r<r _datalength_remaining)rror^rprbr{r>rrr Fs zHTTP2Response.__init__r!cCs|jSr)rr#rrrrb\szHTTP2Response.datacCsdSrrr#rrrget_redirect_location`sz#HTTP2Response.get_redirect_locationcCsdSrrr#rrrrcszHTTP2Response.close)F) r+r,r-r propertyrbrrrrrr>rrmDs rm)% __future__rloggingrertypestypingZ h2.configrAZ h2.connectionZ h2.eventsZ_base_connectionr _collectionsrrBrr exceptionsrresponser Zorig_HTTPSConnectionTypeVarr getLoggerr+logcompilerrrrGenericrr0rmrrrrs0          p