^ bc@sdZdZddlmZddlZddlZddlZddlZddlm Z m Z m Z e Iej re dde e dde nddlZddlZWdQXydd lmZWn!ek rdd lmZnXd d d d ddddddddddddddgZdZeadZdZdZead aeejd d d!Zd d d"Zd d ed#Zd$Z d%Z!d&Z"d fd'YZ#d fd(YZ$d ejfd)YZ%d e%fd*YZ&de&fd+YZ'de%fd,YZ(ejd-Z)eeeed.Z*ejd/Z+d0Z,d1Z-d2Z.d3Z/ed4Z0d5d6Z1e2d7kre)ndS(8sSupport module for CGI (Common Gateway Interface) scripts. This module defines a number of utilities for use by CGI scripts written in Python. s2.6i(t attrgetterN(tfilterwarningstcatch_warningstwarntignores.*mimetools has been removeds.*rfc822 has been removed(tStringIOtMiniFieldStoraget FieldStoragetFormContentDicttSvFormContentDicttInterpFormContentDictt FormContenttparsetparse_qst parse_qsltparse_multipartt parse_headertprint_exceptiont print_environt print_formtprint_directorytprint_argumentstprint_environ_usagetescapetcGsZtr7t r7yttdaWq7tk r3q7XntsFtantat|dS(sWrite a log message, if there is a log file. Even though this function is called initlog(), you should always use log(); log is a variable that is set either to initlog (initially), to dolog (once the log file has been opened), or to nolog (when logging is disabled). The first argument is a format string; the remaining arguments (if any) are arguments to the % operator, so e.g. log("%s: %s", "a", "b") will write "a: b" to the log file, followed by a newline. If the global logfp is not None, it should be a file object to which log data is written. If the global logfp is None, the global logfile may be a string giving a filename to open, in append mode. This file should be world writable!!! If the file can't be opened, logging is silently disabled (since there is no safe place where we could send an error message). taN(tlogfiletlogfptopentIOErrortnologtlogtdolog(tallargs((s(/opt/alt/python27/lib64/python2.7/cgi.pytinitlogGs   cGstj||ddS(s=Write a log message to the log file. See initlog() for docs.s N(Rtwrite(tfmttargs((s(/opt/alt/python27/lib64/python2.7/cgi.pyR jscGsdS(s9Dummy function, assigned to log when logging is disabled.N((R!((s(/opt/alt/python27/lib64/python2.7/cgi.pyRnsicCs|d krtj}nd|kr1d|dR5R(R=R8R9tmax_num_fields((s(/opt/alt/python27/lib64/python2.7/cgi.pyRs cCsd}d|kr|d}nt|s>td|fnd|}d|d}i}d}x'||krd}d}|rtj|} | jd} | ryt| }Wqtk rqXn|dkr tr|tkrtdn|j|}qd}ng} xb|j } | s8|}Pn| d dkrm| j }|||fkrmPqmn| j | qW|dkrqen|dkr| r| d} | d d kr| d } n| dd kr| d } n| | ds    cCs8|jdkrtdnttd|jDS(sDictionary style keys() method.s not indexablecss|]}|jVqdS(N(RA(t.0R((s(/opt/alt/python27/lib64/python2.7/cgi.pys MsN(RmR-Rtset(Rf((s(/opt/alt/python27/lib64/python2.7/cgi.pyRIs cs8|jdkrtdntfd|jDS(s"Dictionary style has_key() method.s not indexablec3s|]}|jkVqdS(N(RA(RR(RS(s(/opt/alt/python27/lib64/python2.7/cgi.pys SsN(RmR-Rtany(RfRS((RSs(/opt/alt/python27/lib64/python2.7/cgi.pythas_keyOs cs8|jdkrtdntfd|jDS(s%Dictionary style __contains__ method.s not indexablec3s|]}|jkVqdS(N(RA(RR(RS(s(/opt/alt/python27/lib64/python2.7/cgi.pys YsN(RmR-RR(RfRS((RSs(/opt/alt/python27/lib64/python2.7/cgi.pyt __contains__Us cCst|jS(s Dictionary style len(x) support.(RYR(Rf((s(/opt/alt/python27/lib64/python2.7/cgi.pyt__len__[scCs t|jS(N(tboolRm(Rf((s(/opt/alt/python27/lib64/python2.7/cgi.pyt __nonzero___scCs|jj|j}|jr2|d|j7}ntj||j|j|j}g|D]\}}t ||^qZ|_ |j dS(s+Internal: read data in query string format.R,N( R6R3RxRuR5RR8R9R?RRmt skip_lines(RfR=tqueryRSRe((s(/opt/alt/python27/lib64/python2.7/cgi.pyRzbs +c Cs|j}t|s(td|fng|_|jrtj|j|j|j|j }|jj d|Dd}n|j }|dk r|t |j8}n|j p|j}||ji|||||} x| jstj|j} ||j| |||||} |dk rq|d8}| jrS|t | j8}n|dkrqtdqqn|jj| qW|jdS(s/Internal: read a part that is itself multipart.s&Invalid boundary in multipart form: %rcss$|]\}}t||VqdS(N(R(RRSRe((s(/opt/alt/python27/lib64/python2.7/cgi.pys ysiisMax number of fields exceededN(RwRBR2RmRuR5RR8R9R?textendR-RYtFieldStorageClasst __class__R6Rytrfc822RDRHR( RfR7R8R9tibRRR?tklasstpartRP((s(/opt/alt/python27/lib64/python2.7/cgi.pyR{ns@                   cCsD|jdkr&|j|jn |j|jjddS(sInternal: read an atomic part.iN(Rxt read_binaryRt read_linesRoR(Rf((s(/opt/alt/python27/lib64/python2.7/cgi.pyR|s    iicCs|jd|_|j}|dkrxd|dkr|jjt||j}|sgd|_Pn|jj||t |}q*WndS(sInternal: read binary data.tbiiN( t make_fileRoRxR6R3tmintbufsizeRyR#RY(RfttodoRO((s(/opt/alt/python27/lib64/python2.7/cgi.pyRs   cCs7t|_|_|jr)|jn |jdS(s0Internal: read lines until EOF or outerboundary.N(RRot_FieldStorage__fileRvtread_lines_to_outerboundarytread_lines_to_eof(Rf((s(/opt/alt/python27/lib64/python2.7/cgi.pyRs  cCs||jdk rh|jjt|dkrh|jd|_|jj|jjd|_qhn|jj|dS(NiR(RR-ttellRYRRoR#R(RfRR((s(/opt/alt/python27/lib64/python2.7/cgi.pyt__writes cCs=x6|jjd}|s(d|_Pn|j|qWdS(sInternal: read lines until EOF.iiiNi(R6RFRyt_FieldStorage__write(RfRR((s(/opt/alt/python27/lib64/python2.7/cgi.pyRs  cCs^d|j}|d}d}t}x4|jjd }|sKd|_Pn|dkrj||}d}n|d dkr|r|j}||krPn||krd|_Pqn|}|dd krd }|d }t}n^|dd krd }|d }t}n5|ddkr9d}|d }t}n d}t}|j||q&Wd S( s)Internal: read lines until outerboundary.s--Riiis iis s Ni(RvtTrueR6RFRyRGtFalseR(RfR_tlasttdelimtlast_line_lfendRRt strippedlinetodelim((s(/opt/alt/python27/lib64/python2.7/cgi.pyRsF                cCs|j s|jrdSd|j}|d}t}x|jjd}|s\d|_Pn|d dkr|r|j}||krPn||krd|_Pqn|jd}q7WdS( s5Internal: skip lines until outer boundary if defined.Ns--iiiis i(RvRyRR6RFRGtendswith(RfR_RRRRR((s(/opt/alt/python27/lib64/python2.7/cgi.pyRs$       cCsddl}|jdS(sOverridable: return a readable & writable file. The file will be used as follows: - data is written to it - seek(0) - data is read from it The 'binary' argument is unused -- the file is always opened in binary mode. This version opens a temporary file for reading and writing, and immediately deletes (unlinks) it. The trick (on Unix!) is that the file can still be used, but it can't be opened by another process, and it will automatically be deleted when it is closed or when the current process terminates. If you want a more permanent file, you derive a class which overrides this method. If you want a visible temporary file that is nevertheless automatically deleted when the script terminates, try defining a __del__ method in a derived class which unlinks the temporary files you have created. iNsw+b(ttempfilet TemporaryFile(RftbinaryR((s(/opt/alt/python27/lib64/python2.7/cgi.pyRs Ni (RiRjRkR-tosR7RgRhRRRRRRRRRRRRzRR{R|RRRRRRRR(((s(/opt/alt/python27/lib64/python2.7/cgi.pyRas8)   w          *    ) cBs#eZdZejdddZRS(sxForm content as dictionary with a list of values per field. form = FormContentDict() form[key] -> [value, value, ...] key in form -> Boolean form.keys() -> [key, key, ...] form.values() -> [[val, val, ...], [val, val, ...], ...] form.items() -> [(key, [val, val, ...]), (key, [val, val, ...]), ...] form.dict == {key: [val, val, ...], ...} icCs6td|d|d||_|_|d|_dS(NR7R8R9R+(R tdictROt query_string(RfR7R8R9((s(/opt/alt/python27/lib64/python2.7/cgi.pyRg4s (RiRjRkRR7Rg(((s(/opt/alt/python27/lib64/python2.7/cgi.pyR's cBs2eZdZdZdZdZdZRS(sForm content as dictionary expecting a single value per field. If you only expect a single value for each field, then form[key] will return that single value. It will raise an IndexError if that expectation is not true. If you expect a field to have possible multiple values, than you can use form.getlist(key) to get all of the values. values() and items() are a compromise: they return single strings where there is a single value, and lists of strings otherwise. cCs4t|j|dkr%tdn|j|dS(Nisexpecting a single valuei(RYRt IndexError(RfRS((s(/opt/alt/python27/lib64/python2.7/cgi.pyRGs cCs |j|S(N(R(RfRS((s(/opt/alt/python27/lib64/python2.7/cgi.pyRKscCsWg}xJ|jjD]9}t|dkrB|j|dq|j|qW|S(Nii(RtvaluesRYRH(RftresultRe((s(/opt/alt/python27/lib64/python2.7/cgi.pyRMs cCsig}x\|jjD]K\}}t|dkrN|j||dfq|j||fqW|S(Nii(RtitemsRYRH(RfRRSRe((s(/opt/alt/python27/lib64/python2.7/cgi.pyRTs (RiRjRkRRRR(((s(/opt/alt/python27/lib64/python2.7/cgi.pyR ;s     cBs)eZdZdZdZdZRS(s7This class is present for backwards compatibility only.cCsstj||}|ddkriyt|SWqitk reyt|SWqftk raqfXqiXn|jS(Nis 0123456789+-.(R RR0R2tfloatRG(RfRStv((s(/opt/alt/python27/lib64/python2.7/cgi.pyR_s  cCs^g}xQ|jD]C}y|j||Wqtk rU|j|j|qXqW|S(N(RRHRR(RfRRS((s(/opt/alt/python27/lib64/python2.7/cgi.pyRgs cCsjg}x]|jD]O}y|j|||fWqtk ra|j||j|fqXqW|S(N(RRHRR(RfRRS((s(/opt/alt/python27/lib64/python2.7/cgi.pyRos "(RiRjRkRRR(((s(/opt/alt/python27/lib64/python2.7/cgi.pyR ]s  cBsDeZdZdZdZdZdZdZdZRS(s7This class is present for backwards compatibility only.cCs"||jkr|j|SdSdS(N(RR-(RfRS((s(/opt/alt/python27/lib64/python2.7/cgi.pyR{s cCsF||jkr>t|j||kr7|j||SdSndSdS(N(RRYR-(RfRStlocation((s(/opt/alt/python27/lib64/python2.7/cgi.pyt indexed_value~s cCs&||jkr|j|dSdSdS(Ni(RR-(RfRS((s(/opt/alt/python27/lib64/python2.7/cgi.pyRescCst|j|S(N(RYR(RfRS((s(/opt/alt/python27/lib64/python2.7/cgi.pyRxscCs,||jkr$|j|djSdSdS(Ni(RRGR-(RfRS((s(/opt/alt/python27/lib64/python2.7/cgi.pytstrippedscCs|jS(N(R(Rf((s(/opt/alt/python27/lib64/python2.7/cgi.pytparss( RiRjRkRRReRxRR(((s(/opt/alt/python27/lib64/python2.7/cgi.pyR ys     cCsdGHHtjt_yWt}ttt|t|td}|d}dGH|Wnt nXdGHda y/t}ttt|t|Wnt nXdS(sRobust test CGI script, usable as main program. Write minimal HTTP headers and dump all information provided to the script in HTML form. sContent-type: text/htmlcRs ddUdS(Ns,testing print_exception() -- italics?((((s(/opt/alt/python27/lib64/python2.7/cgi.pyR\scSs |dS(N((R\((s(/opt/alt/python27/lib64/python2.7/cgi.pytgss9

What follows is a test, not an actual exception:

s*

Second try with a small maxlen...

i2N( R.tstdouttstderrRRRRRRRR1(R7tformR\R((s(/opt/alt/python27/lib64/python2.7/cgi.pyttests4          cCs|dkr$tj\}}}nddl}HdGH|j|||j||}dtdj|d t|dfGH~dS(Nis+

Traceback (most recent call last):

s
%s%s
R(R-R.texc_infot tracebackt format_tbtformat_exception_onlyRRI(RnRettbtlimitRRm((s(/opt/alt/python27/lib64/python2.7/cgi.pyRs  cCs]|j}|jHdGHdGHx/|D]'}dGt|GdGt||GHq(WdGHHdS(s#Dump the shell environment as HTML.s

Shell Environment:

s
s
s
s
N(RtsortR(R7RRS((s(/opt/alt/python27/lib64/python2.7/cgi.pyRs   %cCs|j}|jHdGH|s*dGHndGHx^|D]V}dt|dG||}dttt|dGHdtt|GHq6Wd GHHd S( s$Dump the contents of a form as HTML.s

Form Contents:

s

No form fields.s

s
t:sss
s
N(RRRtreprRn(RRRSRe((s(/opt/alt/python27/lib64/python2.7/cgi.pyRs    cCsTHdGHytj}Wn+tjk rC}dGtt|GHn Xt|GHHdS(s#Dump the current directory as HTML.s#

Current Working Directory:

s os.error:N(RtgetcwdterrorRtstr(tpwdtmsg((s(/opt/alt/python27/lib64/python2.7/cgi.pyRs cCsHdGHHtjGHHdS(Ns

Command Line Arguments:

(R.R4(((s(/opt/alt/python27/lib64/python2.7/cgi.pyRs cCs dGHdS(s9Dump a list of environment variables used by CGI as HTML.s

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
N((((s(/opt/alt/python27/lib64/python2.7/cgi.pyRs)cCsU|jdd}|jdd}|jdd}|rQ|jdd}n|S( sReplace special characters "&", "<" and ">" to HTML-safe sequences. If the optional flag quote is true, the quotation mark character (") is also translated.R,s&ts>RVs"(Ra(RZtquote((s(/opt/alt/python27/lib64/python2.7/cgi.pyR's s^[ -~]{0,200}[!-~]$cCsddl}|j||S(Ni(tretmatch(RZt _vb_patternR((s(/opt/alt/python27/lib64/python2.7/cgi.pyRB2s t__main__(3Rkt __version__toperatorRR.RtUserDictR5twarningsRRRt py3kwarningtDeprecationWarningRCRt cStringIORt ImportErrort__all__RR-RR"R RRR1R7R R RRR]RRRRR R R RRRRRRRRRBRi(((s(/opt/alt/python27/lib64/python2.7/cgi.pytsp                  #  8 b "'   /