B
    v9azT  ã               @   sü   d dl mZmZmZ d dlmZ ddlZddlZddlZddddd	d
gZ	dddddddgZ
dddddddddddddgZdd„ Zd d!d"d#d$d%d&d'd(d)h
jZd*d+„ Zd,d
„ ZG d-d„ dƒZG d.d„ deƒZG d/d„ deƒZG d0d„ deƒZG d1d	„ d	eƒZdS )2é   )ÚFileWrapperÚguess_schemeÚis_hop_by_hop)ÚHeadersé    NÚBaseHandlerÚSimpleHandlerÚBaseCGIHandlerÚ
CGIHandlerÚIISCGIHandlerÚread_environZMonZTueZWedZThuZFriZSatZSunZJanZFebZMarZAprZMayZJunZJulZAugZSepZOctZNovZDecc       
   	   C   s:   t  | ¡\	}}}}}}}}}	dt| |t| ||||f S )Nz#%s, %02d %3s %4d %02d:%02d:%02d GMT)ÚtimeZgmtimeÚ_weekdaynameÚ
_monthname)
Z	timestampZyearZmonthZdayZhhZmmÚssZwdÚyÚz© r   ú/usr/lib/python3.7/handlers.pyÚformat_date_time   s    r   ÚSCRIPT_NAMEÚ	PATH_INFOZQUERY_STRINGZREQUEST_METHODZ	AUTH_TYPEZCONTENT_TYPEZCONTENT_LENGTHZHTTPSZREMOTE_USERZREMOTE_IDENTc             C   s6   t | ƒp4|  d¡p4|  d¡p4|  d¡o4t| dd … ƒS )NZHTTP_ZSSL_Z	REDIRECT_é	   )Ú_is_requestÚ
startswithÚ_needs_transcode)Úkr   r   r   r      s    r   c              C   sô   t  ¡ } d}yd d|¡ W n tk
r4   d}Y nX i }x´tj ¡ D ]¦\}}t|ƒrät jdkrÒtj 	dd¡ 
¡ }| d¡rŽ| d¡ d¡}qä| d	¡ršqä| d
¡r¾d|kr¾| d¡ d¡}qä| | d¡ d¡}n| | |¡ d¡}|||< qFW |S )NÚsurrogateescapeÚ zutf-8ÚreplaceÚwin32ÚSERVER_SOFTWAREzmicrosoft-iis/z
iso-8859-1zapache/zsimplehttp/zpython/3)ÚsysÚgetfilesystemencodingÚencodeÚLookupErrorÚosÚenvironÚitemsr   ÚplatformÚgetÚlowerr   Údecode)ÚencZescr'   r   ÚvZsoftwarer   r   r   r   "   s,    

	


c               @   s  e Zd ZdZdZdZdZdZdZdZ	e
ƒ ZeZeZdZdZdgZdZd ZZdZdZd	Zd
d„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zd:dd„Zdd„ Z dd„ Z!dd„ Z"dd„ Z#d d!„ Z$d"d#„ Z%d$d%„ Z&d&d'„ Z'd(d)„ Z(d*d+„ Z)d,d-„ Z*d.d/„ Z+d0d1„ Z,d2d3„ Z-d4d5„ Z.d6d7„ Z/d8d9„ Z0dS );r   )r   r   TFz1.0Nz500 Internal Server Error)zContent-Typez
text/plains;   A server error occurred.  Please contact the administrator.r   c          
   C   sr   y$|   ¡  || j| jƒ| _|  ¡  W nH tttfk
r>   d S    y|  ¡  W n   |  	¡  ‚ Y nX Y nX d S )N)
Úsetup_environr'   Ústart_responseÚresultÚfinish_responseÚConnectionAbortedErrorÚBrokenPipeErrorÚConnectionResetErrorÚhandle_errorÚclose)ÚselfZapplicationr   r   r   Úrun€   s    zBaseHandler.runc             C   s–   | j  ¡  }| _|  ¡  |  ¡ |d< |  ¡ |d< | j|d< | j|d< |  ¡ |d< | j	|d< | j
|d< | jd k	rx| j|d< | jr’| jr’| d	| j¡ d S )
Nz
wsgi.inputzwsgi.errorszwsgi.versionzwsgi.run_oncezwsgi.url_schemezwsgi.multithreadzwsgi.multiprocesszwsgi.file_wrapperr!   )Ú
os_environÚcopyr'   Úadd_cgi_varsÚ	get_stdinÚ
get_stderrÚwsgi_versionÚwsgi_run_onceÚ
get_schemeÚwsgi_multithreadÚwsgi_multiprocessÚwsgi_file_wrapperÚorigin_serverÚserver_softwareÚ
setdefault)r8   Úenvr   r   r   r/   ˜   s    





