łň Ąl‡Jc@sadZdZddklZddkZddkZddkZddkZddkZddk Z yddk l Z Wn#e j oddk l Z nXdddd d d d d ddddddddddgZ dZead„Zd„Zd„Zeadaeeiddd„Zddd„Zddd„Zd „Zd!„Zdfd"„ƒYZdfd#„ƒYZde i fd$„ƒYZd efd%„ƒYZd efd&„ƒYZ d efd'„ƒYZ!eid(„Z"eeeed)„Z#eid*„Z$d+„Z%d,„Z&d-„Z'd.„Z(ed/„Z)d0d1„Z*e+d2jo e"ƒndS(3sSupport 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(tStringIOtMiniFieldStoraget FieldStoragetFormContentDicttSvFormContentDicttInterpFormContentDictt FormContenttparsetparse_qst parse_qsltparse_multipartt parse_headertprint_exceptiont print_environt print_formtprint_directorytprint_argumentstprint_environ_usagetescapetcGsato5t o-yttdƒaWq<tj oq<Xntp tantat|ŒdS(s„Write 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/sys/lib/python/cgi.pytinitlog>s  cGsti||dƒdS(s=Write a log message to the log file. See initlog() for docs.s N(Rtwrite(tfmttargs((s/sys/lib/python/cgi.pyRascGsdS(s9Dummy function, assigned to log when logging is disabled.N((R((s/sys/lib/python/cgi.pyResicCsĽ|d jo ti}nd|jod|dg}|idƒD]#}|idƒD] }||q'q~}g}xń|D]é}| o| oqMn|iddƒ} t| ƒdjo:|otd|f‚n|o| idƒqÉqMnt| dƒp|oUti| did d ƒƒ} ti| did d ƒƒ} |i| | fƒqMqMW|S( sžParse a query given as a string argument. Arguments: qs: URL-encoded query string to be parsed keep_blank_values: flag indicating whether blank values in URL encoded queries should be treated as blank strings. A true value indicates that blanks should be retained as blank strings. The default false value indicates that blank values are to be ignored and treated as if they were not included. strict_parsing: flag indicating what to do with parsing errors. If false (the default), errors are silently ignored. If true, errors raise a ValueError exception. Returns a list, as G-d intended. R(t;t=iisbad query field: %rRit+t (tsplittlenR.R9turllibtunquotetreplace( R8R3R4t_[1]ts1ts2tpairstrt name_valuetnvR;R<((s/sys/lib/python/cgi.pyR Âs$@c Cs¸d}d|jo|d}nt|ƒptd|f‚nd|}d|d}h}d}xK||jo=d}d}|ošti|ƒ} | idƒ} | o*yt| ƒ}WqŘtj oqŘXn|djo4to|tjo td‚n|i|ƒ}q#d}ng} xg|i ƒ} | p |}Pn| d djo(| i ƒ}|||fjoPqƒn| i | ƒq,|djoqin|djop| oe| d} | d d jo| d } n | dd jo| d } n| | diii˙˙˙˙t"s\\s\s\"(RARStpoptlowertfindRBRE( R]RFtxtplistR^R6tptiR;R<((s/sys/lib/python/cgi.pyR Ms0 9 "cBsPeZdZdZdZdZdZhZdZ hZ hZ d„Z d„Z RS(s=Like FieldStorage, for use when no file uploads are possible.cCs||_||_dS(s&Constructor from field name and value.N(R;R<(tselfR;R<((s/sys/lib/python/cgi.pyt__init__ss cCsd|i|ifS(s Return printable representation.sMiniFieldStorage(%r, %r)(R;R<(Rh((s/sys/lib/python/cgi.pyt__repr__ysN(t__name__t __module__t__doc__R)tfilenametlistttypetfilet type_optionst dispositiontdisposition_optionsR[RiRj(((s/sys/lib/python/cgi.pyRes cBsţeZdZdddeiddd„Zd„Zd„Zd„Z d„Z dd„Z dd „Z d „Z d „Zd „Zd „Zd„Zd„ZdZd„Zd„ZdZd„Zd„Zd„Zd„Zd„Zd„Zdd„ZRS(sšStore a sequence of fields, reading multipart/form-data. This class provides naming, typing, files stored on disk, and more. At the top level, it is accessible like a dictionary, whose keys are the field names. (Note: None can occur as a field name.) The items are either a Python list (if there's multiple values) or another FieldStorage or MiniFieldStorage object. If it's a single object, it has the following attributes: name: the field name, if specified; otherwise None filename: the filename, if specified; otherwise None; this is the client side filename, *not* the file name on which it is stored (that's a temporary file you don't deal with) value: the value as a *string*; for file uploads, this transparently reads the file every time you request the value file: the file(-like) object from which you can read the data; None if the data is stored a simple string type: the content-type, or None if not specified type_options: dictionary of options specified on the content-type line disposition: content-disposition, or None if not specified disposition_options: dictionary of corresponding options headers: a dictionary(-like) object (sometimes rfc822.Message or a subclass thereof) containing *all* headers The class is subclassable, mostly for the purpose of overriding the make_file() method, which is called internally to come up with a file open for reading and writing. This makes it possible to override the default choice of storing all files in a temporary directory and unlinking them as soon as they have been opened. Ric Cshd}||_||_d|jo|diƒ}n|djp |djopd|jo|d}n&tidotid}nd}t|ƒ}|djohdd<}qĂn|djoch}|d jod|dx7|iidƒ}|pd|_Pn|i|ƒqdS(sInternal: read lines until EOF.iii˙˙˙˙Ni(R1RRRzt_FieldStorage__write(RhR]((s/sys/lib/python/cgi.pyRŚżs  cCs"d|i}|d}d}t}xř|iid ƒ}|pd|_Pn|d djoD|o=|iƒ}||joPn||jod|_Pq˘n|}|ddjod}|d }t}n8|dd jod }|d }t}n d}t}|i||ƒq&d S( s)Internal: read lines until outerboundary.s--Riii˙˙˙˙iiţ˙˙˙s s Ni(RwR‘R1RRRzRSR’RŠ(Rhtnexttlasttdelimtlast_line_lfendR]t strippedlinetodelim((s/sys/lib/python/cgi.pyRĽČs8            cCsĎ|i p |iodSnd|i}|d}t}xŽ|iidƒ}|pd|_Pn|d djoD|o=|iƒ}||joPn||jod|_Pqšn|idƒ}q=dS( s5Internal: skip lines until outer boundary if defined.Ns--iii˙˙˙˙is i(RwRzR‘R1RRRStendswith(RhRŞRŤR­R]RŽ((s/sys/lib/python/cgi.pyR–čs$        cCsddk}|idƒS(sąOverridable: 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. i˙˙˙˙Nsw+b(ttempfilet TemporaryFile(RhtbinaryRą((s/sys/lib/python/cgi.pyR ýs Ni (RkRlRmR)tosR2RiRjRƒR†R‹RŽRRR‚R“R”R•R{R—R|R}R˘RRžRŠRŚRĽR–R (((s/sys/lib/python/cgi.pyR~s4) p            cBseZdZeid„ZRS(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, ...], ...} cCs*td|ƒ|_|_|d|_dS(NR2R'(RR:RZt query_string(RhR2((s/sys/lib/python/cgi.pyRi*s(RkRlRmR´R2Ri(((s/sys/lib/python/cgi.pyRs cBs2eZdZd„Zd„Zd„Zd„ZRS(s÷Form 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. cCs6t|i|ƒdjo td‚n|i|dS(Nisexpecting a single valuei(RBR:t IndexError(RhR^((s/sys/lib/python/cgi.pyR‹;s cCs |i|S(N(R:(RhR^((s/sys/lib/python/cgi.pyR?scCsYg}xL|iiƒD];}t|ƒdjo|i|dƒq|i|ƒqW|S(Nii(R:tvaluesRBR9(RhtresultR<((s/sys/lib/python/cgi.pyRˇAscCskg}x^|iiƒD]M\}}t|ƒdjo|i||dfƒq|i||fƒqW|S(Nii(R:titemsRBR9(RhR¸R^R<((s/sys/lib/python/cgi.pyRšHs (RkRlRmR‹RRˇRš(((s/sys/lib/python/cgi.pyR/s     cBs)eZdZd„Zd„Zd„ZRS(s7This class is present for backwards compatibility only.cCsyti||ƒ}|ddjoLyt|ƒSWqotj o+yt|ƒSWqktj oqkXqoXn|iƒS(Nis 0123456789+-.(RR‹R,R.tfloatRS(RhR^tv((s/sys/lib/python/cgi.pyR‹SscCs`g}xS|iƒD]E}y|i||ƒWqtj o|i|i|ƒqXqW|S(N(R‚R9RśR:(RhR¸R^((s/sys/lib/python/cgi.pyRˇ[s cCslg}x_|iƒD]Q}y|i|||fƒWqtj o!|i||i|fƒqXqW|S(N(R‚R9RśR:(RhR¸R^((s/sys/lib/python/cgi.pyRšcs #(RkRlRmR‹RˇRš(((s/sys/lib/python/cgi.pyRQs  cBsDeZdZd„Zd„Zd„Zd„Zd„Zd„ZRS(s7This class is present for backwards compatibility only.cCs'||ijo|i|SndSdS(N(R:R)(RhR^((s/sys/lib/python/cgi.pyRˇoscCsM||ijo5t|i|ƒ|jo|i||SqIdSndSdS(N(R:RBR)(RhR^tlocation((s/sys/lib/python/cgi.pyt indexed_valuers cCs+||ijo|i|dSndSdS(Ni(R:R)(RhR^((s/sys/lib/python/cgi.pyR<xs#cCst|i|ƒS(N(RBR:(RhR^((s/sys/lib/python/cgi.pyRy{scCs1||ijo|i|diƒSndSdS(Ni(R:RSR)(RhR^((s/sys/lib/python/cgi.pytstripped}s)cCs|iS(N(R:(Rh((s/sys/lib/python/cgi.pytpars€s( RkRlRmRˇR˝R<RyRžRż(((s/sys/lib/python/cgi.pyRms     cCsÉdGHHtit_yWtƒ}tƒtƒt|ƒt|ƒtƒd„}|d„}dGH|ƒWnt ƒnXdGHda y/tƒ}tƒtƒt|ƒt|ƒWnt ƒnXdS(s—Robust 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/sys/lib/python/cgi.pytf˜scSs |ƒdS(N((RŔ((s/sys/lib/python/cgi.pytgšss9

