f~.jdZgdZddlZddlZddlZddlZddlZddlZddl Zddl Z ddl Z ddlmZdZdadZdZdZee j,j.Zej2d Zd Zd Zd Zd ZdZgdZ gdZ!e!Dcgc]}|jEc}Z#dKdZ$dKdZ%dddddZ&ej2dejNZ(dZ)dZ*ej2dejNZ+ej2dejXejNzZ-ej2dej\ejNzZ/dZ0ej2dej\ejNzZ1dZ2dZ3ej2dZ4ej2dZ5ej2d Z6ej2d!Z7d"Z8ej2d#Z9d$Z:d%Z;d&Z<ej2d'ejNZ=d(Z>d)Z?d*Z@d+ZAej2d,ejNZBd-ZCd.ZDd/ZEd0ZFd1ZGej2d2ZHd3ZId4ZJd5ZKd6ZLGd7d8ZMGd9d:ZNGd;dd?ZQGd@dAZRGdBdCeSZTGdDdEeRZUdFZVGdGdHeUZWGdIdJeUZXycc}w)LaHTTP 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)timegmFcjtsytsddl}|jdatj|S)Nr zhttp.cookiejar)debugloggerlogging getLogger)argsrs %/usr/lib/python3.12/http/cookiejar.py_debugr,s.  ""#34 << HTTPOnlyz #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. cddl}ddl}ddl}|j}|j d||j }|j d|zdy)Nr zhttp.cookiejar bug! %s) stacklevel)iowarnings tracebackStringIO print_excgetvaluewarn)rrrfmsgs r_warn_unhandled_exceptionr"BsF#" A a **,C MM+c1aM@ric|dd\}}}}}}|tk\rPd|cxkrdkrEnyd|cxkrdkr7nyd|cxkrdkr)nyd|cxkrdkrnyd|cxkrdkr t|Syy) N r ;=) EPOCH_YEARr )ttyearmonthmdayhourminsecs r_timegmr3Qs(*2A%D%tS#  !u"2"2 :;db db !C~2~56NNbz5Cr)MonTueWedThuFriSatSun) JanFebMarAprMayJunJulAugSepOctNovDeccP|/tjjtj}n/tjj|tj}d|j|j |j |j|j|jfzS)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 tzz%04d-%02d-%02d %02d:%02d:%02dZ) datetimenowUTC fromtimestampr-r.dayr0minutesecondtdts r time2isozrT^s y    " "hll " 3    , ,Q8<< , @ + 266277BIIryy/B BBrc|/tjjtj}n/tjj|tj}dt|j |j t|jdz |j|j|j|jfzS)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 rHz#%s, %02d-%s-%04d %02d:%02d:%02d GMTr%) rJrKrLrMDAYSweekdayrNMONTHSr.r-r0rOrPrQs r time2netscaperYqs y    " "hll " 3    , ,Q8<< , @ 0 RZZ\BFFF288A:$6 "))RYY40 00r)GMTrLUTZz^([-+])?(\d\d?):?(\d\d)?$cd}|tvrd}|Stj|}|redt|j dz}|j dr |dt|j dzz}|j ddk(r| }|S)Nr ir<r%-) UTC_ZONES TIMEZONE_REsearchintgroup)rIoffsetms roffset_from_tz_stringrhs F Y M   r " C O+Fwwqz"s1771:"66wwqzS   Mrc t|}|tjkDry tj |j dz}|d}|d}|d}t|}t|}t|}t|}|dkr\tjtjd}|dz} |} ||z| z }| | z } t| dkDr| dkDr|dz}n|dz }t|||||||f} | '|d}|j}t|} | y| | z } | S#t $r5 t|}n#t $rYYywxYwd|cxkrdkrnYy|}nYyYwxYw)Nr%r&r id2rL) rdrJMAXYEAR MONTHS_LOWERindexlower ValueErrortime localtimeabsr3upperrh) rNmonyrhrr1r2rIimoncur_yrrgtmprRrfs r _str2timer{s RB H     -a/ z2 {!C {!C c(C RB c(C c(C Dy ,Q/ SL &[1_ G q6B;1u28bCx" S#r3R01A} :B XXZ&r* > J H[   s8D   ??C  s;&D E' D32E3 E<E?EEEEzV^[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 .tj|}|r|j}tj |dj dz}t |d|t |dt |dt |dt|df}t|S|j}tjd|d}dgd z\}}}}}} } tj|}||j\}}}}}} } nyt|||||| | 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 r^N)STRICT_DATE_RErcgroupsrmrnrordfloatr3lstrip WEEKDAY_REsubLOOSE_HTTP_DATE_REr{) textrggrur,rNrvrwr1r2rIs r http2timers< d#A HHJ  1.2!A$ic!A$i!A$iQqTE!A$K1r{ ;;=D >>"dA &D'+VAX"Cb"c3 !!$'A})*&S"b#sB S#r2sC 44ra^ (\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 |j}dgdz\}}}}}}}tj|}||j\}}}}}}}} nyt |||||||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 Nr)r ISO_DATE_RErcrr{) rrNrurvrwr1r2rIrg_s riso2timer2sz ;;=D'+VAX"Cb"c3 4 A}-.HHJ)Cb#sB S#r2sC 44rch|jd\}}|jd||j|dzS)z)Return unmatched part of re.Match object.r N)spanstring)matchstartends r unmatchedrSs4AJE3 <<  ST 2 22rz^\s*([^=\s;,]+)z&^\s*=\s*\"([^\"\\]*(?:\\.[^\"\\]*)*)\"z^\s*=\s*([^\s;,]*)z\\(.)c t|trJg}|D]s}|}g}|rTtj|}|rt |}|j d}t j|}|r3t |}|j d}tjd|}nFtj|}|r-t |}|j d}|j}nd}|j||fny|jjdr)|jdd}|r|j|g}n1tjdd|\}} | dkDsJd|d |d ||}|rT|sc|j|v|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;]*rr zsplit_header_words bug: 'z', 'z', ) isinstancestrHEADER_TOKEN_RErcrreHEADER_QUOTED_VALUE_REHEADER_ESCAPE_RErHEADER_VALUE_RErstripappendr startswithresubn) header_valuesresultr orig_textpairsrgnamevaluenon_junk nr_junk_charss rsplit_header_wordsr\s~Z--- - F#' &&t,A |wwqz*11$7$QE'..t4A(| !  % !% dE]+))#.{{}QR(&--.+-''+r4*H'-$q(.e-.( ?@ &--&G#'H Mr([\"\\])c*g}|D]|}g}|D]P\}}|8tjd|stjd|}d|z}|d|}|j |R|s]|j dj |~dj |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"' z^\w+$\\\1z"%s"=; , )rrcHEADER_JOIN_ESCAPE_RErrjoin)listsheadersrattrkvs rjoin_header_wordsrsG 1 DAq}yy1--11'1=A A !$ KKN    $0 1 99W rc^|jdr|dd}|jdr|dd}|S)N"r%)rendswithrs r strip_quotesrs5 sABx }}SCRy Krcd}g}|D]}g}d}t|jdD]\}}|j}|jd\}} } |j}|s|dk(rnuE| r| jnd} |dk7rF|j } | |vr| }|dk(r| t | } d}n|d k(r| t t | } |j|| f|s|s|jd |j||S) a5Ad-hoc parser for Netscape protocol cookie-attributes. The old Netscape cookie format for Set-Cookie can for instance contain an unquoted "," in the expires field, so we have to use this ad-hoc parser instead of split_header_words. XXX This may not make the best possible effort to parse all the crap that Netscape Cookie headers contain. Ronald Tschalar's HTTPClient parser is probably better, so could do worse than following that if this ever gives any trouble. Currently, this is also used for parsing RFC 2109 cookies. )expiresdomainpathsecureversionportmax-ageF;rr NrTr)r0) enumeratesplitstrip partitionrorrr) ns_headers known_attrsr ns_headerr version_setiiparamkeysepvallcs rparse_ns_headersrs)1KF+!  #9??3#78 %IBKKME!OOC0MCc))+C7"%#))+$CQwYY[$C)#*3/"&KI%' S(9: LL#s $= %@  -. MM% W+!Z Mrz\.\d+$c^tj|ry|dk(ry|ddk(s|ddk(ryy)z*Return True if text is a host domain name.Frr .rTIPV4_RErcrs ris_HDNrs8~~d rz Aw#~bS rc|j}|j}||k(ryt|sy|j|}|dk(s|dk(ry|jdsyt|ddsyy)aReturn True if domain A domain-matches domain B, according to RFC 2965. A and B may be host domain names or IP addresses. RFC 2965, section 1: Host names can be specified either as an IP address or a HDN string. Sometimes we compare one host name with another. (Such comparisons SHALL be case-insensitive.) Host A's name domain-matches host B's if * their host name strings string-compare equal; or * A is a HDN string and has the form NB, where N is a non-empty name string, B has the form .B', and B' is a HDN string. (So, x.y.com domain-matches .Y.com but not Y.com.) Note that domain-match is not a commutative operation: a.b.c.com domain-matches .c.com, but not the reverse. TFrr rr%N)rorrfindr)ABis r domain_matchr&sm.  A  AAv !9  ABw!q& <<  !AB%= rc0tj|ryy)zdReturn True if text is a sort-of-like a host domain name. For accepting/blocking domains. FTrrs rliberal_is_HDNrMs ~~d rc|j}|j}t|r t|s||k(ryy|jd}|r|j|ry|s||k(ryy)z\For blocking/accepting domains. A and B may be host domain names or IP addresses. TFr)rorrr)rr initial_dots ruser_domain_matchrWsc  A  A 1 ."3 6,,s#Kqzz!} 16 rz:\d+$c|j}tjj|d}|dk(r|j dd}t j d|d}|jS)zReturn request-host, as defined by RFC 2965. Variation from RFC: returned value is lowercased, for convenient comparison. r%rHost) get_full_urlurllibparseurlparse get_header cut_port_rerro)requesturlhosts r request_hostrlsd    C <<  %a (D rz!!&"- ??2tQ 'D ::<rc6t|x}}d|vr|dz}||fS)zzReturn a tuple (request-host, effective request-host name). As defined by RFC 2965, except both are lowercased. r.local)r)rerhnreq_hosts reff_request_hostr|s/ #7++D8 ((" T>rc|j}tjj|}t |j }|j dsd|z}|S)z6Path component of request-URI, as defined by RFC 2965./)rrrurlsplit escape_pathrr)rrpartsrs r request_pathrsL    C LL ! !# &E uzz "D ??3 Tz Krc|j}|jd}|dk\r||dzd} t||St }|S#t$rt d|YywxYw)N:r r%znonnumeric port: '%s')rfindrdrprDEFAULT_HTTP_PORT)rrrrs r request_portrsl <>> reach("www.acme.com") '.acme.com' >>> reach("acme.com") 'acme.com' >>> reach("acme.local") '.local' rr r%Nlocal)rr)hrbs rreachrsU4 s AAv acdG FF3K !9!q&ALq5L HrcZt|}t|t|jsyy)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)rrs ris_third_partyrs)G$H %(?(?"@ ArcBeZdZdZ d dZdZd dZdZd dZdZ d Z y) 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. c| t|}| tt| } ||dur td||_||_||_||_||_|j|_ ||_ ||_ | |_ | |_ | |_| |_| |_||_||_||_t)j(||_y)NTz-if port is None, port_specified must be false)rdrrprrrrport_specifiedrordomain_specifieddomain_initial_dotrpath_specifiedrrdiscardcomment comment_urlrfc2109copy_rest)selfrrrrrrrrrrrrrrrrestrs r__init__zCookie.__init__s  #g,  #eGn*= rc|jd}nd|jz}|j|z|jz}|j|jd|j}n |j}d|d|dS)Nrrrz)rrrrr)rplimit namevalues r__str__zCookie.__str__0sd 99 "adii-a a$))+ :: !#'99djj9I I'0%88rc\g}dD]-}t||}|j|dt|/|jdt|jz|jdt|jz|j j ddj|dS)N)rrrrrrrrrrrrrrrrzrest=%sz rfc2109=%s(r))getattrrreprrr __class____name__r)rrrrs r__repr__zCookie.__repr__:s 6D 4&D KK4d4 5 6 ITZZ 001 L4 #556>>22DIIdODDr)Fr#) r; __module__ __qualname____doc__r!r%r)r+r.r4r<rrrrs16(%T"-! 9 Errc(eZdZdZdZdZdZdZy)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. ct)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. NotImplementedErrorrcookiers rset_okzCookiePolicy.set_okRs "##rct)zAReturn true if (and only if) cookie should be returned to server.rCrEs r return_okzCookiePolicy.return_ok[ !##rcy)zMReturn false if cookies should not be returned, given cookie domain. Tr@)rrrs rdomain_return_okzCookiePolicy.domain_return_ok_rcy)zKReturn false if cookies should not be returned, given cookie path. Tr@)rrrs rpath_return_okzCookiePolicy.path_return_okdrMrN)r;r=r>r?rGrIrLrOr@rrrrIs$$ rrc eZdZdZdZdZdZdZeezZdddddddddeddd f d Z d Z d Z d Z dZ dZdZdZdZdZdZdZdZdZdZdZdZdZdZdZdZdZd Zy)!rzBImplements the standard rules for accepting and returning cookies.r%rr}r NTF)httpswssc||_||_||_||_||_||_| |_| |_| |_| |_ | |_ |t||_ nd|_ | t|}||_ y)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_domainsrTrUrVrWrXrYrZr[r\r]r^s rr!zDefaultCookiePolicy.__init__ts!  #6 (*+F(&<# 0,H)"4 0  &$)/$:D !$&D !  &#O4O /rc|jS)z4Return the sequence of blocked domains (as a tuple).)r`rs rrbz#DefaultCookiePolicy.blocked_domains$$$rc$t||_y)z$Set the sequence of blocked domains.N)r_r`)rrbs rset_blocked_domainsz'DefaultCookiePolicy.set_blocked_domainss %o 6rcB|jD]}t||syyr-)r`r)rrblocked_domains r is_blockedzDefaultCookiePolicy.is_blockeds)"33 N 8 rc|jS)z=Return None, or the sequence of allowed domains (as a tuple).)rares rrcz#DefaultCookiePolicy.allowed_domainsrfrc,| t|}||_y)z-Set the sequence of allowed domains, or None.N)r_ra)rrcs rset_allowed_domainsz'DefaultCookiePolicy.set_allowed_domainss  &#O4O /rc\|jy|jD]}t||syy)NFT)rar)rrallowed_domains ris_not_allowedz"DefaultCookiePolicy.is_not_alloweds8  ("33 N 8 rctd|j|j|jJdD]}d|z}t||}|||ryy)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 verifiabilityrrrrset_ok_FTrrrr8rrFrnfn_namefns rrGzDefaultCookiePolicy.set_oks_ )6;; E{{&&&M AkGw'Bfg&   rc|j"td|j|jy|jdkDr|js tdy|jdk(r|j s tdyy)Nz0 Set-Cookie2 without version attribute (%s=%s)Fr $ RFC 2965 cookies are switched off$ Netscape cookies are switched offT)rrrrrUrTrEs rset_ok_versionz"DefaultCookiePolicy.set_ok_versionsh >> ! E;;  . >>A dll 9 : ^^q  9 :rc|jrYt|rN|jdkDr|jr t dy|jdk(r|j r t dyyNr z> third-party RFC 2965 cookie during unverifiable transactionFz> third-party Netscape cookie during unverifiable transactionT unverifiablerrrYrrZrEs rset_ok_verifiabilityz(DefaultCookiePolicy.set_ok_verifiabilitys]   N7$;~~!d&F&F891$)D)D89rc|jdk(r>|jr2|jjdrt d|jyy)Nr $z' illegal name (starts with '$'): '%s'FT)rr\rrrrEs r set_ok_namezDefaultCookiePolicy.set_ok_names? NNa D$E$E KK " "3 ' >A dll 9 : ^^q  9 :rc|jrYt|rN|jdkDr|jr t dy|jdk(r|j r t dyyrrrEs rreturn_ok_verifiabilityz+DefaultCookiePolicy.return_ok_verifiabilitycs]   N7$;~~!d&F&F%&1$)D)D%&rcd|jr$|j|jvr tdyy)Nz( secure cookie with non-secure requestFT)rtyper^rrEs rreturn_ok_securez$DefaultCookiePolicy.return_ok_secureos) ==W\\1F1FF = >rcR|j|jr tdyy)Nz cookie expiredFT)r._nowrrEs rreturn_ok_expiresz%DefaultCookiePolicy.return_ok_expiresus#   TYY ' & 'rc|jrNt|}|d}|jjdD] }||k(s ytd||jyy)Nrrz0 request port %s does not match cookie port %sFT)rrrrrs rreturn_ok_portz"DefaultCookiePolicy.return_ok_port{sg ;;#G,H[[&&s+ =  I.rct|\}}|j}|r|jdsd|z}n|}|jdk(r6|j|j zr|j s||k7r tdy|jdkDrt||std||y|jdk(r"d|zj|std||yy)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) rrrrr[DomainStrictNonDomainrrrr)rrFrrrr dotdomains rreturn_ok_domainz$DefaultCookiePolicy.return_ok_domains)'2$ &++C0f II NNa   " "T%?%? ?''FdN - . >>A l4&@ /04f > >>Q D':':9'E !6 +rclt|\}}|jdsd|z}|jdsd|z}|r|jdsd|z}n|}|j|s|j|sy|j|r t d|y|j |r t d|yy)NrFrrT)rrrrkrrq)rrrrrrs rrLz$DefaultCookiePolicy.domain_return_oks*'2$""3'8|Hs#t8D &++C0f II!!), i0H ??6 " 7 @   v & ;V Drctd|t|}t|}||k(ry|j|r|j ds |||dzdk(rytd||y)Nz- checking cookie path=%sTrr%z %s does not path-match %sF)rrrrr)rrrrpathlens rrOz"DefaultCookiePolicy.path_return_oksj*D1(d) t !!$'}}S!Xggai%@C%G,h=r) r;r=r>r?rrr DomainLiberal DomainStrictr!rbrhrkrcrnrqrGr~rrrrrrIrrrrrrrLrOr@rrrrjsLM%&;;L"&t%)#$-1(-"/.3$)"2!0F%7 %0 &   :x*$   64 rrc#Kt|jD]-}d} |jd}t|Ed{|r*|/y7#t$rYwxYww)z)Iterates over nested mapping, depth-firstFTN)listvaluesitems deepvaluesAttributeError)mappingobjs rrrsfGNN$%  ' IIG!# & &I  '    s7A AAA AA AAAAc eZdZy)AbsentNr;r=r>r@rrrrrrceZdZdZej dZej dZej dZej dZ ej dZ ej dejZ dd Z d Zd Zd Zd ZdZdZdZdZdZdZdZdZdZddZdZdZdZdZdZ dZ!y) rzCollection 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+)Ncj| t}||_tj|_i|_yr#)r_policy _threadingRLock _cookies_lock_cookiesrpolicys rr!zCookieJar.__init__s. >(*F '--/ rc||_yr#)rrs r set_policyzCookieJar.set_policys  rcg}|jj||sgStd||j|}|j D]}}|jj ||s ||}|j D]F}|jj||s td+td|j|H|S)Nz!Checking %s for cookies to returnz not returning cookiez it's a match) rrLrrkeysrOrrIr)rrrcookiescookies_by_pathrcookies_by_namerFs r_cookies_for_domainzCookieJar._cookies_for_domains||,,VW=I2F;--/#((* 'D<<..tW=-d3O)002 '||--fg>45()v&  ' 'rcg}|jjD]#}|j|j||%|S)z2Return a list of cookies to be returned to server.)rrextendr)rrrrs r_cookies_for_requestzCookieJar._cookies_for_request sCmm((* FF NN433FGD E Frc|jddd}g}|D]}|j}|sd}|dkDr|jd|z|jQ|jj |jr,|dkDr'|j jd|j}n |j}|j|j|jn |j|jd ||dkDs|jr|jd |jz|jjd rB|j}|js|jd r|d d}|jd |z|jyd}|jr|d|jzz}|j||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). c,t|jSr#)rr)as rz)CookieJar._cookie_attrs..s3qvv;rT)rreverseFr z $Version=%sNrrz $Path="%s"rr%z $Domain="%s"z$Portz="%s")sortrrr non_word_rercquote_rerrrrrrrrr) rrrattrsrFrrrr1s r _cookie_attrszCookieJar._cookie_attrss  . = ( $FnnG" Q;LL!89 )  '' 5'A+ ))'6<<@ ||# V[[)  U;<{((LL !;<==++C0#]]F"55))#.!'LL&!89;;*A,,6;;!67LLOQ( $T rctd|jj tt jx|j _|_|j|}|j|}|r2|jds!|jddj||j jrQ|j js;|jds*|D]%}|jdk7s|jddn|jj|j!y#|jjwxYw)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)rracquirerdrqrrrr has_headeradd_unredirected_headerrrUrWrreleaseclear_expired_cookies)rrrrrFs rrzCookieJar.add_cookie_headerLs "# ""$ ),/ ,< R    & & ( ""$    & & (sCEEEc(g}d}d}|D]}|d\}}d}d} i} i} |ddD]\} } | j}||vs||vr|} | |vr| d} | | vr-| dk(r!| td d} nw| j} | d k(r|r[| td i| d k(rd} t| } d } |j| z} | |vs| |vr| | dvrtd| zd} n | | | <| | | <| r|j ||| | f|S#t$rtd d} Y5wxYw)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)rorrdrprr)r attrs_set cookie_tuples boolean_attrs value_attrs cookie_attrsrr max_age_set bad_cookiestandardr rrrs r_normalized_cookie_tuplesz#CookieJar._normalized_cookie_tuplesms + 0 &H @L&q/KD% KJHD$QR(2 1WWY$m(;A %!)A==yFG%)  A >" y FG  >"&KF"A A A$!}*< !BBBQFG%) "#HQKDGe2 h  $x!> ?QH @T5& 23%)  s C77DDcR|\}}}}|jdt}|jdt}|jdt} |jdt} |jdd} | t| } |jdd} |jdd} |jd d}|jd d}|tur|d k7rd }t |}nFd}t |}|j d }|dk7r| dk(r|d|}n|d|dz}t|dk(rd }|tu}d}|rt|jd}|turt|\}}|}n|jdsd|z}d}| tur(| t|} nd }tjdd | } nd} | turd} d } n2| |jkr# |j|||t#d|||yt%| ||| ||||||| | | |||S#t$rYywxYw#t $rYCwxYw)NrrrrrrFrrrrTrrr r%rz\s+z2Expiring cookie, domain='%s', path='%s', name='%s')r'rrdrprrrrboolrrrrrrclearKeyErrorrr)rtuprrrrr rrrrrrrrrrrrrrrrs r_cookie_from_cookie_tuplez#CookieJar._cookie_from_cookie_tuplesv'*#eXth/||FF+||FF+,,y&1,,y$/   g,h.,,y%0,,y$/ll<6  v $"*!Nt$D"N(D 3ABwa<8D1:D4yA~ct"/" !%f&7&7&.no_matching_rfc2965ls'#**INNINNJCf,,r)rget_allrdrqrrrUrTr r Exceptionr"rrrrrfilterr) rresponserr rfc2965_hdrsns_hdrsrUrTr ns_cookiesrrFrs r make_cookieszCookieJar.make_cookies?sw--/}b9 //,3(+DIIK(88 DI,,&&<<((gh'I 22"<0';G x !99$W-w8  ) )* 5%MFHLFFMM6;; DEM;A-$$7D z*C  % 'G  )+  s$'E E% E"!E"%E=<E=cl|jj ttjx|j_|_|jj ||r|j||jjy#|jjwxYw)z-Set a cookie if policy says it's OK to do so.N) rrrdrqrrrG set_cookierrEs rset_cookie_if_okzCookieJar.set_cookie_if_okvs~ ""$ ),/ ,<  |jjy#|jjwxYw)zAExtract cookies from response, where allowable given the request.zextract_cookies: %sz setting cookie: %sN) rrrrrrrGrr)rrrrFs rextract_cookieszCookieJar.extract_cookiess$hmmo6 ""$ )++Hg> ,<<&&vw70&9OOF+ ,    & & (D   & & (s1B#(B##B?c|#|| td|j|||=y|| td|j||=y||j|=yi|_y)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)rpr)rrrrs rrzCookieJar.clears  DL NPP f%d+D1  ~ DFF f%d+   f%DMrc8|jj |D]@}|js|j|j|j |j B |jjy#|jjwxYw)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)rrFs rclear_session_cookieszCookieJar.clear_session_cookiessv ""$ ) H>>JJv}}fkk6;;G H    & & (D   & & (sA=3A==Bcj|jj tj}|D]E}|j|s|j |j |j |jG |jjy#|jjwxYw)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) rrrqr.rrrrr)rrKrFs rrzCookieJar.clear_expired_cookiess ""$ )))+C H$$S)JJv}}fkk6;;G H    & & (D   & & (s*B3BB2c,t|jSr#)rrres r__iter__zCookieJar.__iter__s$--((rc"d}|D]}|dz} |S)z#Return number of contained cookies.r r%r@)rrrFs r__len__zCookieJar.__len__s %FAA%rcg}|D]}|jt|d|jjddj |dSN<[rz]>)rr9r:r;rrrrFs rr<zCookieJar.__repr__s> 2FAHHT&\22!^^44diilCCrcg}|D]}|jt|d|jjddj |dSr.)rrr:r;rr1s rr4zCookieJar.__str__s> 1FAHHS[11!^^44diilCCrr#)NNN)"r;r=r>r?rcompilerrstrict_domain_re domain_redots_reASCIImagic_rer!rrrrrrr r rrrrr$rr'rr*r,r<r4r@rrrrs "**U#Krzz+&H!rzz+. 8$Ibjj!Grzz6AH$9v%B_BZx '5n ) ) )6 ))&) D Drrc eZdZy)rNrr@rrrrrrrc4eZdZdZddZddZddZ ddZy) rz6CookieJar that can be loaded from and saved to a file.Nctj|||tj|}||_t ||_y)z} Cookies are NOT loaded from the named file until either the .load() or .revert() method is called. N)rr!osfspathfilenamer delayload)rr?r@rs rr!zFileCookieJar.__init__s: 4(  yy*H  irct)zSave cookies to a file.rC)rr?ignore_discardignore_expiress rsavezFileCookieJar.saverJrc|(|j |j}nttt|5}|j ||||dddy#1swYyxYw)zLoad cookies from a file.N)r?rpMISSING_FILENAME_TEXTopen _really_loadrr?rBrCr s rloadzFileCookieJar.loads\  }}(T]]("#899 (^ Kq   a>> J K K Ks AAc|(|j |j}ntt|jj  t j |j}i|_ |j||| |jjy#t$r ||_wxYw#|jjwxYw)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) r?rprFrrrdeepcopyrrJOSErrorr)rr?rBrC old_states rrevertzFileCookieJar.revert s  }}(T]]("#899 ""$ ) dmm4IDM  (NNC    & & (   )      & & (s&B1-BB..B11C )NFNNFF)r;r=r>r?r!rDrJrOr@rrrrs"@ )$K#49)rrc|j|jfd|jfd|jfg}|j|j d|jf|j r|j d|jr|j d|jr|j d|jr|j d|jr/|j dtt|jf|jr|j d |jr|j d |jf|jr|j d |jft!|j"j%}|D]+}|j |t'|j"|f-|j d t'|j(ft+|gS) zReturn string representation of Cookie in the LWP cookie file format. Actually, the format is extended a bit -- see module docstring. rrr) path_specN) port_specN) domain_dotN)rNr)rNrrr)rrrrrrrrrrrrTrrrrsortedrrrrr)rFr rrs rlwp_cookie_strrV's ++v|| $ &++  FMM " $A{{&&++)> ? ahh':; ahh':;   !((+?"@ }}ahh/0 ~~qxx(v~~)>?!AB ~~qxx 12 ~~qxxFNN ;< 188\63E3E$FG &,,##% &D , !Sa)*+,HHiV^^, -. aS !!rc&eZdZdZddZddZdZy)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) ctj}g}|D]B}|s |jr|s|j|r&|jdt |zDdj |dgzS)zReturn cookies as a string of "\n"-separated "Set-Cookie3" headers. ignore_discard and ignore_expires: see docstring for FileCookieJar.save zSet-Cookie3: %s r)rqrr.rrVr)rrBrCrKr2rFs r as_lwp_strzLWPCookieJar.as_lwp_strPsr iik  AF!fnn!f&7&7&< HH&)?? @  A yyB4  rNc|(|j |j}ntttjtj |tj tjztjzdd5}|jd|j|j||dddy#1swYyxYw)Nwz#LWP-Cookies-2.0 ) r?rprFr=fdopenrGO_CREATO_WRONLYO_TRUNCwriterZrIs rrDzLWPCookieJar.save`s  }}(T]]("#899 YY GGHbjj2;;6CU K   E GG( ) GGDOONNC D E E Es 3CC cJ|j}|jj|sd|z}t|t j}d}d} d} |jx} dk7r| j |s(| t |dj} t| gD]0} | d\} }i}i}| D]}d||< | ddD]A\}}||j}nd}|| vs|| vr|}|| vr |d }|||<3|| vr|||<=|||<C|j}|d }|d }| t|}|d }|d }|j d }t|d| ||d|d|||d|d|d|d|||d|d|}|s|jr |s|j|r |j|3|jx} dk7ryy#t $rt"$rt%td|d wxYw)Nz5%r does not look like a Set-Cookie3 (LWP) format filez Set-Cookie3:)rSrRrTrr)rrrrrrrrr Fr%TrrrrrrrSrTrrRrrrz invalid Set-Cookie3 format file : )readliner9rcrrqrrrrror'rrrr.rrMrr")rr r?rBrCmagicr!rKheaderrrlinedatarrrr rrrr rrrrr s rrHzLWPCookieJar._really_loadosu }}##E*$%CC. iik. 0 9 .::<'4B.v.CKL)//1.v6-'D"&q'KD%!HD*,&+ , $QR(1=!"B!%B+-23F "A - yd!*+HQK+-*+HQK&'DG( ! A lG lG*"*7"3"&x[F'-'8'8'=$q|T5 y!K.%'7< y!K. {&& | # %A*aii )all3.? OOA&[-' ::<'4B.f   . % '%t-. . .s FG44.H")TTrP)r;r=r>r?rZrDrHr@rrrrCs ! EJ.rrceZdZdZdZddZy)r a 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). ctj}tj|jst d|z |jx}dk7r2i}|j t rd|t<|tt d}|jdr|dd}|jj ds|jdk(r|jd\}} } } } } }| dk(} | dk(} | dk(r|} d}|j d}| |k(sJd }| dk(rd} d }td | |dd || || d | | |dd|}|s|jr |s|j|r|j||jx}dk7r1yy#t $rt"$rt%t d |d wxYw)Nz4%r does not look like a Netscape format cookies filerrYr)#r TRUErFTr z%invalid Netscape format cookies file rd)rqNETSCAPE_MAGIC_RGXrrerrHTTPONLY_PREFIX HTTPONLY_ATTRrrrrrrr.rrMrr")rr r?rBrCrKrhr rrrrrrrrrr s rrHzMozillaCookieJar._really_loadsiik!'' 5F < .::<'4B. ???3*,D'O 4 56D==&tCRyJJL++J7JJLB& 4(M($u F*$4$> 2:!D E$//4 ';666b="G"G1dE!#3[!"" !&!))%!,,s*;"i::<'4B.l   . % '%t-. . .s EF.F>Nc |(|j |j}ntttjtj |tj tjztjzdd5}|jttj}|D]}|j}|s |jr|s|j|r3|jrd}nd}|j!drd} nd} |j"t%|j"} nd} |j&d} |j(} n|j(} |j&} |j+t,r t.|z}|jdj1|| |j2|| | | gdz dddy#1swYyxYw) Nr\r]rnFALSErrrmrY)r?rprFr=r^rGr_r`rarbNETSCAPE_HEADER_TEXTrqrrr.rrrrrrr%rqrprr) rr?rBrCr rKrFrrrrrrs rrDzMozillaCookieJar.save"s  }}(T]]("#899 YY GGHbjj2;;6CU K  "  GG( )))+C %&..%&*;*;C*@==6&&f$$S);$+k>>-!&..1G G<<'D"KKE!;;D"LLE..}=,v5FIIv{FKK%we=>3  " " " s D2GG rP)r;r=r>r?rHrDr@rrr r s>D.L'rr r#)Yr?__all__r=rrJrrq urllib.parserurllib.request threadingr http.clienthttpcalendarr r rrrqrprclient HTTP_PORTrr4rorFrtr"r+r3rVrXrormrTrYrar8rbrhr{rIrXrrrrrrrrrrrrrrrrrrrrrrrrr r rrrrrrrrrrrMrrrVrr )r.s0rrs6 M  #    --.RZZ @A:A 9 4+12% 2 B&0(T = bjj5rxx@  6 p,-/XX7RZZ2BDD288OE RZZ & DD288O)*65pbjj 44")) # $5B3 $$67#$MN#$9:2::h'Sj# ;/2AH "**Y ) %N(bjj288,    $"**9:+ ! F"_E_EDB_,_B "LDLD`4)I4)n"8v.=v.rM}MA;3sJ0