YfO@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)valuer?/opt/alt/python35/lib64/python3.5/email/_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_lenwsZmarginZtrimrrrappend_if_fitssf                     z_Folded.append_if_fits) __name__ __module__ __qualname__rr!r"r%rr/rrrrrgs     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{}({}))formatr8r0r4__repr__)r)r8rrr=szTokenList.__repr__cCsdjdd|DS)Nr#css!|]}|jr|jVqdS)N)r)r9r:rrrr;sz"TokenList.value..)r$)rrrrrszTokenList.valuecCstdd|D|jS)Ncss|]}|jVqdS)N) all_defects)r9r:rrrr;sz(TokenList.all_defects..)sumr5)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)r8startswith_fwsr(r pop_trailing_wsr)rklassthisr,Zend_wsrrrpartss   '     zTokenList.partscCs|djS)Nr)r@)rrrrr@ szTokenList.startswith_fwscCs.|djdkr |jdS|djS)Nrfws) token_typepopr*)rrrrr* s zTokenList.pop_leading_fwscCs.|djdkr |jdS|djS)Nr'cfwsrIrI)rFrGrA)rrrrrAs zTokenList.pop_trailing_wscCs"x|D]}|jrdSqWdS)NTF)r))rpartrrrr)s  zTokenList.has_fwscCs|djS)Nr)has_leading_comment)rrrrrKszTokenList.has_leading_commentcCs+g}x|D]}|j|jq W|S)N)rcomments)rrLr,rrrrL!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'rEr#rIrI)r*rrFrG_ewencoder r$)rcharsetresr.Ztrailerrrras_encoded_word0s  ( zTokenList.as_encoded_wordcCs=g}x'|D]}|j|j||q Wdj|S)Nr#)r cte_encoder$)rrRrrSrJrrrrU<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)r9r:rrrr;Jsz"TokenList._fold..z unknown-8bit)rutf8rDr r(rQUnicodeEncodeErroranyr>rUr/r*rr)r+rr!)rrNencodingrJtstrtlenrRr.rrrr+Bs2          zTokenList._foldr#cCs#tdj|jdddS)N indentr#)printr$_pp)rr`rrrpprintcszTokenList.pprintcCsdj|jddS)Nr_r`r#)r$rb)rr`rrrppstrfszTokenList.ppstrccsdj||jj|jVxH|D]@}t|dsN|dj|Vq$|j|dEdHq$W|jrdj|j}nd}dj||VdS)Nz{}{}/{}(rbz* !! invalid element in token list: {!r}z z Defects: {}r#z{}){})r<r8r0rFhasattrrbr5)rr`r,Zextrarrrrbis      z TokenList._pp)r0r1r2rFrr%r=propertyrr>rDr@r*rAr)rKrLrOrTrUr+rcrdrbrr)r8rr3s(  +       !r3c@s4eZdZeddZeddZdS)WhiteSpaceTokenListcCsdS)N r)rrrrr}szWhiteSpaceTokenList.valuecCsdd|DS)NcSs(g|]}|jdkr|jqS)comment)rFcontent)r9r:rrr s z0WhiteSpaceTokenList.comments..r)rrrrrLszWhiteSpaceTokenList.commentsN)r0r1r2rfrrLrrrrrg{s rgc@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-8rVFcss!|]}t|tjVqdS)N)rWrrX)r9r:rrrr;sz.UnstructuredTokenList._fold..z unknown-8bitr#css|]}t|VqdS)N)r()r9r:rrrr;sTr')rrYrDr rQrZr[r>get_unstructuredr$rrTr?r(rrrr/Z _fold_as_ewr*rr)r+r!) rrNlast_ewr\rJr]is_ewrRchunkZ 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 rQrrZrUr(rnr$rT)rrRrrSrorJsparttlrrrrUs     &z UnstructuredTokenList.cte_encodeN)r0r1r2rFr+rUrrrrrls  5rlc@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'rHr#zbare-quoted-stringcss|]}t|VqdS)N)r()r9r:rrrr;sTz quoted-stringrIrI)rrYrDr r(rQrZr[r>rKrFrLrG enumeraterlrnr$rrTrrr?rr/r+)rrNror\rJr]r^Zhas_ewrR remainderir,rqrrrsrrrr+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 r~c@seZdZdZdS)Tokenr,N)r0r1r2rFrrrrrCs rc@s:eZdZdZdZdZdZeddZdS) EncodedWordz encoded-wordNcCs3|jdk r|jStjt||jdS)N)cterPrQr rR)rrrrencodedOszEncodedWord.encoded) r0r1r2rFrrRlangrfrrrrrrHs rc@sLeZdZdZeddZeddZeddZdS) QuotedStringz quoted-stringcCs+x$|D]}|jdkr|jSqWdS)Nzbare-quoted-string)rFr)rr:rrrrj[s zQuotedString.contentcCsYg}xC|D];}|jdkr8|jt|q |j|jq Wdj|S)Nzbare-quoted-stringr#)rFrr rr$)rrSr:rrr quoted_valueas  zQuotedString.quoted_valuecCs+x$|D]}|jdkr|jSqWdS)Nzbare-quoted-string)rFr)rr,rrrstripped_valueks zQuotedString.stripped_valueN)r0r1r2rFrfrjrrrrrrrWs  rc@s4eZdZdZddZeddZdS)BareQuotedStringzbare-quoted-stringcCs tdjdd|DS)Nr#css|]}t|VqdS)N)r )r9r:rrrr;wsz+BareQuotedString.__str__..)rr$)rrrrr%vszBareQuotedString.__str__cCsdjdd|DS)Nr#css|]}t|VqdS)N)r )r9r:rrrr;{sz)BareQuotedString.value..)r$)rrrrryszBareQuotedString.valueN)r0r1r2rFr%rfrrrrrrrs  rc@sReZdZdZddZddZeddZedd Zd S) Commentrics8djtdgfddDdgggS)Nr#rcsg|]}j|qSr)quote)r9r:)rrrrks z#Comment.__str__..))r$r?)rr)rrr%s   zComment.__str__cCsG|jdkrt|St|jddjddjddS)Nrir z\\rz\(rz\))rFr r)rrrrrrs   z Comment.quotecCsdjdd|DS)Nr#css|]}t|VqdS)N)r )r9r:rrrr;sz"Comment.content..)r$)rrrrrjszComment.contentcCs |jgS)N)rj)rrrrrLszComment.commentsN) r0r1r2rFr%rrfrjrLrrrrr~s   rc@sLeZdZdZeddZeddZeddZdS) AddressListz address-listcCsdd|DS)NcSs%g|]}|jdkr|qS)address)rF)r9r:rrrrks z)AddressList.addresses..r)rrrr addressesszAddressList.addressescCstdd|DgS)Ncss'|]}|jdkr|jVqdS)rN)rF mailboxes)r9r:rrrr;sz(AddressList.mailboxes..)r?)rrrrrs zAddressList.mailboxescCstdd|DgS)Ncss'|]}|jdkr|jVqdS)rN)rF all_mailboxes)r9r:rrrr;sz,AddressList.all_mailboxes..)r?)rrrrrs zAddressList.all_mailboxesN)r0r1r2rFrfrrrrrrrrs rc@sLeZdZdZeddZeddZeddZdS) AddressrcCs"|djdkr|djSdS)Nrgroup)rF display_name)rrrrrszAddress.display_namecCs@|djdkr|dgS|djdkr5gS|djS)Nrmailboxzinvalid-mailbox)rFr)rrrrrs  zAddress.mailboxescCsG|djdkr|dgS|djdkr<|dgS|djS)Nrrzinvalid-mailbox)rFr)rrrrrs   zAddress.all_mailboxesN)r0r1r2rFrfrrrrrrrrs rc@s:eZdZdZeddZeddZdS) MailboxListz mailbox-listcCsdd|DS)NcSs%g|]}|jdkr|qS)r)rF)r9r:rrrrks z)MailboxList.mailboxes..r)rrrrrszMailboxList.mailboxescCsdd|DS)NcSs%g|]}|jdkr|qS)rinvalid-mailbox)rr)rF)r9r:rrrrks z-MailboxList.all_mailboxes..r)rrrrrszMailboxList.all_mailboxesN)r0r1r2rFrfrrrrrrrs rc@s:eZdZdZeddZeddZdS) GroupListz group-listcCs)| s|djdkrgS|djS)Nrz mailbox-list)rFr)rrrrrszGroupList.mailboxescCs)| s|djdkrgS|djS)Nrz mailbox-list)rFr)rrrrrszGroupList.all_mailboxesN)r0r1r2rFrfrrrrrrrs rc@sLeZdZdZeddZeddZeddZdS) GrouprcCs"|djdkrgS|djS)Nz group-list)rFr)rrrrrszGroup.mailboxescCs"|djdkrgS|djS)Nrz group-list)rFr)rrrrrszGroup.all_mailboxescCs |djS)Nr)r)rrrrrszGroup.display_nameN)r0r1r2rFrfrrrrrrrrs rc@speZdZdZeddZeddZeddZedd Zed d Z d S) NameAddrz name-addrcCs!t|dkrdS|djS)Nr'r)r(r)rrrrrszNameAddr.display_namecCs |djS)Nr'rI) local_part)rrrrrszNameAddr.local_partcCs |djS)Nr'rI)domain)rrrrrszNameAddr.domaincCs |djS)Nr'rI)route)rrrrr szNameAddr.routecCs |djS)Nr'rI) addr_spec)rrrrrszNameAddr.addr_specN) r0r1r2rFrfrrrrrrrrrrs rc@s^eZdZdZeddZeddZeddZedd Zd S) AngleAddrz angle-addrcCs+x$|D]}|jdkr|jSqWdS)Nz addr-spec)rFr)rr:rrrrs zAngleAddr.local_partcCs+x$|D]}|jdkr|jSqWdS)Nz addr-spec)rFr)rr:rrrrs zAngleAddr.domaincCs+x$|D]}|jdkr|jSqWdS)Nz obs-route)rFdomains)rr:rrrr#s zAngleAddr.routecCs/x(|D]}|jdkr|jSqWdSdS)Nz addr-specz<>)rFr)rr:rrrr)s  zAngleAddr.addr_specN) r0r1r2rFrfrrrrrrrrrs rc@s(eZdZdZeddZdS)ObsRoutez obs-routecCsdd|DS)NcSs(g|]}|jdkr|jqS)r)rFr)r9r:rrrrk8s z$ObsRoute.domains..r)rrrrr6szObsRoute.domainsN)r0r1r2rFrfrrrrrr2s rc@speZdZdZeddZeddZeddZedd Zed d Z d S) MailboxrcCs"|djdkr|djSdS)Nrz name-addr)rFr)rrrrr?szMailbox.display_namecCs |djS)Nr)r)rrrrrDszMailbox.local_partcCs |djS)Nr)r)rrrrrHszMailbox.domaincCs"|djdkr|djSdS)Nrz name-addr)rFr)rrrrrLsz Mailbox.routecCs |djS)Nr)r)rrrrrQszMailbox.addr_specN) r0r1r2rFrfrrrrrrrrrr;s rc@s:eZdZdZeddZeZZZZ dS)InvalidMailboxzinvalid-mailboxcCsdS)Nr)rrrrrZszInvalidMailbox.display_nameN) r0r1r2rFrfrrrrrrrrrrVs rcs.eZdZdZefddZS)DomainrcsdjtjjS)Nr#)r$r4rsplit)r)r8rrresz Domain.domain)r0r1r2rFrfrrr)r8rras rc@seZdZdZdS)DotAtomzdot-atomN)r0r1r2rFrrrrrjs rc@seZdZdZdS) DotAtomTextz dot-atom-textN)r0r1r2rFrrrrros rc@s^eZdZdZeddZeddZeddZedd Zd S) AddrSpecz addr-speccCs |djS)Nr)r)rrrrrxszAddrSpec.local_partcCs!t|dkrdS|djS)Nr'rI)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) r0r1r2rFrfrrrrrrrrrts rc@seZdZdZdS) ObsLocalPartzobs-local-partN)r0r1r2rFrrrrrs rcs@eZdZdZeddZefddZS) DisplayNamez display-namecCst|}|djdkr/|jdn5|ddjdkrdt|ddd|d<|djdkr|jn5|ddjdkrt|ddd|d <|jS) NrrHr'rIrIrIrIrIrI)r3rFrGr)rrSrrrrs  zDisplayName.display_namecsd}|jrd}n&x#|D]}|jdkrd}qW|rd}}|djdksx|ddjdkr~d}|d jdks|d d jdkrd}|t|j|StjSdS) NFTz quoted-stringr#rrHrhr'rIrIrI)r5rFrrr4r)rrr:ZpreZpost)r8rrrs     **zDisplayName.value)r0r1r2rFrfrrrr)r8rrs rc@s:eZdZdZeddZeddZdS) LocalPartz local-partcCs-|djdkr|djS|djSdS)Nrz quoted-string)rFrr)rrrrrs zLocalPart.valuecCstg}t}d}x|dtgD]}|jdkr?q'|r|jdkr|djdkrt|dd|dszTerminal.all_defectsr#csIdj||jj|jtj|js3dndj|jgS)Nz {}{}/{}({}){}r#z {})r<r8r0rFr4r=r5)rr`)r8rrrbs   z Terminal._ppc CsJt|}y|jd|SWn"tk rEtj||SYnXdS)Nzus-ascii)r rQrZrP)rrRrrrrrrUs    zTerminal.cte_encodecCsdS)Nr)rrrrrAszTerminal.pop_trailing_wscCsdS)Nr)rrrrr*szTerminal.pop_leading_fwscCsgS)Nr)rrrrrLszTerminal.commentscCsdS)NFr)rrrrrKszTerminal.has_leading_commentcCst||jfS)N)r rF)rrrr__getnewargs__szTerminal.__getnewargs__)r0r1r2rr=rfr>rbrUrAr*rLrKrrr)r8rrs     rc@s4eZdZeddZddZdZdS)WhiteSpaceTerminalcCsdS)Nrhr)rrrrrszWhiteSpaceTerminal.valuecCsdS)NTr)rrrrr@ sz!WhiteSpaceTerminal.startswith_fwsTN)r0r1r2rfrr@r)rrrrrs  rc@s@eZdZeddZddZdZddZdS) ValueTerminalcCs|S)Nr)rrrrrszValueTerminal.valuecCsdS)NFr)rrrrr@szValueTerminal.startswith_fwsFcCstjt||S)N)rPrQr )rrRrrrrTszValueTerminal.as_encoded_wordN)r0r1r2rfrr@r)rTrrrrrs  rc@sFeZdZeddZeddZddZdZdS) EWWhiteSpaceTerminalcCsdS)Nr#r)rrrrr!szEWWhiteSpaceTerminal.valuecCs|ddS)Nr)rrrrr%szEWWhiteSpaceTerminal.encodedcCsdS)Nr#r)rrrrr%)szEWWhiteSpaceTerminal.__str__TN)r0r1r2rfrrr%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_finderr5rrZNonPrintableDefectrrrX)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$)rendcharsZfragmentryZvcharsescapehad_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. NrE)rrr()rZnewvaluerErrrget_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(rr5rrrrPrrrRrrWSPrrrr) rZewrryZremstrrrrRrr5r,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'rEz&missing whitespace before encoded wordFrz encoded-wordrr#rIrIrI)rlrrrrrrrr(rFr5rrrrrr$)rrmr,Zhave_wsrryrrrrrns:       rncCs;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  rcCs;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)rrrrrr get_qcontents  rcCsot|}|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)rmrrrr get_atext s   r c 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<rrrrr5rrr)rZbare_quoted_stringr,rrrget_bare_quoted_strings,   r cCs |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_commentrrr5r)rrir,rrrr <s"   r cCstt}x^|ri|dtkri|dtkrGt|\}}nt|\}}|j|q W||fS)z,CFWS = (1*([FWS] comment) [FWS]) / FWS r)r| CFWS_LEADERrrr r)rrHr,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=?) r~rrr 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 '{}'rI)rrrrr<r rr)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) rvrrrrr5r PHRASE_ENDSrObsoleteHeaderDefectrr)rrwr,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 wordsrHz!Invalid leading '.' in local partrz"Invalid trailing '.' in local partzinvalid-obs-local-partrIrIrIr) rrr5rrrrrrFrrrr)rrZlast_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)rrr5rrrr)rrrrrr get_dtextVs   rcCs:|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)rrr,rrrget_domain_literalrsD             r"c Cst}d}|dtkr1t|\}}|sOtjdj||ddkrt|\}}|dk r|g|dd<|j|||fSyt|\}}Wn'tjk rt |\}}YnX|r |ddkr tjd|dk r+|g|dd<|j||r|ddkr|j jtj d|dj d kr|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<rr5rr$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)rrrr5)rrr,rrrget_display_name&s  r,cCs5t}d}|dtkrOt|\}}|sOtjdj||ddkr|dtkrtjdj|t|\}}|stjdj||dk r|g|ddd.zinvalid-mailbox) rr-rrr$r<r[r>rFr)rrr,rrr get_mailboxSs    r.cCst}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_mailboxhs  r/c 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-mailboxrI)rr.rrrrrr5rr/rrFrr&)rZ mailbox_listr,rrrrrget_mailbox_listzsN              r1cCsct}|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-listr0zgroup-list with empty entries) rr5rrrrrr1r(rrr)rZ group_listrr,rrrget_group_lists8               r2cCst}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'Nr0zgroup-terminatorzend of header in groupz)expected ';' at end of group but found {}) rr,rrr<rrr2r5rrr)rrr,rrr get_groups2       r3cCst}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 '{}')rr3rrr.r<r)rrr,rrr get_addresss  r4cCs_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-separatorrI)rr4rrrrrr5rr/rrrFrr)rZ address_listr,errrrrrrget_address_list sN           r6cCsst}|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)rr5rrHeaderMissingRequiredValuerrisdigitrr<rintrr)rZ mime_versionr,r7rrrparse_mime_versionQ 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. rr0zmisplaced-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)rr r=rrr get_ttext s   r?cCst}|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<r?)rZmtokenr,rrr get_token s    rAcCsot|}|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)rr rrrr get_attrtext s   rCcCst}|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<rC)rrr,rrr get_attribute s    rEcCsot|}|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)rr rrrrget_extended_attrtext s   rGcCst}|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<rG)rrr,rrrget_extended_attribute s    rIcCs3t}| 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 0r7) rrrr<rrr9r5ZInvalidHeaderErrorr:r)rrr7rrr get_section# s$     rLcCst}|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<rrIr)rvrr,rrr get_valueA s      rNc 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 r| st||}||fS|jjtjd|s|jjtjd|j||d kr(||fSn7|d k rDx|D]} | jdkrPqW| jdk|j| | j|_|ddkrltj dj||jt dd|dd }|r|ddkrt|\}}|j||j|_| s|ddkrtj dj||jt dd|dd }|d k rt} x|r|dtkrkt|\}}nD|dd krt d d}|dd }nt|\}}| j|q@W| }nt|\}}|j||d k r | st||}||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. rr0z)Parameter contains name ({}) but no valuerJTzIncomplete 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 {}ZDQUOTE)rrErr5rrr<rLrrrrrrrrrrCrGrFrNAssertionErrorrrRrrrrr) rrr,rryZappendtoZqstringZ inner_valueZ semi_validrtrMrrr get_parameterW s                                         rScCst}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. Nrr0zparameter entry with no contentzinvalid parameter {!r}r'zinvalid-parameterz)parameter with invalid trailing text {!r}zparameter-separatorrI)rrSrrrrrr5rr<r<rFrr)rZmime_parametersr,r5rrrrrparse_mime_parameters sD       &   rTcCsxv|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 rr0zmisplaced-specialr'Nzparameter-separator)rrrrrT)Z tokenlistrr,rrr_find_mime_parameters srUcCs;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}r0zrBrFrrrrrnrrr r r rrrrrrrrrrr r"r#r$r(r+r,r-r.r/r1r2r3r4r6r;r<r?rArCrErGrIrLrNrSrTrUrXrYrZrrrrDs         T K`     '# U3 $ 0 0 0 !  * 8           & ' /   ' &  ) .     9 %   > D          4  9