zBaseHandler.setup_environc             C   sh   y6|   ¡ r|  ¡ s4x| jD ]}|  |¡ qW |  ¡  W n$   t| jdƒrT| j ¡  ‚ Y n
X |  ¡  d S )Nr7   )Úresult_is_fileÚsendfiler1   ÚwriteÚfinish_contentÚhasattrr7   )r8   Údatar   r   r   r2   ­   s    
zBaseHandler.finish_responsec             C   s
   t | jƒS )N)r   r'   )r8   r   r   r   rA   Ç   s    zBaseHandler.get_schemec          
   C   sJ   yt | jƒ}W n tttfk
r(   Y nX |dkrFt| jƒ| jd< d S d S )Nr   zContent-Length)Úlenr1   Ú	TypeErrorÚAttributeErrorÚNotImplementedErrorÚstrÚ
bytes_sentÚheaders)r8   Zblocksr   r   r   Úset_content_lengthÌ   s    zBaseHandler.set_content_lengthc             C   s   d| j kr|  ¡  d S )NzContent-Length)rU   rV   )r8   r   r   r   Úcleanup_headersÙ   s    
zBaseHandler.cleanup_headersc             C   sh   |r2z$| j r&|d |d ƒ |d ¡‚W d d }X n| jd k	rDtdƒ‚|| _|  |¡| _|  |d¡}| jS )Nr   r   é   zHeaders already set!ZStatus)Úheaders_sentÚwith_tracebackrU   ÚAssertionErrorÚstatusÚheaders_classÚ_convert_string_typerK   )r8   r\   rU   Úexc_infor   r   r   r0   á   s    
zBaseHandler.start_responsec             C   s(   t |ƒtkr|S td |t|ƒ¡ƒ‚d S )Nz!{0} must be of type str (got {1}))ÚtyperS   r[   ÚformatÚrepr)r8   ÚvalueÚtitler   r   r   r^   ý   s    z BaseHandler._convert_string_typec             C   s’   | j rx|  ¡ rŽ|  d| j| jf  d¡¡ d| jkrP|  dtt ¡ ƒ  d¡¡ | j	rŽd| jkrŽ|  d| j	  d¡¡ n|  d| j  d¡¡ d S )NzHTTP/%s %s
z
iso-8859-1ZDatez
Date: %s
ZServerzServer: %s
zStatus: %s
)
rE   Úclient_is_modernÚ_writeÚhttp_versionr\   r$   rU   r   r   rF   )r8   r   r   r   Úsend_preamble  s    
zBaseHandler.send_preamblec             C   sR   | j stdƒ‚n,| js*t|ƒ| _|  ¡  n|  jt|ƒ7  _|  |¡ |  ¡  d S )Nzwrite() before start_response())r\   r[   rY   rO   rT   Úsend_headersrf   Ú_flush)r8   rN   r   r   r   rK     s    



zBaseHandler.writec             C   s   dS )NFr   )r8   r   r   r   rJ   (  s    zBaseHandler.sendfilec             C   s"   | j s| j dd¡ |  ¡  n d S )NzContent-LengthÚ0)rY   rU   rG   ri   )r8   r   r   r   rL   =  s    
zBaseHandler.finish_contentc          	   C   sF   zt | jdƒr| j ¡  W d d  | _ | _ | _| _d| _d| _X d S )Nr7   r   F)rM   r1   r7   rU   r\   r'   rT   rY   )r8   r   r   r   r7   G  s     zBaseHandler.closec             C   s8   |   ¡  d| _| jr|  ¡ r4|  ¡  |  t| jƒ¡ d S )NT)rW   rY   rE   re   rh   rf   ÚbytesrU   )r8   r   r   r   ri   T  s
    zBaseHandler.send_headersc             C   s   | j }|d k	ot| j|ƒS )N)rD   Ú