What follows is a test, not an actual exception:

s*

Second try with a small maxlen...

i2N( R*tstdouttstderrRRRRRRR R-(R2tformRŔRÁ((s/sys/lib/python/cgi.pyttest‡s4          cCs|djotiƒ\}}}nddk}HdGH|i||ƒ|i||ƒ}dtdi|d ƒƒt|dƒfGH~dS(Ni˙˙˙˙s+

Traceback (most recent call last):

s
%s%s
R(R)R*texc_infot tracebackt format_tbtformat_exception_onlyRRT(RpR<ttbtlimitRÇRo((s/sys/lib/python/cgi.pyR Žs  cCs]|iƒ}|iƒHdGHdGHx/|D]'}dGt|ƒGdGt||ƒGHq(WdGHHdS(s#Dump the shell environment as HTML.s

Shell Environment:

s
s
s
s
N(R‚tsortR(R2R‚R^((s/sys/lib/python/cgi.pyRźs  %cCsœ|iƒ}|iƒHdGH|p dGHndGHx^|D]V}dt|ƒdG||}dttt|ƒƒƒdGHdtt|ƒƒGHq8Wd GHHd S( s$Dump the contents of a form as HTML.s

Form Contents:

s

No form fields.s

s
t:sss
s
N(R‚RĚRtreprRp(RÄR‚R^R<((s/sys/lib/python/cgi.pyRČs    cCsVHdGHytiƒ}Wn-tij o}dGtt|ƒƒGHn Xt|ƒGHHdS(s#Dump the current directory as HTML.s#

Current Working Directory:

s os.error:N(R´tgetcwdterrorRtstr(tpwdtmsg((s/sys/lib/python/cgi.pyRŮs cCsHdGHHtiGHHdS(Ns

Command Line Arguments:

(R*R0(((s/sys/lib/python/cgi.pyRĺs 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/sys/lib/python/cgi.pyRěs)cCsW|iddƒ}|iddƒ}|iddƒ}|o|iddƒ}n|S( s¤Replace special characters "&", "<" and ">" to HTML-safe sequences. If the optional flag quote is true, the quotation mark character (") is also translated.R(s&ts>R`s"(RE(tstquote((s/sys/lib/python/cgi.pyRs s^[ -~]{0,200}[!-~]$cCsddk}|i||ƒS(Ni˙˙˙˙(tretmatch(RÖt _vb_patternRŘ((s/sys/lib/python/cgi.pyRN&s t__main__(,Rmt __version__toperatorRR*R´RCROR™tUserDictt cStringIORt ImportErrort__all__RR)RRRRRR-R2RR R R R RRRRRRRĹR RRRRRRRNRk(((s/sys/lib/python/cgi.pyss`             #  5) b ˙ "'   /