o ?Ogg.@sdZgdZddlZddlZddlZddlZddlZddlZddl Zddl Z ddl Z ddlmZdZdaddZdZd Zee jjZed Zd Zd Zd dZdZddZgdZ gdZ!gZ"e!D] Z#e"$e#%qadiddZ&diddZ'dddddZ(edej)Z*ddZ+ddZ,edej)Z-edej.ej)BZ/ed ej0ej)BZ1d!d"Z2ed#ej0ej)BZ3d$d%Z4d&d'Z5ed(Z6ed)Z7ed*Z8ed+Z9d,d-Z:ed.Z;d/d0Zed5ej)Z?d6d7Z@d8d9ZAd:d;ZBdej)ZDd?d@ZEdAdBZFdCdDZGdEdFZHdGZIedHZJdIdJZKdKdLZLdMdNZMdOdPZNGdQdRdRZOGdSdTdTZPGdUdVdVePZQdWdXZRdYdZZSGd[d\d\ZTGd]d^d^ZUGd_d`d`eVZWGdadbdbeUZXdcddZYGdedfdfeXZZGdgdhdheXZ[dS)jaHTTP cookie handling for web clients. This module has (now fairly distant) origins in Gisle Aas' Perl module HTTP::Cookies, from the libwww-perl library. Docstrings, comments and debug strings in this code refer to the attributes of the HTTP cookie system as cookie-attributes, to distinguish them clearly from Python attributes. Class diagram (note that BSDDBCookieJar and the MSIE* classes are not distributed with the Python standard library, but are available from http://wwwsearch.sf.net/): CookieJar____ / \ \ FileCookieJar \ \ / | \ \ \ MozillaCookieJar | LWPCookieJar \ \ | | \ | ---MSIEBase | \ | / | | \ | / MSIEDBCookieJar BSDDBCookieJar |/ MSIECookieJar )Cookie CookieJar CookiePolicyDefaultCookiePolicy FileCookieJar LWPCookieJar LoadErrorMozillaCookieJarN)timegmFcGs(tsdStsddl}|datj|S)Nr zhttp.cookiejar)debugloggerloggingZ getLogger)argsr r5/opt/alt/python310/lib64/python3.10/http/cookiejar.py_debug,s   rZHTTPOnlyz #HttpOnly_z#( Netscape)? HTTP Cookie FilezQa filename was not supplied (nor was the CookieJar instance initialised with one)zr# Netscape HTTP Cookie File # http://curl.haxx.se/rfc/cookie_spec.html # This is a generated file! Do not edit. cCsJddl}ddl}ddl}|}|d||}|jd|dddS)Nr zhttp.cookiejar bug! %s) stacklevel)iowarnings tracebackStringIO print_excgetvaluewarn)rrrfmsgrrr_warn_unhandled_exceptionBs  ricCs|dd\}}}}}}|tkrTd|krdkrTndSd|kr'dkrTndSd|kr4dkrTndSd|krAdkrTndSd|krQdkrTt|SdSdS) N r ;=) EPOCH_YEARr )ttyearmonthZmdayhourminsecrrr_timegmQsr,)ZMonZTueZWedZThuZFriZSatZSun) ZJanZFebZMarZAprZMayZJunZJulZAugZSepZOctZNovZDeccCs@|dur tj}ntj|}d|j|j|j|j|j|jfS)aHReturn a string representing time in seconds since epoch, t. If the function is called without an argument, it will use the current time. The format of the returned string is like "YYYY-MM-DD hh:mm:ssZ", representing Universal Time (UTC, aka GMT). An example of this format is: 1994-11-24 08:49:37Z Nz%04d-%02d-%02d %02d:%02d:%02dZ) datetimeutcnowutcfromtimestampr'r(dayr)minutesecondtZdtrrr time2isoz_s   r5cCsR|dur tj}ntj|}dt||jt|jd|j|j |j |j fS)zReturn a string representing time in seconds since epoch, t. If the function is called without an argument, it will use the current time. The format of the returned string is like this: Wed, DD-Mon-YYYY HH:MM:SS GMT Nz#%s, %02d-%s-%04d %02d:%02d:%02d GMTr) r-r.r/DAYSZweekdayr0MONTHSr(r'r)r1r2r3rrr time2netscapers  r8)ZGMTUTCZUTZz^([-+])?(\d\d?):?(\d\d)?$cCsld}|tvr d}|St|}|r4dt|d}|dr*|dt|d}|ddkr4| }|S)Nr ir<r-) UTC_ZONES TIMEZONE_REsearchintgroup)tzoffsetmrrroffset_from_tz_strings  rFc Cst|}|tjkr dSz t|d}Wn/tyEzt|}Wn ty.YYdSwd|kr9dkr@nYdS|}nYdSYnw|durLd}|durRd}|durXd}t|}t|}t|}t|}|dkrttd}|d} |} ||| }| | } t | dkr| dkr|d}n|d}t |||||||f} | dur|durd}| }t |} | durdS| | } | S)Nrr r id2r9) rAr-ZMAXYEAR MONTHS_LOWERindexlower ValueErrortime localtimeabsr,upperrF) r0monyrhrr*r+rCZimonZcur_yrrEtmpr4rDrrr _str2timesV        rUzV^[SMTWF][a-z][a-z], (\d\d) ([JFMASOND][a-z][a-z]) (\d\d\d\d) (\d\d):(\d\d):(\d\d) GMT$z+^(?:Sun|Mon|Tue|Wed|Thu|Fri|Sat)[a-z]*,?\s*a^ (\d\d?) # day (?:\s+|[-\/]) (\w+) # month (?:\s+|[-\/]) (\d+) # year (?: (?:\s+|:) # separator before clock (\d\d?):(\d\d) # hour:min (?::(\d\d))? # optional seconds )? # optional clock \s* (?: ([-+]?\d{2,4}|(?![APap][Mm]\b)[A-Za-z]+) # timezone \s* )? (?: \(\w+\) # ASCII representation of timezone in parens. \s* )?$c Cst|}|r6|}t|dd}t|d|t|dt|dt|dt|df}t|S| }t d|d}dgd \}}}}}} } t |}|durb|\}}}}}} } ndSt |||||| | S) aReturns time in seconds since epoch of time represented by a string. Return value is an integer. None is returned if the format of str is unrecognized, the time is outside the representable range, or the timezone string is not recognized. If the string contains no timezone, UTC is assumed. The timezone in the string may be numerical (like "-0800" or "+0100") or a string timezone (like "UTC", "GMT", "BST" or "EST"). Currently, only the timezone strings equivalent to UTC (zero offset) are known to the function. The function loosely parses the following formats: Wed, 09 Feb 1994 22:23:32 GMT -- HTTP format Tuesday, 08-Feb-94 14:15:29 GMT -- old rfc850 HTTP format Tuesday, 08-Feb-1994 14:15:29 GMT -- broken rfc850 HTTP format 09 Feb 1994 22:23:32 GMT -- HTTP format (no weekday) 08-Feb-94 14:15:29 GMT -- rfc850 format (no weekday) 08-Feb-1994 14:15:29 GMT -- broken rfc850 format (no weekday) The parser ignores leading and trailing whitespace. The time may be absent. If the year is given with only 2 digits, the function will select the century that makes the year closest to the current date. rrr r;N)STRICT_DATE_REr@groupsrIrJrKrAfloatr,lstrip WEEKDAY_REsubLOOSE_HTTP_DATE_RErU) textrEgrQr&r0rRrSr*r+rCrrr http2times  rca^ (\d{4}) # year [-\/]? (\d\d?) # numerical month [-\/]? (\d\d?) # day (?: (?:\s+|[-:Tt]) # separator before clock (\d\d?):?(\d\d) # hour:min (?::?(\d\d(?:\.\d*)?))? # optional seconds (and fractional) )? # optional clock \s* (?: ([-+]?\d\d?:?(:?\d\d)? |Z|z) # timezone (Z is "zero meridian", i.e. GMT) \s* )?$c Csd|}dgd\}}}}}}}t|}|dur&|\}}}}}}}} ndSt|||||||S)av As for http2time, but parses the ISO 8601 formats: 1994-02-03 14:15:29 -0100 -- ISO 8601 format 1994-02-03 14:15:29 -- zone is optional 1994-02-03 -- only date 1994-02-03T14:15:29 -- Use T as separator 19940203T141529Z -- ISO 8601 compact format 19940203 -- only date NrY)r] ISO_DATE_REr@r[rU) rar0rQrRrSr*r+rCrE_rrriso2time3s  rfcCs*|d\}}|jd||j|dS)z)Return unmatched part of re.Match object.r N)spanstring)matchstartendrrr unmatchedTsrlz^\s*([^=\s;,]+)z&^\s*=\s*\"([^\"\\]*(?:\\.[^\"\\]*)*)\"z^\s*=\s*([^\s;,]*)z\\(.)c Csg}|D]{}|}g}|rxt|}|rRt|}|d}t|}|r3t|}|d}td|}nt|}|rHt|}|d}|}nd}| ||fn$| drk| dd}|rh| |g}n t dd|\}} |}|s |r| |q|S)amParse header values into a list of lists containing key,value pairs. The function knows how to deal with ",", ";" and "=" as well as quoted values after "=". A list of space separated tokens are parsed as if they were separated by ";". If the header_values passed as argument contains multiple values, then they are treated as if they were a single value separated by comma ",". This means that this function is useful for parsing header fields that follow this syntax (BNF as from the HTTP/1.1 specification, but we relax the requirement for tokens). headers = #header header = (token | parameter) *( [";"] (token | parameter)) token = 1* separators = "(" | ")" | "<" | ">" | "@" | "," | ";" | ":" | "\" | <"> | "/" | "[" | "]" | "?" | "=" | "{" | "}" | SP | HT quoted-string = ( <"> *(qdtext | quoted-pair ) <"> ) qdtext = > quoted-pair = "\" CHAR parameter = attribute "=" value attribute = token value = token | quoted-string Each header is represented by a list of key/value pairs. The value for a simple token (not part of a parameter) is None. Syntactically incorrect headers will not necessarily be parsed as you would want. This is easier to describe with some examples: >>> split_header_words(['foo="bar"; port="80,81"; discard, bar=baz']) [[('foo', 'bar'), ('port', '80,81'), ('discard', None)], [('bar', 'baz')]] >>> split_header_words(['text/html; charset="iso-8859-1"']) [[('text/html', None), ('charset', 'iso-8859-1')]] >>> split_header_words([r'Basic realm="\"foo\bar\""']) [[('Basic', None), ('realm', '"foobar"')]] rz\1N,z^[=\s;]*rX)HEADER_TOKEN_REr@rlrBHEADER_QUOTED_VALUE_REHEADER_ESCAPE_REr_HEADER_VALUE_RErstripappendr] startswithresubn) Z header_valuesresultraZ orig_textpairsrEnamevalueZnon_junkZ nr_junk_charsrrrsplit_header_words]s>.        r{([\"\\])cCs|g}|D]4}g}|D]#\}}|dur(td|s"td|}d|}d||f}||q |r8|d|qd|S)aDo the inverse (almost) of the conversion done by split_header_words. Takes a list of lists of (key, value) pairs and produces a single header value. Attribute values are quoted if needed. >>> join_header_words([[("text/plain", None), ("charset", "iso-8859-1")]]) 'text/plain; charset="iso-8859-1"' >>> join_header_words([[("text/plain", None)], [("charset", "iso-8859-1")]]) 'text/plain, charset="iso-8859-1"' Nz^\w+$\\\1z"%s"%s=%s; , )rur@HEADER_JOIN_ESCAPE_REr_rsjoin)Zlistsheadersrxattrkvrrrjoin_header_wordss       rcCs0|dr |dd}|dr|dd}|S)N"r)rtendswithrarrr strip_quotess    rc Csd}g}|D]s}g}d}t|dD]W\}}|}|d\}} } |}|s0|dkr/n>> reach("www.acme.com") '.acme.com' >>> reach("acme.com") 'acme.com' >>> reach("acme.local") '.local' rr rNZlocal)rr)hrbrrrreachs  rcCs t|}t|t|jsdSdS)z RFC 2965, section 3.3.6: An unverifiable transaction is to a third-party host if its request- host U does not domain-match the reach R of the request-host O in the origin transaction. TF)rrrZorigin_req_host)rrrrris_third_partys rc@sPeZdZdZ dddZddZddd Zd d Zdd d ZddZ ddZ dS)raHTTP Cookie. This class represents both Netscape and RFC 2965 cookies. This is deliberately a very simple class. It just holds attributes. It's possible to construct Cookie instances that don't comply with the cookie standards. CookieJar.make_cookies is the factory function for Cookie objects -- it deals with cookie parsing, supplying defaults, and normalising to the representation used in this class. CookiePolicy is responsible for checking them to see whether they should be accepted from and returned to the server. Note that the port may be present in the headers, but unspecified ("Port" rather than"Port=80", for example); if this is the case, port is None. FcCs|durt|}| durtt| } |dur|durtd||_||_||_||_||_||_ ||_ ||_ | |_ | |_ | |_| |_| |_||_||_||_t||_dS)NTz-if port is None, port_specified must be false)rAr\rLrryrzrport_specifiedrKrdomain_specifieddomain_initial_dotrpath_specifiedrrdiscardcomment comment_urlrfc2109copy_rest)selfrryrzrrrrrrrrrrrrrestrrrr__init__s*  zCookie.__init__cCs ||jvSNr)rryrrrhas_nonstandard_attr$ zCookie.has_nonstandard_attrNcCs|j||Sr)rget)rrydefaultrrrget_nonstandard_attr&zCookie.get_nonstandard_attrcCs||j|<dSrr)rryrzrrrset_nonstandard_attr(rzCookie.set_nonstandard_attrcCs,|durt}|jdur|j|krdSdSNTF)rMr)rnowrrr is_expired+szCookie.is_expiredcCsX|jdurd}nd|j}|j||j}|jdur#d|j|jf}n|j}d||fS)NrXrr~z)rrrrzry)rplimitZ namevaluerrr__str__1s   zCookie.__str__cCslg}dD]}t||}|d|t|fq|dt|j|dt|jd|jjd|fS)N)rryrzrrrrrrrrrrrrr~zrest=%sz rfc2109=%sz%s(%s)r)getattrrsreprrr __class____name__r)rrryrrrr__repr__;s zCookie.__repr__)Fr) r __module__ __qualname____doc__rrrrrrrrrrrrs *   rc@s0eZdZdZddZddZddZdd Zd S) ra Defines which cookies get accepted from and returned to server. May also modify cookies, though this is probably a bad idea. The subclass DefaultCookiePolicy defines the standard rules for Netscape and RFC 2965 cookies -- override that if you want a customized policy. cCt)zReturn true if (and only if) cookie should be accepted from server. Currently, pre-expired cookies never get this far -- the CookieJar class deletes such cookies itself. NotImplementedErrorrcookierrrrset_okSszCookiePolicy.set_okcCr)zAReturn true if (and only if) cookie should be returned to server.rrrrr return_ok\zCookiePolicy.return_okcCdS)zMReturn false if cookies should not be returned, given cookie domain. Tr)rrrrrrdomain_return_ok`zCookiePolicy.domain_return_okcCr)zKReturn false if cookies should not be returned, given cookie path. Tr)rrrrrrpath_return_okerzCookiePolicy.path_return_okN)rrrrrrrrrrrrrJs   rc @seZdZdZdZdZdZdZeeBZdddddddddeddd f d d Z d d Z ddZ ddZ ddZ ddZddZddZddZddZddZd d!Zd"d#Zd$d%Zd&d'Zd(d)Zd*d+Zd,d-Zd.d/Zd0d1Zd2d3Zd4d5Zd6d7ZdS)8rzBImplements the standard rules for accepting and returning cookies.rrrVr NTF)ZhttpsZwsscCsv||_||_||_||_||_||_| |_| |_| |_| |_ | |_ |dur+t ||_ nd|_ |dur6t |}||_ dS)zAConstructor arguments should be passed as keyword arguments only.Nr)netscaperfc2965rfc2109_as_netscape hide_cookie2 strict_domainstrict_rfc2965_unverifiablestrict_ns_unverifiablestrict_ns_domainstrict_ns_set_initial_dollarstrict_ns_set_pathsecure_protocolstuple_blocked_domains_allowed_domains)rblocked_domainsallowed_domainsrrrrrrrrrrrrrrrus"  zDefaultCookiePolicy.__init__cC|jS)z4Return the sequence of blocked domains (as a tuple).)rrrrrrrz#DefaultCookiePolicy.blocked_domainscCst||_dS)z$Set the sequence of blocked domains.N)rr)rrrrrset_blocked_domainssz'DefaultCookiePolicy.set_blocked_domainscCs |jD] }t||r dSqdSr)rr)rrZblocked_domainrrr is_blockeds  zDefaultCookiePolicy.is_blockedcCr)z=Return None, or the sequence of allowed domains (as a tuple).)rr rrrrrz#DefaultCookiePolicy.allowed_domainscCs|durt|}||_dS)z-Set the sequence of allowed domains, or None.N)rr)rrrrrset_allowed_domainss z'DefaultCookiePolicy.set_allowed_domainscCs.|jdurdS|jD] }t||rdSq dS)NFT)rr)rrZallowed_domainrrris_not_alloweds   z"DefaultCookiePolicy.is_not_allowedcC@td|j|jdD]}d|}t||}|||sdSq dS)z If you override .set_ok(), be sure to call this method. If it returns false, so should your subclass (assuming your subclass wants to be more strict about which cookies to accept).  - checking cookie %s=%s)r verifiabilityryrrrZset_ok_FTrryrzrrrrnZfn_namefnrrrrs  zDefaultCookiePolicy.set_okcCsZ|jdurtd|j|jdS|jdkr|jstddS|jdkr+|js+tddSdS)Nz0 Set-Cookie2 without version attribute (%s=%s)Fr $ RFC 2965 cookies are switched off$ Netscape cookies are switched offT)rrryrzrrrrrrset_ok_versions z"DefaultCookiePolicy.set_ok_versioncCJ|jr#t|r#|jdkr|jrtddS|jdkr#|jr#tddSdSNr z> third-party RFC 2965 cookie during unverifiable transactionFz> third-party Netscape cookie during unverifiable transactionTZ unverifiablerrrrrrrrrset_ok_verifiabilityz(DefaultCookiePolicy.set_ok_verifiabilitycCs0|jdkr|jr|jdrtd|jdSdS)Nr $z' illegal name (starts with '$'): '%s'FT)rrryrtrrrrr set_ok_names   zDefaultCookiePolicy.set_ok_namecCsL|jr$t|}|jdks|jdkr$|jr$||j|s$td|j|dSdS)Nr z7 path attribute %s is not a prefix of request path %sFT)rrrrrrr)rrrreq_pathrrr set_ok_paths  zDefaultCookiePolicy.set_ok_pathc Cs||jrtd|jdS||jrtd|jdS|jrt|\}}|j}|jre|ddkre|d}|dd|}|dkre||dd}||d|} | dvret |dkretd |dS| drq|dd} n|} | ddk} | s|d krtd |dS|j dkr||s| dsd||std ||dS|j dks|j|j@rt||std ||dS|j dks|j|j@r|dt | } | ddkrt|std| |dSdS)N" domain %s is in user block-listF& domain %s is not in user allow-listrrr r)coZacZcomZeduZorgZnetZgovZmilrAZaeroZbizcatZcoopinfoZjobsZmobiZmuseumryZproZtravelZeuz& country-code second level domain %srz/ non-local domain %s contains no embedded dotzO effective request-host %s (even with added initial dot) does not end with %sz5 effective request-host %s does not domain-match %sz. host prefix %s for domain %s contains a dotT)r rrr rrrcountrrKlenrtrrrrDomainRFC2965MatchrDomainStrictNoDotsrr@) rrrrrrrjZtldZsldZundotted_domainZ embedded_dotsZ host_prefixrrr set_ok_domainst                   z!DefaultCookiePolicy.set_ok_domainc Cs|jrBt|}|durd}nt|}|jdD] }zt|Wnty0td|YdSw||kr8dSqtd||jdSdS)N80rmz bad port %s (not numeric)Fz$ request port (%s) not found in %sT)rrstrrrrArLrrrrZreq_portrrrr set_ok_port3s*   zDefaultCookiePolicy.set_ok_portcCr)z If you override .return_ok(), be sure to call this method. If it returns false, so should your subclass (assuming your subclass wants to be more strict about which cookies to return). r)rrrrrrZ return_ok_FTrrrrrrHs   zDefaultCookiePolicy.return_okcCs<|jdkr|jstddS|jdkr|jstddSdS)Nr rFrT)rrrrrrrrreturn_ok_versionZsz%DefaultCookiePolicy.return_ok_versioncCrrrrrrrreturn_ok_verifiabilitycrz+DefaultCookiePolicy.return_ok_verifiabilitycCs"|jr|j|jvrtddSdS)Nz( secure cookie with non-secure requestFT)rtyperrrrrrreturn_ok_secureosz$DefaultCookiePolicy.return_ok_securecCs||jr tddSdS)Nz cookie expiredFT)r_nowrrrrrreturn_ok_expiresus z%DefaultCookiePolicy.return_ok_expirescCsP|jr&t|}|dur d}|jdD] }||krdSqtd||jdSdS)Nr,rmz0 request port %s does not match cookie port %sFT)rrrrr.rrrreturn_ok_port{sz"DefaultCookiePolicy.return_ok_portcCst|\}}|j}|r|dsd|}n|}|jdkr/|j|j@r/|js/||kr/tddS|jdkrAt||sAtd||dS|jdkrUd| |sUtd||dSdS)Nrr zQ cookie with unspecified domain does not string-compare equal to request domainFzQ effective request-host name %s does not domain-match RFC 2965 cookie domain %sz; request-host %s does not match Netscape cookie domain %sT) rrrtrrDomainStrictNonDomainrrrr)rrrrrr dotdomainrrrreturn_ok_domains0    z$DefaultCookiePolicy.return_ok_domaincCst|\}}|dsd|}|dsd|}|r$|ds$d|}n|}||s2||s2dS||r>td|dS||rJtd|dSdS)NrFr!r"T)rrtrr rr )rrrrrr8rrrrs"        z$DefaultCookiePolicy.domain_return_okcCsbtd|t|}t|}||krdS||r)|ds'|||ddkr)dStd||dS)Nz- checking cookie path=%sTrrz %s does not path-match %sF)rrr'rtr)rrrrZpathlenrrrrs   z"DefaultCookiePolicy.path_return_ok) rrrrr)r7r(Z DomainLiberalZ DomainStrictrrr r rr r rrrrr r+r/rr0r1r3r5r6r9rrrrrrrksR #   ;   rcCst|}t|j|Sr)sortedkeysmapr)Zadictr;rrrvals_sorted_by_keys  r=c csVt|}|D]!}d}z|jWn tyYn wd}t|EdH|s(|VqdS)zBIterates over nested mapping, depth-first, in sorted order by key.FTN)r=itemsAttributeError deepvalues)mappingvaluesobjrrrr@s  r@c@ eZdZdS)AbsentNrrrrrrrrE rEc@seZdZdZedZedZedZedZ edZ edej Z d3d d Z d d Zd dZddZddZddZddZddZddZddZddZdd Zd!d"Zd#d$Zd4d%d&Zd'd(Zd)d*Zd+d,Zd-d.Zd/d0Z d1d2Z!dS)5rzCollection of HTTP cookies. You may not need to know about this class: try urllib.request.build_opener(HTTPCookieProcessor).open(url). z\Wr|z\.?[^.]*z[^.]*z^\.+z^\#LWP-Cookies-(\d+\.\d+)NcCs(|durt}||_t|_i|_dSr)r_policy _threadingRLock _cookies_lock_cookiesrpolicyrrrrs   zCookieJar.__init__cCs ||_dSr)rHrMrrr set_policyrzCookieJar.set_policycCsg}|j||s gStd||j|}|D]*}|j||s#q||}|D]}|j||s9tdq+td||q+q|S)Nz!Checking %s for cookies to returnz not returning cookiez it's a match) rHrrrLr;rrBrrs)rrrcookiesZcookies_by_pathrZcookies_by_namerrrr_cookies_for_domains"     zCookieJar._cookies_for_domaincCs*g}|jD] }||||q|S)z2Return a list of cookies to be returned to server.)rLr;extendrQ)rrrPrrrr_cookies_for_requestszCookieJar._cookies_for_requestc Cs6|jddddd}g}|D]}|j}|s#d}|dkr#|d||jdur<|j|jr<|dkr<|jd |j}n|j}|jdurK||jn |d |j|f|dkr|j rd|d |j |j d r|j }|j s{| d r{|d d}|d||jdurd}|jr|d|j}||q|S)zReturn a list of cookie-attributes to be returned to server. like ['foo="bar"; $Path="/"', ...] The $Version attribute is also added when appropriate (currently only once per request). cS t|jSr)r'r)arrr s z)CookieJar._cookie_attrs..T)rreverseFr z $Version=%sNr}r~z $Path="%s"rrz $Domain="%s"z$Portz="%s")sortrrsrz non_word_rer@quote_rer_ryrrrrtrrr) rrPrattrsrrrzrrrrr _cookie_attrssF        zCookieJar._cookie_attrscCstd|jzKtt|j_|_||}||}|r/| ds/| dd ||jj rN|jj sN| dsN|D]}|jdkrM| ddnq>W|jn|jw|dS)zAdd correct Cookie: header to request (urllib.request.Request object). The Cookie2 header is also added unless policy.hide_cookie2 is true. add_cookie_headerrrZCookie2rz $Version="1"N)rrKacquirerArMrHr4rSr\Z has_headerZadd_unredirected_headerrrrrreleaseclear_expired_cookies)rrrPr[rrrrr]Qs,        zCookieJar.add_cookie_headerc Cstg}d}d}|D]}|d\}}d}d} i} i} |ddD]\} } | }||vs.||vr0|} | |vr:| dur:d} | | vr?q| dkrS| durOtd d} n\| } | d krc|rZq| durctd q| d krd}zt| } Wntytd d} Yn,wd } |j| } | |vs| |vr| dur| dvrtd| d} n | | | <q| | | <q| rq|||| | fq|S)aReturn list of tuples containing normalised cookie information. attrs_set is the list of lists of key,value pairs extracted from the Set-Cookie or Set-Cookie2 headers. Tuples are name, value, standard, rest, where name and value are the cookie name and value, standard is a dictionary containing the standard cookie-attributes (discard, secure, version, expires or max-age, domain, path and port) and rest is a dictionary containing the rest of the cookie-attributes. )rr)rrrrrrr commenturlr FrNTrz% missing value for domain attributerzM missing or invalid value for expires attribute: treating as session cookierz? missing or invalid (non-numeric) value for max-age attribute)rrraz! missing value for %s attribute)rKrrArLr4rs)r attrs_set cookie_tuples boolean_attrs value_attrsZ cookie_attrsryrzZ max_age_setZ bad_cookiestandardrrrrrrr_normalized_cookie_tuplesrsh         z#CookieJar._normalized_cookie_tuplescCs|\}}}}|dt}|dt}|dt} |dt} |dd} | dur9zt| } Wn ty8YdSw|dd} |dd} |d d}|d d}|tur`|d kr`d }t|}n*d}t|}|d }|dkr| dkrz|d|}n|d|d}t|dkrd }|tu}d}|rt| d}|turt |\}}|}n | dsd|}d}| tur| durt |} n d }t dd | } nd} | turd} d } n!| |jkrz ||||Wn tyYnwtd|||dSt| ||| ||||||| | | |||S)NrrrrrrFrrrarXTrrr rrz\s+z2Expiring cookie, domain='%s', path='%s', name='%s')rrErArLrrrr'boolrtrrrur_r4clearKeyErrorrr)rtuprryrzrfrrrrrrrrrrrrrrrrrrrr_cookie_from_cookie_tuples                   z#CookieJar._cookie_from_cookie_tuplecCs6||}g}|D]}|||}|r||q |Sr)rgrlrs)rrbrrcrPrkrrrr_cookies_from_attrs_set/s  z!CookieJar._cookies_from_attrs_setcCsHt|jdd}|dur|jj }|D]}|jdkr!d|_|r!d|_qdS)NrrTr )rrHrrr)rrPZ rfc2109_as_nsrrrr_process_rfc2109_cookies8s  z"CookieJar._process_rfc2109_cookiesc Cs.|}|dg}|dg}tt|j_|_|jj}|jj}|s'|r3|s+|r3|s/|r3|s5|s5gSz |t ||}Wnt yMt g}Ynw|r|rz |t ||} Wnt yjt g} Ynw| | |ri} |D] } d| | j| j| jf<qv| fdd} t| | } | r|| |S)zAReturn sequence of Cookie objects extracted from response object.z Set-Cookie2z Set-CookieNcSs|j|j|jf}||vSr)rrry)Z ns_cookielookuprrrrno_matching_rfc2965qsz3CookieJar.make_cookies..no_matching_rfc2965)r%Zget_allrArMrHr4rrrmr{ ExceptionrrrnrrryfilterrR) rresponserrZ rfc2965_hdrsZns_hdrsrrrPZ ns_cookiesrorrprrr make_cookiesDs^        zCookieJar.make_cookiescCsf|jz'tt|j_|_|j||r%||W|jdSW|jdS|jw)z-Set a cookie if policy says it's OK to do so.N) rKr^rArMrHr4r set_cookier_rrrrset_cookie_if_ok{s  zCookieJar.set_cookie_if_okcCst|j}|jz+|j|vri||j<||j}|j|vr"i||j<||j}|||j<W|jdS|jw)z?Set a cookie, without checking whether or not it should be set.N)rLrKr^rrryr_)rrcZc2Zc3rrrrus    zCookieJar.set_cookiecCsjtd||jz"|||D]}|j||r&td|||qW|jdS|jw)zAExtract cookies from response, where allowable given the request.zextract_cookies: %sz setting cookie: %sN) rr%rKr^rtrHrrur_)rrsrrrrrextract_cookiess   zCookieJar.extract_cookiescCsz|dur|dus |durtd|j|||=dS|dur.|dur&td|j||=dS|dur8|j|=dSi|_dS)aClear some cookies. Invoking this method without arguments will clear all cookies. If given a single argument, only cookies belonging to that domain will be removed. If given two arguments, cookies belonging to the specified path within that domain are removed. If given three arguments, then the cookie with the specified name, path and domain is removed. Raises KeyError if no matching cookie exists. Nz8domain and path must be given to remove a cookie by namez.domain must be given to remove cookies by path)rLrL)rrrryrrrris   zCookieJar.clearcCsL|jz|D]}|jr||j|j|jqW|jdS|jw)zDiscard all session cookies. Note that the .save() method won't save session cookies anyway, unless you ask otherwise by passing a true ignore_discard argument. N)rKr^rrirrryr_)rrrrrclear_session_cookiess zCookieJar.clear_session_cookiescCsX|jz t}|D]}||r||j|j|jq W|jdS|jw)aDiscard all expired cookies. You probably don't need to call this method: expired cookies are never sent back to the server (provided you're using DefaultCookiePolicy), this method is called by CookieJar itself every so often, and the .save() method won't save expired cookies anyway (unless you ask otherwise by passing a true ignore_expires argument). N) rKr^rMrrirrryr_)rrrrrrr`s  zCookieJar.clear_expired_cookiescCrTr)r@rLr rrr__iter__rzCookieJar.__iter__cCsd}|D]}|d}q|S)z#Return number of contained cookies.r rr)rrrrrr__len__szCookieJar.__len__cC2g}|D] }|t|qd|jjd|fSNz<%s[%s]>r)rsrrrrrrrrrrrzCookieJar.__repr__cCr|r})rsr-rrrr~rrrrrzCookieJar.__str__r)NNN)"rrrrrucompilerYrZZstrict_domain_reZ domain_reZdots_reASCIImagic_rerrOrQrSr\r]rgrlrmrnrtrvrurxriryr`rzr{rrrrrrrs:      ;!a\  7   rc@rD)rNrFrrrrrrGrc@s<eZdZdZd ddZd ddZd dd Z  d d d ZdS)rz6CookieJar that can be loaded from and saved to a file.NFcCs2t|||durt|}||_t||_dS)z} Cookies are NOT loaded from the named file until either the .load() or .revert() method is called. N)rrosfspathfilenamerh delayload)rrrrNrrrrs  zFileCookieJar.__init__cCr)zSave cookies to a file.r)rrignore_discardignore_expiresrrrsaverzFileCookieJar.savecCs`|dur|jdur |j}nttt|}|||||WddS1s)wYdS)zLoad cookies from a file.N)rrLMISSING_FILENAME_TEXTopen _really_loadrrrrrrrrloads  "zFileCookieJar.loadcCs|dur|jdur |j}ntt|jz&t|j}i|_z ||||Wn t y4||_wW|j dS|j w)zClear all cookies and reload cookies from a saved file. Raises LoadError (or OSError) if reversion is not successful; the object's state will not be altered if this happens. N) rrLrrKr^rZdeepcopyrLrOSErrorr_)rrrrZ old_staterrrreverts   zFileCookieJar.revert)NFNNFF)rrrrrrrrrrrrrs   rcCs |j|jfd|jfd|jfg}|jdur|d|jf|jr$|d|jr,|d|jr4|d|j r<|d|j rK|d t t |j f|j rS|d |jr^|d |jf|jri|d |jft|j}|D]}||t|j|fqr|d t|jft|gS)zReturn string representation of Cookie in the LWP cookie file format. Actually, the format is extended a bit -- see module docstring. rrNr) path_specN) port_specN) domain_dotN)rNr)rNrrar)ryrzrrrrsrrrrrr5r\rrrr:rr;r-rr)rrr;rrrrlwp_cookie_str,s(    rc@s,eZdZdZd ddZd ddZd d ZdS) ra[ The LWPCookieJar saves a sequence of "Set-Cookie3" lines. "Set-Cookie3" is the format used by the libwww-perl library, not known to be compatible with any browser, but which is easy to read and doesn't lose information about RFC 2965 cookies. Additional methods as_lwp_str(ignore_discard=True, ignore_expired=True) TcCsTt}g}|D]}|s|jrq|s||rq|dt|qd|dgS)zReturn cookies as a string of "\n"-separated "Set-Cookie3" headers. ignore_discard and ignore_expires: see docstring for FileCookieJar.save zSet-Cookie3: %s rX)rMrrrsrr)rrrrrrrrr as_lwp_strUs zLWPCookieJar.as_lwp_strNFcCsn|dur|jdur |j}nttt|d}|d||||WddS1s0wYdS)Nwz#LWP-Cookies-2.0 )rrLrrwriterrrrrres  "zLWPCookieJar.savecCs |}|j|sd|}t|t}d}d} d} z |} | dkr)WdS| |s/q| t|d} t| gD]} | d\} }i}i}| D]}d||<qL| ddD]5\}}|durf| }nd}|| vsp|| vrr|}|| vr|dur|d }|||<qY|| vr|||<qY|||<qY|j }|d }|d }|durt |}|durd }|d }|d }t |d| ||d|d|||d|d|d|d|||d|d|}|s|j rq>|s||rq>||q>qtytyttd|| fw)Nz5%r does not look like a Set-Cookie3 (LWP) format filez Set-Cookie3:)rrrrr)rrrrrrrarrXr FTrrrrrrrrrrrrraz&invalid Set-Cookie3 format file %r: %r)readlinerr@rrMrtr'rr{rKrrfrrrrurrqr)rrrrrmagicrrheaderrdrelinedataryrzrfrrrrrrrrrrwrrrrqs                 5zLWPCookieJar._really_load)TTr)rrrrrrrrrrrrHs   rc@s"eZdZdZddZdddZdS) ra WARNING: you may want to backup your browser's cookies file if you use this class to save cookies. I *think* it works, but there have been bugs in the past! This class differs from CookieJar only in the format it uses to save and load cookies to and from a file. This class uses the Mozilla/Netscape `cookies.txt' format. curl and lynx use this file format, too. Don't expect cookies saved while the browser is running to be noticed by the browser (in fact, Mozilla on unix will overwrite your saved cookies if you change them on disk while it's running; on Windows, you probably can't save at all while the browser is running). Note that the Mozilla/Netscape format will downgrade RFC2965 cookies to Netscape cookies on saving. In particular, the cookie version and port number information is lost, together with information about whether or not Path, Port and Discard were specified by the Set-Cookie2 (or Set-Cookie) header, and whether or not the domain as set in the HTTP header started with a dot (yes, I'm aware some domains in Netscape files start with a dot and some don't -- trust me, you really don't want to know any more about this). Note that though Mozilla and Netscape use the same format, they use slightly different headers. The class saves cookies using the Netscape header by default (Mozilla can cope with that). cCsjt}t|std|z |}i}|dkr WdS|tr1d|t<|ttd}| dr<|dd}| dsI| dkrJq| d\}} } } } } }| dk} | dk} | dkrf|} d}|d }d }| dkrud} d }t d | |dd || || d | | |dd|}|s|j rq|s||rq||qtytyttd ||fw)Nz4%r does not look like a Netscape format cookies filerrXrr)#r TRUErFTr z+invalid Netscape format cookies file %r: %r)rMNETSCAPE_MAGIC_RGXrirrrtHTTPONLY_PREFIX HTTPONLY_ATTRr'rrrrrrrurrqr)rrrrrrrrrrrrrryrzrrrwrrrrsp      9 zMozillaCookieJar._really_loadNFc Cs&|dur|jdur |j}nttt|ds}|tt}|D]^}|j}|s-|jr-q"|s5| |r5q"|j r;d}nd}| drEd} nd} |j durRt |j } nd} |jdur_d} |j} n|j} |j} |trnt|}|d|| |j|| | | gdq"WddS1swYdS)NrrZFALSErrXrr)rrLrrrNETSCAPE_HEADER_TEXTrMrrrrrtrr-rzryrrrrr) rrrrrrrrrrrryrzrrrr)sF         "zMozillaCookieJar.saver)rrrrrrrrrrrsIrr)\r__all__rrr-rurMZ urllib.parserZurllib.requestZ threadingrIZ http.clientZhttpZcalendarr r r rrrr-ZclientZ HTTP_PORTrrrrrrr%r,r6r7rIr(rsrKr5r8r>rr?rFrUrZIr^Xr`rcrdrfrlrnrorqrpr{rrrrrrrrrrrrrrrrrrrrrrrr=r@rErrrrrrrrrrrs      8  8 !     UD'    #b!b7x