3 \@sdZddlmZmZmZddlmZddlZddlZddl Z ddl m Z ddl mZddlmZddlZddlZddlZdd d d d d dddddddddgZdadaddZddZddZddZeadadejddfd d Zd4d!d Zd5d"d Z d#d Z!d$d%Z"d&dZ#Gd'ddZ$Gd(d d Z%ejfd)dZ&d6d*dZ'ejfd+dZ(d,dZ)d-dZ*d.dZ+d/dZ,d7d0dZ-d1d2Z.e/d3kre&dS)8z2.6)StringIOBytesIO TextIOWrapper)MappingN) FeedParser)Message)warnMiniFieldStorage FieldStorageparseparse_qs parse_qslparse_multipart parse_headertestprint_exception print_environ print_formprint_directoryprint_argumentsprint_environ_usageescapec GsHtr.t r.yttdaWntk r,YnXts8tantat|dS)Na)logfilelogfpopenOSErrornologlogdolog)allargsr"/usr/lib64/python3.6/cgi.pyinitlog:s r$cGstj||ddS)N )rwrite)Zfmtargsr"r"r#r ]sr cGsdS)Nr")r!r"r"r#rasrcCsdatrtjdatadS)Nr)rrcloser$rr"r"r"r#closeloges r)c CsX|dkrtj}t|dr |j}nd}t|tr4|j}d|krDd|d<|ddkrt|d\}}|dkrtt||S|dkrt |d }t r|t krt d |j |j |}nd }d |kr|r|d }||d }n*tjddr|r|d }|tjd}||d <nr r )rFrArBr"r"r#r scCstdtdtjj|||S)Nz?cgi.parse_qsl is deprecated, use urllib.parse.parse_qsl insteadrG)rrHr>r r )rFrArBr"r"r#r sc Csddl}d}d|kr|d}t|s2td|fd|}d|d}i}d}x||krd}d} |r|jj|} | jd} | ry t| }Wntk rYnX|dkrtr|tkrtd|j|} nd} g} xD|j } | s|}P| j dr | j }|||fkr P| j | qW| dkr&qR|dkr| r| d} | ddd kr^| dd} n| ddd kr|| dd} | | d<dj | } | d } | sqRt| \}}|d krqRd|krR|d}nqR||kr||j | qR| g||<qRW|S)Nrboundaryz&Invalid boundary in multipart form: %rs--r2zcontent-lengthzMaximum content length exceededrGs  zcontent-dispositionz form-datanamerMrNrMrMrM)Z http.clientvalid_boundaryr:ZclientZ parse_headersgetr8r9r;readline startswithrstripappendjoinr)r?rDZhttprJZnextpartZlastpartZpartdict terminatorbytesdataheadersrElineslinekeyZparamsrLr"r"r#rs|                ccsx|dddkr|dd}|jd}x<|dkrd|jdd||jdd|drd|jd|d}q*W|dkrvt|}|d|}|jV||d}qWdS)Nr2;r"z\"rG)findcountlenstrip)sendfr"r"r# _parseparam-s  *  rfcCstd|}|j}i}x|D]}|jd}|dkr|d|jj}||ddj}t|dkr|d|d kodknr|dd }|jddjd d}|||<qW||fS) Nr]=rr2rGr^z\\\z\"rMrM)rf__next__r_rblowerrareplace)r[partsr\rDpirLvaluer"r"r#r9s   ,  c@s<eZdZdZdZdZdZiZdZiZ iZ ddZ ddZ dS)r NcCs||_||_dS)N)rLro)selfrLror"r"r#__init___szMiniFieldStorage.__init__cCsd|j|jfS)NzMiniFieldStorage(%r, %r))rLro)rpr"r"r#__repr__eszMiniFieldStorage.__repr__) __name__ __module__ __qualname__filenamelisttypefile type_options dispositiondisposition_optionsrYrqrrr"r"r"r#r Qsc @seZdZdddejddddddf ddZdd Zd d Zd d ZddZ ddZ ddZ ddZ d:ddZ d;ddZddZddZddZd d!Zd"d#Zd$d%ZdZd&d'Zd(d)ZdszFieldStorage.__enter__cGs|jjdS)N)ryr()rpr'r"r"r#__exit__AszFieldStorage.__exit__cCsd|j|j|jfS)NzFieldStorage(%r, %r, %r))rLrvro)rpr"r"r#rrDszFieldStorage.__repr__cCs t|jS)N)iterkeys)rpr"r"r#__iter__IszFieldStorage.__iter__cCsT|dkrt||jr:|jjd|jj}|jjdn|jdk rL|j}nd}|S)Nror)rryseekr;rw)rprLror"r"r# __getattr__Ls   zFieldStorage.__getattr__cCsb|jdkrtdg}x"|jD]}|j|kr|j|qW|sFt|t|dkrZ|dS|SdS)Nz not indexabler2r)rwrrLrTKeyErrorra)rpr\founditemr"r"r# __getitem__Ys    zFieldStorage.__getitem__cCs8||kr0||}t|tr(dd|DS|jSn|SdS)NcSsg|] }|jqSr")ro).0xr"r"r# lsz)FieldStorage.getvalue..)r6rwro)rpr\defaultror"r"r#getvaluegs  zFieldStorage.getvaluecCs4||kr,||}t|tr$|djS|jSn|SdS)Nr)r6rwro)rpr\rror"r"r#getfirstrs   zFieldStorage.getfirstcCs:||kr2||}t|tr(dd|DS|jgSngSdS)NcSsg|] }|jqSr")ro)rrr"r"r#rsz(FieldStorage.getlist..)r6rwro)rpr\ror"r"r#getlist}s   zFieldStorage.getlistcCs*|jdkrtdttdd|jDS)Nz not indexablecss|] }|jVqdS)N)rL)rrr"r"r# sz$FieldStorage.keys..)rwrset)rpr"r"r#rs zFieldStorage.keyscs*|jdkrtdtfdd|jDS)Nz not indexablec3s|]}|jkVqdS)N)rL)rr)r\r"r#rsz,FieldStorage.__contains__..)rwrany)rpr\r")r\r# __contains__s zFieldStorage.__contains__cCs t|jS)N)rar)rpr"r"r#__len__szFieldStorage.__len__cCs|jdkrtdt|jS)NzCannot be converted to bool.)rwrbool)rpr"r"r#__bool__s zFieldStorage.__bool__cCs|jj|j}t|ts0td|jt|jf|j|j |j }|j rT|d|j 7}t j j||j|j|j |j |jd}dd|D|_|jdS)Nz%s should return bytes, got %sr1)r*rrcSsg|]\}}t||qSr")r )rr\ror"r"r#rsz0FieldStorage.read_urlencoded..)r?r;rr6rWr:rxrsr<r*rrr>r r rArBrrw skip_lines)rprFqueryr"r"r#rs   zFieldStorage.read_urlencodedc Cs8|j}t|std|fg|_|jrbtjj|j|j|j |j |j |j d}|jj dd|D|jpl|j}|jj}t|tstd|jt|jf|jt|7_x6|jd|jkr|r|jj}|jt|7_qW|j }|dk r|t|j8}x&t} d} x$|jj} | | 7} | jsPqW| s>P|jt| 7_| j| j|j |j | j} d| kr~| d=||j| |||||j|j|j |j | } |dk r|d 8}| jr|t| j8}|d krtd |j| j7_|jj| | js$|j|j kod knrPqW|j!dS) Nz&Invalid boundary in multipart form: %r)r*rrcss|]\}}t||VqdS)N)r )rr\ror"r"r#rsz*FieldStorage.read_multi..z%s should return bytes, got %ss--rIzcontent-lengthr2rzMax number of fields exceeded)"rrOr:rwrr>r r rArBr*rrextendFieldStorageClass __class__r?rQr6rWrxrsrrarbrZfeedr<r(rrTrrr)rpr@rArBZibrklassZ first_linerparserZhdr_textrXrYpartr"r"r#rsh              (zFieldStorage.read_multicCs4|jdkr|j|jn|j|jjddS)Nr)r read_binaryr read_linesryr)rpr"r"r#rs   zFieldStorage.read_singleicCs|j|_|j}|dkrxx|dkr|jjt||j}t|tsXt d|jt |j f|j t |7_ |svd|_P|jj||t |}qWdS)Nrz%s should return bytes, got %sr2rM) make_fileryrr?r;minbufsizer6rWr:rxrsrrarr&)rpZtodorXr"r"r#rs    zFieldStorage.read_binarycCs@|jrt|_|_nt|_|_|jr4|jn|jdS)N)rrry_FieldStorage__filerrread_lines_to_outerboundaryread_lines_to_eof)rpr"r"r#rs  zFieldStorage.read_linescCsv|jdk rF|jjt|dkrF|j|_|jj}|jj|d|_|jrZ|jj|n|jj|j|j |j dS)Ni) rtellrarryrr&rr<r*r)rpr[rXr"r"r#Z__writes    zFieldStorage.__writecCs>x8|jjd}|jt|7_|s,d|_P|j|qWdS)Nr2irM)r?rQrrar_FieldStorage__write)rpr[r"r"r#r(s zFieldStorage.read_lines_to_eofc Cs,d|j}|d}d}d}d}x||jkr.P|jjd }|jt|7_|t|7}|sdd |_P|dkrx||}d}|jdr|r|j}||krP||krd|_P|}|j drd}|dd}d}nJ|j d rd }|dd}d}n*|j drd}|dd}d }nd}d }|j ||q"WdS)Ns--rITrr2r s rGrKFirMrNrMrM) rrr?rQrrarrRrSendswithr) rp next_boundary last_boundaryZdelimlast_line_lfendZ_readr[ strippedlineZodelimr"r"r#r2sP          z(FieldStorage.read_lines_to_outerboundarycCs|j s|jrdSd|j}|d}d}xh|jjd}|jt|7_|sTd|_P|jdr|r|j}||krtP||krd|_P|jd}q*WdS)Ns--Tr2rrKirM)rrr?rQrrarrb)rprrrr[rr"r"r#rcs&  zFieldStorage.skip_linescCs&|jrtjdStjd|jddSdS)Nzwb+zw+r%)r*newline)rtempfileZ TemporaryFiler*)rpr"r"r#rys zFieldStorage.make_file)N)Ni ) rsrtruosr@rqrrrrrrrrrrrrrrrrrrrrrrrrrrrr"r"r"r#r js>+ !   C   1cCstdttjt_yNt}ttt|t|t dd}|fdd}td|Wnt YnXtdda y&t}ttt|t|Wnt YnXdS) NzContent-type: text/htmlcSs tddS)Nz,testing print_exception() -- italics?)execr"r"r"r#resztest..fcSs |dS)Nr")rer"r"r#gsztest..gz9

