3 fc@sdZddlmZmZmZddlmZddlZddlZddl Z ddl m Z ddl mZddlmZddlZddlZddlZdd d d d d dddddddddgZdadaddZddZddZddZeadadejdddfd 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 Cs\|dkrtj}t|dr |j}nd}t|tr4|j}d|krDd|d<|ddkr t|d\}}|dkrxt|||dS|d krt |d }t r|t krt d |j |j |} nd } d |kr| r| d} | |d } n*tjddr| r| d} | tjd} | |d <nencodelocalegetpreferredencodingrr7rr TypeErrorrZr5r8r@rr6r*errorsrXryrt outerboundary bytes_readlimitrr|r}rMrw _binary_filer{ innerboundaryr9r;r:lengthrxrzdoneread_urlencoded read_multi read_single)rqr@rZrrArBrCrr*rrr/methodrGZcdisprErDZclenr"r"r#rrs/                         zFieldStorage.__init__c Cs(y|jjWntk r"YnXdS)N)rzr(AttributeError)rqr"r"r#__del__;szFieldStorage.__del__cCs|S)Nr")rqr"r"r# __enter__AszFieldStorage.__enter__cGs|jjdS)N)rzr()rqr'r"r"r#__exit__DszFieldStorage.__exit__cCsd|j|j|jfS)NzFieldStorage(%r, %r, %r))rMrwrp)rqr"r"r#rsGszFieldStorage.__repr__cCs t|jS)N)iterkeys)rqr"r"r#__iter__LszFieldStorage.__iter__cCsT|dkrt||jr:|jjd|jj}|jjdn|jdk rL|j}nd}|S)Nrpr)rrzseekr<rx)rqrMrpr"r"r# __getattr__Os   zFieldStorage.__getattr__cCsb|jdkrtdg}x"|jD]}|j|kr|j|qW|sFt|t|dkrZ|dS|SdS)Nz not indexabler3r)rxrrMrUKeyErrorrb)rqr]founditemr"r"r# __getitem__\s    zFieldStorage.__getitem__cCs8||kr0||}t|tr(dd|DS|jSn|SdS)NcSsg|] }|jqSr")rp).0xr"r"r# osz)FieldStorage.getvalue..)r7rxrp)rqr]defaultrpr"r"r#getvaluejs  zFieldStorage.getvaluecCs4||kr,||}t|tr$|djS|jSn|SdS)Nr)r7rxrp)rqr]rrpr"r"r#getfirstus   zFieldStorage.getfirstcCs:||kr2||}t|tr(dd|DS|jgSngSdS)NcSsg|] }|jqSr")rp)rrr"r"r#rsz(FieldStorage.getlist..)r7rxrp)rqr]rpr"r"r#getlists   zFieldStorage.getlistcCs*|jdkrtdttdd|jDS)Nz not indexablecss|] }|jVqdS)N)rM)rrr"r"r# sz$FieldStorage.keys..)rxrset)rqr"r"r#rs zFieldStorage.keyscs*|jdkrtdtfdd|jDS)Nz not indexablec3s|]}|jkVqdS)N)rM)rr)r]r"r#rsz,FieldStorage.__contains__..)rxrany)rqr]r")r]r# __contains__s zFieldStorage.__contains__cCs t|jS)N)rbr)rqr"r"r#__len__szFieldStorage.__len__cCs|jdkrtdt|jS)NzCannot be converted to bool.)rxrbool)rqr"r"r#__bool__s zFieldStorage.__bool__c Cs|jj|j}t|ts0td|jt|jf|j|j |j }|j rT|d|j 7}t j j||j|j|j |j |j|jd}dd|D|_|jdS)Nz%s should return bytes, got %sr2)r*rrr/cSsg|]\}}t||qSr")r )rr]rpr"r"r#rsz0FieldStorage.read_urlencoded..)r@r<rr7rXr;ryrtr=r*rrr?r r rBrCrr/rx skip_lines)rqrGqueryr"r"r#rs  zFieldStorage.read_urlencodedc CsB|j}t|std|fg|_|jrftjj|j|j|j |j |j |j |j d}|jjdd|D|jpp|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| sBP|jt| 7_| j| j|j |j | j} d| kr| d=||j| |||||j|j|j |j ||j d } |dk r|d 8}| jr|t| j8}|d krtd |j| j7_|jj| | j s.|j|j!ko&d knr Pq W|j"dS) Nz&Invalid boundary in multipart form: %r)r*rrr/css|]\}}t||VqdS)N)r )rr]rpr"r"r#rsz*FieldStorage.read_multi..z%s should return bytes, got %ss--rJzcontent-length)r/r3rzMax number of fields exceeded)#rrPr;rxrr?r r rBrCr*rrr/extendFieldStorageClass __class__r@rRr7rXryrtrrbrcrZfeedr=r(rrUrrr)rqrArBrCZibrklassZ first_linerparserZhdr_textrYrZpartr"r"r#rsj               (zFieldStorage.read_multicCs4|jdkr|j|jn|j|jjddS)Nr)r read_binaryr read_linesrzr)rqr"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 %sr3rN) make_filerzrr@r<minbufsizer7rXr;ryrtrrbrr&)rqZtodorYr"r"r#rs    zFieldStorage.read_binarycCs@|jrt|_|_nt|_|_|jr4|jn|jdS)N)rrrz_FieldStorage__filerrread_lines_to_outerboundaryread_lines_to_eof)rqr"r"r#rs  zFieldStorage.read_linescCsv|jdk rF|jjt|dkrF|j|_|jj}|jj|d|_|jrZ|jj|n|jj|j|j |j dS)Ni) rtellrbrrzrr&rr=r*r)rqr\rYr"r"r#Z__writes    zFieldStorage.__writecCs>x8|jjd}|jt|7_|s,d|_P|j|qWdS)Nr3irN)r@rRrrbr_FieldStorage__write)rqr\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--rJTrr3r s rHrLFirNrOrNrN) rrr@rRrrbrrSrTendswithr) rq next_boundary last_boundaryZdelimlast_line_lfendZ_readr\ strippedlineZodelimr"r"r#r6sP          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--Tr3rrLirN)rrr@rRrrbrrc)rqrrrr\rr"r"r#rgs&  zFieldStorage.skip_linescCs&|jrtjdStjd|jddSdS)Nzwb+zw+r%)r*newline)rtempfileZ TemporaryFiler*)rqr"r"r#r}s zFieldStorage.make_file)N)Ni ) rtrurvosrArrrrrrsrrrrrrrrrrrrrrrrrrrrrrr"r"r"r#r ls>+"   D   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#rfsztest..fcSs |dS)Nr")rfr"r"r#gsztest..gz9

