a �DOg���@sRdZddlmZmZmZddlmZddlZddlZddl Z ddl m Z ddl mZddlZddlZddlZgd�Zdadad d �Zd d �Zd d�Zdd�Zeadadejdddfdd�Zd1dd�Zdd�Zdd�ZGdd�d�Z Gdd�d�Z!ejfd d!�Z"d2d"d#�Z#ejfd$d%�Z$d&d'�Z%d(d)�Z&d*d+�Z'd,d-�Z(d.d/�Z)e*d0k�rNe"�dS)3z2.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)N�a)�logfile�logfp�open�OSError�nolog�log�dolog�Zallargs�r�/usr/lib64/python3.9/cgi.py�initlog8s r cGst�||d�dS)N� )r�write)�fmt�argsrrrr[srcGsdS�Nrrrrrr_srcCsdatrt��datadS)Nr)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 )rArFr)rMr.rJrErLrrQrr �s    �r ccs�|dd�dkr�|dd�}|�d�}|dkr`|�dd|�|�dd|�dr`|�d|d�}q&|dkrpt|�}|d|�}|��V||d�}qdS)Nr3�;r�"�\"�)�find�count�len�strip)�s�end�frrr� _parseparam�s  (  rbcCs�td|�}|��}i}|D]�}|�d�}|dkr|d|�����}||dd���}t|�dkr�|d|dkr�dkr�nn |dd�}|�dd ��d d�}|||<q||fS) NrW�=rr3rZ�����rXz\\�\rY)rb�__next__r[r^�lowerr]rI)�line�parts�keyrF�p�i�name�valuerrrr �s  ,  r c@s<eZdZdZdZdZdZiZdZiZ iZ dd�Z dd�Z dS)rNcCs||_||_dSr%�rmrn��selfrmrnrrr�__init__ szMiniFieldStorage.__init__cCsd|j|jfS)NzMiniFieldStorage(%r, %r)ro�rqrrr�__repr__szMiniFieldStorage.__repr__) �__name__� __module__� __qualname__�filename�list�type�file� type_options� disposition�disposition_optionsrLrrrtrrrrr�src @s�eZdZdddejdddddddf dd�Zd d �Zd d �Zd d�Zdd�Z dd�Z dd�Z dd�Z d:dd�Z d;dd�Zdd�Zdd�Zdd �Zd!d"�Zd#d$�Zd%d&�ZdZd'd(�Zd)d*�Zd+Zd,d-�Zd.d/�Zd0d1�Zd2d3�Zd4d5�Zd6d7�Zd8d9�ZdS)�encode�locale�getpreferredencodingrr7rr� TypeErrorrLr5r8rArr6r)rM�bytesrzru� outerboundary� bytes_read�limitr r}r~rmrx� _binary_filer|� innerboundaryr9r;r:�lengthryr{�done�read_urlencoded� read_multi� read_single)rqrArLr�rBrCrDr�r)rMr�r.�methodrGZcdisprFrEZclenrrrrr?s�/   �       �            �    zFieldStorage.__init__cCs&z|j��Wnty Yn0dSr%)r{r&�AttributeErrorrsrrr�__del__�s zFieldStorage.__del__cCs|Sr%rrsrrr� __enter__�szFieldStorage.__enter__cGs|j��dSr%)r{r&)rqr$rrr�__exit__�szFieldStorage.__exit__cCsd|j|j|jfS)NzFieldStorage(%r, %r, %r))rmrxrnrsrrrrt�s �zFieldStorage.__repr__cCs t|���Sr%)�iter�keysrsrrr�__iter__�szFieldStorage.__iter__cCsT|dkrt|��|jr:|j�d�|j��}|j�d�n|jdurL|j}nd}|S)Nrnr)r�r{�seekr<ryrprrr� __getattr__�s   zFieldStorage.__getattr__cCs^|jdurtd��g}|jD]}|j|kr|�|�q|sBt|��t|�dkrV|dS|SdS)N� not indexabler3r)ryr�rm�appendrVr])rqrj�found�itemrrr� __getitem__s   zFieldStorage.__getitem__cCs8||vr0||}t|t�r(dd�|D�S|jSn|SdS)NcSsg|] }|j�qSr�rn�rO�xrrr� rTz)FieldStorage.getvalue..�r7ryrn�rqrj�defaultrnrrr�getvalues  zFieldStorage.getvaluecCs4||vr,||}t|t�r$|djS|jSn|SdS�Nrr�r�rrr�getfirsts   zFieldStorage.getfirstcCs:||vr2||}t|t�r(dd�|D�S|jgSngSdS)NcSsg|] }|j�qSrr�r�rrrr�.rTz(FieldStorage.getlist..r�)rqrjrnrrrrN)s   zFieldStorage.getlistcCs*|jdurtd��ttdd�|jD���S)Nr�css|] }|jVqdSr%�rm�rOr�rrr� 8rTz$FieldStorage.keys..)ryr��setrsrrrr�4s zFieldStorage.keyscs*|jdurtd��t�fdd�|jD��S)Nr�c3s|]}|j�kVqdSr%r�r��rjrrr�>rTz,FieldStorage.__contains__..)ryr��any)rqrjrr�r� __contains__:s zFieldStorage.__contains__cCs t|���Sr%)r]r�rsrrr�__len__@szFieldStorage.__len__cCs|jdurtd��t|j�S)NzCannot be converted to bool.)ryr��boolrsrrr�__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)N�%s should return bytes, got %sr(�r)rMr�r.cSsg|]\}}t||��qSr�r�rOrjrnrrrr�VrTz0FieldStorage.read_urlencoded..)rAr<r�r7r�r;rzrur=r)rMr�r?r � parse_qslrCrDr�r.ry� skip_lines)rqrG�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) Nz&Invalid boundary in multipart form: %rr�css|]\}}t||�VqdSr%r�r�rrrr�frTz*FieldStorage.read_multi..r��--rTr�r3rzMax number of fields exceeded)#r��valid_boundaryr;ryr�r?r r�rCrDr)rMr�r.�extend�FieldStorageClass� __class__rAr�r7r�rzrur�r]r^rZfeedr=r&r�r�r�r�r�)rqrBrCrDZibr��klassZ first_liner��parserZhdr_text�datarLr��partrrrr�[sr �   ��      ��   (zFieldStorage.read_multicCs4|jdkr|��|��n|��|j�d�dSr�)r�� read_binaryr�� read_linesr{r�rsrrrr��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)Nrr�rd)� make_filer{r�rAr<�min�bufsizer7r�r;rzrur�r]r�r")rqZtodor�rrrr��s  � zFieldStorage.read_binarycCs@|jrt�|_|_nt�|_|_|jr4|��n|��dSr%)r�rr{�_FieldStorage__filerr��read_lines_to_outerboundary�read_lines_to_eofrsrrrr��s  zFieldStorage.read_linescCsv|jdurF|j��t|�dkrF|��|_|j��}|j�|�d|_|jrZ|j�|�n|j�|�|j |j ��dS)Ni�) r��tellr]r�r{r�r"r�r=r)rM)rqrhr�rrrZ__write�s    zFieldStorage.__writecCs:|j�d�}|jt|�7_|s*d|_q6|�|�qdS)N�rd)rAr�r�r]r��_FieldStorage__write)rqrhrrrr��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) Nr�rTTrr�rd� r3s ������ F) r�r�rAr�r�r]r�� startswith�rstrip�endswithr�) rq� next_boundary� last_boundary�delim�last_line_lfend�_readrh� 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)Nr�Tr�rdr3r�)r�r�rAr�r�r]r�r^)rqr�r�r�rhr�rrrr�s$   zFieldStorage.skip_linescCs&|jrt�d�Stjd|jdd�SdS)Nzwb+zw+r!)r)�newline)r��tempfile� TemporaryFiler)rsrrrr�(s  �zFieldStorage.make_file)N)N) rurvrw�osrBrrr�r�r�rtr�r�r�r�r�rNr�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�rrrrr s@+� &   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 �Yn0dS) NzContent-type: text/htmlcSs td�dS)Nz,testing print_exception() -- italics?)�execrrrrra[sztest..fcSs |�dSr%r)rarrr�g]sztest..gz9

