m mEc@sYdZdkZdkZdkZdkZdkZdkZdkZdkZdk Z dk Z ydk Z e Z [ Wn%e j odkZeZ [nXdkZdZeeiZdklZlZdklZlZlZlZe idiZfdZeidZ dZ!d Z"d Z#d Z$eid Z%d Z&dZ'dZ(dZ)dZ*eidZ+dZ,dZ-dZ.dZ/dfdYZ0dfdYZ1de1fdYZ2dZ3dfdYZ4d fd!YZ5d"fd#YZ6d$e7fd%YZ8d&e6fd'YZ9dS((sHTTP cookie handling for web clients, plus some other stuff. This module originally developed from my port of 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. CookieJar____ / \ FileCookieJar \ / | \ \ MozillaCookieJar | LWPCookieJar \ | | | ---MSIEBase | | / | | | / MSIEDBCookieJar BSDDBCookieJar |/ MSIECookieJar Comments to John J Lee . Copyright 2002-2006 John J Lee Copyright 1997-1999 Gisle Aas (original libwww-perl code) Copyright 2002-2003 Johnny Lee (original MSIE Perl code) This code is free software; you can redistribute it and/or modify it under the terms of the BSD or ZPL 2.1 licenses (see the file COPYING.txt included with the distribution). NsQa filename was not supplied (nor was the CookieJar instance initialised with one)(ssplit_header_wordssparse_ns_headers(s startswithsendswiths isstringlikes getheaderssmechanize.cookiescCsdk}dk}|ipn|tttf}tid}t ||ondk }dk }|i }|id||i}|id|dddS(Nismechanize bug! %st stackleveli(t mechanizetwarningstUSE_BARE_EXCEPTtunmaskedtKeyboardInterruptt SystemExitt MemoryErrortsystexc_infotetypet issubclasst tracebacktStringIOtft print_exctNonetgetvaluetmsgtwarn(RRRR RR RR ((t6/data/zmath/zope/lib/python/mechanize/_clientcookie.pytreraise_unmasked_exceptions6s   s\.\d+$cCs=ti|p,|djp|ddjp|ddj S(s*Return True if text is a host domain name.tit.iN(tIPV4_REtsearchttext(R((Rtis_HDNJscCsti|}ti|}||jotSnt|ptSnti||}|djp |dj }|ot |dot|dS(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( tstringtlowertAtBtTrueRtFalsetrfindtit has_form_nbt startswith(RRR#R$((Rt domain_matchUs  cCsti| S(sdReturn True if text is a sort-of-like a host domain name. For accepting/blocking domains. N(RRR(R((Rtliberal_is_HDNzscCsti|}ti|}t|o t|p||jotSntSnt|d}|ot ||otSn| o||jotSntS(s\For blocking/accepting domains. A and B may be host domain names or IP addresses. RN( RRRRR'R R!R%t initial_dottendswith(RRR(((Rtuser_domain_matchs s:\d+$cCsd|i}ti|d}|djo|idd}ntid|d}ti |S(sReturn request-host, as defined by RFC 2965. Variation from RFC: returned value is lowercased, for convenient comparison. iRtHostN( trequestt get_full_urlturlturlparsethostt get_headert cut_port_retsubRR(R,R.R0((Rt request_hosts  cCsRt|}}ti|ddjoti| o|d}n||fS(szReturn a tuple (request-host, effective request-host name). As defined by RFC 2965, except both are lowercased. Ris.localN(R4R,terhntreq_hostRtfindRR(R,R6R5((Rteff_request_hosts *cCs|i}ti|d\}}}}|od||f}nt|}ti dd|d||f}t |dpd|}n|S(s$request-URI, as defined by RFC 2965.is%s;%sRt/N( R,R-R.R/tpatht parameterstquerytfragt escape_patht urlunparsetreq_pathR%(R,R=R;R.R<R:R@((Rt request_paths  !cCs||i}ti|d}|djoG||d}yt|Wqxtj ot d|dSqxXnt }|S(Nt:iisnonnumeric port: '%s'( R,tget_hostR0RR7R#tporttintt ValueErrortdebugRtDEFAULT_HTTP_PORT(R,R#R0RD((Rt request_ports    s%/;:@&=+$,!~*'()s%([0-9a-fA-F][0-9a-fA-F])cCsdti|idS(Ns%%%si(Rtuppertmatchtgroup(RK((Rtuppercase_escaped_charscCsNt|tio|id}nti|t}ti t |}|S(sEEscape any invalid characters in HTTP URL, and uppercase all escapes.sutf-8N( t isinstanceR:ttypest UnicodeTypetencodeturllibtquotetHTTP_PATH_SAFEtESCAPED_CHAR_RER3RM(R:((RR>s  cCszti|d}|djoW||d}ti|d}t|o&|djp |djo d|Sqvn|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(RR7thR#tbR(RWR#RX((Rtreachs 'cCs#t|}t|t|i S(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(R4R,R6R&RYtorigin_req_host(R,R6((Rtis_third_partys  tCookiecBs_tZdZedZdZedZdZdZ edZ dZ dZ RS( slHTTP 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. version: integer; name: string; value: string (may be None); port: string; None indicates no attribute was supplied (eg. "Port", rather than eg. "Port=80"); otherwise, a port string (eg. "80") or a port list string (eg. "80,8080") port_specified: boolean; true if a value was supplied with the Port cookie-attribute domain: string; domain_specified: boolean; true if Domain was explicitly set domain_initial_dot: boolean; true if Domain as set in HTTP header by server started with a dot (yes, this really is necessary!) path: string; path_specified: boolean; true if Path was explicitly set secure: boolean; true if should only be returned over secure connection expires: integer; seconds since epoch (RFC 2965 cookies should calculate this value from the Max-Age attribute) discard: boolean, true if this is a session cookie; (if no expires value, this should be true) comment: string; comment_url: string; rfc2109: boolean; true if cookie arrived in a Set-Cookie: (not Set-Cookie2:) header, but had a version cookie-attribute of 1 rest: mapping of other cookie-attributes 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||_||_ ||_ ||_||_t i ||_ ||_||_| |_| |_| |_| |_| |_||_||_||_ti||_dS(Ns-if port is None, port_specified must be false(tversionRREtexpiresRDtport_specifiedR RFtselftnametvalueRRtdomaintdomain_specifiedtdomain_initial_dotR:tpath_specifiedtsecuretdiscardtcommentt comment_urltrfc2109tcopytrestt_rest(R`R]RaRbRDR_RcRdReR:RfRgR^RhRiRjRmRk((Rt__init__Cs.                 cCs|ii|S(N(R`Rnthas_keyRa(R`Ra((Rthas_nonstandard_attrmscCs|ii||S(N(R`RntgetRatdefault(R`RaRs((Rtget_nonstandard_attroscCs||i|( R`RDRtpRcR:tlimitRbRat namevalue(R`R{R|R}((Rt__str__zs   cCsg}xgddddddddd d d d d ddgD]2}t||}|id|t|fq:W|idt|i|idt|idt i |dS(NR]RaRbRDR_RcRdReR:RfRgR^RhRiRjs%s=%ssrest=%ss rfc2109=%ss Cookie(%s)s, ( targsRatgetattrR`tattrtappendtreprRnRkRtjoin(R`RaRR((Rt__repr__s4!( t__name__t __module__t__doc__R!RoRqRRtRuRwRzR~R(((RR\s ) *      t CookiePolicycBs2tZdZdZdZdZdZRS(sDefines which cookies get accepted from and returned to server. May also modify cookies. The subclass DefaultCookiePolicy defines the standard rules for Netscape and RFC 2965 cookies -- override that if you want a customised policy. As well as implementing set_ok and return_ok, implementations of this interface must also supply the following attributes, indicating which protocols should be used, and how. These can be read and set at any time, though whether that makes complete sense from the protocol point of view is doubtful. Public attributes: netscape: implement netscape protocol rfc2965: implement RFC 2965 protocol rfc2109_as_netscape: WARNING: This argument will change or go away if is not accepted into the Python standard library in this form! If true, treat RFC 2109 cookies as though they were Netscape cookies. The default is for this attribute to be None, which means treat 2109 cookies as RFC 2965 cookies unless RFC 2965 handling is switched off (which it is, by default), and as Netscape cookies otherwise. hide_cookie2: don't add Cookie2 header to requests (the presence of this header indicates to the server that we understand RFC 2965 cookies) cCs tdS(sVReturn 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. cookie: mechanize.Cookie object request: object implementing the interface defined by CookieJar.extract_cookies.__doc__ N(tNotImplementedError(R`tcookieR,((Rtset_oks cCs tdS(sReturn true if (and only if) cookie should be returned to server. cookie: mechanize.Cookie object request: object implementing the interface defined by CookieJar.add_cookie_header.__doc__ N(R(R`RR,((Rt return_okscCstS(s\Return false if cookies should not be returned, given cookie domain. This is here as an optimization, to remove the need for checking every cookie with a particular domain (which may involve reading many files). The default implementations of domain_return_ok and path_return_ok (return True) leave all the work to return_ok. If domain_return_ok returns true for the cookie domain, path_return_ok is called for the cookie path. Otherwise, path_return_ok and return_ok are never called for that cookie domain. If path_return_ok returns true, return_ok is called with the Cookie object itself for a full check. Otherwise, return_ok is never called for that cookie path. Note that domain_return_ok is called for every *cookie* domain, not just for the *request* domain. For example, the function might be called with both ".acme.com" and "www.acme.com" if the request domain is "www.acme.com". The same goes for path_return_ok. For argument documentation, see the docstring for return_ok. N(R (R`RcR,((Rtdomain_return_okscCstS(s}Return false if cookies should not be returned, given cookie path. See the docstring for domain_return_ok. N(R (R`R:R,((Rtpath_return_oks(RRRRRRR(((RRs  tDefaultCookiePolicyc Bs,tZdZdZdZdZdZeeBZeee e ee e e e ee e d Z dZ dZ dZd Zd Zd Zd Zd ZdZdZdZdZdZdZdZdZdZdZdZdZdZ dZ!dZ"RS(s+Implements the standard rules for accepting and returning cookies. Both RFC 2965 and Netscape cookies are covered. RFC 2965 handling is switched off by default. The easiest way to provide your own policy is to override this class and call its methods in your overriden implementations before adding your own additional checks. import mechanize class MyCookiePolicy(mechanize.DefaultCookiePolicy): def set_ok(self, cookie, request): if not mechanize.DefaultCookiePolicy.set_ok( self, cookie, request): return False if i_dont_want_to_store_this_cookie(): return False return True In addition to the features required to implement the CookiePolicy interface, this class allows you to block and allow domains from setting and receiving cookies. There are also some strictness switches that allow you to tighten up the rather loose Netscape protocol rules a little bit (at the cost of blocking some benign cookies). A domain blacklist and whitelist is provided (both off by default). Only domains not in the blacklist and present in the whitelist (if the whitelist is active) participate in cookie setting and returning. Use the blocked_domains constructor argument, and blocked_domains and set_blocked_domains methods (and the corresponding argument and methods for allowed_domains). If you set a whitelist, you can turn it off again by setting it to None. Domains in block or allow lists that do not start with a dot must string-compare equal. For example, "acme.com" matches a blacklist entry of "acme.com", but "www.acme.com" does not. Domains that do start with a dot are matched by more specific domains too. For example, both "www.acme.com" and "www.munitions.acme.com" match ".acme.com" (but "acme.com" itself does not). IP addresses are an exception, and must match exactly. For example, if blocked_domains contains "192.168.1.2" and ".168.1.2" 192.168.1.2 is blocked, but 193.168.1.2 is not. Additional Public Attributes: General strictness switches strict_domain: don't allow sites to set two-component domains with country-code top-level domains like .co.uk, .gov.uk, .co.nz. etc. This is far from perfect and isn't guaranteed to work! RFC 2965 protocol strictness switches strict_rfc2965_unverifiable: follow RFC 2965 rules on unverifiable transactions (usually, an unverifiable transaction is one resulting from a redirect or an image hosted on another site); if this is false, cookies are NEVER blocked on the basis of verifiability Netscape protocol strictness switches strict_ns_unverifiable: apply RFC 2965 rules on unverifiable transactions even to Netscape cookies strict_ns_domain: flags indicating how strict to be with domain-matching rules for Netscape cookies: DomainStrictNoDots: when setting cookies, host prefix must not contain a dot (eg. www.foo.bar.com can't set a cookie for .bar.com, because www.foo contains a dot) DomainStrictNonDomain: cookies that did not explicitly specify a Domain cookie-attribute can only be returned to a domain that string-compares equal to the domain that set the cookie (eg. rockets.acme.com won't be returned cookies from acme.com that had no Domain cookie-attribute) DomainRFC2965Match: when setting cookies, require a full RFC 2965 domain-match DomainLiberal and DomainStrict are the most useful combinations of the above flags, for convenience strict_ns_set_initial_dollar: ignore cookies in Set-Cookie: headers that have names starting with '$' strict_ns_set_path: don't allow setting cookies whose path doesn't path-match request URI iiiic Cs||_||_||_||_||_||_| |_| |_| |_ | |_ |dj ot ||_n f|_|dj ot |}n||_dS(s Constructor arguments should be used as keyword arguments only. blocked_domains: sequence of domain names that we never accept cookies from, nor return cookies to allowed_domains: if not None, this is a sequence of the only domains for which we accept and return cookies For other arguments, see CookiePolicy.__doc__ and DefaultCookiePolicy.__doc__.. N(tnetscapeR`trfc2965trfc2109_as_netscapet hide_cookie2t strict_domaintstrict_rfc2965_unverifiabletstrict_ns_unverifiabletstrict_ns_domaintstrict_ns_set_initial_dollartstrict_ns_set_pathtblocked_domainsRttuplet_blocked_domainstallowed_domainst_allowed_domains( R`RRRRRRRRRRRR((RRoBs"             cCs|iS(s4Return the sequence of blocked domains (as a tuple).N(R`R(R`((RRqscCst||_dS(s$Set the sequence of blocked domains.N(RRR`R(R`R((Rtset_blocked_domainstscCs0x)|iD]}t||otSq q WtS(N(R`Rtblocked_domainR*RcR R!(R`RcR((Rt is_blockedxs   cCs|iS(s=Return None, or the sequence of allowed domains (as a tuple).N(R`R(R`((RR~scCs*|dj ot|}n||_dS(s-Set the sequence of allowed domains, or None.N(RRRR`R(R`R((Rtset_allowed_domainss cCsH|idjotSnx)|iD]}t||otSq"q"WtS(N(R`RRR!tallowed_domainR*RcR (R`RcR((Rtis_not_alloweds  cCsjtd||id j ptx?d D]7}d|}t||}|||pt Sq+q+Wt 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 %sR]t verifiabilityRaR:RcRDtset_ok_N(sversionRsnamespathsdomainsport( RGRRaRtAssertionErrortntfn_nameRR`tfnR,R!R (R`RR,RRR((RRs   cCs|idjotd|tSn|idjo|i otdtSn.|idjo|i otdtSntS(Ns- Set-Cookie2 without version attribute (%s)is$ RFC 2965 cookies are switched offs$ Netscape cookies are switched off( RR]RRGR!R`RRR (R`RR,((Rtset_ok_versions   cCsw|ioit|o\|idjo|iotdtSqs|idjo|i otdtSqsnt S(Nis> third-party RFC 2965 cookie during unverifiable transactions> third-party Netscape cookie during unverifiable transaction( R,t unverifiableR[RR]R`RRGR!RR (R`RR,((Rtset_ok_verifiabilitys   cCsI|idjo5|io+t|idotd|itSntS(Nit$s' illegal name (starts with '$'): '%s'( RR]R`RR%RaRGR!R (R`RR,((Rt set_ok_names-cCsw|ioit|}|idjp|idjo|io/t||i  ot d|i |t Sqsnt S(Nis7 path attribute %s is not a prefix of request path %s( RRfRAR,R@R]R`RR%R:RGR!R (R`RR,R@((Rt set_ok_paths  >  cCs|io|io|i}|idptti|ddjoti |d}||d}|d|!}ti |dddddd d d d d ddddddddddgjot|djotSqqntS(sReturn False if explicit cookie domain is not acceptable. Called by set_ok_domain, for convenience of overriding by subclasses. RiitcotactcomtedutorgtnettgovtmilREtaerotbiztcattcooptinfotjobstmobitmuseumRatprottravelN(RRdR`RRcR%RRtcountR"R#ttldtsldRtlenR!R (R`RR,RcR#RR((Rtset_ok_countrycode_domains  ec Cs<|i|iotd|itSn|i|iotd|itSn|i||ptd|itSn|i ot |\}}|i}t |do|d}n|}ti|ddj}| o"|djotd|tSn|idjoSt|| o>t |d o-td|| otd ||tSqon|idjp|i|i@o,t||ptd ||tSqn|idjp|i|i@oW|t| }ti|ddjo)ti| otd ||tSq4q8ntS( Ns" domain %s is in user block-lists& domain %s is not in user allow-lists& country-code second level domain %sRiis.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(R`RRRcRGR!RRR,RdR8R6R5R%tundotted_domainRR7t embedded_dotsR]R)RtDomainRFC2965MatchR&tDomainStrictNoDotsRt host_prefixRRR ( R`RR,RcRR6RRR5((Rt set_ok_domainsJ  7  !  !* cCs|iot|}|djo d}n t|}xti|i dD]M}yt |Wn$t j ot d|tSnX||joPqOqOWt d||i tSntS(Nt80t,s bad port %s (not numeric)s$ request port (%s) not found in %s(RR_RIR,treq_portRtstrRtsplitRDR{RERFRGR!R (R`RR,R{R((Rt set_ok_ports$          cCsStd|x?d D]7}d|}t||}|||ptSqqWt 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 %sR]RRgR^RDRct return_ok_N(sversions verifiabilityssecuresexpiressportsdomain( RGRRRRR`RR,R!R (R`RR,RRR((RR.s   cCs^|idjo|i otdtSn.|idjo|i otdtSntS(Nis$ RFC 2965 cookies are switched offs$ Netscape cookies are switched off(RR]R`RRGR!RR (R`RR,((Rtreturn_ok_version@s  cCsw|ioit|o\|idjo|iotdtSqs|idjo|i otdtSqsnt S(Nis> third-party RFC 2965 cookie during unverifiable transactions> third-party Netscape cookie during unverifiable transaction( R,RR[RR]R`RRGR!RR (R`RR,((Rtreturn_ok_verifiabilityIs   cCs3|io%|idjotdtSntS(Nthttpss( secure cookie with non-secure request(RRgR,tget_typeRGR!R (R`RR,((Rtreturn_ok_secureUs cCs)|i|iotdtSntS(Ns cookie expired(RRzR`t_nowRGR!R (R`RR,((Rtreturn_ok_expires[s cCs~|iopt|}|djo d}nxJti|idD]}||joPqCqCWt d||it Snt S(NRRs0 request port %s does not match cookie port %s( RRDRIR,RRRRR{RGR!R (R`RR,R{R((Rtreturn_ok_portas       cCst|\}}|i}|idjo;|i|i @o*|i o||jot dt Sn|idjo)t || ot d||t Sn|idjo-td|| 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(R8R,R6R5RRcR]R`RtDomainStrictNonDomainRdRGR!R&R)R (R`RR,RcR6R5((Rtreturn_ok_domainos 9 ! % cCst|\}}t|dpd|}nt|dpd|}nt||p t||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( R8R,tdotted_req_hostt dotted_erhnR%R)RcR!R`RRGRR (R`RcR,RR((RRs   cCsEtd|t|}t||ptd||tSntS(Ns- checking cookie path=%ss %s does not path-match %s(RGR:RAR,R@R%R!R (R`R:R,R@((RRs   (#RRRRRRt DomainLiberalt DomainStrictRR R!RoRRRRRRRRRRRRRRRRRRRRRRR(((RRs< P -/          .       cCs&|i}|it|i|S(N(tadictRvtsorttmapRr(RRv((Rtvals_sorted_by_keys  tMappingIteratorcBs)tZdZdZdZdZRS(sBIterates over nested mapping, depth-first, in sorted order by key.cCst|ddfg|_dS(Ni(RtmappingRR`t_s(R`R((RRoscCs|S(N(R`(R`((Rt__iter__scCsxy|ii\}}}Wntj otnX|t|jou||}|d}|ii |||fy |i Wnt j oPqX|ii t |d|fqqqW|S(Nii(R`RtpoptvalsR#t prev_itemt IndexErrort StopIterationRtitemRtitemstAttributeErrorR(R`R#RRR((Rtnexts"    (RRRRoRR(((RRs   tAbsentcBstZRS(N(RR(((RRst CookieJarcBs+tZdZeidZeidZeidZeidZeidZ e dZ dZ dZ d Zd Zd Zd Zd ZdZdZdZdZdZdZe e e dZdZdZdZdZdZdZdZ RS(sCollection of HTTP cookies. You may not need to know about this class: try mechanize.urlopen(). The major methods are extract_cookies and add_cookie_header; these are all you are likely to need. CookieJar supports the iterator protocol: for cookie in cookiejar: # do something with cookie Methods: add_cookie_header(request) extract_cookies(response, request) make_cookies(response, request) set_cookie_if_ok(cookie, request) set_cookie(cookie) clear_session_cookies() clear_expired_cookies() clear(domain=None, path=None, name=None) Public attributes policy: CookiePolicy object s\Ws([\"\\])s\.?[^.]*s[^.]*s^\.+cCs9|djo t}n||_h|_d|_dS(sD See CookieJar.__doc__ for argument documentation. iN(tpolicyRRR`t_policyt_cookiest_prev_getitem_index(R`R((RRos     cCs ||_dS(N(RR`R(R`R((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(tcookiesR`RRRcR,RGRtcookies_by_pathRvR:Rtcookies_by_nametvaluesRRR(R`RcR,RRRRR:((Rt_cookies_for_domains$       cCs=g}x0|iiD]}|i|i||qW|S(s2Return a list of cookies to be returned to server.N(RR`RRvRctextendRR,(R`R,RRc((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|int|idoG|i}|i ot|do|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(tcmpRRXR:ta(RRX((Rtdecreasing_size'sis $Version=%ss\\\1s%s=%ss $Path="%s"Ris $Domain="%s"s$Ports="%s"N(RRRR!t version_settattrsRR]R RRbRR`t non_word_reRtquote_reR3RaRfR:R%RcReRDR{R_( R`RRRbR{R]RRRcR((Rt _cookie_attrss@    3     cCstdtti|i_|_t|\}}|ii |ii @}|i |}|i|}|o4|idp |idti|dqn|iioY|ii oKxH|D]<}|idjo&|id o|iddPqqWn|idS(sAdd correct Cookie: header to request (urllib2.Request object). The Cookie2 header is also added unless policy.hide_cookie2 is true. The request object (usually a urllib2.Request instance) must support the methods get_full_url, get_host, get_type, has_header, get_header, header_items and add_unredirected_header, as documented by urllib2, and the port attribute (the port number). Actually, RequestUpgradeProcessor will automatically upgrade your Request object to one with has_header, get_header, header_items and add_unredirected_header, if it lacks those methods, for compatibility with pre-2.4 versions of urllib2. tadd_cookie_headerR\s; itCookie2s $Version="1"N(RGRERyR`RRR8R,R6R5RRtstrict_non_domainRRR Rt has_headertadd_unredirected_headerRRRRRR]tclear_expired_cookies(R`R,RR5R6R RR((RR Ys" $! cCsMg}d}d} x4|D],}|d \} } t}t} h}h}x|d D]\} }ti| }|| jp ||jo |} n| |jo|djo t}n|i| oqRn| djo5|djotd t} Pnti|}n| djo0|oqRn|djotdqRqKn| djoTt}yt|}Wn$tj otdt} PnXd} |i|}n| | jp | |joJ|djo/| dd d gjotd| t} Pn||| RAR,RR"R#RRdRetboolR%R8R6R5R_RItreR3R`RtcleartKeyErrorRGR\(R`RR,RiRcRgRmRDReR6R]RfR_R^RR5R:RdRaR#RbRhRj((Rt_cookie_from_cookie_tuples|              cCsT|i|}g}x8|D]0}|i||}|o|i |qqW|S(N( R`RRRRRR#R,RR(R`RR,RRRR((Rt_cookies_from_attrs_set9scCsv|iidjo|ii }n |ii}x?|D]7}|idjo!t|_ |o d|_qnq7q7WdS(Nii( R`RRRRRRR]R Rk(R`RRR((Rt_process_rfc2109_cookiesBs  c Cs|i}t|d}t|d} |ii}|ii }| o| p0| o| p | o| p| o| ogSny|i t ||}Wntg}nX| o|oy|i t| |}Wntg}nX|i||oRh} x*|D]"} d| | i| i| if|D]6}|i|o |i|i|i|iqqWdS(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( RyRxR`RRzR!RcR:Ra(R`RxR((RRs  cCs|djo|i|_n%|i|djotdn||_y|iiSWntj otnXdS(Niis8CookieJar.__getitem__ only supports sequential iteration(R#R`Rt_getitem_iteratorRRRR(R`R#((Rt __getitem__s  cCs t|iS(N(RR`R(R`((RRscCs%d}x|D]}|d}q W|S(s#Return number of contained cookies.iiN(R#R`R(R`R#R((Rt__len__s cCsGg}x!|D]}|it|q Wd|iti|dfS(Ns<%s[%s]>s, (trR`RRRt __class__RR(R`R>R((RRs cCsGg}x!|D]}|it|q Wd|iti|dfS(Ns<%s[%s]>s, (R>R`RRRR?RR(R`R>R((RR~s (!RRRR tcompileRR tstrict_domain_ret domain_retdots_reRRoRRRR R RR#R$R%R3R5R4R9R!R:RR<RR=RR~(((RRs8      < ' a X ;      t LoadErrorcBstZRS(N(RR(((RRDst FileCookieJarcBsVtZdZeeedZeeedZeeedZeeedZRS(sCookieJar that can be loaded from and saved to a file. Additional methods save(filename=None, ignore_discard=False, ignore_expires=False) load(filename=None, ignore_discard=False, ignore_expires=False) revert(filename=None, ignore_discard=False, ignore_expires=False) Additional public attributes filename: filename for loading and saving cookies Additional public readable attributes delayload: request that cookies are lazily loaded from disk; this is only a hint since this only affects performance, not behaviour (unless the cookies on disk are changing); a CookieJar object may ignore it (in fact, only MSIECookieJar lazily loads cookies at the moment) cCsWti|||dj ot| otdn||_t||_ dS(s See FileCookieJar.__doc__ for argument documentation. Cookies are NOT loaded from the named file until either the load or revert method is called. sfilename must be string-likeN( RRoR`RtfilenameRt isstringlikeRFRt delayload(R`RFRHR((RRo1s  cCs tdS(sSave cookies to a file. filename: name of file in which to save cookies ignore_discard: save even cookies set to be discarded ignore_expires: save even cookies that have expired The file is overwritten if it already exists, thus wiping all its cookies. Saved cookies can be restored later using the load or revert methods. If filename is not specified, self.filename is used; if self.filename is None, ValueError is raised. N(R(R`RFtignore_discardtignore_expires((Rtsave?s cCsr|djo-|idj o |i}q:ttnt|}z|i||||Wd|i XdS(sLoad cookies from a file. Old cookies are kept unless overwritten by newly loaded ones. Arguments are as for .save(). If filename is not specified, self.filename is used; if self.filename is None, ValueError is raised. The named file must be in the format understood by the class, or LoadError will be raised. This format will be identical to that written by the save method, unless the load format is not sufficiently well understood (as is the case for MSIECookieJar). N( RFRR`RFtMISSING_FILENAME_TEXTtopenRt _really_loadRIRJtclose(R`RFRIRJR((RtloadNs    cCs|djo-|idj o |i}q:ttnti|i}h|_y|i |||Wn%t t fj o||_nXdS(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(RFRR`RFRLRltdeepcopyRt old_stateRPRIRJRDtIOError(R`RFRIRJRR((Rtrevertfs    ( RRRRR!RoRKRPRT(((RREs (:RRR R/RRlRytstructRRROtloggingt threadingt _threadingt ImportErrortdummy_threadingthttplibRLRt HTTP_PORTRHt _headersutilR/R0t_utilR%R)RGR,t getLoggerRGRR@RRR&R'R*R2R4R8RARIRTRURMR>RYR[R\RRRRRRt ExceptionRDRE(1RTRURR[RR)RRR4RURR>RDR/RRRMR,R RR\R2RR%R*R8RRLR'RYRRRRlR&RORAR[RVRERXRGR/RWRHRyRGR0RIRZ((Rt?!sTZ      %        # {V  L