m %UIc @sdZdkZdkZdkZdkZdkZdkZdkZdkl Z y dk Z Wne j odk Z nXdkZdklZeidiZeeiZdZfdZdZdZd d d d d ddgZddddddddddddg ZgZx!eD]Zeieiq(We dZ!e dZ"hde <de <d e <d!e d:Z?d;Z@d<ZAd=ZBd>ZCei$d?ZDd@ZEdAZFdBZGdCZHdDfdEYZIdFfdGYZJdHeJfdIYZKdJZLdKZMdLfdMYZNdNfdOYZOdPePfdQYZQdReOfdSYZRdTkSlTZTlUZUdUkVlWZWdS(VsHTTP 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 the classes which do not derive from FileCookieJar are not distributed with the Python standard library, but are available from http://wwwsearch.sf.net/): CookieJar____ / \ FileCookieJar \ / | \ \ MozillaCookieJar | LWPCookieJar \ | | | ---MSIEBase | | / | | | / MSIEDBCookieJar BSDDBCookieJar |/ MSIECookieJar N(s StringTypes(stimegmt cookielibsQa filename was not supplied (nor was the CookieJar instance initialised with one)cCs|tttf}tid}t||ondk}dk }dk }|i }|i d||i}|id|dddS(Niscookielib bug! %st stackleveli(tunmaskedtKeyboardInterruptt SystemExitt MemoryErrortsystexc_infotetypet issubclasstwarningst tracebacktStringIOtft print_exctNonetgetvaluetmsgtwarn(RR R R RRR ((t&/data/zmath/lib/python2.4/cookielib.pytreraise_unmasked_exceptions+s  icCs|d \}}}}}}|tjod|jo djnod|jo djnohd|jo djnoJd|jo djno,d|jo djnot|SndSdS( Niii iiii;i=( ttttyeartmonthtmdaythourtmintsect EPOCH_YEARttimegmR(RRRRRRR((Rt_timegm?stMontTuetWedtThutFritSattSuntJantFebtMartAprtMaytJuntJultAugtSeptOcttNovtDeccCs\|djoti}nti|d \}}}}}}d||||||fS(sHReturn 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 is%04d-%02d-%02d %02d:%02d:%02dZN( ttRttimetgmtimeRtmonRRRR(R2RRRRRR5((Rt time2isozMs  %cCsn|djoti}nti|d \}}}}}}}dt ||t |d||||fS(sReturn 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 is"%s %02d-%s-%04d %02d:%02d:%02d GMTiN( R2RR3R4RR5RRRRtwdaytDAYStMONTHS(R2RRRRRR7R5((Rt time2netscape^s  (tGMTtUTCtUTtZs^([-+])?(\d\d?):?(\d\d)?$cCsd}|tjo d}nti|}|oodt|id}|ido!|dt|id}n|iddjo | }qn|S(Niiiii<it-( Rtoffsetttzt UTC_ZONESt TIMEZONE_REtsearchtmtinttgroup(RARER@((Rtoffset_from_tz_stringrs  !c Csyti|id}Wnitj o]yt|} Wntj o dSnXd| jo djno | }qdSnX|djo d}n|djo d}n|djo d}nt|}t|}t|}t|}t|}|djot it i d} | d} |}|| | }| |} t| djo)| djo|d}q|d}qnt|||||||f} | dj oR|djo d}n|i}t|}|djodSn| |} n| S(Nii iiidi2R<(t MONTHS_LOWERtindexR5tlowert ValueErrorRFtimonRthrRRtyrtdayR3t localtimetcur_yrREttmptabsRRAR2tupperRHR@( RPR5RORNRRRARSR@RERMRRR2((Rt _str2timesR               !      sV^[SMTWF][a-z][a-z], (\d\d) ([JFMASOND][a-z][a-z]) (\d\d\d\d) (\d\d):(\d\d):(\d\d) GMT$s+^(?:Sun|Mon|Tue|Wed|Thu|Fri|Sat)[a-z]*,?\s*s^ (\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 Cs;ti|}|o|i}ti|did}t |d|t |dt |dt |dt |df}t |Sn|i}tid|d}d gd\} }}}}} }ti|}|d j o%|i\} }}}}} }nd St| ||||| |S( sReturns 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. iiiiiitiN(tSTRICT_DATE_RERDttextREtgroupstgRIRJRKR5RFtfloatRRtlstript WEEKDAY_REtsubRRPRORNRRRAtLOOSE_HTTP_DATE_RERV( RYRORAR[RRRERNR5RRP((Rt http2times J " %s^ (\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 Cs|i}dgd\}}}}}}}t i |}|dj o(|i \}}}}}}}} ndSt|||||||S(sv 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 iN(RYR]RRPR5RORNRRRAt ISO_DATE_RERDRERZt_RV( RYRARRNRERR5RORPRc((Rtiso2times  " (cCs+|id\}}|i| |i|S(s)Return unmatched part of re.Match object.iN(tmatchtspantstarttendtstring(ReRgRh((Rt unmatched4ss^\s*([^=\s;,]+)s&^\s*=\s*\"([^\"\\]*(?:\\.[^\"\\]*)*)\"s^\s*=\s*([^\s;,]*)s\\(.)c Cst|tjptg}x|D]}|}g}xo|ogti |}|ot |}|i d}ti |}|o1t |}|i d} tid| } nHti |}|o+t |}|i d} | i} nd} |i|| fq<|iido2|id}|o|i|ng}q<tidd|\}}|djptd|||f|}q<W|o|i|q'q'W|S( smParse 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"')]] is\1t,s^[=\s;]*RWis&split_header_words bug: '%s', '%s', %sN(ttypet header_valuest StringTypestAssertionErrortresultRYt orig_texttpairstHEADER_TOKEN_RERDRERjRGtnametHEADER_QUOTED_VALUE_REtvaluetHEADER_ESCAPE_RER_tHEADER_VALUE_REtrstripRtappendR]t startswithtretsubntnon_junkt nr_junk_chars( RmRrRtRRYR~RERqRpRv((Rtsplit_header_words=sH,    $ s([\"\\])cCsg}x|D]}g}xu|D]m\}}|dj oGtid|p t i d|}d|}nd||f}n|i |q W|o|i di |q q Wdi |S(sDo 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"' s^\w+$s\\\1s"%s"s%s=%ss; s, N( theaderstlistsRrtattrtktvRR|RDtHEADER_JOIN_ESCAPE_RER_Rztjoin(RRrRRRR((Rtjoin_header_wordss   c Csd}g}x|D]} g}t} xBtti d| D](\}}|i }|djoq>nd |jo|d}}n(ti d |d \}}|i}|d jo|i}||jo |}n|d jo t} n|djoL|ido|d }n|ido|d }nt|}qSn|i||fq>W|o)| p|idn|i|qqW|S(s5Ad-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. texpirestdomaintpathtsecuretportsmax-ages;\s*RWt=s\s*=\s*iitversiont"it0N(sexpiressdomainspathssecuresportsmax-age(sversionR(t known_attrsRpt ns_headerst ns_headerRrtFalset version_sett enumerateR|tsplittiitparamRyRRRR]RKtlctTrueR{tendswithRaRz( RRrRRRRRRRpRR((Rtparse_ns_headerssD            s\.\d+$cCs[ti|otSn|djotSn|ddjp|ddjotSntS(s*Return True if text is a host domain name.RWit.iN(tIPV4_RERDRYRR(RY((Rtis_HDNs "cCs|i}|i}||jotSnt|ptSn|i|}|djp |djotSn|idptSnt|dptSntS(sReturn 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. iiRiN( tARKtBRRRtrfindtiR{(RRR((Rt domain_matchs    cCsti|otSntS(sdReturn True if text is a sort-of-like a host domain name. For accepting/blocking domains. N(RRDRYRR(RY((Rtliberal_is_HDNscCs|i}|i}t|o t|p||jotSntSn|id}|o|i|otSn| o||jotSntS(s\For blocking/accepting domains. A and B may be host domain names or IP addresses. RN( RRKRRRRR{t initial_dotR(RRR((Rtuser_domain_match%s   s:\d+$cCsa|i}ti|d}|djo|idd}ntid|d}|iS(sReturn request-host, as defined by RFC 2965. Variation from RFC: returned value is lowercased, for convenient comparison. iRWtHostN( trequestt get_full_urlturlturlparsethostt get_headert cut_port_reR_RK(RRR((Rt request_host:s  cCsOt|}}|iddjoti| o|d}n||fS(szReturn a tuple (request-host, effective request-host name). As defined by RFC 2965, except both are lowercased. Ris.localN(RRterhntreq_hosttfindRRD(RRR((Rteff_request_hostJs 'cCs|i}ti|d\}}}}|od||f}nt|}ti dd|d||f}|i dpd|}n|S(s$request-URI, as defined by RFC 2965.is%s;%sRWt/N( RRRRRt parameterstquerytfragt escape_patht urlunparsetreq_pathR{(RRRRRRR((Rt request_pathUs  !cCsy|i}|id}|djoG||d}yt|Wqutj otd|dSquXnt }|S(Nt:iisnonnumeric port: '%s'( Rtget_hostRRRRRFRLtdebugRtDEFAULT_HTTP_PORT(RRRR((Rt request_portds    s%/;:@&=+$,!~*'()s%([0-9a-fA-F][0-9a-fA-F])cCsd|idiS(Ns%%%si(ReRGRU(Re((Rtuppercase_escaped_charvscCsKt|to|id}nti|t}tit |}|S(sEEscape any invalid characters in HTTP URL, and uppercase all escapes.sutf-8N( t isinstanceRtunicodetencodeturllibtquotetHTTP_PATH_SAFEtESCAPED_CHAR_RER_R(R((RRxs  cCst|id}|djoT||d}|id}t|o&|djp |djo d|Sqpn|S(sBReturn reach of host h, as defined by RFC 2965, section 1. The reach R of a host name H is defined as follows: * If - H is the host domain name of a host; and, - H has the form A.B; and - A has no embedded (that is, interior) dots; and - B has at least one embedded dot, or B is the string "local". then the reach of H is .B. * Otherwise, the reach of H is H. >>> reach("www.acme.com") '.acme.com' >>> reach("acme.com") 'acme.com' >>> reach("acme.local") '.local' RiitlocalN(thRRtbR(RRR((Rtreachs 'cCs8t|}t|t|iptSntSdS(s 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. N(RRRRRtget_origin_req_hostRR(RR((Rtis_third_partys  tCookiecBsStZdZdZdZedZdZedZdZ dZ RS(sHTTP 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. cCs|dj ot|}n| dj ot| } n|djo|tjotdn||_||_ ||_ ||_||_|i |_ ||_ ||_| |_| |_| |_| |_| |_||_||_ti||_dS(Ns-if port is None, port_specified must be false(RRRFRRtport_specifiedRRLtselfRtRvRRKtdomain_specifiedtdomain_initial_dotRtpath_specifiedRtdiscardtcommentt comment_urltcopytrestt_rest(RRRtRvRRRRRRRRRRRRR((Rt__init__s,                cCs ||ijS(N(RtRR(RRt((Rthas_nonstandard_attrscCs|ii||S(N(RRtgetRttdefault(RRtR((Rtget_nonstandard_attrscCs||i|( RRRtpRRtlimitRvRtt namevalue(RRRR((Rt__str__s   cCsg}xgddddddddd d d d d ddgD]2}t||}|id|t|fq:W|idt|iddi|S(NRRtRvRRRRRRRRRRRRs%s=%ssrest=%ss Cookie(%s)s, ( targsRttgetattrRRRztreprRR(RRtRR((Rt__repr__ s4!( t__name__t __module__t__doc__RRRRRRRR(((RRs  '     t CookiePolicycBs2tZdZdZdZdZdZRS(s 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 customised policy. cCs tdS(sReturn 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. N(tNotImplementedError(RtcookieR((Rtset_ok#scCs tdS(sAReturn true if (and only if) cookie should be returned to server.N(R(RRR((Rt return_ok,scCstS(sMReturn false if cookies should not be returned, given cookie domain. N(R(RRR((Rtdomain_return_ok0scCstS(sKReturn false if cookies should not be returned, given cookie path. N(R(RRR((Rtpath_return_ok5s(RRRRRRR(((RRs   tDefaultCookiePolicyc Bs tZdZdZdZdZdZeeBZeee e e e e e ee e d Z dZ dZ dZd Zd Zd Zd Zd ZdZdZdZdZdZdZdZdZdZdZdZdZdZ dZ!RS(sBImplements the standard rules for accepting and returning cookies.iiiic Cs||_||_||_||_||_||_| |_| |_| |_ |dj ot ||_ n f|_ |dj ot |}n||_dS(sAConstructor arguments should be passed as keyword arguments only.N(tnetscapeRtrfc2965t hide_cookie2t strict_domaintstrict_rfc2965_unverifiabletstrict_ns_unverifiabletstrict_ns_domaintstrict_ns_set_initial_dollartstrict_ns_set_pathtblocked_domainsRttuplet_blocked_domainstallowed_domainst_allowed_domains( RRRRRRRRRRRR((RREs             cCs|iS(s4Return the sequence of blocked domains (as a tuple).N(RR(R((RRdscCst||_dS(s$Set the sequence of blocked domains.N(RRRR(RR((Rtset_blocked_domainsgscCs0x)|iD]}t||otSq q WtS(N(RRtblocked_domainRRRR(RRR((Rt is_blockedks   cCs|iS(s=Return None, or the sequence of allowed domains (as a tuple).N(RR(R((RRqscCs*|dj ot|}n||_dS(s-Set the sequence of allowed domains, or None.N(RRRRR(RR((Rtset_allowed_domainsts cCsH|idjotSnx)|iD]}t||otSq"q"WtS(N(RRRRtallowed_domainRRR(RRR ((Rtis_not_allowedzs  cCsstd|i|i|id j ptx?d D]7}d|}t||}|||pt Sq4q4Wt S( s 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). s - checking cookie %s=%sRt verifiabilityRtRRRtset_ok_N(sversionR snamespathsdomainsport(RRRtRvRRotntfn_nameRRtfnRRR(RRRRRR((RRs  cCs|idjotd|i|itSn|idjo|i otdtSn.|idjo|i otdtSnt S(Ns0 Set-Cookie2 without version attribute (%s=%s)is$ RFC 2965 cookies are switched offs$ Netscape cookies are switched off( RRRRRtRvRRRRR(RRR((Rtset_ok_versions  cCsz|ioit|o\|idjo|iotdtSqv|idjo|i otdtSqvnt S(Nis> third-party RFC 2965 cookie during unverifiable transactions> third-party Netscape cookie during unverifiable transaction( Rtis_unverifiableRRRRRRRRR(RRR((Rtset_ok_verifiabilitys   cCsI|idjo5|io+|iidotd|itSntS(Nit$s' illegal name (starts with '$'): '%s'( RRRRRtR{RRR(RRR((Rt set_ok_names-cCsw|ioit|}|idjp|idjo|io/|i|i  ot d|i |t Sqsnt S(Nis7 path attribute %s is not a prefix of request path %s( RRRRRRRRR{RRRR(RRRR((Rt set_ok_paths  >  c Cs|i|iotd|itSn|i|iotd|itSn|iout|\}} |i}|i o|i ddjo|id}|idd|}|djoy||d} ||d|!} | iddd d d d d ddg jo(t| djotd|tSqEqIn|ido|d}n|}|iddj}| o"|djotd|tSn|idjoS| i| o>| id o-d| i| otd| |tSqn|idjp|i|i@o,t| |ptd| |tSq\n|idjp|i|i@oT|t| } | iddjo)t i!| otd| |tSqqnt"S(Ns" domain %s is in user block-lists& domain %s is not in user allow-listRiiitcotactcomtedutorgtnettgovtmilRFs& country-code second level domain %ss.locals/ non-local domain %s contains no embedded dotsS effective request-host %s (even with added initial dot) does not end end with %ss5 effective request-host %s does not domain-match %ss. host prefix %s for domain %s contains a dot(#RR RRRRR RRRRRRtcountRRtjttldtsldRKtlenR{tundotted_domainRt embedded_dotsRRRtDomainRFC2965MatchRtDomainStrictNoDotst host_prefixRRDR( RRRRR%RR!RR&R#R)RR"((Rt set_ok_domainsV    A 7  !  !' cCs|iot|}|djo d}n t|}x||iidD]M}yt |Wn$t j ot d|t SnX||joPqLqLWt d||it SntS(Nt80Rks bad port %s (not numeric)s$ request port (%s) not found in %s(RRRRtreq_portRtstrRRRRFRLRRR(RRRRR,((Rt set_ok_ports$          cCs\td|i|ix?d D]7}d|}t||}|||pt SqqWt S( s 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). s - checking cookie %s=%sRR RRRRt return_ok_N(sversions verifiabilityssecuresexpiressportsdomain( RRRtRvRRRRRRRR(RRRRRR((RRs  cCs^|idjo|i otdtSn.|idjo|i otdtSntS(Nis$ RFC 2965 cookies are switched offs$ Netscape cookies are switched off(RRRRRRRR(RRR((Rtreturn_ok_version#s  cCsz|ioit|o\|idjo|iotdtSqv|idjo|i otdtSqvnt S(Nis> third-party RFC 2965 cookie during unverifiable transactions> third-party Netscape cookie during unverifiable transaction( RRRRRRRRRRR(RRR((Rtreturn_ok_verifiability,s   cCs3|io%|idjotdtSntS(Nthttpss( secure cookie with non-secure request(RRRtget_typeRRR(RRR((Rtreturn_ok_secure8s cCs)|i|iotdtSntS(Ns cookie expired(RRRt_nowRRR(RRR((Rtreturn_ok_expires>s cCs{|iomt|}|djo d}nxG|iidD]}||joPq@q@Wtd||it Snt S(NR+Rks0 request port %s does not match cookie port %s( RRRRR,RRRRRR(RRRRR,((Rtreturn_ok_portDs       cCst|\}}|i}|idjo;|i|i @o*|i o||jot dt Sn|idjo)t || ot d||t Sn|idjo-d|i| ot d||t SntS(NisQ cookie with unspecified domain does not string-compare equal to request domainsQ effective request-host name %s does not domain-match RFC 2965 cookie domain %sRs; request-host %s does not match Netscape cookie domain %s(RRRRRRRRRtDomainStrictNonDomainRRRRRR(RRRRRR((Rtreturn_ok_domainRs 9 ! % cCst|\}}|idpd|}n|idpd|}n|i|p |i|ptSn|i |ot d|tSn|i |ot d|tSnt S(NRs" domain %s is in user block-lists& domain %s is not in user allow-list( RRRRR{RRRRR RR R(RRRRR((RRhs   cCsEtd|t|}|i|ptd||tSntS(Ns- checking cookie path=%ss %s does not path-match %s(RRRRRR{RR(RRRR((RR~s   ("RRRR(R8R't DomainLiberalt DomainStrictRRRRRRR RR R RRRRRR*R.RR0R1R4R6R7R9RR(((RR;s:  *         8       cCs&|i}|it|i|S(N(tadicttkeystsorttmapR(R<R=((Rtvals_sorted_by_keys  ccsxt|}xe|D]]}t}y |iWntj on#Xt}xt|D] }|VqSW|p|VqqWdS(sBIterates over nested mapping, depth-first, in sorted order by key.N( R@tmappingtvaluestobjRtitemstAttributeErrorRt deepvaluestsubobj(RARCRGRB((RRFs   tAbsentcBstZRS(N(RR(((RRHst CookieJarcBstZdZeidZeidZeidZeidZeidZ dZ e dZ dZ d Zd Zd Zd Zd ZdZdZdZdZdZdZe e e dZdZdZdZdZdZdZRS(sCollection of HTTP cookies. You may not need to know about this class: try urllib2.build_opener(HTTPCookieProcessor).open(url). s\Ws([\"\\])s\.?[^.]*s[^.]*s^\.+s^\#LWP-Cookies-(\d+\.\d+)cCs?|djo t}n||_ti|_h|_dS(N( tpolicyRRRt_policyt _threadingtRLockt _cookies_lockt_cookies(RRJ((RRs    cCs ||_dS(N(RJRRK(RRJ((Rt set_policyscCsg}|ii||pgSntd||i|}x|i D]}|ii ||pqKn||}xR|i D]D}|ii||ptdqntd|i|qWqKW|S(Ns!Checking %s for cookies to returns not returning cookies it's a match(tcookiesRRKRRRRROtcookies_by_pathR=RRtcookies_by_nameRBRRRz(RRRRRRSRQRR((Rt_cookies_for_domains$       cCs=g}x0|iiD]}|i|i||qW|S(s2Return a list of cookies to be returned to server.N(RQRROR=RtextendRTR(RRRQR((Rt_cookies_for_requests c Csd}|i|t} g}x|D]}|i}| p,t} |djo|i d|qkn|i d j o?|i i|i o)|djo|iid|i }n |i }|i d jo|i |in|i d|i|f|djo|io|i d|in|iidoG|i}|i o|ido|d}n|i d |n|id j o6d }|io|d |i}n|i |qq)q)W|S( sReturn 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). cCstt|it|iS(N(tcmpR$RRta(RXR((Rtdecreasing_sizesis $Version=%ss\\\1s%s=%ss $Path="%s"Ris $Domain="%s"s$Ports="%s"N(RYRQR>RRtattrsRRRRzRvRRt non_word_reRDtquote_reR_RtRRRR{RRRR( RRQRYRvRRRRZRR((Rt _cookie_attrss@    3     cCs5td|iitti|i_|_t|\}}|ii |ii @}|i|}|i|}|o1|idp|iddi|qn|iioY|ii oK|id o:x7|D]+}|idjo|iddPqqWn|ii|idS(sAdd correct Cookie: header to request (urllib2.Request object). The Cookie2 header is also added unless policy.hide_cookie2 is true. tadd_cookie_headerRs; tCookie2is $Version="1"N(RRRNtacquireRFR3RKR5RRRRRR8tstrict_non_domainRVRQR]RZt has_headertadd_unredirected_headerRRRRRtreleasetclear_expired_cookies(RRRQRRRaRRZ((RR^s&  !,  cCsDg}d} d}x+|D]#}|d \}}t}t} h}h} x|d D]\} } | i}||jp || jo |} n| | jo| djo t} n| |joqRn| djo2| djotd t} Pn| i} n| djo0|oqRn| djotdqRqBn| djoTt}yt| } Wn$tj otdt} PnXd} |i| } n| |jp | | joJ| djo/| dd d gjotd| t} Pn| || |D]6}|i|o |i|i|i |i q q W|ii dS(sDiscard 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( RRNR`R3RRRRrRRRtRd(RRR((RReus   $cCs t|iS(N(RFRRO(R((Rt__iter__scCs%d}x|D]}|d}q W|S(s#Return number of contained cookies.iiN(RRR(RRR((Rt__len__s cCsDg}x!|D]}|it|q Wd|idi|fS(Ns<%s[%s]>s, (trRRRzRt __class__R(RRR((RRs cCsDg}x!|D]}|it|q Wd|idi|fS(Ns<%s[%s]>s, (RRRRzR-RR(RRR((RRs ( RRRR|tcompileR[R\tstrict_domain_ret domain_retdots_retmagic_reRRRPRTRVR]R^RoRtRuRRRRRrRReRRRR(((RRIs6      < # a X 4     t LoadErrorcBstZRS(N(RR(((RRst FileCookieJarcBsVtZdZeeedZeeedZeeedZeeedZRS(s6CookieJar that can be loaded from and saved to a file.cCs_ti|||dj o&y |dWqCtdqCXn||_t||_dS(s} Cookies are NOT loaded from the named file until either the .load() or .revert() method is called. RWsfilename must be string-likeN( RIRRRJtfilenameRRLRqt delayload(RRRRJ((RRs   cCs tdS(sSave cookies to a file.N(R(RRtignore_discardtignore_expires((RtsavescCsr|djo-|idj o |i}q:ttnt|}z|i||||Wd|i XdS(sLoad cookies from a file.N( RRRRLtMISSING_FILENAME_TEXTtopenR t _really_loadRRtclose(RRRRR ((Rtloads   cCs|djo-|idj o |i}q:ttn|iiti|i }h|_ y|i |||Wn%ttfj o||_ nX|iidS(sClear all cookies and reload cookies from a saved file. Raises LoadError (or IOError) if reversion is not successful; the object's state will not be altered if this happens. N(RRRRLRRNR`RtdeepcopyROt old_stateRRRRtIOErrorRd(RRRRR((Rtreverts     ( RRRRRRRRR(((RRs  (s LWPCookieJarslwp_cookie_str(sMozillaCookieJar(XRRR|RRR3RtloggingttypesRnt threadingRLt ImportErrortdummy_threadingthttplibtcalendarRt getLoggerRR-t HTTP_PORTRRRRRR8R9RIRRzRKRR6R:RBRRCRHRVRXtIR^tXR`RaRbRdRjRsRuRxRwRRRRRRRRRRRRRRRRRRRRRRRR@RFRHRIRRRt _LWPCookieJart LWPCookieJartlwp_cookie_strt_MozillaCookieJartMozillaCookieJar(FRRRRRRR@RRRRRjR6RbRRRRRRuRRdRRRRRRR|R:RVRRnRBRRxRRRsRHRIRRRRRCRRIRFRRRwRRRRRLR9RaR8RHRR^RR3RRRRXR`((Rt?s?      *  *  5 8 !  U  8  '       # ^!M  :