What follows is a test, not an actual exception:

z*

Second try with a small maxlen...

2) printr4stdoutstderrr rrrrrrr:)rAformrfrr"r"r#rs4    cCsx|dkrtj\}}}ddl}ttd|j|||j||}tdtjdj|ddtj|df~dS)Nrz+

Traceback (most recent call last):

z
%s%s
rr3rNrN) r4exc_info tracebackr format_tbformat_exception_onlyhtmlrrV)ryrptbrrrxr"r"r#rs cCs`t|j}ttdtdx*|D]"}tdtj|dtj||q(WtdtdS)Nz

Shell Environment:

z
z
z
z
)sortedrrrr)rArr]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
: )rezzz
z
)rrrrrreprry)rrr]rpr"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:

)rr4r>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 insteadrH) stacklevelr2z&z>r_z")rrIrl)rdZquoter"r"r#r1s     cCs(ddl}t|trd}nd}|j||S)Nrs^[ -~]{0,200}[!-~]$z^[ -~]{0,200}[!-~]$)rer7rXmatch)rdrZ _vb_patternr"r"r#rP=s  rP__main__)rrN)rrN)NNNN)N)0 __version__iorrr collectionsrr4rZ urllib.parser?Z email.parserrZ email.messagerwarningsrrrr__all__rrr$r rr)rr:rAr r r rrgrr r rrrrrrrrrPrtr"r"r"r#s`    # E  c 7'   /