What follows is a test, not an actual exception:

z*

Second try with a small maxlen...

2) printr3stdoutstderrr rrrrrrr9)r@formrerr"r"r#rs4    cCsx|dkrtj\}}}ddl}ttd|j|||j||}tdtjdj|ddtj|df~dS)Nrz+

Traceback (most recent call last):

z
%s%s
rr2rMrM) r3exc_info tracebackr format_tbformat_exception_onlyhtmlrrU)rxrotbrrrwr"r"r#rs cCs`t|j}ttdtdx*|D]"}tdtj|dtj||q(WtdtdS)Nz

Shell Environment:

z
z
z
z
)sortedrrrr)r@rr\r"r"r#rs  "cCst|j}ttd|s&tdtdxb|D]Z}tdtj|ddd||}tdtjtt|d td tjt|q4Wtd tdS) Nz

Form Contents:

z

No form fields.z

z
: )rdzzz
z
)rrrrrreprrx)rrr\ror"r"r#rs  cCsjttdy tj}Wn6tk rP}ztdtjt|WYdd}~XnXttj|tdS)Nz#

Current Working Directory:

zOSError:)rrgetcwdrrrstr)pwdmsgr"r"r#rs &cCs(ttdtttjtdS)Nz

Command Line Arguments:

)rr3r=r"r"r"r#rs  cCs tddS)Na

