Ñò ìÿÒXc @szddkZddkZddkZddkZddklZddklZddk l Z de fd„ƒYZ dS(iÿÿÿÿN(turlparse(ttimegm(tlog_mgrtCookiecBsºeZdZdefd„ƒYZdefd„ƒYZeidƒZeidei ƒZ hdd6dd6d d 6d d 6d d 6d d 6Z e d„ƒZ e d#d„ƒZe d„ƒZe d„ƒZe d#d„ƒZe d#d„ƒZd#d#d#d#d#d#d#d„Zed„ƒZeid„ƒZed„ƒZeid„ƒZed„ƒZeid„ƒZd„Zd„Zd„Zd„Zd„Zd „Zd!„Z d"„Z!RS($só A Cookie object has the following attributes: key The name of the cookie value The value of the cookie A Cookie also supports these predefined optional attributes. If an optional attribute is not set on the cookie it's value is None. domain Restrict cookie usage to this domain path Restrict cookie usage to this path or below expires Cookie is invalid after this UTC timestamp max_age Cookie is invalid this many seconds in the future. Has precedence over the expires attribute. secure Cookie should only be returned on secure (i.e. SSL/TLS) connections. httponly Cookie is intended only for HTTP communication, it can never be utilized in any other context (e.g. browser Javascript). See the documentation of get_expiration() for an explanation of how the expires and max-age attributes interact as well as the role of the timestamp attribute. Expiration values are stored as datetime objects for easy manipulation and comparision. There are two ways to instantiate a Cookie object. Either directly via the constructor or by calling the class function parse() which returns a list of Cookie objects found in a string. To create a cookie to sent to a client: Example: cookie = Cookie('session', session_id, domain=my_domain, path=mypath, httpOnly=True, secure=True, expires=expiration) headers.append(('Set-Cookie', str(cookie))) To receive cookies from a request: Example: cookies = Cookie.parse(response.getheader('Set-Cookie'), request_url) tExpiredcBseZRS((t__name__t __module__(((s4/usr/lib/python2.6/site-packages/ipapython/cookie.pyRst URLMismatchcBseZRS((RR(((s4/usr/lib/python2.6/site-packages/ipapython/cookie.pyR„ss;\s*s@^\s*([a-zA-Z0-9\!\#\$\%\&\'\*\+\-\.\^\_\`\|\~]+)\s*=\s*(.*?)\s*$tdomaintpathtmax_agesmax-agetexpirestsecurethttponlycCst|iƒƒS(sg Timestamps (timestamp & expires) are stored as datetime objects in UTC. It's non-obvious how to convert a naive UTC datetime into a unix time value (seconds since the epoch UTC). That functionality is oddly missing from the datetime and time modules. This utility provides that missing functionality. (Rt utctimetuple(tclstdt((s4/usr/lib/python2.6/site-packages/ipapython/cookie.pytdatetime_to_time–s cCs[|iƒ}|dj o#|iƒdjotdƒ‚ntii|i|ƒdtƒS(sO Given a datetime object in UTC generate RFC 1123 date string. gstimezone is not UTCtusegmtN( t utcoffsettNonet total_secondst ValueErrortemailtutilst formatdateRtTrue(RRR((s4/usr/lib/python2.6/site-packages/ipapython/cookie.pytdatetime_to_string£s  cCsd|iƒ}y&titii|ƒdd!Œ}Wn+tj o}td||fƒ‚nX|S(s_ Parse a RFC 822, RFC 1123 date string, return a datetime naive object in UTC. iis)unable to parse expires datetime '%s': %s(tstriptdatetimeRRt parsedatet ExceptionR(RtsRte((s4/usr/lib/python2.6/site-packages/ipapython/cookie.pytparse_datetimeµs  &cCsY|iƒ}|pdS|idƒpdS|idƒdjodS||idƒ S(s+ Given a URL path, possibly empty, return a path consisting only of directory components. The URL path must end with a trailing slash for the last path element to be considered a directory. Also the URL path must begin with a slash. Empty input returns '/'. Examples: '' -> '/' '/' -> '/' 'foo' -> '/' 'foo/' -> '/' '/foo -> '/' '/foo/' -> '/foo' '/foo/bar' -> '/foo' '/foo/bar/' -> '/foo/bar' t/i(tlowert startswithtcounttrindex(Rturl_path((s4/usr/lib/python2.6/site-packages/ipapython/cookie.pytnormalize_url_pathÇs c CsØg}tii|ƒ}d}x}|D]u}tii|ƒ}|oo|idƒ}|idƒ} | ddjo6| ddjo| dd!} q©td| ƒ‚nt} n|}t} t } |i ƒti j} |djo"| o| ot|| ƒ}q%|oM| oF| o>|dj o|i |ƒn|i |ƒt|| ƒ}q%|o| o|i|| ƒq%|otd|ƒ‚q%q%W|o/|dj o|i |ƒn|i |ƒn|S( sí Given a string containing one or more cookies (the HTTP_COOKIES environment variable typically contains multiple cookies) parse the string and return a list of Cookie objects found in the string. iiit"iÿÿÿÿsunterminated quote in '%s'sunknown cookie token '%s'N(Rtfield_retsplitRt kv_pair_retsearchtgroupRRtFalseR$tattrst normalizetappendt_Cookie__set_attr( Rt cookie_stringt request_urltcookiestfieldstcookietfieldtmatchtkeytvaluetkv_pairt is_attribute((s4/usr/lib/python2.6/site-packages/ipapython/cookie.pytparseésD     cCsbd}|i|ƒ}x(|D] }|i|jo |}qqW|dj o|i|ƒn|S(s$ A cookie string may contain multiple cookies, parse the cookie string and return the last cookie in the string matching the cookie name or None if not found. This is basically a utility wrapper around the parse() class method which iterates over what parse() returns looking for the specific cookie. When cookie_name appears more than once the last instance is returned rather than the first because the ordering sequence makes the last instance the current value. N(RR@R<R2(RR5t cookie_nameR6t target_cookieR7R9((s4/usr/lib/python2.6/site-packages/ipapython/cookie.pytget_named_cookie_from_stringEs c Cseti|tƒ||_||_||_||_||_||_||_ ||_ | |_ dS(N( Rt get_loggerRR<R=RR R R R R t timestamp( tselfR<R=RR R R R R RE((s4/usr/lib/python2.6/site-packages/ipapython/cookie.pyt__init__as        cCs|iS(sŸ The UTC moment at which cookie was received for purposes of computing the expiration given a Max-Age offset. The expiration will be timestamp + max_age. The timestamp value will aways be a datetime object. By default the timestamp will be the moment the Cookie object is created as this often corresponds to the moment the cookie is received (the intent of the Max-Age attribute). But becuase it's sometimes desirable to force a specific moment for purposes of computing the expiration from the Max-Age the Cookie timestamp can be updated. Setting a value of None causes the timestamp to be set to the current UTC time (now). You may also assign with a numeric UNIX timestamp (seconds since the epoch UTC) or a formatted time sting, in all cases the value will be converted to a datetime object. (t _timestamp(RF((s4/usr/lib/python2.6/site-packages/ipapython/cookie.pyREpscCs¬|djo d|_nt|tiƒo ||_not|tttfƒotii|ƒ|_n=t|tƒot i |ƒ|_nt d|i i ƒ‚dS(NsDvalue must be datetime, int, long, float, basestring or None, not %s(RRHt isinstanceRtinttlongtfloattutcfromtimestampt basestringRR"t TypeErrort __class__R(RFR=((s4/usr/lib/python2.6/site-packages/ipapython/cookie.pyRE‡s   cCs|iS(sH The expiration timestamp (in UTC) as a datetime object for the cookie, or None if not set. You may assign a value of None, a datetime object, a numeric UNIX timestamp (seconds since the epoch UTC) or formatted time string (the latter two will be converted to a datetime object. (t_expires(RF((s4/usr/lib/python2.6/site-packages/ipapython/cookie.pyR •s cCs¬|djo d|_nt|tiƒo ||_not|tttfƒotii|ƒ|_n=t|tƒot i |ƒ|_nt d|i i ƒ‚dS(NsDvalue must be datetime, int, long, float, basestring or None, not %s(RRQRIRRJRKRLRMRNRR"RORPR(RFR=((s4/usr/lib/python2.6/site-packages/ipapython/cookie.pyR ¡s   cCs|iS(sq The lifetime duration of the cookie. Computed as an offset from the cookie's timestamp. (t_max_age(RF((s4/usr/lib/python2.6/site-packages/ipapython/cookie.pyR ¯scCsW|djo d|_n:yt|ƒ|_Wn#tj otd|ƒ‚nXdS(Ns-Max-Age value '%s' not convertable to integer(RRRRJRR(RFR=((s4/usr/lib/python2.6/site-packages/ipapython/cookie.pyR ·s   cCsPtii|iƒdƒ}|djotd|ƒ‚nt|||ƒdS(s? Sets one of the predefined cookie attributes. sunknown cookie attribute '%s'N(RR1tgetR$RRtsetattr(RFtnameR=t attr_name((s4/usr/lib/python2.6/site-packages/ipapython/cookie.pyt __set_attrÁs cCsg}|id|i|ifƒ|idj o|id|iƒn|idj o|id|iƒn|idj o|id|iƒn|idj o!|idti |iƒƒn|i o|idƒn|i o|idƒndi |ƒS( Ns%s=%ss Domain=%ssPath=%ss Max-Age=%ss Expires=%stSecuretHttpOnlys; ( R3R<R=RRR R R RRR R tjoin(RFt components((s4/usr/lib/python2.6/site-packages/ipapython/cookie.pyt__str__Ês!  cCsG|idj o|itid|iƒS|idj o|iSdS(s/ Return the effective expiration of the cookie as a datetime object or None if no expiration is defined. Expiration may be defined either by the "Expires" timestamp attribute or the "Max-Age" duration attribute. If both are set "Max-Age" takes precedence. If neither is set the cookie has no expiration and None will be returned. "Max-Age" specifies the number of seconds in the future from when the cookie is received until it expires. Effectively it means adding "Max-Age" seconds to a timestamp to arrive at an expiration. By default the timestamp used to mark the arrival of the cookie is set to the moment the cookie object is created. However sometimes it is desirable to adjust the received timestamp to something other than the moment of object creation, therefore you can explicitly set the arrival timestamp used in the "Max-Age" calculation. "Expires" specifies an explicit timestamp. If "Max-Age" is set a datetime object is returned which is the sum of the arrival timestamp and "Max-Age". If "Expires" is set a datetime object is returned matching the timestamp specified as the "Expires" value. If neither is set None is returned. tsecondsN(R RRERt timedeltaR (RF((s4/usr/lib/python2.6/site-packages/ipapython/cookie.pytget_expirationãs cCs|iƒ|_d|_|iS(s An expiration may be specified either with an explicit timestamp in the "Expires" attribute or via an offset specified witht the "Max-Age" attribute. The "Max-Age" attribute has precedence over "Expires" if both are specified. This method normalizes the expiration of the cookie such that only a "Expires" attribute remains after consideration of the "Max-Age" attribute. This is useful when storing the cookie for future reference. N(R_R RR (RF((s4/usr/lib/python2.6/site-packages/ipapython/cookie.pytnormalize_expiration s cCsnti|ƒ\}}}}}}|idjo|iƒ|_n|idjo|i|ƒ|_ndS(s¥ If cookie domain and path attributes are not specified then they assume defaults from the request url the cookie was received from. N(RRRR$R R)(RFturltschemeRR tparamstquerytfragment((s4/usr/lib/python2.6/site-packages/ipapython/cookie.pytset_defaults_from_urls !cCs|i|ƒ|iƒdS(s‘ Missing cookie attributes will receive default values derived from the request URL. The expiration value is normalized. N(RfR`(RFRa((s4/usr/lib/python2.6/site-packages/ipapython/cookie.pyR2+s cCsd|i|ifS(s‘ Return a string with just the key and value (no attributes). This is appropriate for including in a HTTP Cookie header. s%s=%s;(R<R=(RF((s4/usr/lib/python2.6/site-packages/ipapython/cookie.pyt http_cookie4sc sd„}‡fd†}ˆi}ti|ƒ\}}}}} } ˆiƒ} | d j oFtiiƒ} | | jo&tid|ˆi| ƒfƒ‚q¡n||ˆi ƒp#ti d|ˆi |fƒ‚n||ˆi ƒp#ti d|ˆi |fƒ‚n|i ƒ}ˆi o.|d joti d||fƒ‚qQnˆio.|d joti d ||fƒ‚q‰ntS( s¬ Tests to see if a cookie should be returned when a request is sent to a specific URL. * The request url's host must match the cookie's doman otherwise raises Cookie.URLMismatch. * The path in the request url must contain the cookie's path otherwise raises Cookie.URLMismatch. * If the cookie defines an expiration date then the current time must be less or equal to the cookie's expiration timestamp. Will raise Cookie.Expired if a defined expiration is not valid. If the test fails Cookie.Expired or Cookie.URLMismatch will be raised, otherwise True is returned. cSs ddkl}y||ƒWntj o }tSX|djotS|iƒ}|iƒ}||jotS|i|ƒo|idƒotSntS(s~ Compute domain component and perform test per RFC 6265, Section 5.1.3. "Domain Matching" iÿÿÿÿ(tvalidate_domain_namet.N( t ipalib.utilRhRR0RRR$tendswithR%(t url_domaint cookie_domainRhR!((s4/usr/lib/python2.6/site-packages/ipapython/cookie.pyt domain_validPs     cs˜|djotS|iƒ}ˆi|ƒ}||jotS|oN|i|ƒo>|idƒotS|t|ƒ}|idƒotSntS(s Compute path component and perform test per RFC 6265, Section 5.1.4. "Paths and Path-Match" R#N(RRR$R)R%RktlenR0(R(t cookie_patht request_pathttail(RF(s4/usr/lib/python2.6/site-packages/ipapython/cookie.pyt path_validms    s!cookie named '%s'; expired at %s'sBcookie named '%s'; it's domain '%s' does not match URL domain '%s'sDcookie named '%s'; it's path '%s' does not contain the URL path '%s'thttpthttpssDcookie named '%s'; is restricted to HTTP but it's URL scheme is '%s'sPcookie named '%s'; is restricted to secure transport but it's URL scheme is '%s'N(RtRu(Ru(R<RR_RRtutcnowRRRRRR R$R R R( RFRaRnRsRAt url_schemeRlR(t url_paramst url_queryt url_fragmenttcookie_expirationtnow((RFs4/usr/lib/python2.6/site-packages/ipapython/cookie.pythttp_return_ok;s6  !             N("RRt__doc__RRRtretcompileR+t IGNORECASER-R1t classmethodRRRR"R)R@RCRGtpropertyREtsetterR R R4R\R_R`RfR2RgR}(((s4/usr/lib/python2.6/site-packages/ipapython/cookie.pyRIsH6   "[    &   ( RttimeRt email.utilsRturllib2RtcalendarRtipapython.ipa_log_managerRtobjectR(((s4/usr/lib/python2.6/site-packages/ipapython/cookie.pyts    /