Your IP : 216.73.216.52


Current Path : /opt/alt/python38/lib64/python3.8/test/__pycache__/
Upload File :
Current File : //opt/alt/python38/lib64/python3.8/test/__pycache__/test_httpservers.cpython-38.pyc

U

i�f���@sdZddlmZmZmZmZddlmZmZddl	Z	ddl
Z
ddlZddlZddl
Z
ddlZddlZddlZddlZddlZddlZddlZddlZddlZddlZddlZddlmZddlmZmZddlZddl m!Z!Gdd	�d	�Z"Gd
d�dej#�Z$Gdd
�d
ej%�Z&Gdd�de&�Z'Gdd�de&�Z(Gdd�de&�Z)dZ*dZ+dZ,e�-e.e	d��ode	�/�dkd�Gdd�de&��Z0Gdd�de�Z1Gdd�de1�Z2Gdd �d �Z3Gd!d"�d"ej%�Z4Gd#d$�d$ej%�Z5Gd%d&�d&ej%�Z6Gd'd(�d(ej%�Z7d,d)d*�Z8e9d+k�r
e8�dS)-z�Unittests for the various HTTPServer modules.

Written by Cody A.W. Somerville <cody-somerville@ubuntu.com>,
Josip Dzolonga, and Michael Otteneder for the 2007/08 GHOP contest.
�)�BaseHTTPRequestHandler�
HTTPServer�SimpleHTTPRequestHandler�CGIHTTPRequestHandler)�server�
HTTPStatusN)�mock)�BytesIO�StringIO)�supportc@seZdZdd�Zddd�ZdS)�NoLogRequestHandlercGsdS�N�)�self�argsrr�:/opt/alt/python38/lib64/python3.8/test/test_httpservers.py�log_message#szNoLogRequestHandler.log_messageNcCsdS�N�r)r�nrrr�read'szNoLogRequestHandler.read)N)�__name__�
__module__�__qualname__rrrrrrr"src@s$eZdZdd�Zdd�Zdd�ZdS)�TestServerThreadcCstj�|�||_||_dSr
)�	threading�Thread�__init__�request_handler�test_object)rrrrrrr,szTestServerThread.__init__cCsZtd|j�|_|jj��\|j_|j_|jj�	�d|_z|j�d�W5|j�
�XdS)N)�	localhostrg�������?)rrr�socket�getsocknamer�HOST�PORT�server_started�setZserver_closeZ
serve_forever�rrrr�run1szTestServerThread.runcCs|j��|��dSr
)rZshutdown�joinr'rrr�stop;s
zTestServerThread.stopN)rrrrr(r*rrrrr+s
rc@s,eZdZdd�Zdd�Zddifdd�ZdS)	�BaseTestCasecCsDt��|_t��t_t��|_t	||j
�|_|j��|j�
�dSr
)rZthreading_setup�_threadsZEnvironmentVarGuard�os�environrZEventr%rr�thread�start�waitr'rrr�setUpAs



zBaseTestCase.setUpcCs*|j��d|_tj��tj|j�dSr
)r/r*r-r.�__exit__rZthreading_cleanupr,r'rrr�tearDownIs

zBaseTestCase.tearDown�GETNcCs0tj�|j|j�|_|j�||||�|j��Sr
)�http�client�HTTPConnectionr#r$Z
connection�request�getresponse)rZuri�method�body�headersrrrr9OszBaseTestCase.request)rrrr2r4r9rrrrr+@sr+c@s�eZdZGdd�dee�Zdd�Zdd�Zdd�Zd	d
�Z	dd�Z
d
d�Zdd�Zdd�Z
dd�Zdd�Zdd�Zdd�Zdd�Zdd�Zdd �Zd!d"�Zd#d$�Zd%d&�Zd'd(�Zd)d*�Zd+S),�BaseHTTPServerTestCasec@s\eZdZdZdZdd�Zdd�Zdd�Zdd	�Zd
d�Z	dd
�Z
dd�Zdd�Zdd�Z
dS)z&BaseHTTPServerTestCase.request_handler�HTTP/1.1cCs0|�tj�|�dd�|�dd�|��dS)N�Content-Type�	text/html�
Connection�close��
send_responser�
NO_CONTENT�send_header�end_headersr'rrr�do_TESTZsz.BaseHTTPServerTestCase.request_handler.do_TESTcCs0|�tj�|�dd�|�dd�|��dS)Nr@rArB�
keep-aliverDr'rrr�do_KEEP`sz.BaseHTTPServerTestCase.request_handler.do_KEEPcCs|�d�dS)N����
send_errorr'rrr�do_KEYERRORfsz2BaseHTTPServerTestCase.request_handler.do_KEYERRORcCs|�tj�dSr
�rNr�	NOT_FOUNDr'rrr�do_NOTFOUNDisz2BaseHTTPServerTestCase.request_handler.do_NOTFOUNDcCs|�ddd�dS)NrLz
Short MessagezThis is a long 
 explanationrMr'rrr�do_EXPLAINERRORls�z6BaseHTTPServerTestCase.request_handler.do_EXPLAINERRORcCs.|�d�|�dd�|�dd�|��dS)NrLr@rArBrC)rErGrHr'rrr�	do_CUSTOMps
z0BaseHTTPServerTestCase.request_handler.do_CUSTOMcCsJ|�d�|�dd�|�dd�|��|jd�d�}|j�|�dS)NrL�	X-Special�Dängerous MindrBrCzx-special-incoming�utf-8)rErGrHr=�encode�wfile�write)rr<rrr�do_LATINONEHEADERvs
z8BaseHTTPServerTestCase.request_handler.do_LATINONEHEADERcCs|�t|jdd���dS�N��rN�int�pathr'rrr�
do_SEND_ERROR~sz4BaseHTTPServerTestCase.request_handler.do_SEND_ERRORcCs|�t|jdd���dSr\r^r'rrr�do_HEAD�sz.BaseHTTPServerTestCase.request_handler.do_HEADN)rrr�protocol_version�default_request_versionrIrKrOrRrSrTr[rarbrrrrrVsrcCs,t�|�tj�|j|j�|_|j��dSr
)	r+r2r6r7r8r#r$�con�connectr'rrrr2�s
zBaseHTTPServerTestCase.setUpcCs,|j�dd�|j��}|�|jtj�dS)Nr5�/)rer9r:�assertEqual�statusr�NOT_IMPLEMENTED�r�resrrr�test_command�s
z#BaseHTTPServerTestCase.test_commandcCs>d|j_|j�dd�|j��|j��}|�|jtj�dS)Nz	HTTP/1.1
ZXYZBOGUSrg�	re�
_http_vsn_str�
putrequest�
endheadersr:rhrirrjrkrrr�test_request_line_trimming�s