isinstancer1   )r8   Úwrapperr   r   r   rI   ]  s    zBaseHandler.result_is_filec             C   s   | j d  ¡ dkS )NZSERVER_PROTOCOLzHTTP/0.9)r'   Úupper)r8   r   r   r   re   c  s    zBaseHandler.client_is_modernc             C   sJ   z>ddl m} |  ¡ }||d |d |d | j|ƒ | ¡  W d d }X d S )Nr   )Úprint_exceptionr   rX   )Ú	tracebackrp   r>   Útraceback_limitÚflush)r8   r_   rp   Ústderrr   r   r   Úlog_exceptionh  s    
zBaseHandler.log_exceptionc             C   s2   |   t ¡ ¡ | js.|  | j| j¡| _|  ¡  d S )N)	ru   r"   r_   rY   Úerror_outputr'   r0   r1   r2   )r8   r   r   r   r6   x  s    zBaseHandler.handle_errorc             C   s$   || j | jd d … t ¡ ƒ | jgS )N)Úerror_statusÚerror_headersr"   r_   Ú
error_body)r8   r'   r0   r   r   r   rv   €  s    zBaseHandler.error_outputc             C   s   t ‚d S )N)rR   )r8   rN   r   r   r   rf   “  s    zBaseHandler._writec             C   s   t ‚d S )N)rR   )r8   r   r   r   rj   œ  s    zBaseHandler._flushc             C   s   t ‚d S )N)rR   )r8   r   r   r   r=   ¤  s    zBaseHandler.get_stdinc             C   s   t ‚d S )N)rR   )r8   r   r   r   r>   ¨  s    zBaseHandler.get_stderrc             C   s   t ‚d S )N)rR   )r8   r   r   r   r<   ¬  s    zBaseHandler.add_cgi_vars)N)1Ú__name__Ú
__module__Ú__qualname__r?   rB   rC   r@   rE   rg   rF   r   r:   r   rD   r   r]   rr   rw   rx   ry   r\   r1   rY   rU   rT   r9   r/   r2   rA   rV   rW   r0   r^   rh   rK   rJ   rL   r7   ri   rI   re   ru   r6   rv   rf   rj   r=   r>   r<   r   r   r   r   r   ^   sT   

		c               @   s>   e Zd Zddd„Zdd„ Zdd„ Zd	d
„ Zdd„ Zdd„ ZdS )r   TFc             C   s(   || _ || _|| _|| _|| _|| _d S )N)ÚstdinÚstdoutrt   Úbase_envrB   rC   )r8   r}   r~   rt   r'   ÚmultithreadÚmultiprocessr   r   r   Ú__init__¾  s    zSimpleHandler.__init__c             C   s   | j S )N)r}   )r8   r   r   r   r=   È  s    zSimpleHandler.get_stdinc             C   s   | j S )N)rt   )r8   r   r   r   r>   Ë  s    zSimpleHandler.get_stderrc             C   s   | j  | j¡ d S )N)r'   Úupdater   )r8   r   r   r   r<   Î  s    zSimpleHandler.add_cgi_varsc             C   sb   | j  |¡}|d ks |t|ƒkr$d S ddlm} |dtƒ x"||d … }|sNP | j  |¡}q<W d S )Nr   )Úwarnz9SimpleHandler.stdout.write() should not do partial writes)r~   rK   rO   Úwarningsr„   ÚDeprecationWarning)r8   rN   r1   r„   r   r   r   rf   Ñ  s    zSimpleHandler._writec             C   s   | j  ¡  | j j| _d S )N)r~   rs   rj   )r8   r   r   r   rj   Þ  s    
zSimpleHandler._flushN)TF)	rz   r{   r|   r‚   r=   r>   r<   rf   rj   r   r   r   r   r   ±  s   
	c               @   s   e Zd ZdZdS )r	   FN)rz   r{   r|   rE   r   r   r   r   r	   ã  s   c               @   s   e Zd ZdZi Zdd„ ZdS )r
   Tc          	   C   s(   t j| tjjtjjtjtƒ ddd d S )NFT)r€   r   )r	   r‚   r"   r}   Úbufferr~   rt   r   )r8   r   r   r   r‚     s    zCGIHandler.__init__N)rz   r{   r|   r@   r:   r‚   r   r   r   r   r
   û  s   c               @   s   e Zd ZdZi Zdd„ ZdS )r   Tc          	   C   sj   t ƒ }| dd¡}| dd¡}|d  |d ¡rD|t|ƒd … |d< tj| tjjtj	jtj
|ddd d S )Nr   r   r   ú/FT)r€   r   )r   r*   r   rO   r	   r‚   r"   r}   r‡   r~   rt   )r8   r'   ÚpathZscriptr   r   r   r‚   1  s    zIISCGIHandler.__init__N)rz   r{   r|   r@   r:   r‚   r   r   r   r   r     s   )Úutilr   r   r   rU   r   r"   r&   r   Ú__all__r   r   r   Ú__contains__r   r   r   r   r   r	   r
   r   r   r   r   r   Ú<module>   s(   
<  U2