Fa@sUdZdZddlmZmZmZddlmZddlZddl Z ddl Z ddl m Z ddlmZddlmZddlZddlZddlZd d d d d dddddddddgZdadaddZddZddZddZeadade jddd d!d Zddd"d Z ddd#d Z!d d$dZ"d%d&Z#d'dZ$Gd(d d Z%Gd)d d Z&e jd*d+Z'ddddd,dZ(e jd-dZ)d.dZ*d/dZ+d0dZ,d1dZ-dd2dZ.d3d4Z/e0d5krQe'dS)6zSupport module for CGI (Common Gateway Interface) scripts. This module defines a number of utilities for use by CGI scripts written in Python. z2.6)StringIOBytesIO TextIOWrapper)MappingN) FeedParser)Message)warnMiniFieldStorage FieldStorageparseparse_qs parse_qslparse_multipart parse_headerprint_exception print_environ print_formprint_directoryprint_argumentsprint_environ_usageescapec GsXtr5t r5yttdaWntk r4YnXtsDtantat|dS)aWrite 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). aN)logfilelogfpopenOSErrornologlogdolog)allargsr!/usr/lib/python3.5/cgi.pyinitlog:s   r#cGstj||ddS)z=Write a log message to the log file. See initlog() for docs. N)rwrite)Zfmtargsr!r!r"r]srcGsdS)z9Dummy function, assigned to log when logging is disabled.Nr!)r r!r!r"rasrcCs&datrtjdatadS)zClose the log file.rN)rrcloser#rr!r!r!r"closeloges  r(&c Cs|dkrtj}t|dr0|j}nd}t|trN|j}d|krdd|d<|ddkrrt|d\}}|dkrt||d |S|d krt |d }t r|t krt d |j |j |} nd } d|kr1| r | d} | |d} n4tjddre| rT| d} | tjd} | |dr r )rFrArBr!r!r"r s cCs&tdtdtjj|||S)z)Parse a query given as a string argument.z?cgi.parse_qsl is deprecated, use urllib.parse.parse_qsl insteadrG)rrHr>r r )rFrArBr!r!r"r s c Csddl}d}d|kr(|d}t|sGtd|fd|}d|d}i}d}x!||krd} d} |r|jj|} | jd} | ryt| } Wntk rYnX| dkrtr| tkrtd |j| } nd} g} xX|j }|s<|}P|j drj|j }|||fkrjP| j |q#W| dkrqn| dkr| r| d}|ddd kr|dd}n&|ddd kr|dd}|| dks z)FieldStorage.getvalue..N)r6rwrn)ror[defaultrnr!r!r"getvaluefs    zFieldStorage.getvaluecCsB||kr:||}t|tr0|djS|jSn|SdS)z! Return the first value received.rN)r6rwrn)ror[rrnr!r!r"getfirstqs     zFieldStorage.getfirstcCsK||krC||}t|tr6dd|DS|jgSngSdS)z Return list of received values.cSsg|]}|jqSr!)rn)rrr!r!r"rs z(FieldStorage.getlist..N)r6rwrn)ror[rnr!r!r"getlist|s    zFieldStorage.getlistcCs;|jdkrtdttdd|jDS)zDictionary style keys() method.Nz not indexablecss|]}|jVqdS)N)rK)rrr!r!r" sz$FieldStorage.keys..)rwrset)ror!r!r"rs zFieldStorage.keyscs;|jdkrtdtfdd|jDS)z%Dictionary style __contains__ method.Nz not indexablec3s|]}|jkVqdS)N)rK)rr)r[r!r"rsz,FieldStorage.__contains__..)rwrany)ror[r!)r[r" __contains__s zFieldStorage.__contains__cCst|jS)z Dictionary style len(x) support.)r`r)ror!r!r"__len__szFieldStorage.__len__cCs(|jdkrtdt|jS)NzCannot be converted to bool.)rwrbool)ror!r!r"__bool__s zFieldStorage.__bool__c Cs|jj|j}t|tsFtd|jt|jf|j|j |j }|j rx|d|j 7}g|_ t jj||j|jd|j d|j d|j}x-|D]%\}}|j jt||qW|jdS)z+Internal: read data in query string format.z%s should return bytes, got %sr)r*rr/N)r?r;rr6rVr:rxrrr<r*rrrwr>r r rArBr/rSr skip_lines)rorFqueryr[rnr!r!r"rs   zFieldStorage.read_urlencodedc Cs|j}t|s(td|fg|_|jrtjj|j|j|j d|j d|j d|j }x-|D]%\}}|jj t||q}W|jp|j}|jj} t| tstd|jt| jf|jt| 7_xG| jd|jkrV| rV|jj} |jt| 7_qWx%t} d} x*|jj} | | 7} | jslPqlW| sP|jt| 7_| j| j|j |j | j} d| kr| d=||j| |||||j|j|j |j |j }|j|j7_|jj ||jsz|j|j koud knrZPqZW|j!d S) z/Internal: read a part that is itself multipart.z&Invalid boundary in multipart form: %rr*rr/z%s should return bytes, got %ss--rIzcontent-lengthrN)"rrNr:rwrr>r r rArBr*rr/rSr FieldStorageClass __class__r?rPr6rVrxrrrr`rarZfeedr<r'rrrr)ror@rArBZibrr[rnklassZ first_lineparserZhdr_textrWrXpartr!r!r"rsV          +zFieldStorage.read_multicCsD|jdkr&|j|jn |j|jjddS)zInternal: read an atomic part.rN)r read_binaryr read_linesryr)ror!r!r"rs    zFieldStorage.read_singleicCs|j|_|j}|dkrx|dkr|jjt||j}t|tst d|jt |j f|j t |7_ |sd|_P|jj||t |}q'WdS)zInternal: read binary data.rz%s should return bytes, got %sr2NrL) make_fileryrr?r;minbufsizer6rVr:rxrrrr`rr%)roZtodorWr!r!r"rs   zFieldStorage.read_binarycCsV|jrt|_|_nt|_|_|jrH|jn |jdS)z0Internal: read lines until EOF or outerboundary.N)rrry_FieldStorage__filerrread_lines_to_outerboundaryread_lines_to_eof)ror!r!r"rs    zFieldStorage.read_linescCs|jdk re|jjt|dkre|j|_|jj}|jj|d|_|jr|jj|n"|jj|j|j |j dS)z line is always bytes, not stringNi) rtellr`rryrr%rr<r*r)rorZrWr!r!r"Z__write s  zFieldStorage.__writecCsOxH|jjd}|jt|7_|s:d|_P|j|qWdS)zInternal: read lines until EOF.r2NirL)r?rPrr`r_FieldStorage__write)rorZr!r!r"rs zFieldStorage.read_lines_to_eofc Csd|j}|d}d}d}d}xh||jkr<P|jjd }|jt|7_|t|7}|sd|_P|dkr||}d}|jdr|r|j}||krP||krd|_P|}|j drd}|d d}d}nh|j d rEd }|d d}d}n:|j drsd}|d d}d }n d}d }|j ||q,Wd S)zInternal: read lines until outerboundary. Data is read as bytes: boundaries and line ends must be converted to bytes for comparisons. s--rITrr2rs s NrGs FirLrMrLrL) rrr?rPrr`rrQrRendswithr) ro next_boundary last_boundaryZdelimlast_line_lfendZ_readrZ strippedlineZodelimr!r!r"r%sP            z(FieldStorage.read_lines_to_outerboundarycCs|j s|jrdSd|j}|d}d}x|jjd}|jt|7_|snd|_P|jdr|r|j}||krP||krd|_P|jd}q7WdS) z5Internal: skip lines until outer boundary if defined.Ns--Tr2rs irL)rrr?rPrr`rra)rorrrrZrr!r!r"rVs&       zFieldStorage.skip_linescCs6|jrtjdStjdd|jddSdS)aOverridable: 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. zwb+zw+r*newliner$N)rtempfileZ TemporaryFiler*)ror!r!r"rls   zFieldStorage.make_filei )!rrrsrtruosr@rprrrrqrrrrrrrrrrrrrrrrrrrrrrr!r!r!r"r ms< *               6    1 cCstdttjt_ybt}ttt|t|t dd}|dd}td|Wnt YnXtdda y/t}ttt|t|Wnt YnXd S) zRobust test CGI script, usable as main program. Write minimal HTTP headers and dump all information provided to the script in HTML form. zContent-type: text/htmlcSstddS)Nz,testing print_exception() -- italics?)execr!r!r!r"rdsztest..fcSs |dS)Nr!)rdr!r!r"gsztest..gz9