z1BaseHTTPServerTestCase.test_request_line_trimmingcCs>d|j_|j�dd�|j��|j��}|�|jtj�dS)NZFUBARr5rg�	rerorprqr:rhrirZBAD_REQUESTrkrrr�test_version_bogus�s


z)BaseHTTPServerTestCase.test_version_boguscCs>d|j_|j�dd�|j��|j��}|�|jtj�dS)Nz
HTTP/9.9.9r5rgrsrkrrr�test_version_digits�s


z*BaseHTTPServerTestCase.test_version_digitscCs>d|j_|j�dd�|j��|j��}|�|jtj�dS)Nrr5rgrnrkrrr�test_version_none_get�s


z,BaseHTTPServerTestCase.test_version_none_getcCs>d|j_|j�dd�|j��|j��}|�|jtj�dS)Nr�CUSTOMrgrsrkrrr�test_version_none�s


z(BaseHTTPServerTestCase.test_version_nonecCsFd|j_d|j_|j�dd�|j��|j��}|�|jtj	�dS)N�czHTTP/9.9r5rg)
reZ	_http_vsnrorprqr:rhrirZHTTP_VERSION_NOT_SUPPORTEDrkrrr�test_version_invalid�s

z+BaseHTTPServerTestCase.test_version_invalidcCs>d|j_|j�dd�|j��|j��}|�|jtj�dSrrsrkrrr�test_send_blank�s


z&BaseHTTPServerTestCase.test_send_blankcCsD|j�dd�|j�dd�|j��|j��}|�|jtj�dS)Nr5rgrBrC)	rerp�	putheaderrqr:rhrirrjrkrrr�test_header_close�s


z(BaseHTTPServerTestCase.test_header_closecCsLd|j_|j�dd�|j�dd�|j��|j��}|�|jtj	�dS)Nr?r5rgrBrJ)
rerorpr|rqr:rhrirrjrkrrr�test_header_keep_alive�s

z-BaseHTTPServerTestCase.test_header_keep_alivecCs,|j�dd�|j��}|�|jtj�dS)N�TESTrg)rer9r:rhrirrFrkrrr�test_handler�s
z#BaseHTTPServerTestCase.test_handlercCsJ|j�dd�|j��}|�|�d�d�|j�dd�|�|jj�dS)NZKEEPrgrBrJr)rer9r:rh�	getheaderZ
addCleanuprCrkrrr�test_return_header_keep_alive�s

z4BaseHTTPServerTestCase.test_return_header_keep_alivecCs*|j�dd�|j��}|�|jd�dS)NZKEYERRORrgrL�rer9r:rhrirkrrr�test_internal_key_error�s
z.BaseHTTPServerTestCase.test_internal_key_errorcCs*|j�dd�|j��}|�|jd�dS)NrwrgrLr�rkrrr�test_return_custom_status�s
z0BaseHTTPServerTestCase.test_return_custom_statuscCs>|j�dd�|j��}|�|jd�|�t|�d���dS)NZEXPLAINERRORrgrL�Content-Length)rer9r:rhri�
assertTruer_r�rkrrr�test_return_explain_error�s
z0BaseHTTPServerTestCase.test_return_explain_errorcCsL|jjddddid�|j��}|�|�d�d�|�|��d�d��dS)	NZLATINONEHEADERrgzX-Special-IncominguÄrger mit Unicode�r=rUrVrW)rer9r:rhr�rrXrkrrr�test_latin1_header�s
�
z)BaseHTTPServerTestCase.test_latin1_headercCsN|j�dd�|j��}|�|jtj�|��}|�t|�	d��t
|��dS)NZNOTFOUNDrgr�)rer9r:rhrirrQrr_r��len)rrl�datarrr�test_error_content_length�s

z0BaseHTTPServerTestCase.test_error_content_lengthcCs�tjtjf}tjtjtjtjtjfD]�}|j�dd�|��|j�	�}|�
||j�|�
d|�d��|�
d|�d��||kr�|�
d|�d��|�
�}|�
d|�q$dS)NZ
SEND_ERROR�/{}r�r@�Transfer-Encoding�)r�NOT_MODIFIED�
RESET_CONTENTrFZ
PROCESSING�SWITCHING_PROTOCOLSrer9�formatr:rhrir�r�rZallow_transfer_encoding_codes�coderlr�rrr�test_send_error�s"��
z&BaseHTTPServerTestCase.test_send_errorcCs�tjtjf}tjtjtjtjtjfD]�}|j�dd�|��|j�	�}|�
||j�|tjkr�|�t
|�d��dk�|�d|�d��n$|�
d|�d��|�
d|�d��||kr�|�
d|�d��|��}|�
d|�q$dS)	N�HEADr�r�rrAr@r�r�)rr�r��OKrFr�rer9r�r:rhrir�r_r��assertInrr�rrr�test_head_via_send_errors(��

z/BaseHTTPServerTestCase.test_head_via_send_errorN)rrrrrrr2rmrrrtrurvrxrzr{r}r~r�r�r�r�r�r�r�r�r�rrrrr>Us*.	r>c@s,eZdZGdd�de�Zdd�Zdd�ZdS)�RequestHandlerLoggingTestCasec@s$eZdZdZdZdd�Zdd�ZdS)z-RequestHandlerLoggingTestCase.request_handlerr?cCs|�tj�|��dSr
)rErr�rHr'rrr�do_GET)sz4RequestHandlerLoggingTestCase.request_handler.do_GETcCs|�tjd�dS)NzFile not foundrPr'rrr�do_ERROR-sz6RequestHandlerLoggingTestCase.request_handler.do_ERRORN)rrrrcrdr�r�rrrrr%src	Csbtj�|j|j�|_|j��t���}|j�	dd�|j�
�W5QRX|�|���
d��dS)Nr5rgz"GET / HTTP/1.1" 200 -
)r6r7r8r#r$rerfr�captured_stderrr9r:r��getvalue�endswith)r�errrrr�test_get0s

