PNG  IHDR;IDATxܻn0K )(pA 7LeG{ §㻢|ذaÆ 6lذaÆ 6lذaÆ 6lom$^yذag5bÆ 6lذaÆ 6lذa{ 6lذaÆ `}HFkm,mӪôô! x|'ܢ˟;E:9&ᶒ}{v]n&6 h_tڠ͵-ҫZ;Z$.Pkž)!o>}leQfJTu іچ\X=8Rن4`Vwl>nG^is"ms$ui?wbs[m6K4O.4%/bC%t Mז -lG6mrz2s%9s@-k9=)kB5\+͂Zsٲ Rn~GRC wIcIn7jJhۛNCS|j08yiHKֶۛkɈ+;SzL/F*\Ԕ#"5m2[S=gnaPeғL lذaÆ 6l^ḵaÆ 6lذaÆ 6lذa; _ذaÆ 6lذaÆ 6lذaÆ RIENDB`  oY|@s dZddlZddlZddlmZddlmZddlmZddl m Z ddl m Z ddl m Z ed Zeed BZed ZeeBZeed Zeed ZeedBed ZeeBZeedBZeeBZeedZddZGdddZGdddeZGdddeZGdddeZGdddeZ GdddeZ!Gdd d eZ"Gd!d"d"eZ#Gd#d$d$eZ$Gd%d&d&eZ%Gd'd(d(eZ&Gd)d*d*e&Z'Gd+d,d,eZ(Gd-d.d.eZ)Gd/d0d0eZ*Gd1d2d2eZ+Gd3d4d4eZ,Gd5d6d6eZ-Gd7d8d8eZ.Gd9d:d:eZ/Gd;d<d<eZ0Gd=d>d>eZ1Gd?d@d@eZ2GdAdBdBeZ3GdCdDdDeZ4GdEdFdFeZ5GdGdHdHeZ6GdIdJdJeZ7GdKdLdLe Z8GdMdNdNeZ9GdOdPdPeZ:GdQdRdReZ;GdSdTdTeZ<GdUdVdVe<Z=GdWdXdXeZ>GdYdZdZeZ?Gd[d\d\eZ@Gd]d^d^eZAGd_d`d`eZBGdadbdbeBZCGdcddddeBZDGdedfdfeZEGdgdhdheZFGdidjdjeZGGdkdldleHZIGdmdndneIZJGdodpdpeIZKGdqdrdreJZLeKd dsZMeKdtduZNeKdvdwZOejPdxjQdyjRejSZTejPdzjQdyjRejUd{d|jUd}d~jVZWejPdjXZYejPdzjQdyjRejUd{d|jUd}d~jVZZejPdzjQdyjRejUd{d|jUd}d~jVZ[ejPdzjQdyjRejUd{d|jUd}d~jVZ\ddZ]ddZ^ddZ_ddZ`ddZaddZbddZcddZdddZeddZfddZgddZhddZiddZjddZkddZlddZmddZnddZoddZpddZqddZrddZsddZtddZuddZvddZwddZxddZyddZzddZ{ddZ|ddZ}ddZ~ddZddZddZddZddZddZddZddZddZddZddZddZddZddZddZddZddZdS)alHeader value parser implementing various email-related RFC parsing rules. The parsing methods defined in this module implement various email related parsing rules. Principal among them is RFC 5322, which is the followon to RFC 2822 and primarily a clarification of the former. It also implements RFC 2047 encoded word decoding. RFC 5322 goes to considerable trouble to maintain backward compatibility with RFC 822 in the parse phase, while cleaning up the structure on the generation phase. This parser supports correct RFC 5322 generation by tagging white space as folding white space only when folding is allowed in the non-obsolete rule sets. Actually, the parser is even more generous when accepting input than RFC 5322 mandates, following the spirit of Postel's Law, which RFC 5322 encourages. Where possible deviations from the standard are annotated on the 'defects' attribute of tokens that deviate. The general structure of the parser follows RFC 5322, and uses its terminology where there is a direct correspondence. Where the implementation requires a somewhat different structure than that used by the formal grammar, new terms that mimic the closest existing terms are used. Thus, it really helps to have a copy of RFC 5322 handy when studying this code. Input to the parser is a string that has already been unfolded according to RFC 5322 rules. According to the RFC this unfolding is the very first step, and this parser leaves the unfolding step to a higher level message parser, which will have already detected the line breaks that need unfolding while determining the beginning and end of each header. The output of the parser is a TokenList object, which is a list subclass. A TokenList is a recursive data structure. The terminal nodes of the structure are Terminal objects, which are subclasses of str. These do not correspond directly to terminal objects in the formal grammar, but are instead more practical higher level combinations of true terminals. All TokenList and Terminal objects have a 'value' attribute, which produces the semantically meaningful value of that part of the parse subtree. The value of all whitespace tokens (no matter how many sub-tokens they may contain) is a single space, as per the RFC rules. This includes 'CFWS', which is herein included in the general class of whitespace tokens. There is one exception to the rule that whitespace tokens are collapsed into single spaces in values: in the value of a 'bare-quoted-string' (a quoted-string with no leading or trailing whitespace), any whitespace that appeared between the quotation marks is preserved in the returned value. Note that in all Terminal strings quoted pairs are turned into their unquoted values. All TokenList and Terminal objects also have a string value, which attempts to be a "canonical" representation of the RFC-compliant form of the substring that produced the parsed subtree, including minimal use of quoted pair quoting. Whitespace runs are not collapsed. Comment tokens also have a 'content' attribute providing the string found between the parens (including any nested comments) with whitespace preserved. All TokenList and Terminal objects have a 'defects' attribute which is a possibly empty list all of the defects found while creating the token. Defects may appear on any token in the tree, and a composite list of all defects in the subtree is available through the 'all_defects' attribute of any node. (For Terminal notes x.defects == x.all_defects.) Each object in a parse tree is called a 'token', and each has a 'token_type' attribute that gives the name from the RFC 5322 grammar that it represents. Not all RFC 5322 nodes are produced, and there is one non-RFC 5322 node that may be produced: 'ptext'. A 'ptext' is a string of printable ascii characters. It is returned in place of lists of (ctext/quoted-pair) and (qtext/quoted-pair). XXX: provide complete list of token types. N) hexdigits) OrderedDict) itemgetter)_encoded_words)errors)utilsz (z ()<>@,:;.\"[].z."(z/?=z*'%%cCs*dt|jddjdddS)N"\z\\z\")strreplace)valuer3/opt/python35/lib/python3.5/_header_value_parser.py quote_string`src@s[eZdZddZddZddZddZd d Zd d d Zd S)_FoldedcCsC||_||_d|_d|_d|_g|_g|_dS)NrT)maxlenpolicylastlen stickyspace firstlinedonecurrent)selfrrrrr__init__is      z_Folded.__init__cCsC|jj|j|jj|jj|jjd|_dS)Nr)rextendrappendrlinesepclearr)rrrrnewliners z_Folded.newlinecCs|jr|jdS)N)rr!)rrrrfinalizexs z_Folded.finalizecCsdj|jS)N)joinr)rrrr__str__|sz_Folded.__str__cCs|jj|dS)N)rr)rstokenrrrrsz_Folded.appendNcCs|dkrt|}t|}|jdk r+t|j}|j|||jkr|jj|j|j|7_|jj||j|7_d|_d|_dS|jr |j }|dk r|jt|7_|t|7}|j |dS|r|d|jkr|j|}d|koG|knr||}|jj|jd||j|d|_|}|j |jj|j|jj||||_d|_d|_dS|js|j |jj|j|jj|d|_d|_dS|j||jkrd|jj||j|7_dS||jkr|j |jj|||_dSdS)NFTr) r lenrrrrrrhas_fwspop_leading_fws_foldr!)rtokenr&lZstickyspace_lenwsmarginZtrimrrrappend_if_fitssf                     z_Folded.append_if_fits) __name__ __module__ __qualname__rr!r"r%rr0rrrrrgs     rcs-eZdZdZfddZddZfddZedd Zed d Z ed d Z ddZ ddZ ddZ eddZddZeddZddZddZddZd d!Zd"d#d$Zd"d%d&Zd"d'd(ZS)) TokenListNcs tj||g|_dS)N)superrdefects)rargskw) __class__rrrszTokenList.__init__cCsdjdd|DS)Nr#css|]}t|VqdS)N)r ).0xrrr sz$TokenList.__str__..)r$)rrrrr%szTokenList.__str__csdj|jjtjS)Nz{}({}))formatr9r1r5__repr__)r)r9rrr>szTokenList.__repr__cCsdjdd|DS)Nr#css!|]}|jr|jVqdS)N)r)r:r;rrrr<sz"TokenList.value..)r$)rrrrrszTokenList.valuecCstdd|D|jS)Ncss|]}|jVqdS)N) all_defects)r:r;rrrr<sz(TokenList.all_defects..)sumr6)rrrrr?szTokenList.all_defectsccs|j}g}x|D]|}|jr_|r_t|dkrJ|dn ||V|j|j}|j||r||V|g}qW|rt|dkr|dn ||VdS)Nr'r)r9startswith_fwsr(r pop_trailing_wsr)rklassthisr,Zend_wsrrrpartss   '     zTokenList.partscCs|djS)Nr)rA)rrrrrA szTokenList.startswith_fwscCs.|djdkr |jdS|djS)Nrfws) token_typepopr*)rrrrr* s zTokenList.pop_leading_fwscCs.|djdkr |jdS|djS)Nr'cfwsrJrJ)rGrHrB)rrrrrBs zTokenList.pop_trailing_wscCs"x|D]}|jrdSqWdS)NTF)r))rpartrrrr)s  zTokenList.has_fwscCs|djS)Nr)has_leading_comment)rrrrrLszTokenList.has_leading_commentcCs+g}x|D]}|j|jq W|S)N)rcomments)rrMr,rrrrM!s zTokenList.commentscCsE|jptd}t||}|j||jt|S)Nz+inf)Zmax_line_lengthfloatrr+r"r )rrrfoldedrrrfold(s   zTokenList.foldcCsg}|j}|r%|j||djdkrG|jdnd}|jtjt|||j|dj|S)Nr'rFr#rJrJ)r*rrGrH_ewencoder r$)rcharsetresr.Ztrailerrrras_encoded_word0s  ( zTokenList.as_encoded_wordcCs=g}x'|D]}|j|j||q Wdj|S)Nr#)r cte_encoder$)rrSrrTrKrrrrV<s zTokenList.cte_encodec Cs>|jjrdnd}x|jD]}t|}t|}yt|j|Wn^tk rtdd|jDrd}nd}|j ||j}t|}YnX|j ||rq"|j }|dk rt||_ |j |rq"|j r|j|q"|j||jq"WdS)Nzutf-8asciicss!|]}t|tjVqdS)N) isinstancerUndecodableBytesDefect)r:r;rrrr<Jsz"TokenList._fold..z unknown-8bit)rutf8rEr r(rRUnicodeEncodeErroranyr?rVr0r*rr)r+rr!)rrOencodingrKtstrtlenrSr.rrrr+Bs2          zTokenList._foldr#cCs#tdj|jdddS)N indentr#)printr$_pp)rrarrrpprintcszTokenList.pprintcCsdj|jddS)Nr`rar#)r$rc)rrarrrppstrfszTokenList.ppstrccsdj||jj|jVxH|D]@}t|dsN|dj|Vq$|j|dEdHq$W|jrdj|j}nd}dj||VdS)Nz{}{}/{}(rcz* !! invalid element in token list: {!r}z z Defects: {}r#z{}){})r=r9r1rGhasattrrcr6)rrar,extrarrrrcis      z TokenList._pp)r1r2r3rGrr%r>propertyrr?rErAr*rBr)rLrMrPrUrVr+rdrercrr)r9rr4s(  +       !r4c@s4eZdZeddZeddZdS)WhiteSpaceTokenListcCsdS)N r)rrrrr}szWhiteSpaceTokenList.valuecCsdd|DS)NcSs(g|]}|jdkr|jqS)comment)rGcontent)r:r;rrr s z0WhiteSpaceTokenList.comments..r)rrrrrMszWhiteSpaceTokenList.commentsN)r1r2r3rhrrMrrrrri{s ric@s.eZdZdZddZddZdS)UnstructuredTokenList unstructuredc Cs)d}|jjrdnd}x|jD]}t|}d}yt|j|Wntk rgtdd|jDrd}nd}|dk rNtdj |j |d|gj |}t dd|j d|D} t|} t | } | | |jkrN|j |d=|j| | | |_w(|j |}d }YnX|j||r|r(t |j d }q(|s|r|j|q(|j} | dk rt| |_|j|rq(|jr|j|q(|j||jd}q(WdS) Nzutf-8rWFcss!|]}t|tjVqdS)N)rXrrY)r:r;rrrr<sz.UnstructuredTokenList._fold..z unknown-8bitr#css|]}t|VqdS)N)r()r:r;rrrr<sTr')rrZrEr rRr[r\r?get_unstructuredr$rrUr@r(rrrr0Z _fold_as_ewr*rr)r+r!) rrOlast_ewr]rKr^is_ewrSchunkZ oldlastlenschunklchunkr.rrrr+sV     /&             zUnstructuredTokenList._foldc Csg}d}x|D]}t|}y|jd|j|Wqtk r|dkr|j|j||t|}n<tdj||d|g}|j|j|YqXqWdj|S)Nzus-asciir#) r rRrr[rVr(rpr$rU)rrSrrTrqrKsparttlrrrrVs     &z UnstructuredTokenList.cte_encodeN)r1r2r3rGr+rVrrrrrns  5rnc@s.eZdZdZddZddZdS)Phrasephrasec Csad}|jjrdnd}x<|jD]1}t|}t|}d}yt|j|Wntk rtdd|jDrd}nd}|dk r|j r|dj dkr|j r|j d} nd } xCt |D]5\} } | j d krt| dd|| .z unknown-8bitr'rIr#zbare-quoted-stringcss|]}t|VqdS)N)r()r:r;rrrr<sTz quoted-stringrJrJ)rrZrEr r(rRr[r\r?rLrGrMrH enumeraternrpr$rrUrrr@rr0r+)rrOrqr]rKr^r_Zhas_ewrS remainderir,rsrtrurrrr+sN      /     z Phrase._foldc Csg}d}d}x}|D]u}t|}y|jd|j|Wntk rhd}|dkr|jst|}|j|j||n|jsd|d jdkr|jr|j d }nd}xCt |D]5\} } | jdkrt | dd|| s rc@seZdZdZdS)Tokenr,N)r1r2r3rGrrrrrCs rc@s:eZdZdZdZdZdZeddZdS) EncodedWordz encoded-wordNcCs3|jdk r|jStjt||jdS)N)cterQrRr rS)rrrrencodedOszEncodedWord.encoded) r1r2r3rGrrSlangrhrrrrrrHs rc@sLeZdZdZeddZeddZeddZdS) QuotedStringz quoted-stringcCs+x$|D]}|jdkr|jSqWdS)Nzbare-quoted-string)rGr)rr;rrrrl[s zQuotedString.contentcCsYg}xC|D];}|jdkr8|jt|q |j|jq Wdj|S)Nzbare-quoted-stringr#)rGrr rr$)rrTr;rrr quoted_valueas  zQuotedString.quoted_valuecCs+x$|D]}|jdkr|jSqWdS)Nzbare-quoted-string)rGr)rr,rrrstripped_valueks zQuotedString.stripped_valueN)r1r2r3rGrhrlrrrrrrrWs  rc@s4eZdZdZddZeddZdS)BareQuotedStringzbare-quoted-stringcCs tdjdd|DS)Nr#css|]}t|VqdS)N)r )r:r;rrrr<wsz+BareQuotedString.__str__..)rr$)rrrrr%vszBareQuotedString.__str__cCsdjdd|DS)Nr#css|]}t|VqdS)N)r )r:r;rrrr<{sz)BareQuotedString.value..)r$)rrrrryszBareQuotedString.valueN)r1r2r3rGr%rhrrrrrrrs  rc@sReZdZdZddZddZeddZedd Zd S) Commentrkcs8djtdgfddDdgggS)Nr#rcsg|]}j|qSr)quote)r:r;)rrrrms z#Comment.__str__..))r$r@)rr)rrr%s   zComment.__str__cCsG|jdkrt|St|jddjddjddS)Nrkr z\\rz\(rz\))rGr r)rrrrrrs   z Comment.quotecCsdjdd|DS)Nr#css|]}t|VqdS)N)r )r:r;rrrr<sz"Comment.content..)r$)rrrrrlszComment.contentcCs |jgS)N)rl)rrrrrMszComment.commentsN) r1r2r3rGr%rrhrlrMrrrrr~s   rc@sLeZdZdZeddZeddZeddZdS) AddressListz address-listcCsdd|DS)NcSs%g|]}|jdkr|qS)address)rG)r:r;rrrrms z)AddressList.addresses..r)rrrr addressesszAddressList.addressescCstdd|DgS)Ncss'|]}|jdkr|jVqdS)rN)rG mailboxes)r:r;rrrr<sz(AddressList.mailboxes..)r@)rrrrrs zAddressList.mailboxescCstdd|DgS)Ncss'|]}|jdkr|jVqdS)rN)rG all_mailboxes)r:r;rrrr<sz,AddressList.all_mailboxes..)r@)rrrrrs zAddressList.all_mailboxesN)r1r2r3rGrhrrrrrrrrs rc@sLeZdZdZeddZeddZeddZdS) AddressrcCs"|djdkr|djSdS)Nrgroup)rG display_name)rrrrrszAddress.display_namecCs@|djdkr|dgS|djdkr5gS|djS)Nrmailboxzinvalid-mailbox)rGr)rrrrrs  zAddress.mailboxescCsG|djdkr|dgS|djdkr<|dgS|djS)Nrrzinvalid-mailbox)rGr)rrrrrs   zAddress.all_mailboxesN)r1r2r3rGrhrrrrrrrrs rc@s:eZdZdZeddZeddZdS) MailboxListz mailbox-listcCsdd|DS)NcSs%g|]}|jdkr|qS)r)rG)r:r;rrrrms z)MailboxList.mailboxes..r)rrrrrszMailboxList.mailboxescCsdd|DS)NcSs%g|]}|jdkr|qS)rinvalid-mailbox)rr)rG)r:r;rrrrms z-MailboxList.all_mailboxes..r)rrrrrszMailboxList.all_mailboxesN)r1r2r3rGrhrrrrrrrs rc@s:eZdZdZeddZeddZdS) GroupListz group-listcCs)| s|djdkrgS|djS)Nrz mailbox-list)rGr)rrrrrszGroupList.mailboxescCs)| s|djdkrgS|djS)Nrz mailbox-list)rGr)rrrrrszGroupList.all_mailboxesN)r1r2r3rGrhrrrrrrrs rc@sLeZdZdZeddZeddZeddZdS) GrouprcCs"|djdkrgS|djS)Nz group-list)rGr)rrrrrszGroup.mailboxescCs"|djdkrgS|djS)Nrz group-list)rGr)rrrrrszGroup.all_mailboxescCs |djS)Nr)r)rrrrrszGroup.display_nameN)r1r2r3rGrhrrrrrrrrs rc@speZdZdZeddZeddZeddZedd Zed d Z d S) NameAddrz name-addrcCs!t|dkrdS|djS)Nr'r)r(r)rrrrrszNameAddr.display_namecCs |djS)Nr'rJ) local_part)rrrrrszNameAddr.local_partcCs |djS)Nr'rJ)domain)rrrrrszNameAddr.domaincCs |djS)Nr'rJ)route)rrrrr szNameAddr.routecCs |djS)Nr'rJ) addr_spec)rrrrrszNameAddr.addr_specN) r1r2r3rGrhrrrrrrrrrrs rc@s^eZdZdZeddZeddZeddZedd Zd S) AngleAddrz angle-addrcCs+x$|D]}|jdkr|jSqWdS)Nz addr-spec)rGr)rr;rrrrs zAngleAddr.local_partcCs+x$|D]}|jdkr|jSqWdS)Nz addr-spec)rGr)rr;rrrrs zAngleAddr.domaincCs+x$|D]}|jdkr|jSqWdS)Nz obs-route)rGdomains)rr;rrrr#s zAngleAddr.routecCs/x(|D]}|jdkr|jSqWdSdS)Nz addr-specz<>)rGr)rr;rrrr)s  zAngleAddr.addr_specN) r1r2r3rGrhrrrrrrrrrs rc@s(eZdZdZeddZdS)ObsRoutez obs-routecCsdd|DS)NcSs(g|]}|jdkr|jqS)r)rGr)r:r;rrrrm8s z$ObsRoute.domains..r)rrrrr6szObsRoute.domainsN)r1r2r3rGrhrrrrrr2s rc@speZdZdZeddZeddZeddZedd Zed d Z d S) MailboxrcCs"|djdkr|djSdS)Nrz name-addr)rGr)rrrrr?szMailbox.display_namecCs |djS)Nr)r)rrrrrDszMailbox.local_partcCs |djS)Nr)r)rrrrrHszMailbox.domaincCs"|djdkr|djSdS)Nrz name-addr)rGr)rrrrrLsz Mailbox.routecCs |djS)Nr)r)rrrrrQszMailbox.addr_specN) r1r2r3rGrhrrrrrrrrrr;s rc@s:eZdZdZeddZeZZZZ dS)InvalidMailboxzinvalid-mailboxcCsdS)Nr)rrrrrZszInvalidMailbox.display_nameN) r1r2r3rGrhrrrrrrrrrrVs rcs.eZdZdZefddZS)DomainrcsdjtjjS)Nr#)r$r5rsplit)r)r9rrresz Domain.domain)r1r2r3rGrhrrr)r9rras rc@seZdZdZdS)DotAtomzdot-atomN)r1r2r3rGrrrrrjs rc@seZdZdZdS) DotAtomTextz dot-atom-textN)r1r2r3rGrrrrros rc@s^eZdZdZeddZeddZeddZedd Zd S) AddrSpecz addr-speccCs |djS)Nr)r)rrrrrxszAddrSpec.local_partcCs!t|dkrdS|djS)Nr'rJ)r(r)rrrrr|szAddrSpec.domaincCsJt|dkr|djS|djj|dj|djjS)Nrrr'r)r(rrstriplstrip)rrrrrs zAddrSpec.valuecCsht|j}t|t|tkr=t|j}n |j}|jdk rd|d|jS|S)N@)setrr( DOT_ATOM_ENDSrr)rZnamesetZlprrrrs zAddrSpec.addr_specN) r1r2r3rGrhrrrrrrrrrts rc@seZdZdZdS) ObsLocalPartzobs-local-partN)r1r2r3rGrrrrrs rcs@eZdZdZeddZefddZS) DisplayNamez display-namecCst|}|djdkr/|jdn5|ddjdkrdt|ddd|d<|djdkr|jn5|ddjdkrt|ddd|d <|jS) NrrIr'rJrJrJrJrJrJ)r4rGrHr)rrTrrrrs  zDisplayName.display_namecsd}|jrd}n&x#|D]}|jdkrd}qW|rd}}|djdksx|ddjdkr~d}|d jdks|d d jdkrd}|t|j|StjSdS) NFTz quoted-stringr#rrIrjr'rJrJrJ)r6rGrrr5r)rrr;preZpost)r9rrrs     **zDisplayName.value)r1r2r3rGrhrrrr)r9rrs rc@s:eZdZdZeddZeddZdS) LocalPartz local-partcCs-|djdkr|djS|djSdS)Nrz quoted-string)rGrr)rrrrrs zLocalPart.valuecCstg}t}d}x|dtgD]}|jdkr?q'|r|jdkr|djdkrt|dd|d)r)r9rrr>szTerminal.__repr__cCs t|jS)N)listr6)rrrrr?szTerminal.all_defectsr#csIdj||jj|jtj|js3dndj|jgS)Nz {}{}/{}({}){}r#z {})r=r9r1rGr5r>r6)rra)r9rrrcs   z Terminal._ppc CsJt|}y|jd|SWn"tk rEtj||SYnXdS)Nzus-ascii)r rRr[rQ)rrSrrrrrrVs    zTerminal.cte_encodecCsdS)Nr)rrrrrBszTerminal.pop_trailing_wscCsdS)Nr)rrrrr*szTerminal.pop_leading_fwscCsgS)Nr)rrrrrMszTerminal.commentscCsdS)NFr)rrrrrLszTerminal.has_leading_commentcCst||jfS)N)r rG)rrrr__getnewargs__szTerminal.__getnewargs__)r1r2r3rr>rhr?rcrVrBr*rMrLrrr)r9rrs     rc@s4eZdZeddZddZdZdS)WhiteSpaceTerminalcCsdS)Nrjr)rrrrrszWhiteSpaceTerminal.valuecCsdS)NTr)rrrrrA sz!WhiteSpaceTerminal.startswith_fwsTN)r1r2r3rhrrAr)rrrrrs  rc@s@eZdZeddZddZdZddZdS) ValueTerminalcCs|S)Nr)rrrrrszValueTerminal.valuecCsdS)NFr)rrrrrAszValueTerminal.startswith_fwsFcCstjt||S)N)rQrRr )rrSrrrrUszValueTerminal.as_encoded_wordN)r1r2r3rhrrAr)rUrrrrrs  rc@sFeZdZeddZeddZddZdZdS) EWWhiteSpaceTerminalcCsdS)Nr#r)rrrrr!szEWWhiteSpaceTerminal.valuecCs|ddS)Nr)rrrrr%szEWWhiteSpaceTerminal.encodedcCsdS)Nr#r)rrrrr%)szEWWhiteSpaceTerminal.__str__TN)r1r2r3rhrrr%r)rrrrrs  rr,zlist-separatorrzroute-component-markerz([{}]+)r#z[^{}]+r z\\]z\]z[\x00-\x20\x7F]cCsWt|}|r+|jjtj|tj|rS|jjtjddS)z@If input token contains ASCII non-printables, register a defect.z*Non-ASCII characters found in header tokenN)_non_printable_finderr6rrZNonPrintableDefectrrrY)xtextZnon_printablesrrr_validate_xtextWs  rcCst|d^}}g}d}d}xtt|D]e}||dkrn|red}d}n d}q:|r}d}n|||krP|j||q:W|d}dj|dj||dg||fS)akScan printables/quoted-pairs until endchars and return unquoted ptext. This function turns a run of qcontent, ccontent-without-comments, or dtext-with-quoted-printables into a single string by unquoting any quoted printables. It returns the string, the remaining value, and a flag that is True iff there were any quoted printables decoded. r'Fr Tr#N) _wsp_splitterranger(rr$)rendcharsZfragmentr{Zvcharsescapehad_qpposrrr_get_ptext_to_endcharsas$    rcCs?|j}t|dt|t|d}||fS)zFWS = 1*WSP This isn't the RFC definition. We're using fws to represent tokens where folding can be done, but when we are parsing the *un*folding has already been done so we don't need to watch out for CRLF. NrF)rrr()rZnewvaluerFrrrget_fwss )rc Cs<t}|jds0tjdj||ddjdd^}}||ddkrtjdj|dj|}t|dkr|dtkr|dtkr|jdd^}}|d|}t|jdkr|j j tj d ||_ dj|}y't jd|d\}}}} Wn-tk rtjd j|j YnX||_||_|j j| x|r1|dtkrt|\} }|j | qt|d^} }t| d } t| |j | dj|}qW||fS) zE encoded-word = "=?" charset "?" encoding "?" encoded-text "?=" z=?z"expected encoded word but found {}rNz?=r'r#rzwhitespace inside encoded wordz!encoded word format invalid: '{}'vtext)r startswithrHeaderParseErrorr=rr$r(rr6rrrrQrrrSrrWSPrrrr) rZewrr{ZremstrrrrSrr6r,charsrrrrget_encoded_wordsH "2  '       rc Csot}x_|rj|dtkrDt|\}}|j|q |jdryt|\}}Wntjk rYnXd}t|dkr|d j dkr|j jtj dd}|r t|dkr |d j d kr t |dd|d<|j|q t |d^}}t|d }t||j|d j|}q W|S)aOunstructured = (*([FWS] vchar) *WSP) / obs-unstruct obs-unstruct = *((*LF *CR *(obs-utext) *LF *CR)) / FWS) obs-utext = %d0 / obs-NO-WS-CTL / LF / CR obs-NO-WS-CTL is control characters except WSP/CR/LF. So, basically, we have printable runs, plus control characters or nulls in the obsolete syntax, separated by whitespace. Since RFC 2047 uses the obsolete syntax in its specification, but requires whitespace on either side of the encoded words, I can see no reason to need to separate the non-printable-non-whitespace from the printable runs if they occur, so we parse this into xtext tokens separated by WSP tokens. Because an 'unstructured' value must by definition constitute the entire value, this 'get' routine does not return a remaining value, only the parsed TokenList. rz=?Tr'rFz&missing whitespace before encoded wordFrz encoded-wordrr#rJrJrJ)rnrrrrrrrr(rGr6rrrrrr$)rror,Zhave_wsrr{rrrrrps:       rpcCs;t|d\}}}t|d}t|||fS)actext = This is not the RFC ctext, since we are handling nested comments in comment and unquoting quoted-pairs here. We allow anything except the '()' characters, but if we find any ASCII other than the RFC defined printable ASCII, a NonPrintableDefect is added to the token's defects list. Since quoted pairs are converted to their unquoted values, what is returned is a 'ptext' token. In this case it is a WhiteSpaceTerminal, so it's value is ' '. z()r)rrr)rr_rrr get_qp_ctexts  r cCs;t|d\}}}t|d}t|||fS)aoqcontent = qtext / quoted-pair We allow anything except the DQUOTE character, but if we find any ASCII other than the RFC defined printable ASCII, a NonPrintableDefect is added to the token's defects list. Any quoted pairs are converted to their unquoted values, so what is returned is a 'ptext' token. In this case it is a ValueTerminal. r r)rrr)rrr rrr get_qcontents  r cCsot|}|s*tjdj||j}|t|d}t|d}t|||fS)zatext = We allow any non-ATOM_ENDS in atext, but add an InvalidATextDefect to the token's defects list if we find non-atext characters. zexpected atext but found '{}'Natext)_non_atom_end_matcherrrr=rr(rr)rmr rrr get_atext s   rc CsQ|ddkr(tjdj|t}|dd}x|r|ddkr|dtkrt|\}}n|dddkry/t|\}}|jjtj dWqtjk rt |\}}YqXnt |\}}|j|qDW|s=|jjtj d ||fS||ddfS) zbare-quoted-string = DQUOTE *([FWS] qcontent) [FWS] DQUOTE A quoted-string without the leading or trailing white space. Its value is the text between the quote marks, with whitespace preserved and quoted pairs decoded. rr zexpected '"' but found '{}'r'Nrz=?z!encoded word inside quoted stringz"end of header inside quoted string) rrr=rrrrr6rrr )rZbare_quoted_stringr,rrrget_bare_quoted_strings,   rcCs |r.|ddkr.tjdj|t}|dd}x|r|ddkr|dtkrt|\}}n7|ddkrt|\}}nt|\}}|j|qJW|s|j jtj d||fS||ddfS)zcomment = "(" *([FWS] ccontent) [FWS] ")" ccontent = ctext / quoted-pair / comment We handle nested comments here, and quoted-pair in our qp-ctext routine. rrzexpected '(' but found '{}'r'Nrzend of header inside comment) rrr=rrr get_commentr rr6r)rrkr,rrrr<s"   rcCstt}x^|ri|dtkri|dtkrGt|\}}nt|\}}|j|q W||fS)z,CFWS = (1*([FWS] comment) [FWS]) / FWS r)r~ CFWS_LEADERrrrr)rrIr,rrrget_cfwsUs rcCst}|r>|dtkr>t|\}}|j|t|\}}|j||r|dtkrt|\}}|j|||fS)zquoted-string = [CFWS] [CFWS] 'bare-quoted-string' is an intermediate class defined by this parser and not by the RFC grammar. It is the quoted string without any attached CFWS. r)rrrrr)rZ quoted_stringr,rrrget_quoted_stringbs    rc Cst}|r>|dtkr>t|\}}|j||rl|dtkrltjdj||jdryt |\}}Wqtjk rt |\}}YqXnt |\}}|j||r|dtkrt|\}}|j|||fS)zPatom = [CFWS] 1*atext [CFWS] An atom could be an rfc2047 encoded word. rzexpected atom but found '{}'z=?) rrrr ATOM_ENDSrrr=rrr)rrr,rrrget_atomts$    rcCst}| s |dtkr8tjdj|xl|r|dtkrt|\}}|j||r;|ddkr;|jt|dd}q;W|dtkrtjdjd|||fS)z( dot-text = 1*atext *("." 1*atext) rz8expected atom at a start of dot-atom-text but found '{}'r r'Nz4expected atom at end of dot-atom-text but found '{}'rJ)rrrrr=rrr)rZ dot_atom_textr,rrrget_dot_atom_texts      rc Cst}|dtkr8t|\}}|j||jdryt|\}}Wqtjk rt|\}}YqXnt|\}}|j||r|dtkrt|\}}|j|||fS)z dot-atom = [CFWS] dot-atom-text [CFWS] Any place we can have a dot atom, we could instead have an rfc2047 encoded word. rz=?) rrrrrrrrr)rZdot_atomr,rrr get_dot_atoms    rcCs|dtkr%t|\}}nd}|ddkrPt|\}}n=|dtkr{tjdj|nt|\}}|dk r|g|dd<||fS)aword = atom / quoted-string Either atom or quoted-string may start with CFWS. We have to peel off this CFWS first to determine which type of word to parse. Afterward we splice the leading CFWS, if any, into the parsed sub-token. If neither an atom or a quoted-string is found before the next special, a HeaderParseError is raised. The token returned is either an Atom or a QuotedString, as appropriate. This means the 'word' level of the formal grammar is not represented in the parse tree; this is because having that extra layer when manipulating the parse tree is more confusing than it is helpful. rNr z1Expected 'atom' or 'quoted-string' but found '{}')rrrSPECIALSrrr=r)rleaderr,rrrget_words  rcCsIt}y#t|\}}|j|Wn.tjk r\|jjtjdYnXx|r>|dtkr>|ddkr|jt|jjtj d|dd}q`yt|\}}WnVtjk r-|dt kr&t |\}}|jjtj dnYnX|j|q`W||fS)a phrase = 1*word / obs-phrase obs-phrase = word *(word / "." / CFWS) This means a phrase can be a sequence of words, periods, and CFWS in any order as long as it starts with at least one word. If anything other than words is detected, an ObsoleteHeaderDefect is added to the token's defect list. We also accept a phrase that starts with CFWS followed by a dot; this is registered as an InvalidHeaderDefect, since it is not supported by even the obsolete grammar. zphrase does not start with wordrr zperiod in 'phrase'r'Nzcomment found without atom) rxrrrrr6r PHRASE_ENDSrObsoleteHeaderDefectrr)rryr,rrr get_phrases.    rcCst}d}|dtkr1t|\}}|sOtjdj|yt|\}}Wnotjk ryt|\}}WnAtjk r|ddkr|dtkrt }YnXYnX|dk r|g|dd<|j ||r|ddks)|dtkrt t ||\}}|j dkrp|jj tjdn|jj tjd||dd|_||fS)z' obs-local-part = word *("." word) Frr r zinvalid repeated '.'Tr'Nzmisplaced-specialz/'\' character outside of quoted-string/ccontentrzmissing '.' between wordsrIz!Invalid leading '.' in local partrz"Invalid trailing '.' in local partzinvalid-obs-local-partrJrJrJr) rrr6rrrrrrGrrrr)rr Zlast_non_ws_was_dotr,rrrr'sV )          rcCsZt|d\}}}t|d}|rF|jjtjdt|||fS)a dtext = / obs-dtext obs-dtext = obs-NO-WS-CTL / quoted-pair We allow anything except the excluded characters, but if we find any ASCII other than the RFC defined printable ASCII, a NonPrintableDefect is added to the token's defects list. Quoted pairs are converted to their unquoted values, so what is returned is a ptext token, in this case a ValueTerminal. If there were quoted-printables, an ObsoleteHeaderDefect is added to the returned token's defect list. z[]rz(quoted printable found in domain-literal)rrr6rrrr)rrrrrr get_dtextVs   r"cCs:|r dS|jtjd|jtdddS)NFz"end of input inside domain-literalrzdomain-literal-endT)rrrr)rdomain_literalrrr_check_for_early_dl_endjs   r$cCst}|dtkr8t|\}}|j||sMtjd|ddkrutjdj||dd}t||r||fS|jtdd|dt krt |\}}|j|t |\}}|j|t||r||fS|dt krJt |\}}|j|t||rc||fS|ddkrtjd j||jtdd |dd}|r|dtkrt|\}}|j|||fS) zB domain-literal = [CFWS] "[" *([FWS] dtext) [FWS] "]" [CFWS] rzexpected domain-literal[z6expected '[' at start of domain-literal but found '{}'r'Nzdomain-literal-startrz4expected ']' at end of domain-literal but found '{}'zdomain-literal-end) rrrrrrr=r$rrrr")rr#r,rrrget_domain_literalrsD             r&c Cst}d}|dtkr1t|\}}|sOtjdj||ddkrt|\}}|dk r|g|dd<|j|||fSyt|\}}Wn'tjk rt |\}}YnX|dk r|g|dd<|j||r|ddkr|j jtj d|dj dkri|d|dd" [CFWS] / obs-angle-addr obs-angle-addr = [CFWS] "<" obs-route addr-spec ">" [CFWS] rzangle-addr-endznull addr-spec in angle-addrz*obsolete route specification in angle-addrz.expected addr-spec or obs-route but found '{}'z"missing trailing '>' on angle-addr) rrrrrrr=rr6rr(r,r)rZ angle_addrr,rrrget_angle_addrsJ        r/cCsRt}t|\}}|j|dd|jdd|_||fS)z display-name = phrase Because this is simply a name-rule, we don't return a display-name token containing a phrase, but rather a display-name token with the content of the phrase. N)rrrr6)rrr,rrrget_display_name$s  r0cCs5t}d}|dtkrOt|\}}|sOtjdj||ddkr|dtkrtjdj|t|\}}|stjdj||dk r|g|ddd.zinvalid-mailbox) rr1rrr(r=r\r?rGr)rrr,rrr get_mailboxQs    r2cCst}xv|r|d|kr|dtkr_|jt|dd|dd}q t|\}}|j|q W||fS)z Read everything up to one of the chars in endchars. This is outside the formal grammar. The InvalidMailbox TokenList that is returned acts like a Mailbox, but the data attributes are None. rzmisplaced-specialr'N)rrrrr)rrZinvalid_mailboxr,rrrget_invalid_mailboxfs  r3c CsMt}x7|rB|ddkrBy#t|\}}|j|Wn`tjk rd}|dtkrt|\}}| s|ddkr|j||jjtjdqt |d\}}|dk r|g|dd<|j||jjtj dn|ddkrI|jjtjdnZt |d\}}|dk r}|g|dd<|j||jjtj dYnX|r |ddkr |d }d |_ t |d\}}|j ||jjtj d|r |ddkr |jt |dd}q W||fS) aJ mailbox-list = (mailbox *("," mailbox)) / obs-mbox-list obs-mbox-list = *([CFWS] ",") mailbox *("," [mailbox / CFWS]) For this routine we go outside the formal grammar in order to improve error handling. We recognize the end of the mailbox list only at the end of the value or at a ';' (the group terminator). This is so that we can turn invalid mailboxes into InvalidMailbox tokens and continue parsing any remaining valid mailboxes. We also allow all mailbox entries to be null, and this condition is handled appropriately at a higher level. r;Nz,;zempty element in mailbox-listzinvalid mailbox in mailbox-listrr'zinvalid-mailboxrJ)rr2rrrrrr6rr3rrGrr*)rZ mailbox_listr,rrrrrget_mailbox_listxsN              r5cCsct}|s2|jjtjd||fSd}|r|dtkrt|\}}|s|jjtjd|j|||fS|ddkr|j|||fSt|\}}t|j dkr-|dk r|j||j ||jjtj d||fS|dk rL|g|dd<|j|||fS)zg group-list = mailbox-list / CFWS / obs-group-list obs-group-list = 1*([CFWS] ",") [CFWS] zend of header before group-listNrzend of header in group-listr4zgroup-list with empty entries) rr6rrrrrr5r(rrr)rZ group_listrr,rrrget_group_lists8               r6cCst}t|\}}| s2|ddkrJtjdj||j||jtdd|dd}|r|ddkr|jtdd||ddfSt|\}}|j||s|jjtj d |ddkr#tjd j||jtdd|dd}|r~|dt kr~t |\}}|j|||fS) z7 group = display-name ":" [group-list] ";" [CFWS] rr)z8expected ':' at end of group display name but found '{}'zgroup-display-name-terminatorr'Nr4zgroup-terminatorzend of header in groupz)expected ';' at end of group but found {}) rr0rrr=rrr6r6rrr)rrr,rrr get_groups2       r7cCst}yt|\}}Wn[tjk r|yt|\}}Wn-tjk rwtjdj|YnXYnX|j|||fS)a address = mailbox / group Note that counter-intuitively, an address can be either a single address or a list of addresses (a group). This is why the returned Address object has a 'mailboxes' attribute which treats a single address as a list of length one. When you need to differentiate between to two cases, extract the single element, which is either a mailbox or a group token. zexpected address but found '{}')rr7rrr2r=r)rrr,rrr get_addresss  r8cCs_t}xI|rTy#t|\}}|j|Wntjk r}zbd}|dtkrt|\}}| s|ddkr|j||jjtjdqt |d\}}|dk r|g|dd<|jt |g|jjtj dn|ddkrG|jjtjdnct |d\}}|dk r{|g|dd<|jt |g|jjtj dWYdd}~XnX|r%|ddkr%|d d}d|_ t |d\}}|j ||jjtj d|r |jtdd |dd}q W||fS) a address_list = (address *("," address)) / obs-addr-list obs-addr-list = *([CFWS] ",") address *("," [address / CFWS]) We depart from the formal grammar here by continuing to parse until the end of the input, assuming the input to be entirely composed of an address-list. This is always true in email parsing, and allows us to skip invalid addresses to parse additional valid ones. Nrrz"address-list entry with no contentzinvalid address in address-listzempty element in address-listr'zinvalid-mailboxzlist-separatorrJ)rr8rrrrrr6rr3rrrGrr)rZ address_listr,errrrrrrget_address_list sN           r:cCsst}|s,|jjtjd|S|dtkrzt|\}}|j||sz|jjtjdd}xH|r|ddkr|dtkr||d7}|dd}qW|js|jjtjdj ||jt |d n%t ||_ |jt |d |rl|dtkrlt|\}}|j|| s|ddkr|j dk r|jjtjd |r|jt |d |S|jt dd |dd}|r&|dtkr&t|\}}|j||sX|j dk rT|jjtjd |Sd}x8|r|dtkr||d7}|dd}qaW|js|jjtjd j ||jt |d n%t ||_ |jt |d |r:|dtkr:t|\}}|j||ro|jjtjd|jt |d |S)zE mime-version = [CFWS] 1*digit [CFWS] "." [CFWS] 1*digit [CFWS] z%Missing MIME version number (eg: 1.0)rz0Expected MIME version number but found only CFWSr#r r'Nz1Expected MIME major version number but found {!r}rdigitsz0Incomplete MIME version; found only major numberzversion-separatorz1Expected MIME minor version number but found {!r}z'Excess non-CFWS text after MIME version)rr6rrHeaderMissingRequiredValuerrisdigitrr=rintrr)rZ mime_versionr,r;rrrparse_mime_versionO sv    )        r?cCst}xv|r|ddkr|dtkr_|jt|dd|dd}q t|\}}|j|q W||fS)z Read everything up to the next ';'. This is outside the formal grammar. The InvalidParameter TokenList that is returned acts like a Parameter, but the data attributes are None. rr4zmisplaced-specialr'N)rrrrr)rZinvalid_parameterr,rrrget_invalid_parameter s  r@cCsot|}|s*tjdj||j}|t|d}t|d}t|||fS)a8ttext = We allow any non-TOKEN_ENDS in ttext, but add defects to the token's defects list if we find non-ttext characters. We also register defects for *any* non-printables even though the RFC doesn't exclude all of them, because we follow the spirit of RFC 5322. zexpected ttext but found '{}'Nttext)_non_token_end_matcherrrr=rr(rr)rrrArrr get_ttext s   rCcCst}|r>|dtkr>t|\}}|j||rl|dtkrltjdj|t|\}}|j||r|dtkrt|\}}|j|||fS)ztoken = [CFWS] 1*ttext [CFWS] The RFC equivalent of ttext is any US-ASCII chars except space, ctls, or tspecials. We also exclude tabs even though the RFC doesn't. The RFC implies the CFWS but is not explicit about it in the BNF. rzexpected token but found '{}') rrrr TOKEN_ENDSrrr=rC)rZmtokenr,rrr get_token s    rEcCsot|}|s*tjdj||j}|t|d}t|d}t|||fS)aQattrtext = 1*(any non-ATTRIBUTE_ENDS character) We allow any non-ATTRIBUTE_ENDS in attrtext, but add defects to the token's defects list if we find non-attrtext characters. We also register defects for *any* non-printables even though the RFC doesn't exclude all of them, because we follow the spirit of RFC 5322. z expected attrtext but found {!r}Nr)_non_attribute_end_matcherrrr=rr(rr)rrrrrr get_attrtext s   rGcCst}|r>|dtkr>t|\}}|j||rl|dtkrltjdj|t|\}}|j||r|dtkrt|\}}|j|||fS)aH [CFWS] 1*attrtext [CFWS] This version of the BNF makes the CFWS explicit, and as usual we use a value terminal for the actual run of characters. The RFC equivalent of attrtext is the token characters, with the subtraction of '*', "'", and '%'. We include tab in the excluded set just as we do for token. rzexpected token but found '{}') rrrrATTRIBUTE_ENDSrrr=rG)rrr,rrr get_attribute s    rIcCsot|}|s*tjdj||j}|t|d}t|d}t|||fS)zattrtext = 1*(any non-ATTRIBUTE_ENDS character plus '%') This is a special parsing routine so that we get a value that includes % escapes as a single string (which we decode as a single string later). z)expected extended attrtext but found {!r}Nzextended-attrtext)#_non_extended_attribute_end_matcherrrr=rr(rr)rrrrrrget_extended_attrtext s   rKcCst}|r>|dtkr>t|\}}|j||rl|dtkrltjdj|t|\}}|j||r|dtkrt|\}}|j|||fS)z [CFWS] 1*extended_attrtext [CFWS] This is like the non-extended version except we allow % characters, so that we can pick up an encoded value as a single string. rzexpected token but found '{}') rrrrEXTENDED_ATTRIBUTE_ENDSrrr=rK)rrr,rrrget_extended_attribute s    rMcCs3t}| s |ddkr8tjdj||jtdd|dd}| sv|dj rtjdj|d}x8|r|djr||d7}|dd}qW|dd kr|d kr|jjtjd t ||_ |jt|d ||fS) a6 '*' digits The formal BNF is more complicated because leading 0s are not allowed. We check for that and add a defect. We also assume no CFWS is allowed between the '*' and the digits, though the RFC is not crystal clear on that. The caller should already have dealt with leading CFWS. r*zExpected section but found {}zsection-markerr'Nz$Expected section number but found {}r#0z§ion numberhas an invalid leading 0r;) rrrr=rrr=r6ZInvalidHeaderErrorr>r)rrr;rrr get_section! s$     rPcCst}|stjdd}|dtkrFt|\}}|sdtjdj||ddkrt|\}}nt|\}}|dk r|g|dd<|j|||fS)z quoted-string / attribute z&Expected value but found end of stringNrz Expected value but found only {}r ) rrrrrr=rrMr)rvrr,rrr get_value? s      rRc Cst}t|\}}|j|| s?|ddkrk|jjtjdj|||fS|ddkry,t|\}}d|_|j|Wntj k rYnX|stj d|ddkr|jt dd|dd }d|_ |dd kr2tj d |jt d d |dd }d }|r|dt krt |\}}|j|d }|}|j r|r|dd krt|\}}|j}d}|jdkr>|r |ddkr d}qkt|\}} | rk| ddkrkd}n-yt|\}} WnYn X| skd}|r|jjtjd|j|x4|D],} | jdkrg| d d <| }PqW|}nd }|jjtjd|r|ddkrd }nt|\}}|j s@|jdkr| sW|ddkr|j||d k rv|}||fS|jjtjd|s|jjtjd|j||d kr||fSn7|d k r1x|D]} | jdkrPqW| jdk|j| | j|_|ddkrYtj dj||jt dd|dd }|r|ddkrt|\}}|j||j|_| s|ddkrtj dj||jt dd|dd }|d k rt} xN|rz|dtkrXt|\}}nt|\}}| j|q-W| }nt|\}}|j||d k r|}||fS)aY attribute [section] ["*"] [CFWS] "=" value The CFWS is implied by the RFC but not made explicit in the BNF. This simplified form of the BNF from the RFC is made to conform with the RFC BNF through some extra checks. We do it this way because it makes both error recovery and working with the resulting parse tree easier. rr4z)Parameter contains name ({}) but no valuerNTzIncomplete parameterzextended-parameter-markerr'N=zParameter not followed by '='zparameter-separatorr F'z5Quoted string value for extended parameter is invalidzbare-quoted-stringzZParameter marked as extended but appears to have a quoted string value that is non-encodedzcApparent initial-extended-value but attribute was not marked as extended or was not initial sectionz(Missing required charset/lang delimiterszextended-attrtextrz=Expected RFC2231 char/lang encoding delimiter, but found {!r}zRFC2231 delimiterz;Expected RFC2231 char/lang encoding delimiter, but found {})rrIrr6rrr=rPrrrrrrrrrrGrKrGrRrrSrrrrr ) rrr,rr{ZappendtoZqstringZ inner_valueZ semi_validrtrQrrr get_parameterU s                                         rVcCst}x|ry#t|\}}|j|Wntjk r=}zd}|dtkrut|\}}|s|j||S|ddkr|dk r|j||jjtjdnZt |\}}|r|g|dd<|j||jjtjdj |WYdd}~XnX|r|ddkr|d }d|_ t |\}}|j ||jjtjdj ||r |jt dd |dd}q W|S) a! parameter *( ";" parameter ) That BNF is meant to indicate this routine should only be called after finding and handling the leading ';'. There is no corresponding rule in the formal RFC grammar, but it is more convenient for us for the set of parameters to be treated as its own TokenList. This is 'parse' routine because it consumes the reminaing value, but it would never be called to parse a full header. Instead it is called to parse everything after the non-parameter value of a specific MIME header. Nrr4zparameter entry with no contentzinvalid parameter {!r}r'zinvalid-parameterz)parameter with invalid trailing text {!r}zparameter-separatorrJ)rrVrrrrrr6rr@r=rGrr)rZmime_parametersr,r9rrrrrparse_mime_parameters sD       &   rWcCsxv|rx|ddkrx|dtkrV|jt|dd|dd}qt|\}}|j|qW|sdS|jtdd|jt|dddS)zBDo our best to find the parameters in an invalid MIME header rr4zmisplaced-specialr'Nzparameter-separator)rrrrrW)Z tokenlistrr,rrr_find_mime_parameters srXcCs;t}d}|s2|jjtjd|Syt|\}}WnHtjk r|jjtjdj|t |||SYnX|j|| s|ddkr|jjtjd|rt |||S|j j j |_ |jtdd|dd }yt|\}}WnHtjk r|jjtjd j|t |||SYnX|j||j j j |_|s|S|dd kr|jjtjd j||` |`t |||S|jtd d |jt|dd |S)z maintype "/" subtype *( ";" parameter ) The maintype and substype are tokens. Theoretically they could be checked against the official IANA list + x-token, but we don't do that. Fz"Missing content type specificationz(Expected content maintype but found {!r}r/zInvalid content typezcontent-type-separatorr'Nz'Expected content subtype but found {!r}r4zDs         T K`     '# U3 $ 0 0 0 !  * 8           & ' /   ' $  ) .     9 %   > D          4  9