grthtrhthjhtyjytjytkergtrhtrjytjerhrfh4:24 29/09/2026o ®û|j?…ã@s`dZdZddlmZmZmZddlmZddlZddl Z ddl Z ddl m Z ddlmZddlZddlZddlZddlZgd¢Zd adad d „Zd d „Zdd„Zdd„Zeadade jdddfdd„Zd2dd„Zdd„Z dd„Z!Gdd„dƒZ"Gdd „d ƒZ#e jfd!d"„Z$d3d#d$„Z%e jfd%d&„Z&d'd(„Z'd)d*„Z(d+d,„Z)d-d.„Z*d/d0„Z+e,d1kr®e$ƒdSdS)4z�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ÚcGsXtjdtdd�trtsz ttddd�aWn tyYnwts$tant at|Ž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). z7cgi.log() is deprecated as of 3.10. Use logging insteadé)Ú stacklevelÚaÚlocale)ÚencodingN) ÚwarningsÚwarnÚDeprecationWarningÚlogfileÚlogfpÚopenÚOSErrorÚnologÚlogÚdolog©Zallargs©r%ú*/opt/alt/python310/lib64/python3.10/cgi.pyÚinitlog9sÿ ÿ r'cGst ||d¡dS)z=Write a log message to the log file. See initlog() for docs.Ú N)rÚwrite)ZfmtÚargsr%r%r&r#^sr#cGsdS)z9Dummy function, assigned to log when logging is disabled.Nr%r$r%r%r&r!bsr!cCsdatr t ¡datadS)zClose the log file.rN)rrÚcloser'r"r%r%r%r&Úcloselogfs r,ú&c CsV|durtj}t|dƒr|j}nd}t|tƒr|j}d|vr"d|d<|ddkr„t|dƒ\}}|dkr;t|||d �S|d krXt |d ƒ}t rO|t krOt d ƒ‚|  |¡  |¡} nd } d|vrk| rd| d} | |d} ntjdd…r| rx| d} | tjd} | |d<nd|vr�|d} ntjdd…rštjd} nd } | |d<tjj| ||||d�S)a‘Parse a query in the environment or from a file (default stdin) Arguments, all optional: fp : file pointer; default: sys.stdin.buffer 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. separator: str. The symbol to use for separating the query arguments. Defaults to &. Nrzlatin-1ÚREQUEST_METHODÚGETÚPOSTÚ CONTENT_TYPEzmultipart/form-data)Ú separatorú!application/x-www-form-urlencodedÚCONTENT_LENGTHúMaximum content length exceededrÚ QUERY_STRINGr-é)rr2)ÚsysÚstdinÚhasattrrÚ isinstancerÚbufferr r ÚintÚmaxlenÚ ValueErrorÚreadÚdecodeÚargvÚurllibr Zparse_qs) ÚfpÚenvironÚkeep_blank_valuesÚstrict_parsingr2rÚctypeÚpdictZclengthÚqsr%r%r&r ysF           ÿr úutf-8Úreplacecsv|d d¡}d |¡}tƒ}| |¡z|d|d<Wn ty%Ynwt||||ddi|d�‰‡fd d „ˆDƒS) a™Parse multipart input. Arguments: fp : input file pdict: dictionary containing other parameters of content-type header encoding, errors: request encoding and error handler, passed to FieldStorage Returns a dictionary just like parse_qs(): keys are the field names, each value is a list of values for that field. For non-file fields, the value is a list of strings. ÚboundaryÚasciiz multipart/form-data; boundary={}zCONTENT-LENGTHzContent-Lengthr.r0)ÚheadersrÚerrorsrEr2csi|]}|ˆ |¡“qSr%)Úgetlist)Ú.0Úk©Zfsr%r&Ú Øsz#parse_multipart..)rAÚformatrZset_typeÚKeyErrorr )rDrIrrPr2rMrHrOr%rTr&r ¿s   ÿ ÿr ccsÒ�|dd…dkrg|dd…}| d¡}|dkrD| dd|¡| dd|¡drD| d|d¡}|dkrD| dd|¡| dd|¡ds(|dkrLt|ƒ}|d|…}| ¡V||d…}|dd…dks dSdS)Nr7ú;rú"ú\"r)ÚfindÚcountÚlenÚstrip)ÚsÚendÚfr%r%r&Ú _parseparamÚs€  ((ÿ   ÷rbcCs¸td|ƒ}| ¡}i}|D]I}| d¡}|dkrW|d|… ¡ ¡}||dd… ¡}t|ƒdkrS|d|dkrAdkrSnn|dd…}| d d ¡ d d¡}|||<q||fS) zfParse a Content-type like header. Return the main content-type and a dictionary of options. rXú=rNr7réÿÿÿÿrYz\\ú\rZ)rbÚ__next__r[r^Úlowerr]rL)ÚlineÚpartsÚkeyrIÚpÚiÚnameÚvaluer%r%r&r æs  , €r 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©rmrn©Úselfrmrnr%r%r&Ú__init__ s zMiniFieldStorage.__init__cCsd|j|jfS)z Return printable representation.zMiniFieldStorage(%r, %r)ro©rqr%r%r&Ú__repr__szMiniFieldStorage.__repr__)Ú__name__Ú __module__Ú __qualname__Ú__doc__ÚfilenameÚlistÚtypeÚfileÚ type_optionsÚ dispositionÚdisposition_optionsrOrrrtr%r%r%r&rþs rc @sþeZdZdZdddejdddddddf dd „Zd d „Zd d „Zdd„Z dd„Z dd„Z dd„Z dd„Z d;dd„Zd;dd„Zdd„Zdd„Zd d!„Zd"d#„Zd$d%„Zd&d'„ZdZd(d)„Zd*d+„Zd,Zd-d.„Zd/d0„Zd1d2„Zd3d4„Zd5d6„Zd7d8„Zd9d:„Z dS)Úlengthrzr|ÚdoneÚread_urlencodedÚ read_multiÚ read_single)rqrDrOr‹rErFrGr�rrPr…r2ÚmethodrJZcdisprIrHZclenr%r%r&rrBsº/   ÿ   €    ÿ          ÿ ÿ    zFieldStorage.__init__cCs&z|j ¡WdStyYdSw©N)r|r+ÚAttributeErrorrsr%r%r&Ú__del__çs  ÿzFieldStorage.__del__cCs|Sr–r%rsr%r%r&Ú __enter__íszFieldStorage.__enter__cGs|j ¡dSr–)r|r+)rqr*r%r%r&Ú__exit__ðszFieldStorage.__exit__cCsd|j|j|jfS)z"Return a printable representation.zFieldStorage(%r, %r, %r))rmryrnrsr%r%r&rtós ÿzFieldStorage.__repr__cCó t| ¡ƒSr–)ÚiterÚkeysrsr%r%r&Ú__iter__øó zFieldStorage.__iter__cCsX|dkrt|ƒ‚|jr|j d¡|j ¡}|j d¡|S|jdur(|j}|Sd}|S)Nrnr)r—r|Úseekr@rzrpr%r%r&Ú __getattr__ûs    üÿzFieldStorage.__getattr__cCsZ|jdur tdƒ‚g}|jD] }|j|kr| |¡q|s!t|ƒ‚t|ƒdkr+|dS|S)zDictionary style indexing.Nú not indexabler7r)rzr‰rmÚappendrWr])rqrjÚfoundÚitemr%r%r&Ú __getitem__s  € zFieldStorage.__getitem__cCs2||vr||}t|tƒrdd„|DƒS|jS|S)z8Dictionary style get() method, including 'value' lookup.cSóg|]}|j‘qSr%©rn©rRÚxr%r%r&Ú óz)FieldStorage.getvalue..©r;rzrn©rqrjÚdefaultrnr%r%r&Úgetvalues  zFieldStorage.getvaluecCs.||vr||}t|tƒr|djS|jS|S)z! Return the first value received.rr­r®r%r%r&Úgetfirst!s   zFieldStorage.getfirstcCs4||vr||}t|tƒrdd„|DƒS|jgSgS)z Return list of received values.cSr§r%r¨r©r%r%r&r«1r¬z(FieldStorage.getlist..r­)rqrjrnr%r%r&rQ,s  zFieldStorage.getlistcCs*|jdur tdƒ‚ttdd„|jDƒƒƒS)zDictionary style keys() method.Nr¢css�|]}|jVqdSr–©rm©rRr¥r%r%r&Ú ;s€z$FieldStorage.keys..)rzr‰Úsetrsr%r%r&r�7ó zFieldStorage.keyscs*|jdur tdƒ‚t‡fdd„|jDƒƒS)z%Dictionary style __contains__ method.Nr¢c3s�|]}|jˆkVqdSr–r²r³©rjr%r&r´As€z,FieldStorage.__contains__..)rzr‰Úany)rqrjr%r·r&Ú __contains__=r¶zFieldStorage.__contains__cCr›)z Dictionary style len(x) support.)r]r�rsr%r%r&Ú__len__Có zFieldStorage.__len__cCs|jdur tdƒ‚t|jƒS)NzCannot be converted to bool.)rzr‰Úboolrsr%r%r&Ú__bool__Gs  zFieldStorage.__bool__c Cs–|j |j¡}t|tƒstd|jt|ƒjfƒ‚| |j |j ¡}|j r*|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-©rrPr…r2cSsg|] \}}t||ƒ‘qSr%©r©rRrjrnr%r%r&r«Ysz0FieldStorage.read_urlencoded..N)rDr@r�r;rŠr?r{rurArrPr‡rCr Ú parse_qslrFrGr…r2rzÚ skip_lines)rqrJÚqueryr%r%r&r’Ls ÿ ý zFieldStorage.read_urlencodedc CsJ|j}t|ƒstd|fƒ‚g|_|jr3tjj|j|j|j |j |j |j |j d�}|j dd„|Dƒ¡|jp8|j}|j ¡}t|tƒsOtd|jt|ƒjfƒ‚|jt|ƒ7_| ¡d|jkr||r||j ¡}|jt|ƒ7_| ¡d|jkr||sc|j }|durŠ|t|jƒ8} tƒ} d } |j ¡} | | 7} |  ¡sŸnq‘| s£n||jt| ƒ7_|  |  |j |j ¡¡|  ¡} d | vrÂ| d =|jdurÉdn|j|j} ||j| ||||| |j |j ||j ƒ }|durû|d 8}|jró|t|jƒ8}|d krûtd ƒ‚|j|j7_|j |¡|j �s|j|j!k�rd k�rnnnq‹| "¡dS)z/Internal: read a part that is itself multipart.z&Invalid boundary in multipart form: %rr¿css�|] \}}t||ƒVqdSr–rÀrÁr%r%r&r´is€z*FieldStorage.read_multi..r¾ó--NTr€r‚r7rzMax number of fields exceeded)#r�Úvalid_boundaryr?rzr‡rCr rÂrFrGrrPr…r2ÚextendÚFieldStorageClassÚ __class__rDrƒr;rŠr{rurŒr]r^rZfeedrAr+r�r£r‘r�rÃ)rqrErFrGZibrÄÚklassZ first_liner…ÚparserZhdr_textÚdatarOr�Úpartr%r%r&r“^s€ ý   ÿÿ ýÿ ü ÿþ (Ý $zFieldStorage.read_multicCs4|jdkr| ¡| ¡n| ¡|j d¡dS)zInternal: read an atomic part.rN)r�Ú read_binaryrÃÚ read_linesr|r rsr%r%r&r”£s  zFieldStorage.read_singlei cCs¢| ¡|_|j}|dkrM|dkrO|j t||jƒ¡}t|tƒs+t d|jt |ƒj fƒ‚|j t |ƒ7_ |s;d|_dS|j |¡|t |ƒ}|dksdSdSdS)zInternal: read binary data.rr¾rdN)Ú make_filer|r�rDr@ÚminÚbufsizer;rŠr?r{rurŒr]r‘r))rqZtodorÌr%r%r&rήs$  ÿ   öÿzFieldStorage.read_binarycCsB|jr tƒ|_|_ntƒ|_|_|jr| ¡dS| ¡dS)z0Internal: read lines until EOF or outerboundary.N)rŽrr|Ú_FieldStorage__filerr‹Úread_lines_to_outerboundaryÚread_lines_to_eofrsr%r%r&rÏ¿s   zFieldStorage.read_linescCsx|jdur#|j ¡t|ƒdkr#| ¡|_|j ¡}|j |¡d|_|jr.|j |¡dS|j | |j |j ¡¡dS)z line is always bytes, not stringNiè) rÓÚtellr]rÐr|r°r)rŽrArrP)rqrhrÌr%r%r&Z__writeÊs    zFieldStorage.__writecCs: |j d¡}|jt|ƒ7_|sd|_dS| |¡q)zInternal: read lines until EOF.r7érdN)rDrƒrŒr]r‘Ú_FieldStorage__write)rqrhr%r%r&rÕÙs  úzFieldStorage.read_lines_to_eofc Cs@d|j}|d}d}d}d} |jdur"d|jkr!|kr"dS|j d¡}|jt|ƒ7_|t|ƒ7}|s>d|_dS|d krH||}d}| d¡rb|rb| ¡}||krYdS||krbd|_dS|}|  d ¡rtd }|dd …}d}n$|  d ¡r„d }|dd…}d}n|  d ¡r”d }|dd…}d }nd}d }|  ||¡q)zžInternal: read lines until outerboundary. Data is read as bytes: boundaries and line ends must be converted to bytes for comparisons. rÅr€Trr7Nr×rdó s éþÿÿÿó F) r‹r�rDrƒrŒr]r‘Ú startswithÚrstripÚendswithrØ) rqÚ next_boundaryÚ last_boundaryZdelimÚlast_line_lfendZ_readrhÚ strippedlineZodelimr%r%r&rÔãsT ÿ        Úz(FieldStorage.read_lines_to_outerboundarycCs”|jr|jrdSd|j}|d}d} |j d¡}|jt|ƒ7_|s*d|_dS| d¡rD|rD| ¡}||kr;dS||krDd|_dS| d¡}q)z5Internal: skip lines until outer boundary if defined.NrÅTr×rdr7rÛ)r‹r‘rDrƒrŒr]rÞr^)rqrßràrárhrâr%r%r&rÃs(    ózFieldStorage.skip_linescCs"|jrt d¡Stjd|jdd�S)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Únewline)rŽÚtempfileZ TemporaryFilerrsr%r%r&rÐ+s  ÿzFieldStorage.make_filer–)!rurvrwrxÚosrErrr˜r™ršrtržr¡r¦r°r±rQr�r¹rºr½r’rÈr“r”rÒrÎrÏrØrÕrÔrÃrÐr%r%r%r&r sD) ý&    E   2 r cCs¼tdƒtƒtjt_z'tƒ}tƒtƒt|ƒt|ƒt ƒdd„}|fdd„}tdƒ|ƒWnt ƒYtdƒda ztƒ}tƒtƒt|ƒt|ƒWd St ƒYd 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?)Úexecr%r%r%r&ra^rŸztest..fcSs |ƒdSr–r%)rar%r%r&Úg`s ztest..gz9

