ž ¬ÿfâŠc@sUdZdZddlmZmZmZddlmZddlZddl Z ddl Z ddl m Z ddlmZddlmZddlZddlZddlZd d d d d dddddddddgZdaeadd„Zdd„Zdd„Zdd„Zeadaee jddd d „Z ddd!d „Z!ddd"d „Z"d#d„Z#d$d%„Z$d&d„Z%Gd'd „d ƒZ&Gd(d „d ƒZ'e jd)d*„Z(eeeed+d„Z)e jd,d„Z*d-d„Z+d.d„Z,d/d„Z-d0d„Z.ed1d„Z/ed2d3„Z0e1d4krQe(ƒndS(5uSupport module for CGI (Common Gateway Interface) scripts. This module defines a number of utilities for use by CGI scripts written in Python. u2.6i(uStringIOuBytesIOu TextIOWrapper(uMappingN(u FeedParser(uMessage(uwarnuMiniFieldStorageu FieldStorageuparseuparse_qsu parse_qsluparse_multipartu parse_headeruprint_exceptionu print_environu print_formuprint_directoryuprint_argumentsuprint_environ_usageuescapeuc Gs[tr8t r8yttdƒaWq8tk r4Yq8XntsGtantat|ŒdS(u„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). uaN(ulogfileulogfpuopenuIOErrorunologulogudolog(uallargs((u(/opt/alt/python33/lib64/python3.3/cgi.pyuinitlog:s   uinitlogcGstj||dƒdS(u=Write a log message to the log file. See initlog() for docs.u N(ulogfpuwrite(ufmtuargs((u(/opt/alt/python33/lib64/python3.3/cgi.pyudolog]sudologcGsdS(u9Dummy function, assigned to log when logging is disabled.N((uallargs((u(/opt/alt/python33/lib64/python3.3/cgi.pyunologasunologcCs)datrtjƒdantadS(uClose the log file.uN(ulogfileulogfpucloseuNoneuinitlogulog(((u(/opt/alt/python33/lib64/python3.3/cgi.pyucloseloges   ucloselogc Csé|dkrtj}nt|dƒr3|j}nd}t|tƒrT|j}nd|krmd|d| d krNdi}} n d i}} ||_| |_#d| kr•| dj|jƒ|_$n d|_$d}d|jkryt%|jdƒ}Wnt&k rÞYnXt'r|t'krt&dƒ‚qn||_(|jdkr-|r-||_nd|_)|_*d|_+|d kr_|j,ƒn6|dd…dkr‹|j-|||ƒn |j.ƒdS(u›Constructor. Read multipart/* until last part. Arguments, all optional: fp : file pointer; default: sys.stdin.buffer (not used when the request method is GET) Can be : 1. a TextIOWrapper object 2. an object whose read() and readline() methods return bytes headers : header dictionary-like object; default: taken from environ as per CGI spec outerboundary : terminating multipart boundary (for internal use only) environ : environment dictionary; default: os.environ keep_blank_values: flag indicating whether blank values in percent-encoded forms 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. limit : used internally to read parts of multipart/form-data forms, to exit from the reading loop when reached. It is the difference between the form content-length and the number of bytes already read encoding, errors : the encoding and error handler used to decode the binary stream to strings. Must be the same as the charset defined for the page sending the form (content-type : meta http-equiv or header) uGETuREQUEST_METHODuHEADu QUERY_STRINGiNuusurrogateescapeu!application/x-www-form-urlencodedu content-typeuPOSTu CONTENT_TYPEuCONTENT_LENGTHucontent-lengthu?headers must be mapping or an instance of email.message.Messageureadureadlineufp must be file pointeru#outerboundary must be bytes, not %siucontent-dispositionunameufilenameu text/plainuboundarysuMaximum content length exceededi u multipart/iÿÿÿÿ(/ukeep_blank_valuesustrict_parsinguupperuNoneu qs_on_postusysuargvuencodeulocaleugetpreferredencodinguBytesIOu isinstanceuMappinguMessageu TypeErroruheadersustdinubufferufpu TextIOWrapperuhasattruencodinguerrorsubytesutypeu__name__u outerboundaryu bytes_readulimitu parse_headeru dispositionudisposition_optionsunameufilenameu _binary_fileu type_optionsu innerboundaryuintu ValueErrorumaxlenulengthulistufileudoneuread_urlencodedu read_multiu read_single(uselfufpuheadersu outerboundaryuenvironukeep_blank_valuesustrict_parsingulimituencodinguerrorsumethoduqsucdispupdictuctypeuclen((u(/opt/alt/python33/lib64/python3.3/cgi.pyu__init__•sª+                                        uFieldStorage.__init__cCsd|j|j|jfS(u"Return a printable representation.uFieldStorage(%r, %r, %r)(unameufilenameuvalue(uself((u(/opt/alt/python33/lib64/python3.3/cgi.pyu__repr__3suFieldStorage.__repr__cCst|jƒƒS(N(uiterukeys(uself((u(/opt/alt/python33/lib64/python3.3/cgi.pyu__iter__8suFieldStorage.__iter__cCs{|dkrt|ƒ‚n|jrV|jjdƒ|jjƒ}|jjdƒn!|jdk rq|j}nd}|S(Nuvaluei(uAttributeErrorufileuseekureadulistuNone(uselfunameuvalue((u(/opt/alt/python33/lib64/python3.3/cgi.pyu __getattr__;s   uFieldStorage.__getattr__cCsŽ|jdkrtdƒ‚ng}x0|jD]%}|j|kr.|j|ƒq.q.W|slt|ƒ‚nt|ƒdkr†|dS|SdS(uDictionary style indexing.u not indexableiiN(ulistuNoneu TypeErrorunameuappenduKeyErrorulen(uselfukeyufounduitem((u(/opt/alt/python33/lib64/python3.3/cgi.pyu __getitem__HsuFieldStorage.__getitem__cCsH||kr@||}t|tƒr6dd„|DƒS|jSn|SdS(u8Dictionary style get() method, including 'value' lookup.cSsg|]}|j‘qS((uvalue(u.0ux((u(/opt/alt/python33/lib64/python3.3/cgi.pyu [s u)FieldStorage.getvalue..N(u isinstanceulistuvalue(uselfukeyudefaultuvalue((u(/opt/alt/python33/lib64/python3.3/cgi.pyugetvalueVs    uFieldStorage.getvaluecCsB||kr:||}t|tƒr0|djS|jSn|SdS(u! Return the first value received.iN(u isinstanceulistuvalue(uselfukeyudefaultuvalue((u(/opt/alt/python33/lib64/python3.3/cgi.pyugetfirstas     uFieldStorage.getfirstcCsK||krC||}t|tƒr6dd„|DƒS|jgSngSdS(u Return list of received values.cSsg|]}|j‘qS((uvalue(u.0ux((u(/opt/alt/python33/lib64/python3.3/cgi.pyu qs u(FieldStorage.getlist..N(u isinstanceulistuvalue(uselfukeyuvalue((u(/opt/alt/python33/lib64/python3.3/cgi.pyugetlistls    uFieldStorage.getlistcCs>|jdkrtdƒ‚nttdd„|jDƒƒƒS(uDictionary style keys() method.u not indexablecss|]}|jVqdS(N(uname(u.0uitem((u(/opt/alt/python33/lib64/python3.3/cgi.pyu {su$FieldStorage.keys..N(ulistuNoneu TypeErroruset(uself((u(/opt/alt/python33/lib64/python3.3/cgi.pyukeyswsuFieldStorage.keyscs>|jdkrtdƒ‚nt‡fdd†|jDƒƒS(u%Dictionary style __contains__ method.u not indexablec3s|]}|jˆkVqdS(N(uname(u.0uitem(ukey(u(/opt/alt/python33/lib64/python3.3/cgi.pyu su,FieldStorage.__contains__..N(ulistuNoneu TypeErroruany(uselfukey((ukeyu(/opt/alt/python33/lib64/python3.3/cgi.pyu __contains__}suFieldStorage.__contains__cCst|jƒƒS(u Dictionary style len(x) support.(ulenukeys(uself((u(/opt/alt/python33/lib64/python3.3/cgi.pyu__len__ƒsuFieldStorage.__len__cCs+|jdkrtdƒ‚nt|jƒS(NuCannot be converted to bool.(ulistuNoneu TypeErrorubool(uself((u(/opt/alt/python33/lib64/python3.3/cgi.pyu__bool__‡suFieldStorage.__bool__cCsõ|jj|jƒ}t|tƒsItd|jt|ƒjfƒ‚n|j|j |j ƒ}|j r~|d|j 7}ng|_ t jj||j|jd|j d|j ƒ}x-|D]%\}}|j jt||ƒƒq¾W|jƒdS(u+Internal: read data in query string format.u%s should return bytes, got %su&uencodinguerrorsN(ufpureadulengthu isinstanceubytesu ValueErrorutypeu__name__udecodeuencodinguerrorsu qs_on_postulistuurllibuparseu parse_qslukeep_blank_valuesustrict_parsinguappenduMiniFieldStorageu skip_lines(uselfuqsuqueryukeyuvalue((u(/opt/alt/python33/lib64/python3.3/cgi.pyuread_urlencodedŒs   uFieldStorage.read_urlencodedc Cs7|j}t|ƒs+td|fƒ‚ng|_|jr©tjj|j|j|j d|j d|j ƒ}x-|D]%\}}|jj t ||ƒƒqwWd}n|jp¸|j} |jjƒ} t| tƒsþtd|jt| ƒjfƒ‚n|jt| ƒ7_xtƒ} d} x,|jjƒ} | | 7} | jƒs(Pq(q(| s^Pn|jt| ƒ7_| j| j|j |j ƒƒ| jƒ}| |j||||||j|j|j |j ƒ }|j|j7_|jj |ƒ|js"|j|j kodknrPqq|j!ƒdS(u/Internal: read a part that is itself multipart.u&Invalid boundary in multipart form: %ruencodinguerrorsu%s should return bytes, got %ssiN("u innerboundaryuvalid_boundaryu ValueErrorulistu qs_on_postuurllibuparseu parse_qslukeep_blank_valuesustrict_parsinguencodinguerrorsuappenduMiniFieldStorageuNoneuFieldStorageClassu __class__ufpureadlineu isinstanceubytesutypeu__name__u bytes_readulenu FeedParserustripufeedudecodeucloseulimitudoneulengthu skip_lines(uselfuenvironukeep_blank_valuesustrict_parsinguibuqueryukeyuvalueuFieldStorageClassuklassu first_lineuparseruhdr_textudatauheadersupart((u(/opt/alt/python33/lib64/python3.3/cgi.pyu read_multiŸsL          +uFieldStorage.read_multicCsD|jdkr&|jƒ|jƒn |jƒ|jjdƒdS(uInternal: read an atomic part.iN(ulengthu read_binaryu skip_linesu read_linesufileuseek(uself((u(/opt/alt/python33/lib64/python3.3/cgi.pyu read_singleÌs    uFieldStorage.read_singleiicCsØ|jƒ|_|j}|dkrÔx­|dkrÐ|jjt||jƒƒ}t|tƒs…t d|jt |ƒj fƒ‚n|j t |ƒ7_ |s­d|_Pn|jj|ƒ|t |ƒ}q'WndS(uInternal: read binary data.iu%s should return bytes, got %siNiÿÿÿÿ(u make_fileufileulengthufpureaduminubufsizeu isinstanceubytesu ValueErrorutypeu__name__u bytes_readulenudoneuwrite(uselfutodoudata((u(/opt/alt/python33/lib64/python3.3/cgi.pyu read_binary×s   uFieldStorage.read_binarycCsV|jrtƒ|_|_ntƒ|_|_|jrH|jƒn |jƒdS(u0Internal: read lines until EOF or outerboundary.N(u _binary_fileuBytesIOufileu_FieldStorage__fileuStringIOu outerboundaryuread_lines_to_outerboundaryuread_lines_to_eof(uself((u(/opt/alt/python33/lib64/python3.3/cgi.pyu read_linesès    uFieldStorage.read_linescCs­|jdk rk|jjƒt|ƒdkrk|jƒ|_|jjƒ}|jj|ƒd|_qkn|jr‡|jj|ƒn"|jj|j |j |j ƒƒdS(u line is always bytes, not stringièN( u_FieldStorage__fileuNoneutellulenu make_fileufileugetvalueuwriteu _binary_fileudecodeuencodinguerrors(uselfulineudata((u(/opt/alt/python33/lib64/python3.3/cgi.pyu__writeós uFieldStorage.__writecCsQxJ|jjdƒ}|jt|ƒ7_|s=d|_Pn|j|ƒqdS(uInternal: read lines until EOF.iiNiiÿÿÿÿ(ufpureadlineu bytes_readulenudoneu_FieldStorage__write(uselfuline((u(/opt/alt/python33/lib64/python3.3/cgi.pyuread_lines_to_eofs uFieldStorage.read_lines_to_eofc Cs©d|j}|d}d}d }d}xy||jkr?Pn|jjd ƒ}|jt|ƒ7_|t|ƒ7}|s‰d |_Pn|dkr¨||}d}n|jdƒrõ|rõ|j ƒ}||krÙPn||krõd|_Pqõn|}|j dƒr)d}|dd…}d }nh|j d ƒrWd }|dd…}d }n:|j dƒr…d}|dd…}d}n d}d}|j ||ƒq,dS(užInternal: read lines until outerboundary. Data is read as bytes: boundaries and line ends must be converted to bytes for comparisons. s--siiis s Nis TiiÿÿÿÿiþÿÿÿiÿÿÿÿiÿÿÿÿF( u outerboundaryuTrueulimitufpureadlineu bytes_readulenudoneu startswithurstripuendswithuFalseu_FieldStorage__write( uselfu next_boundaryu last_boundaryudelimulast_line_lfendu_readulineu strippedlineuodelim((u(/opt/alt/python33/lib64/python3.3/cgi.pyuread_lines_to_outerboundary sP             u(FieldStorage.read_lines_to_outerboundarycCsÔ|j s|jrdSd|j}|d}d}x™|jjdƒ}|jt|ƒ7_|sqd|_Pn|jdƒr¾|r¾|jƒ}||kr¢Pn||kr¾d|_Pq¾n|jdƒ}q7dS( u5Internal: skip lines until outer boundary if defined.Ns--iis Tiiÿÿÿÿ( u outerboundaryudoneuTrueufpureadlineu bytes_readulenuendswithustrip(uselfu next_boundaryu last_boundaryulast_line_lfendulineu strippedline((u(/opt/alt/python33/lib64/python3.3/cgi.pyu skip_lines=s&       uFieldStorage.skip_linescCs6|jrtjdƒStjdd|jddƒSdS(u¯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 file is opened in binary mode for files, in text mode for other fields 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. uwb+uw+uencodingunewlineu N(u _binary_fileutempfileu TemporaryFileuencoding(uself((u(/opt/alt/python33/lib64/python3.3/cgi.pyu make_fileSs   uFieldStorage.make_fileNi (u__name__u __module__u __qualname__u__doc__uNoneuosuenvironu__init__u__repr__u__iter__u __getattr__u __getitem__ugetvalueugetfirstugetlistukeysu __contains__u__len__u__bool__uread_urlencodeduFieldStorageClassu read_multiu read_singleubufsizeu read_binaryu read_linesu_FieldStorage__writeuread_lines_to_eofuread_lines_to_outerboundaryu skip_linesu make_file(u __locals__((u(/opt/alt/python33/lib64/python3.3/cgi.pyu FieldStoragejs6*  œ          -    1 cCsætdƒtƒtjt_ybtƒ}tƒtƒt|ƒt|ƒt ƒdd„}|dd„}tdƒ|ƒWnt ƒYnXtdƒda y/tƒ}tƒtƒt|ƒt|ƒWnt ƒYnXd S( u—Robust test CGI script, usable as main program. Write minimal HTTP headers and dump all information provided to the script in HTML form. uContent-type: text/htmlcSstdƒdS(Nu,testing print_exception() -- italics?(uexec(((u(/opt/alt/python33/lib64/python3.3/cgi.pyuf†sutest..fcSs |ƒdS(N((uf((u(/opt/alt/python33/lib64/python3.3/cgi.pyugˆsutest..gu9