�z&RequestHandlerLoggingTestCase.test_getc	Cs�tj�|j|j�|_|j��t���}|j�	dd�|j�
�W5QRX|���d�}|�
|d�d��|�
|d�d��dS)NZERRORrg�
rz code 404, message File not foundr]z"ERROR / HTTP/1.1" 404 -)r6r7r8r#r$rerfrr�r9r:r��splitr�r�)rr��linesrrr�test_err;s

z&RequestHandlerLoggingTestCase.test_errN)rrrrrr�r�rrrrr�$sr�cs�eZdZGdd�dee�Z�fdd�Z�fdd�Zd'dd	�Ze	�
ejd
kd�e	�
ejdkd
�e	�
ejd�dd����Zdd�Zdd�Zdd�Zdd�Zdd�Zdd�Zdd�Zdd �Zd!d"�Zd#d$�Zd%d&�Z�ZS)(�SimpleHTTPServerTestCasec@seZdZdS)z(SimpleHTTPServerTestCase.request_handlerN�rrrrrrrrIsrc	s�t���t��|_t��}t�|�d|_tj	|d�|_
tj�|j
�|_
d|j
|_tj�|j
d�}t|d��}|�|j�|��W5QRXt�|�j}tj�|tjj�}|jdd�|_tjj|��dd	�|_dS)
NsWe are the knights who say Ni!)�dirrg�test�wbr)ZmicrosecondT�Zusegmt) �superr2r-�getcwd�cwd�tempfileZ
gettempdir�chdirr��mkdtemp�tempdirr`�basename�tempdir_name�base_urlr)�openrZ�flush�stat�st_mtime�datetimeZ
fromtimestamp�timezoneZutc�replace�last_modif_datetime�email�utilsZ
formatdate�	timestamp�last_modif_header)rZbasetempdirZtempnameZtemp�mtimeZ
last_modif��	__class__rrr2Ls*


��zSimpleHTTPServerTestCase.setUpcs@z.t�|j�zt�|j�WnYnXW5t���XdSr
)r�r4r-r�r��shutilZrmtreer�r'r�rrr4as
z!SimpleHTTPServerTestCase.tearDownNcs���fdd�}d�|�_���}|���|��j|�|��j�|rT|�||�|��jd�|��j�	dd�d�|���d�dd���
�|S)	Ncs�j�d�_dS)zWDon't close reader yet so we can check if there was leftover
            buffered inputN)�fpr��reader�responserr�
close_connlszDSimpleHTTPServerTestCase.check_status_and_reason.<locals>.close_conn�
rBrC�r�zConnection should be closed)Z_close_connrr�rhri�assertIsNotNone�reason�version�msg�getrC)rr�rir�r�r<rr�r�check_status_and_reasonks
z0SimpleHTTPServerTestCase.check_status_and_reason�darwinz2undecodable name cannot always be decoded on macOSZwin32z+undecodable name cannot be decoded on win32zneed support.TESTFN_UNDECODABLEc	Cst��}t�tj�d}ttj�|j	|�d��}|�
tj�W5QRX|�|jd�}tj
dkr�t�|j	�D]}|dkrl|}q�ql|�|tj�}tjj|dd�}|�d|�|d	�|�|�d
tj|dd��|d	�|�|�|jd|�}|j|tjtjd
�dS)Nz.txtr�rgr�r��
surrogatepass)�errorsz	href="%s"�surrogateescape�>%s<F��quote�r�)�sys�getfilesystemencodingr-�fsdecoder�TESTFN_UNDECODABLEr�r`r)r�rZr9r��platform�listdirr�rr��urllib�parser�r�rX�html�escape)r�enc�filename�fr��namer<Z
quotednamerrr�test_undecodable_filename�s8
����
�z2SimpleHTTPServerTestCase.test_undecodable_filenamecCsd|�|jd���}|�|d|j�d��d��|�|jd���}|�|d|j�d��d��dS)Nz/?x=123zlisting for z/\?x=123�latin1z/?x=%bbu/\?x=�)r9r�rZassertRegexrX�rr�rrr�test_undecodable_parameter�sz3SimpleHTTPServerTestCase.test_undecodable_parameterc	Cs2t�tj�|jd��d|j�d�}|�d�}|�|�}|�|tj	�|�
d�}|j||dd�d|��}|�|�}|�|tj	�|�
d�}|j|�
d�|d�|j||d	d�d|��}|�|�}|�|tj	�|�|�
d�|�d
|��}|�d�}|�|�}|�|tj	�|�
d�}|j|�
d
�|d�dS)aIEnsure //evil.co/..%2f../../X does not put //evil.co/ in Location.

        //netloc/ in a Location header is a redirect to a new host.
        https://github.com/python/cpython/issues/87389

        This checks that a path resolving to a directory on our server cannot
        resolve into a redirect to another server.
        Zexisting_directoryz2/python.org/..%2f..%2f..%2f..%2f..%2f../%0a%0d/../z/existing_directoryrg�Locationznon-attack failed!�r��//zcExpected Location header to start with a single / and end with a / as this is a directory redirect.zhttps://pypi.org/N)r-�mkdirr`r)r�r�r9r�r�MOVED_PERMANENTLYr�rh�assertFalse�
startswithr�)	rZurlZexpected_locationr��locationZ
attack_urlZattack3_urlZattack_scheme_netloc_2slash_urlZexpected_scheme_netloc_locationrrr�3test_get_dir_redirect_location_domain_injection_bug�s2	





�





