o w[eg.@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)debugloggerlogging getLogger)argsr r%/usr/lib/python3.10/http/cookiejar.py_debug,s   rHTTPOnlyz #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 )ttyearmonthmdayhourminsecrrr_timegmQsr/)MonTueWedThuFriSatSun) JanFebMarAprMayJunJulAugSepOctNovDeccCs@|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,minutesecondtdtrrr time2isoz_s   rLcCsR|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!) rCrDrEDAYSweekdayrFMONTHSr*r)r,rGrHrIrrr time2netscapers  rP)GMTUTCUTZz^([-+])?(\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  r`c 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)Nr!r"r id2rR) r[rCMAXYEAR MONTHS_LOWERindexlower ValueErrortime localtimeabsr/upperr`) rFmonyrhrr-r.r]imoncur_yrr_tmprJr^rrr _str2timesV        rrzV^[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. r!rr rUN)STRICT_DATE_RErZgroupsrdrerfr[floatr/lstrip WEEKDAY_REsubLOOSE_HTTP_DATE_RErr) textr_grlr(rFrmrnr-r.r]rrr http2times  ra^ (\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 Nrv)rz ISO_DATE_RErZrxrr) r~rFrlrmrnr-r.r]r__rrriso2time3s  rcCs*|d\}}|jd||j|dS)z)Return unmatched part of re.Match object.r N)spanstring)matchstartendrrr unmatchedTsrz^\s*([^=\s;,]+)z&^\s*=\s*\"([^\"\\]*(?:\\.[^\"\\]*)*)\"z^\s*=\s*([^\s;,]*)z\\(.)c Cs,t|trJg}|D]}|}g}|rt|}|rYt|}|d}t|}|r:t|}|d}td|}nt |}|rOt|}|d}| }nd}| ||fn1| drr| dd}|ro| |g}ntdd|\}} | dksJd|||f|}|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"')]] r!z\1N,z^[=\s;]*rur z&split_header_words bug: '%s', '%s', %s) isinstancestrHEADER_TOKEN_RErZrr\HEADER_QUOTED_VALUE_REHEADER_ESCAPE_REr|HEADER_VALUE_RErstripappendrz startswithresubn) header_valuesresultr~ orig_textpairsr_namevaluenon_junk nr_junk_charsrrrsplit_header_words]sJ-         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; , )rrZHEADER_JOIN_ESCAPE_REr|rjoin)listsheadersrattrkvrrrjoin_header_wordss       rcCs0|dr |dd}|dr|dd}|S)N"r!)rendswithr~rrr 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 r!Nlocal)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)rrrorigin_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)r[ryrgrrrrport_specifiedrfrdomain_specifieddomain_initial_dotrpath_specifiedrrdiscardcomment comment_urlrfc2109copy_rest)selfrrrrrrrrrrrrrrrrestrrrr__init__s*  zCookie.__init__cCs ||jvSNr)rrrrrhas_nonstandard_attr$ zCookie.has_nonstandard_attrNcCs|j||Sr)rget)rrdefaultrrrget_nonstandard_attr&zCookie.get_nonstandard_attrcCs||j|<dSrr)rrrrrrset_nonstandard_attr(r zCookie.set_nonstandard_attrcCs,|durt}|jdur|j|krdSdSNTF)rhr)rnowrrr is_expired+szCookie.is_expiredcCsX|jdurd}nd|j}|j||j}|jdur#d|j|jf}n|j}d||fS)Nrurrz)rrrrr)rplimit namevaluerrr__str__1s   zCookie.__str__cCslg}dD]}t||}|d|t|fq|dt|j|dt|jd|jjd|fS)N)rrrrrrrrrrrrrrrrzrest=%sz rfc2109=%sz%s(%s)r)getattrrreprrr __class____name__r)rrrrrrr__repr__;s zCookie.__repr__)Fr) r __module__ __qualname____doc__rrr r rrrrrrrrs *   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.rr rrr 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_oker'zCookiePolicy.path_return_okN)rrrrr"r#r&r(rrrrrJs   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.r!rrsr NTF)httpswsscCsv||_||_||_||_||_||_| |_| |_| |_| |_ | |_ |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_domainsr+r,r-r.r/r0r1r2r3r4r5rrrrus"  zDefaultCookiePolicy.__init__cC|jS)z4Return the sequence of blocked domains (as a tuple).)r7rrrrr9r$z#DefaultCookiePolicy.blocked_domainscCst||_dS)z$Set the sequence of blocked domains.N)r6r7)rr9rrrset_blocked_domainssz'DefaultCookiePolicy.set_blocked_domainscCs |jD] }t||r dSqdSr)r7r)rrblocked_domainrrr is_blockeds  zDefaultCookiePolicy.is_blockedcCr;)z=Return None, or the sequence of allowed domains (as a tuple).)r8r<rrrr:r$z#DefaultCookiePolicy.allowed_domainscCs|durt|}||_dS)z-Set the sequence of allowed domains, or None.N)r6r8)rr:rrrset_allowed_domainss z'DefaultCookiePolicy.set_allowed_domainscCs.|jdurdS|jD] }t||rdSq dS)NFT)r8r)rrallowed_domainrrris_not_alloweds   z"DefaultCookiePolicy.is_not_allowedcCsNtd|j|j|jdusJdD]}d|}t||}|||s$dSqdS)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=%sN)r verifiabilityrrrrset_ok_FTrrrrrr!rnfn_namefnrrrr"s  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)rrrrr,r+r rrrset_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 transactionT unverifiablerrr0rr1r rrrset_ok_verifiabilityz(DefaultCookiePolicy.set_ok_verifiabilitycCs0|jdkr|jr|jdrtd|jdSdS)Nr $z' illegal name (starts with '$'): '%s'FT)rr3rrrr rrr 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)rrrr4r(rr)rr!rreq_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!)coaccomeduorgnetgovmilr[aerobizcatcoopinfojobsmobimuseumrprotraveleuz& 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?rrrBrrr/countrrflenrrrrr2DomainRFC2965MatchrDomainStrictNoDotsrrZ) rr!rrrrrjtldsldundotted_domain embedded_dots host_prefixrrr set_ok_domainst                   z!DefaultCookiePolicy.set_ok_domainc Cs|jrBt|}|durd}nt|}|jdD] }zt|Wnty0td|YdSw||kr8dSqtd||jdSdS)N80rz bad port %s (not numeric)Fz$ request port (%s) not found in %sT)rrrrrr[rgrrr!rreq_portrrrr set_ok_port3s*   zDefaultCookiePolicy.set_ok_portcCs@td|j|jdD]}d|}t||}|||sdSq dS)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). rC)rrDrrrr return_ok_FTrFrGrrrr#Hs   zDefaultCookiePolicy.return_okcCs<|jdkr|jstddS|jdkr|jstddSdS)Nr rKFrLT)rr,rr+r rrrreturn_ok_versionZsz%DefaultCookiePolicy.return_ok_versioncCrNrOrPr rrrreturn_ok_verifiabilitycrSz+DefaultCookiePolicy.return_ok_verifiabilitycCs"|jr|j|jvrtddSdS)Nz( secure cookie with non-secure requestFT)rtyper5rr rrrreturn_ok_secureosz$DefaultCookiePolicy.return_ok_securecCs||jr tddSdS)Nz cookie expiredFT)r_nowrr rrrreturn_ok_expiresus z%DefaultCookiePolicy.return_ok_expirescCsP|jr&t|}|dur d}|jdD] }||krdSqtd||jdSdS)Nrxrz0 request port %s does not match cookie port %sFT)rrrrryrrrreturn_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) rrrrr2DomainStrictNonDomainrrrr)rr!rrrr dotdomainrrrreturn_ok_domains0    z$DefaultCookiePolicy.return_ok_domaincCst|\}}|dsd|}|dsd|}|r$|ds$d|}n|}||s2||s2dS||r>td|dS||rJtd|dSdS)NrFrXrYT)rrrr?rrB)rrrrrrrrrr&s"        z$DefaultCookiePolicy.domain_return_okcCsbtd|t|}t|}||krdS||r)|ds'|||ddkr)dStd||dS)Nz- checking cookie path=%sTrr!z %s does not path-match %sF)rrrnrr)rrrrVpathlenrrrr(s   z"DefaultCookiePolicy.path_return_ok) rrrrrprro DomainLiberal DomainStrictrr9r=r?r:r@rBr"rMrRrUrWrwr{r#r}r~rrrrr&r(rrrrrksR #   ;   rcCst|}t|j|Sr)sortedkeysmapr )adictrrrrvals_sorted_by_keys  rc csVt|}|D]!}d}z|jWn tyYn wd}t|EdH|s(|VqdS)zBIterates over nested mapping, depth-first, in sorted order by key.FTN)ritemsAttributeError deepvalues)mappingvaluesobjrrrrs  rc@ eZdZdS)AbsentNrrrrrrrr rc@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\Wrz\.?[^.]*z[^.]*z^\.+z^\#LWP-Cookies-(\d+\.\d+)NcCs(|durt}||_t|_i|_dSr)r_policy _threadingRLock _cookies_lock_cookiesrpolicyrrrrs   zCookieJar.__init__cCs ||_dSr)rrrrr 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) rr&rrrr(rr#r)rrrcookiescookies_by_pathrcookies_by_namer!rrr_cookies_for_domains"     zCookieJar._cookies_for_domaincCs*g}|jD] }||||q|S)z2Return a list of cookies to be returned to server.)rrextendr)rrrrrrr_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)rnr)arrr s z)CookieJar._cookie_attrs..T)rreverseFr z $Version=%sNrrz $Path="%s"rr!z $Domain="%s"z$Portz="%s")sortrrr non_word_rerZquote_rer|rrrrrrrr) rrrattrsr!rrrrrrr _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_headerrrCookie2r!z $Version="1"N)rracquirer[rhrrrr has_headeradd_unredirected_headerrr,r.rreleaseclear_expired_cookies)rrrrr!rrrrQs,        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 Fr!NTrz% 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)rrrz! missing value for %s attribute)rfrr[rgrr)r attrs_set cookie_tuples boolean_attrs value_attrs cookie_attrsrr max_age_set 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)NrrrrrrFrrrruTrrr r!rz\s+z2Expiring cookie, domain='%s', path='%s', name='%s')r rr[rgrrrrnboolrrrrr|rclearKeyErrorrr)rtuprrrrrrrrrrrrrrrrrrrrrrrr_cookie_from_cookie_tuples                   z#CookieJar._cookie_from_cookie_tuplecCs6||}g}|D]}|||}|r||q |Sr)rrr)rrrrrrr!rrr_cookies_from_attrs_set/s  z!CookieJar._cookies_from_attrs_setcCsHt|jdd}|dur|jj }|D]}|jdkr!d|_|r!d|_qdS)Nr-r!Tr )rrr,rr)rr rfc2109_as_nsr!rrr_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)rrr) ns_cookielookuprrrrno_matching_rfc2965qsz3CookieJar.make_cookies..no_matching_rfc2965)rfget_allr[rhrrr,r+rr Exceptionrrrrrrfilterr) rresponserr rfc2965_hdrsns_hdrsr,r+r ns_cookiesrr!rrrr 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) rrr[rhrrr" set_cookierr rrrset_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)rrrrrrr)rr!cc2c3rrrrs    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) rrfrrrrr"rr)rrrr!rrrextract_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)rgr)rrrrrrrrs   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)rrrrrrrr)rr!rrrclear_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) rrrhrrrrrr)rrr!rrrrs  zCookieJar.clear_expired_cookiescCrr)rrr<rrr__iter__rzCookieJar.__iter__cCsd}|D]}|d}q|S)z#Return number of contained cookies.r r!r)rrr!rrr__len__szCookieJar.__len__cC2g}|D] }|t|qd|jjd|fSNz<%s[%s]>r)rrrrrrrr!rrrrzCookieJar.__repr__cCrr)rrrrrrrrrrrzCookieJar.__str__r)NNN)"rrrrrcompilerrstrict_domain_re domain_redots_reASCIImagic_rerrrrrrrrrrrrrrrrrrrrrrrrrrs:      ;!a\  7   rc@r)rNrrrrrrrrc@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)rrosfspathfilenamer delayload)rrrrrrrrs  zFileCookieJar.__init__cCr)zSave cookies to a file.r)rrignore_discardignore_expiresrrrsaver$zFileCookieJar.savecCs`|dur|jdur |j}nttt|}|||||WddS1s)wYdS)zLoad cookies from a file.N)rrgMISSING_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) rrgrrrrdeepcopyrrOSErrorr)rrrr 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)rNrrr)rrrrrrrrrrrrLryrrrrrrrrr)r!rrrrrrlwp_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 ru)rhrrrrr)rrrrrr!rrr as_lwp_strUs zLWPCookieJar.as_lwp_strNFcCsn|dur|jdur |j}nttt|d}|d||||WddS1s0wYdS)Nwz#LWP-Cookies-2.0 )rrgrrwriterrrrrres  "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)rrrrrrrr!rur FTrrrrrrrrrrrrrz&invalid Set-Cookie3 format file %r: %r)readlinerrZrrhrrnrrrfr rrrrrrrr)rrrrrmagicrrheaderrrlinedatarrrrrrrrrrrrrrrrrqs                 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). cCsvt}t|std|z |}i}|dkr WdS|tr1d|t<|ttd}| dr<|dd}| dsI| dkrJq| d\}} } } } } }| dk} | dk} | dkrf|} d}|d }| |ksqJd }| dkr{d} d }t d | |dd || || d | | |dd|}|s|j rq|s||rq||qtytyttd ||fw)Nz4%r does not look like a Netscape format cookies filer!rurr)#rT TRUErFTr z+invalid Netscape format cookies file %r: %r)rhNETSCAPE_MAGIC_RGXrrrrHTTPONLY_PREFIX HTTPONLY_ATTRrnrrrrrrrrrr)rrrrrrrrrrrrrrrrrrrrrrsr       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)NrrFALSErrurr)rrgrrrNETSCAPE_HEADER_TEXTrhrrrrrrrrrrr r rr) rrrrrrr!rrrrrrrrrr)sF         "zMozillaCookieJar.saver)rrrrrrrrrrrsIrr)\r__all__rrrCrrh urllib.parserurllib.request threadingr http.clienthttpcalendarr r r rr r rclient HTTP_PORTrrr rr rr'r/rMrOrdr*rrfrLrPrXrrYr`rrrwIr{Xr}rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrs      8  8 !     UD'    #b!b7x