These environment variables could have been set:

  • AUTH_TYPE
  • CONTENT_LENGTH
  • CONTENT_TYPE
  • DATE_GMT
  • DATE_LOCAL
  • DOCUMENT_NAME
  • DOCUMENT_ROOT
  • DOCUMENT_URI
  • GATEWAY_INTERFACE
  • LAST_MODIFIED
  • PATH
  • PATH_INFO
  • PATH_TRANSLATED
  • QUERY_STRING
  • REMOTE_ADDR
  • REMOTE_HOST
  • REMOTE_IDENT
  • REMOTE_USER
  • REQUEST_METHOD
  • SCRIPT_NAME
  • SERVER_NAME
  • SERVER_PORT
  • SERVER_PROTOCOL
  • SERVER_ROOT
  • SERVER_SOFTWARE
In addition, HTTP headers sent by the server may be passed in the environment as well. Here are some common variable names:
  • HTTP_ACCEPT
  • HTTP_CONNECTION
  • HTTP_HOST
  • HTTP_PRAGMA
  • HTTP_REFERER
  • HTTP_USER_AGENT
)rr"r"r"r#rs'cCsFtdtdd|jdd}|jdd}|jdd }|rB|jd d }|S) Nz1cgi.escape is deprecated, use html.escape insteadrG) stacklevelr1z&z>r^z")rrHrk)rcZquoter"r"r#r-s     cCs(ddl}t|trd}nd}|j||S)Nrs^[ -~]{0,200}[!-~]$z^[ -~]{0,200}[!-~]$)rer6rWmatch)rcrZ _vb_patternr"r"r#rO9s  rO__main__)rr)rr)NNNN)N)0 __version__iorrr collectionsrr3rZ urllib.parser>Z email.parserrZ email.messagerwarningsrrrr__all__rrr$r rr)rr9r@r r r rrfrr r rrrrrrrrrOrsr"r"r"r#s^    # E  d 5'   /