What follows is a test, not an actual exception:

z*

Second try with a small maxlen...

�2) �printr4�stdout�stderrr rrrrrrr:)rB�formrar�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
rrd) r4�exc_info� tracebackr�� format_tb�format_exception_only�html�escape�join)rzrn�tbr�r�ryrrrrqs  � �rc Cs\t|���}t�td�td�|D]"}tdt�|�dt�||��q&td�t�dS)Nz

Shell Environment:

�
�
�
�
)�sortedr�r�r�r�)rBr�rjrrrrs  rcCs�t|���}t�td�|s&td�td�|D]Z}tdt�|�ddd�||}tdt�tt|���d �td t�t|���q2td �t�dS) Nz

Form Contents:

z

No form fields.r�r��:� )r`zzr�r�)r�r�r�r�r��reprrz)r�r�rjrnrrrr�s rc Cslt�td�z t��}Wn8tyR}z tdt�t|���WYd}~nd}~00tt�|��t�dS)Nz#

Current Working Directory:

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

Command Line Arguments:

)r�r4r>rrrrr�s  rcCs td�dS)Na�

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
)r�rrrrr�srcCs(ddl}t|t�rd}nd}|�||�S)Nrs^[ -~]{0,200}[!-~]$z^[ -~]{0,200}[!-~]$)�rer7r��match)r_r�Z _vb_patternrrrr��s  r��__main__)rHrIr()NNNN)+� __version__�iorrr�collections.abcrr4r�� urllib.parser?Z email.parserrZ email.messagerr�r�r��__all__rrr rrr'rr:rBr r rbr rr r rrrrrrr�rurrrr�sR   # � F  :'   /