What follows is a test, not an actual exception:

z*

Second try with a small maxlen...

é2N) Úprintr8ÚstdoutÚstderrr rrrrrrr>)rEÚformrarçr%r%r&r Ms4   r c Csx|dur t ¡\}}}ddl}tƒtdƒ| ||¡| ||¡}tdt d |dd…¡¡t |d¡fƒ~dS)Nrz+

Traceback (most recent call last):

z
%s%s
rrd) r8Úexc_infoÚ tracebackréÚ format_tbÚformat_exception_onlyÚhtmlÚescapeÚjoin)r{rnÚtbr�rîrzr%r%r&rts  ÿ þrc Cs\t| ¡ƒ}tƒtdƒtdƒ|D]}tdt |¡dt ||¡ƒqtdƒtƒdS)z#Dump the shell environment as HTML.z

Shell Environment:

ú
ú
ú
ú
N)Úsortedr�rérñrò)rEr�rjr%r%r&r‚s   rcCs t| ¡ƒ}tƒtdƒ|stdƒtdƒ|D]-}tdt |¡ddd�||}tdt tt|ƒƒ¡d ƒtd t t|ƒ¡ƒqtd ƒ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òÚreprr{)rìr�rjrnr%r%r&r�s  rc Csltƒtdƒzt ¡}Wnty)}ztdt t|ƒ¡ƒWYd}~n d}~wwtt |¡ƒtƒdS)z#Dump the current directory as HTML.z#

Current Working Directory:

zOSError:N)réråÚgetcwdr rñròÚstr)ÚpwdÚmsgr%r%r&r�s  €ÿ rcCs(tƒtdƒtƒttjƒtƒdS)Nz

Command Line Arguments:

)rér8rBr%r%r%r&r©s   rcCs 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ér%r%r%r&r°r»rcCs(ddl}t|tƒr d}nd}| ||¡S)Nrs^[ -~]{0,200}[!-~]$z^[ -~]{0,200}[!-~]$)Úrer;rŠÚmatch)r_rZ _vb_patternr%r%r&rÆßs   rÆÚ__main__)rKrLr-)NNNN)-rxÚ __version__ÚiorrrZcollections.abcrr8råZ urllib.parserCZ email.parserrZ email.messagerrñrrärÚ__all__rrr'r#r!r,r"r>rEr r rbr rr r rrrrrrrÆrur%r%r%r&ÚsZ    %  ÿ F : '  / ÿ