[ (@sdZddddddddgZd d lZd d lZd d lZd d lZd d lZd d lZyd d l Z Wne k rd d l Z YnXd d l Zd d lmZd Zd ad dZeejjZdZddZdZddZdddddddgZddddd d!d"d#d$d%d&d'g ZgZx!eD]Zej ej!qYWd d(d)Z"d d*d+Z#id d,6d d-6d d.6d d/6Z$ej%d0ej&Z'd1d2Z(d3d4Z)ej%d5ej&Z*ej%d6ej+ej&BZ,ej%d7ej-ej&BZ.d8d9Z/ej%d:ej-ej&BZ0d;d<Z1d=d>Z2ej%d?Z3ej%d@Z4ej%dAZ5ej%dBZ6dCdDZ7ej%dEZ8dFdGZ9dHdIZ:dJdKZ;ej%dLej&Z<dMdNZ=dOdPZ>dQdRZ?dSdTZ@ej%dUej&ZAdVdWZBdXdYZCdZd[ZDd\d]ZEd^ZFej%d_ZGd`daZHdbdcZIdddeZJdfdgZKGdhddZLGdiddZMGdjddeMZNdkdlZOdmdnZPGdodpdpZQGdqddZRGdrddeSZTGdsddeRZUdtduZVGdvddeUZWGdwddeUZXd S)xaHTTP 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;ts dSts.ddl}|jdantj|S)Nr zhttp.cookiejar)debugloggerloggingZ getLogger)argsr r$/usr/lib/python3.4/http/cookiejar.py_debug.s  rzQa filename was not supplied (nor was the CookieJar instance initialised with one)cCsgddl}ddl}ddl}|j}|jd||j}|jd|dddS)Nr zhttp.cookiejar bug! %s stacklevel)iowarnings tracebackStringIO print_excgetvaluewarn)rrrfmsgrrr_warn_unhandled_exception<s $  ricCs|dd\}}}}}}|tkrd|koEdknrd|koadknrd|ko}dknrd|kodknrd|kodknrt|SdSdS) N r ;=) EPOCH_YEARr )ttyearmonthZmdayhourminsecrrr_timegmKs "DT r,ZMonZTueZWedZThuZFriZSatZSunZJanZFebZMarZAprZMayZJunZJulZAugZSepZOctZNovZDeccCs\|dkrtjj}ntjj|}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)minutesecond)tdtrrr time2isozYs r5cCsq|dkrtjj}ntjj|}dt|j|jt|jd|j|j |j |j fS)zReturn a string representing time in seconds since epoch, t. If the function is called without an argument, it will use the current time. The format of the returned string is like this: Wed, DD-Mon-YYYY HH:MM:SS GMT Nz"%s %02d-%s-%04d %02d:%02d:%02d GMTr) r-r.r/DAYSZweekdayr0MONTHSr(r'r)r1r2)r3r4rrr time2netscapels !r8ZGMTUTCZUTZz^([-+])?(\d\d?):?(\d\d)?$cCsd}|tkrd}ntj|}|rdt|jd}|jdrx|dt|jd}n|jddkr| }qn|S)Nr ir<r-) UTC_ZONES TIMEZONE_REsearchintgroup)tzoffsetmrrroffset_from_tz_strings    rFc Csytj|jd}Wndtk ryt|}Wntk rUdSYnXd|komdknr{|}ndSYnX|dkrd}n|dkrd}n|dkrd}nt|}t|}t|}t|}t|}|dkrtjtjd}|d} |} ||| }| | } t| dkr| dkrw|d}q|d}qnt|||||||f} | dk r|dkrd}n|j }t |} | dkrdS| | } n| S)Nrr r id2r9) MONTHS_LOWERindexlower ValueErrorrAtimeZ localtimeabsr,upperrF) r0monyrhrr*r+rCZimonZcur_yrrEZtmpr3rDrrr _str2timesR                 !       rSzV^[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 Cs4tj|}|r|j}tj|djd}t|d|t|dt|dt|dt|df}t|S|j }t j d|d}dgd \}}}}}} } t j|}|dk r|j\}}}}}} } ndSt |||||| | S) aReturns time in seconds since epoch of time represented by a string. Return value is an integer. None is returned if the format of str is unrecognized, the time is outside the representable range, or the timezone string is not recognized. If the string contains no timezone, UTC is assumed. The timezone in the string may be numerical (like "-0800" or "+0100") or a string timezone (like "UTC", "GMT", "BST" or "EST"). Currently, only the timezone strings equivalent to UTC (zero offset) are known to the function. The function loosely parses the following formats: Wed, 09 Feb 1994 22:23:32 GMT -- HTTP format Tuesday, 08-Feb-94 14:15:29 GMT -- old rfc850 HTTP format Tuesday, 08-Feb-1994 14:15:29 GMT -- broken rfc850 HTTP format 09 Feb 1994 22:23:32 GMT -- HTTP format (no weekday) 08-Feb-94 14:15:29 GMT -- rfc850 format (no weekday) 08-Feb-1994 14:15:29 GMT -- broken rfc850 format (no weekday) The parser ignores leading and trailing whitespace. The time may be absent. If the year is given with only 2 digits, the function will select the century that makes the year closest to the current date. rrr r;N)STRICT_DATE_REr@groupsrIrJrKrAfloatr,lstrip WEEKDAY_REsubLOOSE_HTTP_DATE_RErS) textrEgrPr&r0rQrRr*r+rCrrr http2times -  " $raa^ (\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|j}dgd\}}}}}}}tj|}|dk rp|j\}}}}}}}} 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 NrW)r[ ISO_DATE_REr@rYrS) r_r0rPrQrRr*r+rCrE_rrriso2time$s " 'rdcCs7|jd\}}|jd||j|dS)z)Return unmatched part of re.Match object.r N)spanstring)matchstartendrrr unmatchedEsrjz^\s*([^=\s;,]+)z&^\s*=\s*\"([^\"\\]*(?:\\.[^\"\\]*)*)\"z^\s*=\s*([^\s;,]*)z\\(.)c Cst|t stg}x|D]}|}g}xj|rtj|}|rt|}|jd}tj|}|rt|}|jd}tj d|}nEt j|}|rt|}|jd}|j }nd}|j ||fq8|j jdrX|j dd}|rO|j |ng}q8tjdd|\}} | dkstd|||f|}q8W|r#|j |q#q#W|S) amParse header values into a list of lists containing key,value pairs. The function knows how to deal with ",", ";" and "=" as well as quoted values after "=". A list of space separated tokens are parsed as if they were separated by ";". If the header_values passed as argument contains multiple values, then they are treated as if they were a single value separated by comma ",". This means that this function is useful for parsing header fields that follow this syntax (BNF as from the HTTP/1.1 specification, but we relax the requirement for tokens). headers = #header header = (token | parameter) *( [";"] (token | parameter)) token = 1* separators = "(" | ")" | "<" | ">" | "@" | "," | ";" | ":" | "\" | <"> | "/" | "[" | "]" | "?" | "=" | "{" | "}" | SP | HT quoted-string = ( <"> *(qdtext | quoted-pair ) <"> ) qdtext = > quoted-pair = "\" CHAR parameter = attribute "=" value attribute = token value = token | quoted-string Each header is represented by a list of key/value pairs. The value for a simple token (not part of a parameter) is None. Syntactically incorrect headers will not necessarily be parsed as you would want. This is easier to describe with some examples: >>> split_header_words(['foo="bar"; port="80,81"; discard, bar=baz']) [[('foo', 'bar'), ('port', '80,81'), ('discard', None)], [('bar', 'baz')]] >>> split_header_words(['text/html; charset="iso-8859-1"']) [[('text/html', None), ('charset', 'iso-8859-1')]] >>> split_header_words([r'Basic realm="\"foo\bar\""']) [[('Basic', None), ('realm', '"foobar"')]] rz\1N,z^[=\s;]*rVr z&split_header_words bug: '%s', '%s', %s) isinstancestrAssertionErrorHEADER_TOKEN_REr@rjrBHEADER_QUOTED_VALUE_REHEADER_ESCAPE_REr]HEADER_VALUE_RErstripappendr[ startswithresubn) Z header_valuesresultr_Z orig_textpairsrEnamevalueZnon_junkZ nr_junk_charsrrrsplit_header_wordsNsF-       r|z([\"\\])cCsg}x|D]}g}xq|D]i\}}|dk r|tjd|sitjd|}d|}nd||f}n|j|q W|r |jdj|q q Wdj|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+$z\\\1z"%s"z%s=%sz; z, )rvr@HEADER_JOIN_ESCAPE_REr]rtjoin)Zlistsheadersryattrkvrrrjoin_header_wordss    rcCsH|jdr"|dd}n|jdrD|dd}n|S)N"r)ruendswith)r_rrr strip_quotess rc Cshd}g}xU|D]M}g}d}x ttjd |D]\}}|j}|d krhq>nd |kr|d }} n'tjd |d\}} |j}|dkr|j} | |kr| }n|dkrt| } d}n|dkrtt| } qn|j|| fq>W|r|sP|jdn|j|qqW|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-ageFz;\s*rV=Nz\s*=\s*rr T0)zexpireszdomainzpathzsecureversionzportr)rr) enumeratervsplitrsr[rKrrart) Z ns_headersZ known_attrsrxZ ns_headerry version_setZiiZparamrrlcrrrparse_ns_headerss: %            rz\.\d+$cCsKtj|rdS|dkr#dS|ddksC|ddkrGdSdS)z*Return True if text is a host domain name.FrVr .rTr)IPV4_REr@)r_rrris_HDNs  rcCs|j}|j}||kr(dSt|s8dS|j|}|dks_|dkrcdS|jdsvdSt|ddsdSdS)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 rNr)rKrrfindru)ABirrr domain_matchs    rcCstj|rdSdS)zdReturn True if text is a sort-of-like a host domain name. For accepting/blocking domains. FT)rr@)r_rrrliberal_is_HDN/srcCs|j}|j}t|o-t|sD||kr@dSdS|jd}|rl|j|rldS| r||krdSdS)z\For blocking/accepting domains. A and B may be host domain names or IP addresses. TFr)rKrrur)rr initial_dotrrruser_domain_match9s   rz:\d+$cCsb|j}tjj|d}|dkrC|jdd}ntjd|d}|jS)zReturn request-host, as defined by RFC 2965. Variation from RFC: returned value is lowercased, for convenient comparison. rrVZHost) get_full_urlurllibparseZurlparseZ get_header cut_port_rer]rK)requesturlhostrrr request_hostNs   rcCsLt|}}|jddkrBtj| rB|d}n||fS)zzReturn a tuple (request-host, effective request-host name). As defined by RFC 2965, except both are lowercased. rrz.localr)rfindrr@)rerhnreq_hostrrreff_request_host^s% rcCsM|j}tjj|}t|j}|jdsId|}n|S)z6Path component of request-URI, as defined by RFC 2965./)rrrZurlsplit escape_pathrru)rrpartsrrrr request_pathis   rc Csy|j}|jd}|dkro||dd}yt|Wqutk rktd|dSYquXnt}|S)N:r rznonnumeric port: '%s')rrrArLrDEFAULT_HTTP_PORT)rrrrrrr request_portss     rz%/;:@&=+$,!~*'()z%([0-9a-fA-F][0-9a-fA-F])cCsd|jdjS)Nz%%%sr)rBrO)rgrrruppercase_escaped_charsrcCs+tjj|t}tjt|}|S)zEEscape any invalid characters in HTTP URL, and uppercase all escapes.)rrZquoteHTTP_PATH_SAFEESCAPED_CHAR_REr]r)rrrrrs rcCsq|jd}|dkrm||dd}|jd}t|rm|dksb|dkrmd|Sn|S)aBReturn 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' rr rNZlocal)rr)hrbrrrreachs $ rcCs0t|}t|t|js(dSdSdS)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. TFN)rrrZorigin_req_host)rrrrris_third_partys rc@sseZdZdZdddZddZddd 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 |dk rt|}n| dk r6t| } n|dkr]|dkr]tdn||_||_||_||_||_|j|_||_ ||_ | |_ | |_ | |_ | |_| |_||_||_||_tj||_dS)NTz-if port is None, port_specified must be false)rArLrrzr{rport_specifiedrKrdomain_specifieddomain_initial_dotrpath_specifiedrrdiscardcomment comment_urlrfc2109copy_rest)selfrrzr{rrrrrrrrrrrrrestrrrr__init__s.                 zCookie.__init__cCs ||jkS)N)r)rrzrrrhas_nonstandard_attrszCookie.has_nonstandard_attrNcCs|jj||S)N)rget)rrzdefaultrrrget_nonstandard_attrszCookie.get_nonstandard_attrcCs||j|)rrrr{rz)rplimitZ namevaluerrr__str__s   zCookie.__str__cCsg}x:dD]2}t||}|jd|t|fq W|jdt|j|jdt|jddj|S)Nrrzr{rrrrrrrrrrrrz%s=%szrest=%sz rfc2109=%sz Cookie(%s)z, )zversionznamezvaluezportzport_specifiedzdomainzdomain_specifiedzdomain_initial_dotzpathzpath_specifiedzsecurezexpireszdiscardzcommentz comment_url)getattrrtreprrrr~)rrrzrrrr__repr__s !zCookie.__repr__) __name__ __module__ __qualname____doc__rrrrrrrrrrrrs     c@sFeZdZdZddZddZddZdd 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 customised policy. cCs tdS)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. N)NotImplementedError)rcookierrrrset_ok4szCookiePolicy.set_okcCs tdS)zAReturn true if (and only if) cookie should be returned to server.N)r)rrrrrr return_ok=szCookiePolicy.return_okcCsdS)zMReturn false if cookies should not be returned, given cookie domain. Tr)rrrrrrdomain_return_okAszCookiePolicy.domain_return_okcCsdS)zKReturn false if cookies should not be returned, given cookie path. Tr)rrrrrrpath_return_okFszCookiePolicy.path_return_okN)rrrrrrrrrrrrr+s   c@speZdZdZdZdZdZdZeeBZdddddddddeddd d Z d d Z d dZ ddZ ddZ ddZddZddZddZddZddZdd Zd!d"Zd#d$Zd%d&Zd'd(Zd)d*Zd+d,Zd-d.Zd/d0Zd1d2Zd3d4Zd5d6ZdS)7rzBImplements the standard rules for accepting and returning cookies.rrrTr NTFc Cs||_||_||_||_||_||_| |_| |_| |_| |_ |dk rxt ||_ n f|_ |dk rt |}n||_ dS)zAConstructor arguments should be passed as keyword arguments only.N) netscaperfc2965rfc2109_as_netscape hide_cookie2 strict_domainstrict_rfc2965_unverifiablestrict_ns_unverifiablestrict_ns_domainstrict_ns_set_initial_dollarstrict_ns_set_pathtuple_blocked_domains_allowed_domains) rblocked_domainsallowed_domainsrrrrrrrrrrrrrrVs             zDefaultCookiePolicy.__init__cCs|jS)z4Return the sequence of blocked domains (as a tuple).)r)rrrrrwsz#DefaultCookiePolicy.blocked_domainscCst||_dS)z$Set the sequence of blocked domains.N)rr)rrrrrset_blocked_domainszsz'DefaultCookiePolicy.set_blocked_domainscCs+x$|jD]}t||r dSq WdS)NTF)rr)rrZblocked_domainrrr is_blocked~szDefaultCookiePolicy.is_blockedcCs|jS)z=Return None, or the sequence of allowed domains (as a tuple).)r)rrrrrsz#DefaultCookiePolicy.allowed_domainscCs(|dk rt|}n||_dS)z-Set the sequence of allowed domains, or None.N)rr)rrrrrset_allowed_domainss z'DefaultCookiePolicy.set_allowed_domainscCs>|jdkrdSx$|jD]}t||rdSqWdS)NFT)rr)rrZallowed_domainrrris_not_alloweds z"DefaultCookiePolicy.is_not_allowedcCsltd|j|j|jdk s+tx:d D]2}d |}t||}|||s2d Sq2Wd S) 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). z - checking cookie %s=%sNr verifiabilityrzrrrZset_ok_FT)zversionrznamezpathzdomainzport)rrzr{rnr)rrrnfn_namefnrrrrs  zDefaultCookiePolicy.set_okcCs{|jdkr)td|j|jdS|jdkrP|j rPtddS|jdkrw|j rwtddSdS)Nz0 Set-Cookie2 without version attribute (%s=%s)Fr z$ RFC 2965 cookies are switched offz$ Netscape cookies are switched offT)rrrzr{rr)rrrrrrset_ok_versions  z"DefaultCookiePolicy.set_ok_versioncCsh|jrdt|rd|jdkr;|jr;tddS|jdkrd|jrdtddSndS)Nr z> third-party RFC 2965 cookie during unverifiable transactionFz> third-party Netscape cookie during unverifiable transactionT) unverifiablerrrrr)rrrrrrset_ok_verifiabilitys  z(DefaultCookiePolicy.set_ok_verifiabilitycCsB|jdkr>|jr>|jjdr>td|jdSdS)Nr $z' illegal name (starts with '$'): '%s'FT)rrrzrur)rrrrrr set_ok_names zDefaultCookiePolicy.set_ok_namecCsm|jrit|}|jdks<|jdkri|jri|j|j ritd|j|dSndS)Nr z7 path attribute %s is not a prefix of request path %sFT)rrrrrurr)rrrreq_pathrrr set_ok_paths   zDefaultCookiePolicy.set_ok_pathc Cs|j|jr&td|jdS|j|jrLtd|jdS|jrt|\}}|j}|jr!|jddkr!|jd}|jdd|}|dkr!||dd}||d|} | j d$krt |dkrtd|dSq!n|j drC|dd} n|} | j ddk} | r|dkrtd|dS|j dkr|j| r|j d rd|j| rtd ||dSn|j dks|j|j@r!t||s!td!||dSn|j dks@|j|j@r|dt | } | j ddkrtj| rtd"| |dSqnd#S)%Nz" domain %s is in user block-listFz& domain %s is not in user allow-listrrr rcoaccomeduorgnetgovmilrAaerobizcatcoopinfojobsmobimuseumrzprotraveleuz& country-code second level domain %sz.localz/ 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)zcorrrrrrrzintrrzcatr zinfor r r znamerrr)rrrrrrrcountrrKlenrurrrrDomainRFC2965MatchrDomainStrictNoDotsrr@) rrrrrrrjZtldZsldZundotted_domainZ embedded_dotsZ host_prefixrrr set_ok_domainsf          z!DefaultCookiePolicy.set_ok_domainc Cs|jrt|}|dkr*d}n t|}xx|jjdD]J}yt|Wn#tk rtd|dSYnX||krIPqIqIWtd||jdSndS)N80rkz bad port %s (not numeric)Fz$ request port (%s) not found in %sT)rrrmrrrArLr)rrrreq_portrrrr set_ok_ports"          zDefaultCookiePolicy.set_ok_portcCsWtd|j|jx:d D]2}d|}t||}|||sd SqWd S) 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). z - checking cookie %s=%srrrrrrZ return_ok_FT)zversionz verifiabilityzsecurezexpireszportzdomain)rrzr{r)rrrrrrrrrr's   zDefaultCookiePolicy.return_okcCsR|jdkr'|j r'tddS|jdkrN|j rNtddSdS)Nr z$ RFC 2965 cookies are switched offFz$ Netscape cookies are switched offT)rrrr)rrrrrrreturn_ok_version9s  z%DefaultCookiePolicy.return_ok_versioncCsh|jrdt|rd|jdkr;|jr;tddS|jdkrd|jrdtddSndS)Nr z> third-party RFC 2965 cookie during unverifiable transactionFz> third-party Netscape cookie during unverifiable transactionT)rrrrrr)rrrrrrreturn_ok_verifiabilityBs  z+DefaultCookiePolicy.return_ok_verifiabilitycCs*|jr&|jdkr&tddSdS)NZhttpsz( secure cookie with non-secure requestFT)rtyper)rrrrrrreturn_ok_secureNs z$DefaultCookiePolicy.return_ok_securecCs$|j|jr tddSdS)Nz cookie expiredFT)r_nowr)rrrrrrreturn_ok_expiresTs z%DefaultCookiePolicy.return_ok_expirescCsu|jrqt|}|dkr*d}nxD|jjdD]}||kr=Pq=q=Wtd||jdSndS)Nrrkz0 request port %s does not match cookie port %sFT)rrrr)rrrrrrrrreturn_ok_portZs      z"DefaultCookiePolicy.return_ok_portcCst|\}}|j}|jdkr^|j|j@r^|j r^||kr^tddS|jdkrt|| rtd||dS|jdkrd|j| rtd||dSdS)Nr 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 %srz; request-host %s does not match Netscape cookie domain %sT) rrrrDomainStrictNonDomainrrrr)rrrrrrrrrreturn_ok_domainhs    # z$DefaultCookiePolicy.return_ok_domaincCst|\}}|jds.d|}n|jdsJd|}n|j|pe|j|sldS|j|rtd|dS|j|rtd|dSdS)NrFz" domain %s is in user block-listz& domain %s is not in user allow-listT)rrurrrr)rrrrrrrrr~s    z$DefaultCookiePolicy.domain_return_okcCs@td|t|}|j|s<td||dSdS)Nz- checking cookie path=%sz %s does not path-match %sFT)rrru)rrrrrrrrs   z"DefaultCookiePolicy.path_return_ok) rrrrrr!rZ DomainLiberalZ DomainStrictrrrrrrrrrrrrrrrrrrrr r"rrrrrrrLsL           ;       cCs"t|j}t|j|S)N)sortedkeysmapr)Zadictr$rrrvals_sorted_by_keysr&c csjt|}xW|D]O}d}y |jWntk r>YnXd}t|DdH|s|VqqWdS)zBIterates over nested mapping, depth-first, in sorted order by key.FTN)r&itemsAttributeError deepvalues)mappingvaluesobjrrrr)s    r)c@seZdZdS)AbsentN)rrrrrrrr-s r-c@s~eZdZdZejdZejdZejdZejdZ ejdZ ejdej Z dd d Z d d Zd dZddZddZddZddZddZddZddZddZdd Zd!d"Zd#d$Zdddd%d&Zd'd(Zd)d*Zd+d,Zd-d.Zd/d0Z d1d2Z!dS)3rzCollection of HTTP cookies. You may not need to know about this class: try urllib.request.build_opener(HTTPCookieProcessor).open(url). z\Wz([\"\\])z\.?[^.]*z[^.]*z^\.+z^\#LWP-Cookies-(\d+\.\d+)NcCs=|dkrt}n||_tj|_i|_dS)N)r_policy _threadingRLock _cookies_lock_cookies)rpolicyrrrrs    zCookieJar.__init__cCs ||_dS)N)r.)rr3rrr set_policyszCookieJar.set_policycCsg}|jj||sgStd||j|}x|jD]~}|jj||sgqFn||}xP|jD]B}|jj||stdq~ntd|j|q~WqFW|S)Nz!Checking %s for cookies to returnz not returning cookiez it's a match) r.rrr2r$rr+rrt)rrrcookiesZcookies_by_pathrZcookies_by_namerrrr_cookies_for_domains      zCookieJar._cookies_for_domaincCs=g}x0|jjD]}|j|j||qW|S)z2Return a list of cookies to be returned to server.)r2r$extendr6)rrr5rrrr_cookies_for_requestszCookieJar._cookies_for_requestc Cs|jdddddd}g}x|D]}|j}|smd}|dkrm|jd|qmn|jd k r|jj|jr|dkr|jjd |j}n |j}|jd kr|j|jn|jd |j|f|dkr/|j r)|jd |j n|j j d r|j }|j rp|j d rp|dd }n|jd|n|jd k rd}|jr|d|j}n|j|qq/q/W|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). keycSs t|jS)N)rr)arrrsz)CookieJar._cookie_attrs..reverseTFr z $Version=%sNz\\\1z%s=%sz $Path="%s"rrz $Domain="%s"z$Portz="%s")sortrrtr{ non_word_rer@quote_rer]rzrrrrurrr) rr5rattrsrrr{rrrrr _cookie_attrss>    !      zCookieJar._cookie_attrsc Cs td|jjzttj|j_|_|j|}|j|}|r|j ds|j ddj |qn|jj r|jj r|j d rx4|D])}|jdkr|j ddPqqWnWd|jjX|jdS)zAdd correct Cookie: header to request (urllib.request.Request object). The Cookie2 header is also added unless policy.hide_cookie2 is true. add_cookie_headerrz; ZCookie2rz $Version="1"N)rr1acquirerArMr.rr8rAZ has_headerZadd_unredirected_headerr~rrrreleaseclear_expired_cookies)rrr5r@rrrrrB#s$   zCookieJar.add_cookie_headercCs$g}d}d}x |D]}|d \}}d }d } i} i} x|d dD]\} } | j}||ks||kr|} n| |kr| dkrd} n| | krqXn| dkr| dkrtdd} Pn| j} n| dkr6|rqXn| dkr6tdqXq6n| dkrd}yt| } Wn#tk r}tdd} PYnXd} |j| } n| |ks| |kr| dkr| dkrtd| d} Pn| | | .no_matching_rfc2965)r Zget_allr.rrrUr| ExceptionrrrVrrrzfilterr7) rresponserrZ rfc2965_hdrsZns_hdrsrrr5Z ns_cookiesrWrrXrrr make_cookiessB           zCookieJar.make_cookiesc Csj|jjzHttj|j_|_|jj||rT|j|nWd|jjXdS)z-Set a cookie if policy says it's OK to do so.N) r1rCrArMr.rr set_cookierD)rrrrrrset_cookie_if_okLs  zCookieJar.set_cookie_if_okc Cs|j}|jjzi|j|kr8i||jz, )rtr __class__rr~)rrrrrrrs zCookieJar.__repr__cCsGg}x!|D]}|jt|q Wd|jjdj|fS)Nz<%s[%s]>z, )rtrmrdrr~)rrerrrrrs zCookieJar.__str__)"rrrrrvcompiler>r?Zstrict_domain_reZ domain_reZdots_reASCIImagic_rerr4r6r8rArBrOrTrUrVr\r^r]r`rQrarErbrcrrrrrrrs8     ; ! a \ 6      c@seZdZdS)rN)rrrrrrrrs c@sjeZdZdZdddddZdddddZddddd Zdddd d ZdS) rz6CookieJar that can be loaded from and saved to a file.NFc Cs^tj|||dk rBy |dWqBtdYqBXn||_t||_dS)z} Cookies are NOT loaded from the named file until either the .load() or .revert() method is called. NrVzfilename must be string-like)rrrLfilenamerP delayload)rrirjr3rrrrs   zFileCookieJar.__init__cCs tdS)zSave cookies to a file.N)r)rriignore_discardignore_expiresrrrsaveszFileCookieJar.savec Cse|dkr6|jdk r'|j}q6ttnt|}|j||||WdQXdS)zLoad cookies from a file.N)rirLMISSING_FILENAME_TEXTopen _really_load)rrirkrlrrrrloads   zFileCookieJar.loadcCs|dkr6|jdk r'|j}q6ttn|jjzWtj|j}i|_y|j|||Wnt k r||_YnXWd|jj XdS)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) rirLrnr1rCrdeepcopyr2rqOSErrorrD)rrirkrlZ old_staterrrreverts       zFileCookieJar.revert)rrrrrrmrqrtrrrrrs  cCs|j|jfd|jfd|jfg}|jdk rU|jd|jfn|jrn|jdn|jr|jdn|jr|jdn|j r|jdn|j r|jd t t |j fn|j r|jdn|jr"|jd |jfn|jrD|jd |jfnt|jj}x.|D]&}|j|t|j|fq`W|jd 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_spec port_spec domain_dotrrrrrGr)ruN)rvN)rwN)zsecureN)zdiscardN)rzr{rrrrtrrrrrr5rZrrrr#rr$rmrr)rrr$rrrrlwp_cookie_strs6          $rxc@sIeZdZdZddddZdddddZd d ZdS) raZ The LWPCookieJar saves a sequence of "Set-Cookie3" lines. "Set-Cookie3" is the format used by the libwww-perl libary, 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) TcCstj}g}xW|D]O}| r5|jr5qn| rQ|j|rQqn|jdt|qWdj|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 rV)rMrrrtrxr~)rrkrlrrerrrr as_lwp_str+s  zLWPCookieJar.as_lwp_strNFc Csx|dkr6|jdk r'|j}q6ttnt|d-}|jd|j|j||WdQXdS)Nwz#LWP-Cookies-2.0 )rirLrnrowriterz)rrirkrlrrrrrm;s   zLWPCookieJar.savecCs|j}|jj|s7d|}t|ntj}d}d} d} ywxp|j} | dkrwPn| j|sq[n| t|dj} xt| gD] } | d\} }i}i}x| D]}d||rfrgr?rFrSrXIr\Xr^rarbrdrjrorprrrqr|r}rrrrrrrrrrrrrrrrrrrrrrr&r)r-rrsrrrxrrrrrrs           "  5  8 !  U   4  '      # b!R  : x