zLSimpleHTTPServerTestCase.test_get_dir_redirect_location_domain_injection_bugc
Cs�|�|jd�}|j|tj|jd�|�|jd�}|�|tj�|�|jd�}|�|tj�|�|j�}|�|tj�|�|jd�}|�|tj�|�|jd�}|�|tj�|�|�	d�|jd�|�d	�}|�|tj�|�d
�}|�|tj�d}t
tj�
|jd�d
��}|�|�W5QRX|�|jd�}|�|tj|�tjdk�r�t��dk�r�t�|jd�z"|�|jd�}|�|tj�W5t�|jd�XdS)N�/testr��/test/rg�/?hi=2�?hi=1r��/?hi=1z/ThisDoesNotExistz/ThisDoesNotExist/sDummy index file
z
index.htmlr��posixri�)r9r�r�rr�r�rQr�rhr�r�r-r`r)r�rZr��geteuid�chmodr�)rr�r�r�rrrr��s<�

z!SimpleHTTPServerTestCase.test_getcCsT|j|jddd�}|�|tj�|�|�d�tt|j	���|�|�d�d�dS)Nr�r��r;zcontent-lengthzcontent-typezapplication/octet-stream)
r9r�r�rr�rhr��strr�r�r�rrr�	test_heads���z"SimpleHTTPServerTestCase.test_headcCs�tj��}|j|d<|j|jd|d�}|�|tj�|j	t
jdd�}tj��}tjj
|dd�|d<|j|jd|d�}|�|tj�dS)	z�Check that when a request to /test is sent with the request header
        If-Modified-Since set to date of last modification, the server returns
        status code 304, not 200
        �If-Modified-Sincer�r�r])ZhoursTr�N)r��message�Messager�r9r�r�rr�r�r��	timedeltar��format_datetime)rr=r�Znew_dtrrr�test_browser_cache	s


�
z+SimpleHTTPServerTestCase.test_browser_cachecCsZ|j}|tjdd�}tj��}tjj|dd�|d<|j|j	d|d�}|�
|tj�dS)Nim)ZdaysTr�rr�r�)
r�r�rr�rrr�rr9r�r�rr�)rZdtZold_dtr=r�rrr�test_browser_cache_file_changeds
�
z8SimpleHTTPServerTestCase.test_browser_cache_file_changedcCsBtj��}|j|d<d|d<|j|jd|d�}|�|tj�dS)Nr�*z
If-None-Matchr�r�)	r�rrr�r9r�r�rr�)rr=r�rrr�,test_browser_cache_with_If_None_Match_header&s


zESimpleHTTPServerTestCase.test_browser_cache_with_If_None_Match_headercCsX|jddd�}|�|tj�|jddd�}|�|tj�|jddd�}|�|tj�dS)NrgZFOOrZcustomZGETs)r9r�rrjr�rrr�test_invalid_requests/sz.SimpleHTTPServerTestCase.test_invalid_requestscCs@|�|jd�}|j|tj|jd�|jd}|�||j�dS)z�Checks that the datetime returned in Last-Modified response header
        is the actual datetime of last modification, rounded to the second
        r�r�z
Last-modifiedN)	r9r�r�rr�r�r=rhr�)rr�r�rrr�test_last_modified8s
z+SimpleHTTPServerTestCase.test_last_modifiedcCs�|�|jd�}|j|tj|jd�|�|jd�}|�|tj�|�|jd�}|�|tj�|�|j�}|�|tj�|�|jd�}|�|tj�|�|jd�}|�|tj�|�|�	d�|jd�dS)	Nr�r�r�rgr�r�r�r�)
r9r�r�rr�r�rQr�rhr�r�rrr�test_path_without_leading_slashAs�z8SimpleHTTPServerTestCase.test_path_without_leading_slashc	Cs�d}tj�|j|�}zt|d���Wn"tk
rHt�d|��YnXz,|�
|jd�}|�|t
j�}|j��}W5t�	|�X|�|�dtj|dd�}|�|�|�|�dS)Nz<test&>.txt�wz-Can not create file %s on current file systemrgr�Fr�)r-r`r)r�r�rC�OSError�unittestZSkipTest�unlinkr9r�r�rr�r=Zget_content_charsetr�r�r�r�rX)rr��fullpathr�r<r�Z	html_textrrr�test_html_escape_filenameQs �
z2SimpleHTTPServerTestCase.test_html_escape_filename)N)rrrrrrr2r4r�r�skipIfr�r�Z
skipUnlessrr�r�r�r�r�rrr	rrr
rr�
__classcell__rrr�rr�Hs2

���1&				r�zD#!%s

print("Content-type: text/html")
print()
print("Hello World")
z�#!%s
import cgi

print("Content-type: text/html")
print()

form = cgi.FieldStorage()
print("%%s, %%s, %%s" %% (form.getfirst("spam"), form.getfirst("eggs"),
                          form.getfirst("bacon")))
zR#!%s
import os

print("Content-type: text/html")
print()

