grthtrhthjhtyjytjytkergtrhtrjytjerhrfh4:24 29/09/2026U iÛf™„ã @sjdZdZddlmZmZmZddlmZddlZddl Z ddl Z ddl m Z ddlmZddlZddlZddlZdd d d d d ddddddg Zdadadd„Zdd„Zdd„Zdd„Zeadade jdddfdd „Zd1d!d „Zd"d#„Zd$d „Z Gd%d„dƒZ!Gd&d „d ƒZ"e jfd'd „Z#d2d(d„Z$e jfd)d„Z%d*d„Z&d+d„Z'd,d„Z(d-d„Z)d.d/„Z*e+d0k�rfe#ƒdS)3z�Support module for CGI (Common Gateway Interface) scripts. This module defines a number of utilities for use by CGI scripts written in Python. z2.6é)ÚStringIOÚBytesIOÚ TextIOWrapper)ÚMappingN)Ú FeedParser)ÚMessageÚMiniFieldStorageÚ FieldStorageÚparseÚparse_multipartÚ parse_headerÚtestÚprint_exceptionÚ print_environÚ print_formÚprint_directoryÚprint_argumentsÚprint_environ_usageÚcGsFtr,ts,zttdƒaWntk r*YnXts6tantat|ŽdS)a„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). ÚaN)ÚlogfileÚlogfpÚopenÚOSErrorÚnologÚlogÚdolog©Zallargs©rú(/opt/alt/python38/lib64/python3.8/cgi.pyÚinitlog8sr cGst ||d¡dS)z=Write a log message to the log file. See initlog() for docs.Ú N)rÚwrite)ZfmtÚargsrrrr[srcGsdS)z9Dummy function, assigned to log when logging is disabled.Nrrrrrr_srcCsdatrt ¡datadS)zClose the log file.rN)rrÚcloser rrrrrÚcloselogcs r%ú&c Cs^|dkrtj}t|dƒr |j}nd}t|tƒr4|j}d|krDd|d<|ddk�r t|dƒ\}}|dkrxt|||d �S|d kr²t |d ƒ}t r |t kr t d ƒ‚|  |¡  |¡} nd } d|krØ| rÊ| d} | |d} n*tjdd…�r| rô| d} | tjd} | |d<nÕsz#parse_multipart..)r;ÚformatrZset_typeÚKeyErrorr )r>rCr'rJr,rGrBrIrrNrr ¼s   ÿccs˜|dd…dkr”|dd…}| d¡}|dkr`| dd|¡| dd|¡dr`| d|d¡}q&|dkrpt|ƒ}|d|…}| ¡V||d…}qdS)Nr1ú;rú"ú\"é)ÚfindÚcountÚlenÚstrip)ÚsÚendÚfrrrÚ _parseparam×s  (  r]cCs¸td|ƒ}| ¡}i}|D]’}| d¡}|dkr|d|… ¡ ¡}||dd… ¡}t|ƒdkr¦|d|dkr‚dkr¦nn |dd…}| d d ¡ d d¡}|||<q||fS) zfParse a Content-type like header. Return the main content-type and a dictionary of options. rRú=rNr1rUéÿÿÿÿrSz\\ú\rT)r]Ú__next__rVrYÚlowerrXrF)ÚlineÚpartsÚkeyrCÚpÚiÚnameÚvaluerrrr ãs  ,  c@s@eZdZdZdZdZdZdZiZdZ iZ iZ dd„Z dd„Z dS)rz=Like FieldStorage, for use when no file uploads are possible.NcCs||_||_dS)z&Constructor from field name and value.N©rhri©ÚselfrhrirrrÚ__init__ szMiniFieldStorage.__init__cCsd|j|jfS)z Return printable representation.zMiniFieldStorage(%r, %r)rj©rlrrrÚ__repr__szMiniFieldStorage.__repr__)Ú__name__Ú __module__Ú __qualname__Ú__doc__ÚfilenameÚlistÚtypeÚfileÚ type_optionsÚ dispositionÚdisposition_optionsrIrmrorrrrrûsc @sþeZdZdZdddejdddddddf dd „Zd d „Zd d „Zdd„Z dd„Z dd„Z dd„Z dd„Z d;dd„Zdrr4r'rJÚbytesrvrpÚ outerboundaryÚ bytes_readÚlimitr ryrzrhrtÚ _binary_filerxÚ innerboundaryr7r9r8ÚlengthrurwÚdoneÚread_urlencodedÚ read_multiÚ read_single)rlr>rIr‡r?r@rAr‰r'rJr€r,ÚmethodrDZcdisprCrBZclenrrrrm?s¶/   ÿ       ÿ            ÿ    zFieldStorage.__init__cCs(z|j ¡Wntk r"YnXdS©N)rwr$ÚAttributeErrorrnrrrÚ__del__äszFieldStorage.__del__cCs|Sr’rrnrrrÚ __enter__êszFieldStorage.__enter__cGs|j ¡dSr’)rwr$)rlr#rrrÚ__exit__íszFieldStorage.__exit__cCsd|j|j|jfS)z"Return a printable representation.zFieldStorage(%r, %r, %r))rhrtrirnrrrroðs ÿzFieldStorage.__repr__cCs t| ¡ƒSr’)ÚiterÚkeysrnrrrÚ__iter__õszFieldStorage.__iter__cCsT|dkrt|ƒ‚|jr:|j d¡|j ¡}|j d¡n|jdk rL|j}nd}|S)Nrir)r“rwÚseekr:rurkrrrÚ __getattr__øs   zFieldStorage.__getattr__cCs^|jdkrtdƒ‚g}|jD]}|j|kr| |¡q|sBt|ƒ‚t|ƒdkrV|dS|SdS)zDictionary style indexing.Nú not indexabler1r)rur…rhÚappendrQrX)rlreÚfoundÚitemrrrÚ __getitem__s    zFieldStorage.__getitem__cCs8||kr0||}t|tƒr(dd„|DƒS|jSn|SdS)z8Dictionary style get() method, including 'value' lookup.cSsg|] }|j‘qSr©ri©rLÚxrrrÚ sz)FieldStorage.getvalue..N©r5ruri©rlreÚdefaultrirrrÚgetvalues  zFieldStorage.getvaluecCs4||kr,||}t|tƒr$|djS|jSn|SdS)z! Return the first value received.rNr¥r¦rrrÚgetfirsts   zFieldStorage.getfirstcCs:||kr2||}t|tƒr(dd„|DƒS|jgSngSdS)z Return list of received values.cSsg|] }|j‘qSrr¡r¢rrrr¤.sz(FieldStorage.getlist..Nr¥)rlrerirrrrK)s   zFieldStorage.getlistcCs*|jdkrtdƒ‚ttdd„|jDƒƒƒS)zDictionary style keys() method.Nrœcss|] }|jVqdSr’©rh©rLrŸrrrÚ 8sz$FieldStorage.keys..)rur…Úsetrnrrrr˜4s zFieldStorage.keyscs*|jdkrtdƒ‚t‡fdd„|jDƒƒS)z%Dictionary style __contains__ method.Nrœc3s|]}|jˆkVqdSr’rªr«©rerrr¬>sz,FieldStorage.__contains__..)rur…Úany)rlrerr®rÚ __contains__:s zFieldStorage.__contains__cCs t| ¡ƒS)z Dictionary style len(x) support.)rXr˜rnrrrÚ__len__@szFieldStorage.__len__cCs|jdkrtdƒ‚t|jƒS)NzCannot be converted to bool.)rur…ÚboolrnrrrÚ__bool__Ds zFieldStorage.__bool__c Cs–|j |j¡}t|tƒs0td|jt|ƒjfƒ‚| |j |j ¡}|j rT|d|j 7}t j j||j|j|j |j |j|jd�}dd„|Dƒ|_| ¡dS)z+Internal: read data in query string format.ú%s should return bytes, got %sr&©r'rJr€r,cSsg|]\}}t||ƒ‘qSr©r©rLrerirrrr¤Vsz0FieldStorage.read_urlencoded..N)r>r:rŒr5r†r9rvrpr;r'rJr‚r=r Ú parse_qslr@rAr€r,ruÚ skip_lines)rlrDÚqueryrrrrŽIs& ÿýzFieldStorage.read_urlencodedc CsL|j}t|ƒstd|fƒ‚g|_|jrftjj|j|j|j |j |j |j |j d�}|j dd„|Dƒ¡|jpp|j}|j ¡}t|tƒsžtd|jt|ƒjfƒ‚|jt|ƒ7_| ¡d|jkrä|rä|j ¡}|jt|ƒ7_q°|j }|dk �r|t|jƒ8}tƒ} d} |j ¡} | | 7} |  ¡�s �q0�q | �s:�q@|jt| ƒ7_|  |  |j |j ¡¡|  ¡} d | k�rz| d =|jdk�rŠdn |j|j} ||j| ||||| |j |j ||j ƒ }|dk �rô|d 8}|j�râ|t|jƒ8}|d k�rôtd ƒ‚|j|j7_|j |¡|j �s@|j|j!k�r4d k�rnn�q@�q| "¡dS) z/Internal: read a part that is itself multipart.z&Invalid boundary in multipart form: %rrµcss|]\}}t||ƒVqdSr’r¶r·rrrr¬fsz*FieldStorage.read_multi..r´ó--Nr{r}r1rzMax number of fields exceeded)#r‹Úvalid_boundaryr9rur‚r=r r¸r@rAr'rJr€r,ÚextendÚFieldStorageClassÚ __class__r>r~r5r†rvrprˆrXrYrZfeedr;r$r‰r�r�rŒr¹)rlr?r@rAZibrºÚklassZ first_liner€ÚparserZhdr_textÚdatarIr‰Úpartrrrr�[s‚ý   ÿÿ      ÿþ   (zFieldStorage.read_multicCs4|jdkr| ¡| ¡n| ¡|j d¡dS)zInternal: read an atomic part.rN)rŒÚ read_binaryr¹Ú read_linesrwršrnrrrr� s   zFieldStorage.read_singlei cCs’| ¡|_|j}|dkrŽ|dkrŽ|j t||jƒ¡}t|tƒsVt d|jt |ƒj fƒ‚|j t |ƒ7_ |std|_qŽ|j |¡|t |ƒ}qdS)zInternal: read binary data.rr´r_N)Ú make_filerwrŒr>r:ÚminÚbufsizer5r†r9rvrprˆrXr�r")rlZtodorÂrrrrÄ«s  ÿ zFieldStorage.read_binarycCs@|jrtƒ|_|_ntƒ|_|_|jr4| ¡n| ¡dS)z0Internal: read lines until EOF or outerboundary.N)rŠrrwÚ_FieldStorage__filerr‡Úread_lines_to_outerboundaryÚread_lines_to_eofrnrrrrżs  zFieldStorage.read_linescCsv|jdk rF|j ¡t|ƒdkrF| ¡|_|j ¡}|j |¡d|_|jrZ|j |¡n|j | |j |j ¡¡dS)z line is always bytes, not stringNiè) rÉÚtellrXrÆrwr¨r"rŠr;r'rJ)rlrcrÂrrrZ__writeÇs    zFieldStorage.__writecCs:|j d¡}|jt|ƒ7_|s*d|_q6| |¡qdS)zInternal: read lines until EOF.ér_N)r>r~rˆrXr�Ú_FieldStorage__write)rlrcrrrrËÖs  zFieldStorage.read_lines_to_eofc CsJd|j}|d}d}d}d}|jdk rFd|jkr>|krFnn�qF|j d¡}|jt|ƒ7_|t|ƒ7}|s~d|_�qF|dkr’||}d}| d¡rÆ|rÆ| ¡}||kr´�qF||krÆd |_�qF|}|  d ¡rêd }|dd …}d}nL|  d ¡�r d }|dd…}d}n*|  d¡�r.d}|dd…}d }nd}d }|  ||¡qdS)zžInternal: read lines until outerboundary. Data is read as bytes: boundaries and line ends must be converted to bytes for comparisons. r»r{TrNrÍr_ó r1s éþÿÿÿó F) r‡r‰r>r~rˆrXr�Ú startswithÚrstripÚendswithrÎ) rlÚ next_boundaryÚ last_boundaryZdelimÚlast_line_lfendZ_readrcÚ strippedlineZodelimrrrrÊàsN $        z(FieldStorage.read_lines_to_outerboundarycCs�|jr |jrdSd|j}|d}d}|j d¡}|jt|ƒ7_|sPd|_qŒ| d¡r€|r€| ¡}||krpqŒ||kr€d|_qŒ| d¡}q&dS)z5Internal: skip lines until outer boundary if defined.Nr»TrÍr_r1rÑ)r‡r�r>r~rˆrXrÔrY)rlrÕrÖr×rcrØrrrr¹s$   zFieldStorage.skip_linescCs&|jrt d¡Stjd|jdd�SdS)a¯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. zwb+zw+r!)r'ÚnewlineN)rŠÚtempfileZ TemporaryFiler'rnrrrrÆ(s  ÿzFieldStorage.make_file)N)N)!rprqrrrsÚosr?rmr”r•r–ror™r›r r¨r©rKr˜r°r±r³rŽr¾r�r�rÈrÄrÅrÎrËrÊr¹rÆrrrrr sL)ý &   E   2cCsÂtdƒtƒtjt_zNtƒ}tƒtƒt|ƒt|ƒt ƒdd„}|fdd„}tdƒ|ƒWnt ƒYnXtdƒda z&tƒ}tƒtƒt|ƒt|ƒWnt ƒYnXd S) z—Robust 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/htmlcSs tdƒdS)Nz,testing print_exception() -- italics?)Úexecrrrrr\[sztest..fcSs |ƒdSr’r)r\rrrÚg]sztest..gz9

