a �DOg���@sVdZdZddlmZmZmZddlmZddlZddl Z ddl Z ddl m Z ddlmZddlZddlZddlZgd�Zd adad d �Zd d �Zdd�Zdd�Zeadade jdddfdd�Zd2dd�Zdd�Zdd�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+d1k�rRe#�dS)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�cGsDtr*ts*zttd�aWnty(Yn0ts4tantat|�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�/usr/lib64/python3.9/cgi.py�initlog8s r cGst�||d�dS)z=Write a log message to the log file. See initlog() for docs.� N)r�write)�fmt�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^|durtj}t|d�r |j}nd}t|t�r4|j}d|vrDd|d<|ddk�r t|d�\}}|dkrxt|||d �S|d kr�t |d �}t r�|t kr�t d ��|� |�� |�} nd } d|vr�| r�| d} | |d} n*tjdd��r| r�| d} | tjd} | |d<n��z#parse_multipart..)r<�formatrZset_type�KeyErrorr )r@rEr(rLr-rIrDrKrrPrr �s    �r ccs�|dd�dkr�|dd�}|�d�}|dkr`|�dd|�|�dd|�dr`|�d|d�}q&|dkrpt|�}|d|�}|��V||d�}qdS)Nr2�;r�"�\"�)�find�count�len�strip)�s�end�frrr� _parseparam�s  (  racCs�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. rV�=rNr2rY�����rWz\\�\rX)ra�__next__rZr]�lowerr\rH)�line�parts�keyrE�p�i�name�valuerrrr �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�rlrm��selfrlrmrrr�__init__ szMiniFieldStorage.__init__cCsd|j|jfS)z Return printable representation.zMiniFieldStorage(%r, %r)rn�rprrr�__repr__szMiniFieldStorage.__repr__)�__name__� __module__� __qualname__�__doc__�filename�list�type�file� type_options� disposition�disposition_optionsrKrqrsrrrrr�src @s�eZdZdZdddejdddddddf dd �Zd d �Zd d �Zdd�Z dd�Z dd�Z dd�Z dd�Z d;dd�ZdrSz)FieldStorage.getvalue..N�r6ryrm�rpri�defaultrmrrr�getvalues  zFieldStorage.getvaluecCs4||vr,||}t|t�r$|djS|jSn|SdS)z! Return the first value received.rNr�r�rrr�getfirsts   zFieldStorage.getfirstcCs:||vr2||}t|t�r(dd�|D�S|jgSngSdS)z Return list of received values.cSsg|] }|j�qSrr�r�rrrr�.rSz(FieldStorage.getlist..Nr�)rprirmrrrrM)s   zFieldStorage.getlistcCs*|jdurtd��ttdd�|jD���S)zDictionary style keys() method.Nr�css|] }|jVqdSr��rl�rNr�rrr� 8rSz$FieldStorage.keys..)ryr��setrrrrrr�4s zFieldStorage.keyscs*|jdurtd��t�fdd�|jD��S)z%Dictionary style __contains__ method.Nr�c3s|]}|j�kVqdSr�r�r��rirrr�>rSz,FieldStorage.__contains__..)ryr��any)rprirr�r� __contains__:s zFieldStorage.__contains__cCs t|���S)z Dictionary style len(x) support.)r\r�rrrrr�__len__@szFieldStorage.__len__cCs|jdurtd��t|j�S)NzCannot be converted to bool.)ryr��boolrrrrr�__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(rLr�r-cSsg|]\}}t||��qSr�r�rNrirmrrrr�VrSz0FieldStorage.read_urlencoded..N)r@r;r�r6r�r:rzrtr<r(rLr�r>r � parse_qslrBrCr�r-ry� skip_lines)rprF�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 }|du�r|t|j�8}t�} d} |j��} | | 7} | ���s �q0�q | �s:�q@|jt| �7_| �| �|j |j ��| ��} d | v�rz| d =|jdu�r�dn |j|j} ||j| ||||| |j |j ||j � }|du�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�frSz*FieldStorage.read_multi..r��--NrSr�r2rzMax number of fields exceeded)#r��valid_boundaryr:ryr�r>r r�rBrCr(rLr�r-�extend�FieldStorageClass� __class__r@r�r6r�rzrtr�r\r]rZfeedr<r%r�r�r�r�r�)rprArBrCZibr��klassZ first_liner��parserZhdr_text�datarKr��partrrrr�[sr �   ��      ��   (zFieldStorage.read_multicCs4|jdkr|��|��n|��|j�d�dS)zInternal: read an atomic part.rN)r�� read_binaryr�� read_linesr{r�rrrrrr��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�rcN)� make_filer{r�r@r;�min�bufsizer6r�r:rzrtr�r\r�r")rpZtodor�rrrr��s  � zFieldStorage.read_binarycCs@|jrt�|_|_nt�|_|_|jr4|��n|��dS)z0Internal: read lines until EOF or outerboundary.N)r�rr{�_FieldStorage__filerr��read_lines_to_outerboundary�read_lines_to_eofrrrrrr��s  zFieldStorage.read_linescCsv|jdurF|j��t|�dkrF|��|_|j��}|j�|�d|_|jrZ|j�|�n|j�|�|j |j ��dS)z line is always bytes, not stringNi�) r��tellr\r�r{r�r"r�r<r(rL)rprgr�rrrZ__write�s    zFieldStorage.__writecCs:|j�d�}|jt|�7_|s*d|_q6|�|�qdS)zInternal: read lines until EOF.�rcN)r@r�r�r\r��_FieldStorage__write)rprgrrrr��s  zFieldStorage.read_lines_to_eofc CsJd|j}|d}d}d}d}|jdurFd|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�rSTrNr�rc� r2s ������ F) r�r�r@r�r�r\r�� startswith�rstrip�endswithr�) rp� next_boundary� last_boundary�delim�last_line_lfend�_readrg� 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�rcr2r�)r�r�r@r�r�r\r�r])rpr�r�r�rgr�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��tempfile� TemporaryFiler(rrrrrr�(s  �zFieldStorage.make_file)N)N)!rtrurvrw�osrArqr�r�r�rsr�r�r�r�r�rMr�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�rrrrr sB)� &   E   2r cCs�td�t�tjt_zNt�}t�t�t|�t|�t �dd�}|fdd�}td�|�Wnt �Yn0td�da z&t�}t�t�t|�t|�Wnt �Yn0d 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) �printr3�stdout�stderrr rrrrrrr9)rA�formr`r�rrrr Js4    r c Csx|durt��\}}}ddl}t�td�|�||�|�||�}tdt�d�|dd���t�|d�f�~dS)Nrz+

Traceback (most recent call last):

z
%s%s
rrc) r3�exc_info� tracebackr�� format_tb�format_exception_only�html�escape�join)rzrm�tbr�r�ryrrrrqs  � �rc 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�)rAr�rirrrrs  rcCs�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��reprrz)r�r�rirmrrrr�s rc Cslt�td�z t��}Wn8tyR}z tdt�t|���WYd}~nd}~00tt�|��t�dS)z#Dump the current directory as HTML.z#

Current Working Directory:

zOSError:N)r�r��getcwdrr�r��str)�pwd�msgrrrr�s *rcCs(t�td�t�ttj�t�dS)Nz

Command Line Arguments:

)r�r3r=rrrrr�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�rrrrr�srcCs(ddl}t|t�rd}nd}|�||�S)Nrs^[ -~]{0,200}[!-~]$z^[ -~]{0,200}[!-~]$)�rer6r��match)r^r�Z _vb_patternrrrr��s  r��__main__)rGrHr')NNNN),rw� __version__�iorrr�collections.abcrr3r�� urllib.parser>Z email.parserrZ email.messagerr�r�r��__all__rrr rrr&rr9rAr r rar rr r rrrrrrr�rtrrrr� sT   # � F  :'   /