print(os.environ["%s"])
r�z7This test can't be run reliably as root (issue #13308).c@s�eZdZGdd�dee�Zej�d�Zdd�Z	dd�Z
dd	�Zd
d�Zdd
�Z
dd�Zdd�Zdd�Zdd�Zdd�Zdd�Zdd�Zdd�Zdd�Zd S)!�CGIHTTPServerTestCasec@seZdZdS)z%CGIHTTPServerTestCase.request_handlerNr�rrrrr�sr�asciic	Csxt�|�t��|_t��|_tj�	|jd�|_
tj�	|j
d�|_t�|j
�t�|j�d|_
d|_d|_d|_d|_d|_t��r�tj�	|jd�|_t�|j���|_ntj|_z|j�d�Wn&tk
r�|��|�d�YnXtj�	|jd�|_
t|j
d��}|�t |j�W5QRXt�!|j
d�tj�	|j
d	�|_t|jddd
��}|�t |j�W5QRXt�!|jd�tj�	|j
d�|_t|jddd
��}|�t"|j�W5QRXt�!|jd�tj�	|jd�|_t|jddd
��}|�t |j�W5QRXt�!|jd�tj�	|j
d
�|_t|jddd
��}|�t#|jdf�W5QRXt�!|jd�t�$|j�dS)Nzcgi-binz	child-dir�pythonrWz0Python executable path is not encodable to utf-8znocgi.pyri�zfile1.py)�encodingzfile2.pyzfile3.pyzfile4.pyZQUERY_STRING)%r+r2r-r�r�r�r��
parent_dirr`r)�cgi_dir�
cgi_child_dirr��
nocgi_path�
file1_path�
file2_path�
file3_path�
file4_path�_pythonexe_symlinkrZcan_symlinkZ	pythonexeZ
PythonSymlink�	__enter__r��
executablerX�UnicodeEncodeErrorr4ZskipTestr�rZ�	cgi_file1r��	cgi_file2�	cgi_file4r�)rr�Zfile1Zfile2Zfile3Zfile4rrrr2�sV


zCGIHTTPServerTestCase.setUpc	Cs�z�t�|j�|jr$|j�ddd�|jr6t�|j�|j	rHt�|j	�|j
rZt�|j
�|jrlt�|j�|jr~t�|j�t�
|j�t�
|j�t�
|j�W5t�|�XdSr
)r+r4r-r�r�r#r3r�removerr r!r"�rmdirrrrr'rrrr4�s$zCGIHTTPServerTestCase.tearDowncCs�dttddddddddddddddddd	d
d
tddtddd
�}|��D]R\}}t|t�rtt|t�rt|�|tj|�qFt�|�}|j	||d|||fd�qFdS)Nr�z//\�/cgi-bin/file1.py�//az/C:/�/a/b�/a/b/z/a/b/dz/a/b/d/fz/a/b/fz/a/fz//f)rz..z/.//..rgr�z/\z/.//�cgi-bin/file1.pyr,�az/ar-z./az./C:/r.r/z/a/b/.z	/a/b/c/..z/a/b/c/../dz/a/b/c/../d/e/../fz/a/b/c/../d/e/../../fz/a/b/c/../d/e/.././././..//fz../a/b/c/../d/e/.././././..//fz/a/b/c/../d/e/../../../fz/a/b/c/../d/e/../../../../fz/a/b/c/../d/e/../../../../../fz/a/b/c/../d/e/../../../../f/..z /a/b/c/../d/e/../../../../f/../.zpath = %r
Got:    %r
Wanted: %rr�)
�
IndexError�items�
isinstance�type�
issubclass�	Exception�assertRaisesrZ_url_collapse_pathrh)rZtest_vectorsr`�expectedZactualrrr�test_url_collapse_path�sR��
��z,CGIHTTPServerTestCase.test_url_collapse_pathcCs:|�d�}|�|��|�d�|jfd|jdtjf�dS)Nr,�Content-type�Hello WorldrA)r9rhrr�ri�lineseprr�rkrrr�test_headers_and_contents

�z.CGIHTTPServerTestCase.test_headers_and_contentcCs|�d�}|�|jtj�dS)Nz)///////////nocgi.py/../cgi-bin/nothere.sh)r9rhrirrQrkrrr�test_issue19435s
z%CGIHTTPServerTestCase.test_issue19435cCsFtj�dddd��}ddi}|�dd||�}|�|��d	|j�dS)
Nr]ri@�)ZspamZeggsZbaconr;z!application/x-www-form-urlencodedz/cgi-bin/file2.pyZPOSTs1, python, 123456)r�r�Z	urlencoder9rhrr=)rZparamsr=rlrrr�	test_posts
�zCGIHTTPServerTestCase.test_postcCs&|�d�}|��|�|jtj�dS)Nz/cgi-bin/invalid)r9rrhrirrQrkrrr�test_invaliduris
z%CGIHTTPServerTestCase.test_invaliduricCsRddt�d�i}|jdd|d�}|�d|jdtjf|��|�d	�|j	f�dS)
Ns
AuthorizationsBasic s
username:passr,r5r�r<rAr;)
�base64Z	b64encoder9rhr=rr�rr�ri)rr=rlrrr�test_authorizations��z(CGIHTTPServerTestCase.test_authorizationcCs:|�d�}|�d|jdtjf|��|�d�|jf�dS)Nr0r<rAr;�r9rhr=rr�rr�rirkrrr�test_no_leading_slash's

�z+CGIHTTPServerTestCase.test_no_leading_slashcCsZd}|tjd<|�d�}|�d|jdtjf|��|�d�|j	f�|�tjd|�dS)NzTest CGI ServerZSERVER_SOFTWAREr,r<rAr;)
r-r.r9rhr=rr�rr�ri)rZ	signaturerlrrr�test_os_environ_is_not_altered.s

�z4CGIHTTPServerTestCase.test_os_environ_is_not_alteredcCs:|�d�}|�d|jdtjf|��|�d�|jf�dS)Nz/cgi-bin%2ffile1.pyr<rAr;rDrkrrr�#test_urlquote_decoding_in_cgi_check7s

�z9CGIHTTPServerTestCase.test_urlquote_decoding_in_cgi_checkcCs:|�d�}|�d|jdtjf|��|�d�|jf�dS)Nz/cgi-bin/child-dir/file3.pyr<rAr;rDrkrrr�test_nested_cgi_path_issue21323=s

�z5CGIHTTPServerTestCase.test_nested_cgi_path_issue21323cCs:|�d�}|�d|jdtjf|��|�d�|jf�dS)Nz/cgi-bin/file4.py?a=b?c=dsa=b?c=drAr;rDrkrrr�&test_query_with_multiple_question_markCs

�z<CGIHTTPServerTestCase.test_query_with_multiple_question_markcCs:|�d�}|�d|jdtjf|��|�d�|jf�dS)Nz0/cgi-bin/file4.py?k=aa%2F%2Fbb&//q//p//=//a//b//sk=aa%2F%2Fbb&//q//p//=//a//b//rAr;rDrkrrr�"test_query_with_continuous_slashesIs
��z8CGIHTTPServerTestCase.test_query_with_continuous_slashesN)rrrrrrr-r=rXr2r4r:r>r?r@rArCrErFrGrHrIrJrrrrr�s ;*	rcs,eZdZ�fdd�Zdd�Zdd�Z�ZS)�SocketlessRequestHandlercs2t��}t�|j_t��|dd�d|_d|_dS)NFr?)	rZMockr	Zmakefile�return_valuer�r�
get_calledrc)rr�kwargsr9r�rrrRs

