mò mã¸Ec@s dZdkZdkZdkZdkZdklZdklZeefZdk l Z l Z l Z e d„Zd„ZeidƒZeidƒZeid ƒZeid ƒZd „Zeid ƒZd „Zd„Zd„Zedjo eƒndS(s4Utility functions for HTTP header value parsing and construction. Copyright 1997-1998, Gisle Aas Copyright 2002-2006, John J. Lee 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). N(s StringType(s UnicodeType(s startswithsendswiths http2timecCs¨|pUtiiti|ƒdƒd}ddg}|o|dg7}n||jSnt |ƒddd}dg}|o|dd d d g7}n||jS( sM ct_headers: Sequence of Content-Type headers url: Response URL iis.htms.htmls.xhtmlis text/htmls text/xhtmlstext/xmlsapplication/xmlsapplication/xhtml+xmlN( t ct_headerstostpathtsplitextturlparseturltextt html_extst allow_xhtmltsplit_header_wordstctt html_types(RRRRRR R ((t5/data/zmath/zope/lib/python/mechanize/_headersutil.pytis_htmls#  cCs+|idƒ\}}|i| |i|S(s)Return unmatched part of re.Match object.iN(tmatchtspantstarttendtstring(RRR((R t unmatched*ss^\s*([^=\s;,]+)s&^\s*=\s*\"([^\"\\]*(?:\\.[^\"\\]*)*)\"s^\s*=\s*([^\s;,]*)s\\(.)c CsÔt|ƒtjpt‚g}x­|D]¥}|}g}xx|opti |ƒ}|oÃt |ƒ}|i dƒ}ti |ƒ}|o1t |ƒ}|i dƒ}tid|ƒ}nKti |ƒ}|o.t |ƒ}|i dƒ}ti|ƒ}nd}|i||fƒq<tti|ƒdƒo5ti|ƒd}|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;]*tis&split_header_words bug: '%s', '%s', %sN(ttypet header_valuest STRING_TYPEStAssertionErrortresultttextt orig_texttpairsttoken_retsearchtmRtgrouptnametquoted_value_retvaluet escape_retsubtvalue_reRtrstriptNonetappendt startswithtlstriptretsubntnon_junkt nr_junk_chars( RRR"RR RRR0R$R/((R R 3sH,    $ s([\"\\])cCsàg}xÇ|D]¿}g}xŒ|D]„\}}|dj o^tid|ƒp t i d|ƒ}d|}n|djo |}q—d||f}n|i |ƒq W|o|i t i |dƒƒq q Wt i |dƒS(s²Do the inverse 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(theaderstlistsRtattrtktvR)R-Rtjoin_escape_reR&R*Rtjoin(R2RR3R4R1R5((R tjoin_header_words‰s$     !c Cs»d} g}x¨|D] }g}t}tid|ƒ}xIt t |ƒƒD]5}||}ti|ƒ}|djoqJnd |jo|d} } n+tid |d ƒ\} } ti| ƒ} |d jošti| ƒ}|| jo |} n| d jo t}n| djoLt| dƒo| d } nt| dƒo| d } nt| ƒ} qln|i| | fƒqJW|o)|p|idƒn|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. texpirestdomainRtsecuretportsmax-ages;\s*Rt=s\s*=\s*iitversiont"iÿÿÿÿt0N(R9R:spathR;R<smax-age(sversionR@(t known_attrsRt ns_headerst ns_headerRtFalset version_setR-tsplittparamstrangetlentiitparamRR(R)R4R5R,tlowertlctTrueR+tendswitht http2timeR*( RBRJRRCRERMRKRGRRAR4R5((R tparse_ns_headers¥sH         cCsdk}dk}|i|ƒS(N(tdoctestt _headersutilttestmod(RSRR((R t_testÜst__main__(t__doc__RR-RRttypest StringTypet UnicodeTypeRt_utilR+RORPRDR RtcompileRR#R'R%R R6R8RQRUt__name__(RUR%ROR#RYR RR-RR+RR R6RR'R8RPRRZRQR((R t? s$$      U  7