What follows is a test, not an actual exception:

u*

Second try with a small maxlen...

i2N( uprintusysustdoutustderru FieldStorageuprint_directoryuprint_argumentsu print_formu print_environuprint_environ_usageuprint_exceptionumaxlen(uenvironuformufug((u(/opt/alt/python33/lib64/python3.3/cgi.pyutestus4            utestcCs§|dkr$tjƒ\}}}nddl}tƒtdƒ|j||ƒ|j||ƒ}tdtjdj |dd…ƒƒtj|dƒfƒ~dS(Niu+

Traceback (most recent call last):

u
%s%s
uiiÿÿÿÿiÿÿÿÿ( uNoneusysuexc_infou tracebackuprintu format_tbuformat_exception_onlyuhtmluescapeujoin(utypeuvalueutbulimitu tracebackulist((u(/opt/alt/python33/lib64/python3.3/cgi.pyuprint_exceptionœs   cCs|t|jƒƒ}tƒtdƒtdƒx7|D]/}tdtj|ƒdtj||ƒƒq4WtdƒtƒdS(u#Dump the shell environment as HTML.u

Shell Environment:

u
u
u
u
N(usortedukeysuprintuhtmluescape(uenvironukeysukey((u(/opt/alt/python33/lib64/python3.3/cgi.pyu print_environªs   - cCsÕt|jƒƒ}tƒtdƒ|s6tdƒntdƒx}|D]u}tdtj|ƒdddƒ||}tdtjtt|ƒƒƒd ƒtd tjt|ƒƒƒqGWtd ƒtƒd S( u$Dump the contents of a form as HTML.u

Form Contents:

u

No form fields.u

u
u:uendu uuu
u
N(usortedukeysuprintuhtmluescapeureprutype(uformukeysukeyuvalue((u(/opt/alt/python33/lib64/python3.3/cgi.pyu print_formµs    ! '! cCs†tƒtdƒytjƒ}WnDtjk rg}z!tdtjt|ƒƒƒWYdd}~XnXttj|ƒƒtƒdS(u#Dump the current directory as HTML.u#

Current Working Directory:

u os.error:N(uprintuosugetcwduerroruhtmluescapeustr(upwdumsg((u(/opt/alt/python33/lib64/python3.3/cgi.pyuprint_directoryÅs /cCs0tƒtdƒtƒttjƒtƒdS(Nu

Command Line Arguments:

(uprintusysuargv(((u(/opt/alt/python33/lib64/python3.3/cgi.pyuprint_argumentsÑs   cCstdƒdS(u9Dump a list of environment variables used by CGI as HTML.uØ

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(uprint(((u(/opt/alt/python33/lib64/python3.3/cgi.pyuprint_environ_usageØs'cCshtdtddƒ|jddƒ}|jddƒ}|jdd ƒ}|rd|jd d ƒ}n|S( uDeprecated API.u1cgi.escape is deprecated, use html.escape insteadu stackleveliu&u&uu>u"u"(uwarnuDeprecationWarningureplace(usuquote((u(/opt/alt/python33/lib64/python3.3/cgi.pyuescapes cCs:ddl}t|tƒr$d}nd}|j||ƒS(Nis^[ -~]{0,200}[!-~]$u^[ -~]{0,200}[!-~]$(ureu isinstanceubytesumatch(usu _vb_patternure((u(/opt/alt/python33/lib64/python3.3/cgi.pyuvalid_boundarys   uvalid_boundaryu__main__(2u__doc__u __version__uiouStringIOuBytesIOu TextIOWrapperu collectionsuMappingusysuosu urllib.parseuurllibu email.parseru FeedParseru email.messageuMessageuwarningsuwarnuhtmlulocaleutempfileu__all__ulogfileuNoneulogfpuinitlogudologunologucloselogulogumaxlenuenvironuparseuparse_qsu parse_qsluparse_multipartu _parseparamu parse_headeruMiniFieldStorageu FieldStorageutestuprint_exceptionu print_environu print_formuprint_directoryuprint_argumentsuprint_environ_usageuescapeuvalid_boundaryu__name__(((u(/opt/alt/python33/lib64/python3.3/cgi.pyus\            #   E d ÿÿ '   /