z!SocketlessRequestHandler.__init__cCs6d|_|�tj�|�dd�|��|j�d�dS)NTr@rA� <html><body>Data</body></html>
)rMrErr�rGrHrYrZr'rrrr�Zs
zSocketlessRequestHandler.do_GETcGsdSr
r)rr�rrrrrasz$SocketlessRequestHandler.log_message)rrrrr�rrrrr�rrKQsrKc@seZdZdd�ZdS)�!RejectingSocketlessRequestHandlercCs|�tj�dS)NF)rNrZEXPECTATION_FAILEDr'rrr�handle_expect_100esz3RejectingSocketlessRequestHandler.handle_expect_100N)rrrrQrrrrrPdsrPc@s0eZdZdd�Zdd�Zdd�Zedd��Zd	S)
�AuditableBytesIOcCs
g|_dSr
)�datasr'rrrrlszAuditableBytesIO.__init__cCs|j�|�dSr
)rS�append)rr�rrrrZoszAuditableBytesIO.writecCsd�|j�S)Nr�)r)rSr'rrr�getDatarszAuditableBytesIO.getDatacCs
t|j�Sr
)r�rSr'rrr�	numWritesuszAuditableBytesIO.numWritesN)rrrrrZrU�propertyrVrrrrrRjs
rRc@s�eZdZdZe�d�Zdd�Zdd�Zdd�Z	d	d
�Z
dd�Zd
d�Zdd�Z
dd�Zdd�Zdd�Zdd�Zdd�Zdd�Zdd�Zdd �Zd!d"�Zd#d$�Zd%d&�Zd'd(�Zd)d*�Zd+d,�Zd-d.�Zd/d0�Zd1S)2�BaseHTTPRequestHandlerTestCasezrTest the functionality of the BaseHTTPServer.

       Test the support for the Expect 100-continue header.
       sHTTP/1.[0-9]+ 200 OKcCst�|_dSr
)rK�handlerr'rrrr2�sz$BaseHTTPRequestHandlerTestCase.setUpcCs:t|�}t�}||j_||j_|j��|�d�|��S�Nr)r	rY�rfilerY�handle_one_request�seek�	readlines)rr�input�outputrrr�send_typical_request�s

z3BaseHTTPRequestHandlerTestCase.send_typical_requestcCs|�|jj�dSr
)r�rYrMr'rrr�verify_get_called�sz0BaseHTTPRequestHandlerTestCase.verify_get_calledcs,dD]"�|�t�fdd�|D��d�qdS)N)sServer: sDate: sContent-Type: c3s|]}|���VqdSr
)r�)�.0�h�Z	fieldNamerr�	<genexpr>�szIBaseHTTPRequestHandlerTestCase.verify_expected_headers.<locals>.<genexpr>r])rh�sum)rr=rrer�verify_expected_headers�sz6BaseHTTPRequestHandlerTestCase.verify_expected_headerscCs|j�|�}|�|�dSr
)�HTTPResponseMatch�searchr�)rr��matchrrr�verify_http_server_response�sz:BaseHTTPRequestHandlerTestCase.verify_http_server_responsec	Cs�d|j_tj}tj�tdt���:}||jd�||jd�||jdd�||jdd�W5QRX|�	�}|�
d|�|�
d	|�|��}|�d|d
�|�d|d�|�d
|d�|�d|d�dS)N)�	127.0.0.1i9�stderrz/fooz/barz	/spam %s.r1uŸ beans��rz/\x1bbar\x00\x1br]z/spam a.�u/spam \x1b\x7f\x9f beans.�)
rYZclient_addressrrr�patch�objectr�r
r�ZassertNotIn�
splitlinesr�)rrZfake_stderrrnr�rrr�test_unprintable_not_logged�sz:BaseHTTPRequestHandlerTestCase.test_unprintable_not_loggedcCs�|�d�}|�|d�|�|dd��|��|�|dd�|�|jjd�|�|jjd�|�|jjd�|�|jj	d	�|�
|jj��d
�dS)N�GET / HTTP/1.1