What follows is a test, not an actual exception:

z*

Second try with a small maxlen...

é2N) Úprintr2ÚstdoutÚstderrr rrrrrrr8)r?Úformr\rÝrrrr Js4    c Csx|dkrt ¡\}}}ddl}tƒtdƒ| ||¡| ||¡}tdt d |dd…¡¡t |d¡fƒ~dS)Nrz+

Traceback (most recent call last):

z
%s%s
rr_) r2Úexc_infoÚ tracebackrßÚ format_tbÚformat_exception_onlyÚhtmlÚescapeÚjoin)rvriÚtbr‰rärurrrrqs  ÿ þc Cs\t| ¡ƒ}tƒtdƒtdƒ|D]"}tdt |¡dt ||¡ƒq&tdƒtƒdS)z#Dump the shell environment as HTML.z

Shell Environment:

ú
ú
ú
ú
N)Úsortedr˜rßrçrè)r?r˜rerrrrs  cCs t| ¡ƒ}tƒtdƒ|s&tdƒtdƒ|D]Z}tdt |¡ddd�||}tdt tt|ƒƒ¡d ƒtd t t|ƒ¡ƒq2td ƒtƒd S) z$Dump the contents of a form as HTML.z

Form Contents:

z

No form fields.rërìú:ú )r[zzrírîN)rïr˜rßrçrèÚreprrv)râr˜rerirrrrŠs c Csjtƒtdƒz t ¡}Wn6tk rP}ztdt t|ƒ¡ƒW5d}~XYnXtt |¡ƒtƒdS)z#Dump the current directory as HTML.z#

Current Working Directory:

zOSError:N)rßrÛÚgetcwdrrçrèÚstr)ÚpwdÚmsgrrrršs &cCs(tƒtdƒtƒttjƒtƒdS)Nz

Command Line Arguments:

)rßr2r<rrrrr¦s  cCs tdƒdS)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)rßrrrrr­scCs(ddl}t|tƒrd}nd}| ||¡S)Nrs^[ -~]{0,200}[!-~]$z^[ -~]{0,200}[!-~]$)Úrer5r†Úmatch)rZr÷Z _vb_patternrrrr¼Üs  r¼Ú__main__)rErFr&)NNNN),rsÚ __version__ÚiorrrZcollections.abcrr2rÛZ urllib.parser=Z email.parserrZ email.messagerrçr„rÚÚ__all__rrr rrr%rr8r?r r r]r rr r rrrrrrr¼rprrrrÚ sh   ý# ÿ F  :'   /