What follows is a test, not an actual exception:

z*

Second try with a small maxlen...

2N) printr3stdoutstderrr rrrrrrr9)r@formrdrr!r!r"tests4            rcCs|dkr!tj\}}}ddl}ttd|j|||j||}tdtjdj|ddtj|df~dS)Nrz+

Traceback (most recent call last):

z
%s%s
rr2rLrL) r3exc_info tracebackr format_tbformat_exception_onlyhtmlrrT)rxrntbrrrwr!r!r"rs   cCs|t|j}ttdtdx7|D]/}tdtj|dtj||q4WtdtdS)z#Dump the shell environment as HTML.z

Shell Environment:

z
z
z
z
N)sortedrrrr)r@rr[r!r!r"rs   - cCst|j}ttd|s3tdtdx}|D]u}tdtj|ddd||}tdtjtt|d td tjt|qDWtd td S) z$Dump the contents of a form as HTML.z

Form Contents:

z

No form fields.z

z
:rc zzz
z
N)rrrrrreprrx)rrr[rnr!r!r"rs    ! '! cCsttdytj}WnAtk rd}z!tdtjt|WYdd}~XnXttj|tdS)z#Dump the current directory as HTML.z#

Current Working Directory:

zOSError:N)rrgetcwdrrrstr)pwdmsgr!r!r"rs /cCs0ttdtttjtdS)Nz

Command Line Arguments:

)rr3r=r!r!r!r"rs   cCstddS)z9Dump a list of environment variables used by CGI as HTML.a

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)rr!r!r!r"rs'cCsetdtdd|jdd}|jdd}|jdd }|ra|jd d }|S) zDeprecated API.z1cgi.escape is deprecated, use html.escape instead stacklevelrGr)z&z>r]z")rrHrj)rbZquoter!r!r"r s cCs:ddl}t|tr$d}nd}|j||S)Nrs^[ -~]{0,200}[!-~]$z^[ -~]{0,200}[!-~]$)rer6rVmatch)rbrZ _vb_patternr!r!r"rN,s   rN__main__)1ru __version__iorrr collectionsrr3rZ urllib.parser>Z email.parserrZ email.messagerwarningsrrrr__all__rrr#rrr(rr9r@r r r rrerr r rrrrrrrrrNrrr!r!r!r"s\            #   Hd #'   /