rr]���rO�GET / HTTP/1.1r5rgr?r�
rarlrhrbrhrY�requestline�commandr`�request_version�assertSequenceEqualr=r3�r�resultrrr�
test_http_1_1�s
z,BaseHTTPRequestHandlerTestCase.test_http_1_1cCs�|�d�}|�|d�|�|dd��|��|�|dd�|�|jjd�|�|jjd�|�|jjd�|�|jj	d	�|�
|jj��d
�dS)NsGET / HTTP/1.0

rr]rxrO�GET / HTTP/1.0r5rg�HTTP/1.0rrzrrrr�
test_http_1_0�s
z,BaseHTTPRequestHandlerTestCase.test_http_1_0cCs6|�d�}|�t|�d�|�|dd�|��dS)NsGET / HTTP/0.9

r]rrO)rarhr�rbrrrr�
test_http_0_9�s
z,BaseHTTPRequestHandlerTestCase.test_http_0_9cCsH|�d�}|�|d�d��|�|d|�d���|�|jj�dS)Ns)GET /spaced out HTTP/1.1
Host: dummy

rs
HTTP/1.1 400 r]�
)rar�r�rh�indexr�rYrMrrrr�test_extra_space�s�z/BaseHTTPRequestHandlerTestCase.test_extra_spacecCs�|�d�}|�|d�|�|dd��|��|�|dd�|�|jjd�|�|jjd�|�|jjd�|�|jj	d	�d
}|�
|jj��|�dS)Ns(GET / HTTP/1.0
Expect: 100-continue

rr]rxrOr�r5rgr��)ZExpectz100-continuerz�rr�r=rrr�test_with_continue_1_0�s
z5BaseHTTPRequestHandlerTestCase.test_with_continue_1_0cCs�|�d�}|�|dd�|�|dd�|�|dd�|�|dd��|��|�|dd	�|�|jjd
�|�|jjd�|�|jjd�|�|jjd
�d}|�	|jj
��|�dS)N�(GET / HTTP/1.1
Expect: 100-continue

rsHTTP/1.1 100 Continue
r]r�rqsHTTP/1.1 200 OK
rxrOryr5rgr?r�)rarhrhrbrYr{r|r`r}r~r=r3r�rrr�test_with_continue_1_1�s
z5BaseHTTPRequestHandlerTestCase.test_with_continue_1_1cCsNtd�}t�}t�}||_||_d|_d|_d|_|�d�|�	|j
d�dS)Nrwr?r�rq)r	rRrKr[rYr}r{r|rNrhrV�rr_r`rYrrr�#test_header_buffering_of_send_error�s
zBBaseHTTPRequestHandlerTestCase.test_header_buffering_of_send_errorcCsXtd�}t�}t�}||_||_d|_|�d�|�|jd�|�	�|�|jd�dS)Nrwr?r�rr])
r	rRrKr[rYr}Zsend_response_onlyrhrVrHr�rrr�+test_header_buffering_of_send_response_only�s
zJBaseHTTPRequestHandlerTestCase.test_header_buffering_of_send_response_onlycCsvtd�}t�}t�}||_||_d|_|�dd�|�dd�|�|jd�|�	�|�|�
�d�|�|jd�dS)	Nrwr?ZFooZfooZbarrsFoo: foo
bar: bar

r])r	rRrKr[rYr}rGrhrVrHrUr�rrr�$test_header_buffering_of_send_headerszCBaseHTTPRequestHandlerTestCase.test_header_buffering_of_send_headercCs�dd�}td�}t�}||j_||j_d|j_|j��|�||�d�||��d�}|�|dd�|�|d	d�|�|d
d�dS)NcSs(|��}|�d�|��}|�|�|SrZ)�tellr]r)r��posr�rrr�_readAndReseeks


z[BaseHTTPRequestHandlerTestCase.test_header_unbuffered_when_continue.<locals>._readAndReseekr�r?r�r�rsHTTP/1.1 100 Continuer]rqsHTTP/1.1 200 OK)	r	rYr[rYr}r\ZassertNotEqualr�rh)rr�r_r`r�rrr�$test_header_unbuffered_when_continues
zCBaseHTTPRequestHandlerTestCase.test_header_unbuffered_when_continuecCst|j}t�|_|�d�}|�|dd�|�|dd��|�|jj�|�tdd�|dd�D��d�||_dS)Nr�rs!HTTP/1.1 417 Expectation Failed
r]rxcss|]}|dkVqdS)sConnection: close
Nr)rc�rrrrrf;szMBaseHTTPRequestHandlerTestCase.test_with_continue_rejected.<locals>.<genexpr>)rYrPrarhrhr�rMrg)rZ
usual_handlerr�rrr�test_with_continue_rejected2s
"z:BaseHTTPRequestHandlerTestCase.test_with_continue_rejectedcCsD|�ddd�}|�|dd�|�|jj�|�|jjt�dS)NsGET �x�rs#HTTP/1.1 414 Request-URI Too Long
)rarhr�rYrMZassertIsInstancer{rrrrr�test_request_length>sz2BaseHTTPRequestHandlerTestCase.test_request_lengthcCsH|�dddd�}|�|dd�|�|jj�|�|jjd�dS)NsGET / HTTP/1.1
X-Foo: bar�rr�s

rsHTTP/1.1 431 Line too long
ry�rarhr�rYrMr{rrrr�test_header_lengthFs�z1BaseHTTPRequestHandlerTestCase.test_header_lengthcCs<|�d�}|�|dd�|�|jj�|�|jjd�dS)Ns�GET / HTTP/1.1
X-Foo: bar
X-Foo: bar
X-Foo: bar
X-Foo: bar
X-Foo: bar
X-Foo: bar
X-Foo: bar
X-Foo: bar
X-Foo: bar
X-Foo: bar
X-Foo: bar
X-Foo: bar
X-Foo: bar
X-Foo: bar
X-Foo: bar
X-Foo: bar
X-Foo: bar
X-Foo: bar
X-Foo: bar
X-Foo: bar
X-Foo: bar
X-Foo: bar
X-Foo: bar
X-Foo: bar
X-Foo: bar
X-Foo: bar
X-Foo: bar
X-Foo: bar
X-Foo: bar
X-Foo: bar
X-Foo: bar
X-Foo: bar
X-Foo: bar
X-Foo: bar
X-Foo: bar
X-Foo: bar
X-Foo: bar
X-Foo: bar
X-Foo: bar
X-Foo: bar
X-Foo: bar
X-Foo: bar
X-Foo: bar
X-Foo: bar
X-Foo: bar
X-Foo: bar
X-Foo: bar
X-Foo: bar
X-Foo: bar
X-Foo: bar
X-Foo: bar
X-Foo: bar
X-Foo: bar
X-Foo: bar
X-Foo: bar
X-Foo: bar
X-Foo: bar
X-Foo: bar
X-Foo: bar
X-Foo: bar
X-Foo: bar
X-Foo: bar
X-Foo: bar
X-Foo: bar
X-Foo: bar
X-Foo: bar
X-Foo: bar
X-Foo: bar
X-Foo: bar
X-Foo: bar
X-Foo: bar
X-Foo: bar
X-Foo: bar
X-Foo: bar
X-Foo: bar
X-Foo: bar
X-Foo: bar
X-Foo: bar
X-Foo: bar
X-Foo: bar
X-Foo: bar
X-Foo: bar
X-Foo: bar
X-Foo: bar
X-Foo: bar
X-Foo: bar
X-Foo: bar
X-Foo: bar
X-Foo: bar
X-Foo: bar
X-Foo: bar
X-Foo: bar
X-Foo: bar
X-Foo: bar
X-Foo: bar
X-Foo: bar
X-Foo: bar
X-Foo: bar
X-Foo: bar
X-Foo: bar
X-Foo: bar

rsHTTP/1.1 431 Too many headers
ryr�rrrr�test_too_many_headersNs�z4BaseHTTPRequestHandlerTestCase.test_too_many_headerscCs8|�d�}d�|�}d}|�tj|dd��d�|�dS)Ns*<script>alert("hello")</script> / HTTP/1.1r�z<script>alert("hello")</script>Fr�r)rar)r�r�r�rX)rr��textrrr�test_html_escape_on_errorUs�
z8BaseHTTPRequestHandlerTestCase.test_html_escape_on_errorcsZ��fdd�}|�j_td���j����tt��td���j����tt��dS)Ncst���j_dSr
)�nextrYZclose_connectionr�Zclose_valuesrrrr\_szPBaseHTTPRequestHandlerTestCase.test_close_connection.<locals>.handle_one_request)T)FFT)rYr\�iterZhandler8�
StopIterationr�)rr\rr�r�test_close_connection\s

z4BaseHTTPRequestHandlerTestCase.test_close_connectionc	Csdt��}t�|�\	}}}}}}}}	}
d|jj|||jj|||||f}|�|jj|d�|�dS)Nz#%s, %02d %3s %4d %02d:%02d:%02d GMT)r�)�time�gmtimerYZweekdaynameZ	monthnamerhZdate_time_string)rZnowZyearZmonthZdayZhhZmmZssZwd�y�zr9rrr�test_date_time_stringks

�z4BaseHTTPRequestHandlerTestCase.test_date_time_stringN)rrr�__doc__�re�compilerir2rarbrhrlrvr�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�rrrrrXzs2
	

rXc@s0eZdZdZdd�Zdd�Zdd�Zdd	�Zd
S)� SimpleHTTPRequestHandlerTestCasez Test url parsing cCs(t��|_tj�|jd�|_t�|_dS)Nr�)r-r��
translatedr`r)rKrYr'rrrr2zs
z&SimpleHTTPRequestHandlerTestCase.setUpcCsR|j�d�}|�||j�|j�d�}|�||j�|j�d�}|�||j�dS)Nz	/filenamez/filename?foo=barz/filename?a=b&spam=eggs#zot�rY�translate_pathrhr��rr`rrr�test_query_argumentssz5SimpleHTTPRequestHandlerTestCase.test_query_argumentscCs8|j�d�}|�||j�|j�d�}|�||j�dS)Nz
//filenamez//filename?foo=barr�r�rrr�test_start_with_double_slash�sz=SimpleHTTPRequestHandlerTestCase.test_start_with_double_slashc	Cs�t�tjdt���|j�d�}|�tjtj�}|�	||j
�|j�d�}|�tjtj�}|�	||j
�|j�d�}|�tjtj�}|�	||j
�|j�d�}|�tjtj�}|�	||j
�W5QRXdS)Nr`zc:c:c:foo/filenamez\c:../filenamezc:\c:..\foo/filenamezc:c:foo\c:c:bar/filename)rZ	swap_attrrr-�ntpathrYr�r��seprhr�r�rrr�test_windows_colon�sz3SimpleHTTPRequestHandlerTestCase.test_windows_colonN)rrrr�r2r�r�r�rrrrr�xs
r�c@seZdZdd�ZdS)�MiscTestCasecCsfg}dddh}tt�D]<}|�d�s||kr.qtt|�}t|dd�dkr|�|�q|�tj|�dS)Nr%Z
nobody_uidr��_rzhttp.server)r�rr��getattrrTZassertCountEqual�__all__)rr9Z	blacklistr�Z
module_objectrrr�test_all�s

zMiscTestCase.test_allN)rrrr�rrrrr��sr�c@sdeZdZdd�Ze�d�dd��Ze�d�dd��ZdZd	Z	e�d�d
d��Z
e�d�dd
��ZdS)�ScriptTestCasecCs0tjtjtjtjtjdd�d�d�d�d�d�S)NcSsdS)N)rrrrrrr�<lambda>�r�z2ScriptTestCase.mock_server_class.<locals>.<lambda>)r")r!)rL)r$)rZ	MagicMockr'rrr�mock_server_class�s�����z ScriptTestCase.mock_server_classzbuiltins.printcCs0|��}tj|dd�|�|jtjtjf�dS�N�ZServerClass�bind�r�rr�r��address_familyr!�AF_INET6�AF_INET�rr��mock_serverrrr�test_server_test_unspec�s
�z&ScriptTestCase.test_server_test_unspeccCs0|��}tj|dd�|�|jtjtjf�dS)Nr r�r�r�rrr�test_server_test_localhost�s
�z)ScriptTestCase.test_server_test_localhost)z::z'2001:0db8:85a3:0000:0000:8a2e:0370:7334z::1)z0.0.0.0z8.8.8.8rmcCs6|jD]*}|��}tj||d�|�|jtj�qdSr�)�
ipv6_addrsr�rr�rhr�r!r��rr�r�r�rrr�test_server_test_ipv6�s
z$ScriptTestCase.test_server_test_ipv6cCs6|jD]*}|��}tj||d�|�|jtj�qdSr�)�
ipv4_addrsr�rr�rhr�r!r�r�rrr�test_server_test_ipv4�s
z$ScriptTestCase.test_server_test_ipv4N)rrrr�rrsr�r�r�r�r�r�rrrrr��s



r�c
Cs6t��}zt�ttttt	t
tt�W5t�|�XdSr
)
r-r�r�rZrun_unittestr�rXr>r�rr�r�r�)�verboser�rrr�	test_main�s�r��__main__)N):r�Zhttp.serverrrrrr6rrr-r!r�r�rBr�r�Z
email.messager�Zemail.utilsr�Zhttp.clientZurllib.parser�r�r�r�rrr�ior	r
r�rrrrZTestCaser+r>r�r�r'r(r)r�hasattrr�rrKrPrRrXr�r�r�r�rrrrr�<module>sf	P$!�J(
<