o
    JAf¥T  ã                   @   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g d¢Z	g d¢Z
g d¢Zdd	„ Zh d
£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 dd„ deƒZG dd„ deƒZdS )é   )ÚFileWrapperÚguess_schemeÚis_hop_by_hop)ÚHeadersé    N)ÚBaseHandlerÚSimpleHandlerÚBaseCGIHandlerÚ
CGIHandlerÚIISCGIHandlerÚread_environ)ZMonZTueZWedZThuZFriZSatZSun)NZ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)ÚtimeÚgmtimeÚ_weekdaynameÚ
_monthname)
Z	timestampZyearZmonthZdayZhhZmmÚssZwdÚyÚz© r   ú'/usr/lib/python3.10/wsgiref/handlers.pyÚformat_date_time   s   ÿr   >
   ZREMOTE_IDENTZREMOTE_USERÚ	PATH_INFOZCONTENT_LENGTHZQUERY_STRINGZ	AUTH_TYPEZCONTENT_TYPEZREQUEST_METHODÚSCRIPT_NAMEZHTTPSc                 C   s6   t | ƒp|  d¡p|  d¡p|  d¡ot| 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}zd d|¡ W n ty   d}Y nw i }tj ¡ D ]S\}}t|ƒrqt jdkrhtj 	dd¡ 
¡ }| d¡rF| d¡ d¡}n+| d	¡rLn%| d
¡r^d|v r^| d¡ d¡}n| | d¡ d¡}n	| | |¡ d¡}|||< q"|S )NÚsurrogateescapeÚ zutf-8ÚreplaceÚwin32ÚSERVER_SOFTWAREzmicrosoft-iis/ú
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   "   s2   ÿ

	
ÿ
r   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   sj   	 z|   ¡  || j| jƒ| _|  ¡  W d S  tttfy!   Y d S    z|  ¡  W Y 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 ur=| j|d< | jrL| jrN| d	| j¡ d S d S 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)r;   Úenvr   r   r   r2   ˜   s   





ÿzBaseHandler.setup_environc                 C   s`   	 z|   ¡ r
|  ¡ s| jD ]}|  |¡ q|  ¡  W n   t| jdƒr)| j ¡  ‚ |  ¡  d S )Nr:   )Úresult_is_fileÚsendfiler4   ÚwriteÚfinish_contentÚhasattrr:   ©r;   Údatar   r   r   r5   ­   s   
€
zBaseHandler.finish_responsec                 C   s   	 t | jƒS r1   )r   r)   ©r;   r   r   r   rD   Ç   s   
zBaseHandler.get_schemec              
   C   sL   	 zt | jƒ}W n tttfy   Y d S w |dkr$t| jƒ| jd< d S d S )Nr   úContent-Length)Úlenr4   Ú	TypeErrorÚAttributeErrorÚNotImplementedErrorÚstrÚ
bytes_sentÚheaders)r;   Zblocksr   r   r   Úset_content_lengthÌ   s   ÿþzBaseHandler.set_content_lengthc                 C   s   	 d| j vr|  ¡  d S d S )NrT   )r[   r\   rS   r   r   r   Úcleanup_headersÙ   s   
ÿzBaseHandler.cleanup_headersc                 C   sn   	 |rz| j r|d |d ƒ |d ¡‚W d }nd }w | jd ur$tdƒ‚|| _|  |¡| _|  |d¡}	 | jS )Nr   r   é   zHeaders already set!ZStatus)Úheaders_sentÚwith_tracebackr[   ÚAssertionErrorÚstatusÚheaders_classÚ_convert_string_typerN   )r;   rb   r[   Úexc_infoÚnameÚvalr   r   r   r3   á   s   þ
zBaseHandler.start_responsec                 C   s&   	 t |ƒtu r	|S td |t|ƒ¡ƒ‚)Nz!{0} must be of type str (got {1}))ÚtyperY   ra   ÚformatÚrepr)r;   ÚvalueÚtitler   r   r   rd   þ   s   ÿz BaseHandler._convert_string_typec                 C   s¢   	 | j rD|  ¡ r>|  d| j| jf  d¡¡ d| jvr)|  dtt ¡ ƒ  d¡¡ | j	r@d| jvrB|  d| j	  d¡¡ d S d S d S d S |  d| j  d¡¡ d S )NzHTTP/%s %s
r#   ZDatez
Date: %s
ZServerzServer: %s
zStatus: %s
)
rH   Úclient_is_modernÚ_writeÚhttp_versionrb   r&   r[   r   r   rI   rS   r   r   r   Úsend_preamble  s   
ÿùzBaseHandler.send_preamblec                 C   sR   	 | j stdƒ‚| jst|ƒ| _|  ¡  n	|  jt|ƒ7  _|  |¡ |  ¡  d S )Nzwrite() before start_response())rb   ra   r_   rU   rZ   Úsend_headersrn   Ú_flushrQ   r   r   r   rN     s   


zBaseHandler.writec                 C   s   	 dS )NFr   rS   r   r   r   rM   )  s   zBaseHandler.sendfilec                 C   s(   	 | j s| j dd¡ |  ¡  d S 	 d S )NrT   Ú0)r_   r[   rJ   rq   rS   r   r   r   rO   >  s
   zBaseHandler.finish_contentc              	   C   s”   	 z5t | jdƒr"| j ¡  W d  | _ | _ | _| _d| _d| _d S W d  | _ | _ | _| _d| _d| _d S d  | _ | _ | _| _d| _d| _w )Nr:   r   F)rP   r4   r:   r[   rb   r)   rZ   r_   rS   r   r   r   r:   H  s   üÿzBaseHandler.closec                 C   s>   	 |   ¡  d| _| jr|  ¡ r|  ¡  |  t| jƒ¡ d S d S )NT)r]   r_   rH   rm   rp   rn   Úbytesr[   rS   r   r   r   rq   U  s   þzBaseHandler.send_headersc                 C   s   	 | j }|d uot| j|ƒS r1   )rG   Ú
isinstancer4   )r;   Úwrapperr   r   r   rL   ^  s   zBaseHandler.result_is_filec                 C   s   	 | j d  ¡ dkS )NZSERVER_PROTOCOLzHTTP/0.9)r)   ÚupperrS   r   r   r   rm   d  s   zBaseHandler.client_is_modernc                 C   sN   	 z"ddl m} |  ¡ }||d |d |d | j|ƒ | ¡  W d }d S d }w )Nr   )Úprint_exceptionr   r^   )Ú	tracebackrx   rA   Útraceback_limitÚflush)r;   re   rx   Ústderrr   r   r   Úlog_exceptioni  s   þ
zBaseHandler.log_exceptionc                 C   s8   	 |   t ¡ ¡ | js|  | j| j¡| _|  ¡  d S d S r1   )	r}   r$   re   r_   Úerror_outputr)   r3   r4   r5   rS   r   r   r   r9   y  s   þzBaseHandler.handle_errorc                 C   s&   	 || j | jd d … t ¡ ƒ | jgS r1   )Úerror_statusÚerror_headersr$   re   Ú
error_body)r;   r)   r3   r   r   r   r~     s   zBaseHandler.error_outputc                 C   ó   	 t ‚r1   ©rX   rQ   r   r   r   rn   ”  s   zBaseHandler._writec                 C   r‚   r1   rƒ   rS   r   r   r   rr     s   zBaseHandler._flushc                 C   r‚   r1   rƒ   rS   r   r   r   r@   ¥  ó   zBaseHandler.get_stdinc                 C   r‚   r1   rƒ   rS   r   r   r   rA   ©  r„   zBaseHandler.get_stderrc                 C   r‚   r1   rƒ   rS   r   r   r   r?   ­  r„   zBaseHandler.add_cgi_varsr1   )1Ú__name__Ú
__module__Ú__qualname__rB   rE   rF   rC   rH   ro   rI   r   r=   r   rG   r   rc   rz   r   r€   r   rb   r4   r_   r[   rZ   r<   r2   r5   rD   r\   r]   r3   rd   rp   rN   rM   rO   r:   rq   rL   rm   r}   r9   r~   rn   rr   r@   rA   r?   r   r   r   r   r   ^   sX    

		r   c                   @   sB   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 r1   )ÚstdinÚstdoutr|   Úbase_envrE   rF   )r;   rˆ   r‰   r|   r)   ÚmultithreadÚmultiprocessr   r   r   Ú__init__¿  s   
zSimpleHandler.__init__c                 C   ó   | j S r1   )rˆ   rS   r   r   r   r@   É  ó   zSimpleHandler.get_stdinc                 C   rŽ   r1   )r|   rS   r   r   r   rA   Ì  r   zSimpleHandler.get_stderrc                 C   s   | j  | j¡ d S r1   )r)   ÚupdaterŠ   rS   r   r   r   r?   Ï  s   zSimpleHandler.add_cgi_varsc                 C   s^   | j  |¡}|d u s|t|ƒkrd S ddlm} |dtƒ 	 ||d … }|s(d S | j  |¡}q)Nr   )Úwarnz9SimpleHandler.stdout.write() should not do partial writes)r‰   rN   rU   Úwarningsr‘   ÚDeprecationWarning)r;   rR   r4   r‘   r   r   r   rn   Ò  s   ÿüzSimpleHandler._writec                 C   s   | j  ¡  | j j| _d S r1   )r‰   r{   rr   rS   r   r   r   rr   ß  s   
zSimpleHandler._flushN)TF)	r…   r†   r‡   r   r@   rA   r?   rn   rr   r   r   r   r   r   ²  s    
ÿ
r   c                   @   s   e Zd Z	 dZdS )r	   FN)r…   r†   r‡   rH   r   r   r   r   r	   ä  s    r	   c                   @   ó   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‰   r|   r   rS   r   r   r   r     s   
þzCGIHandler.__init__N©r…   r†   r‡   rC   r=   r   r   r   r   r   r
   ü  s
    r
   c                   @   r”   )r   Tc              	   C   sj   t ƒ }| dd¡}| dd¡}|d  |d ¡r"|t|ƒd … |d< tj| tjjtj	jtj
|ddd d S )Nr   r   r   ú/FTr•   )r   r,   r   rU   r	   r   r$   rˆ   r–   r‰   r|   )r;   r)   ÚpathZscriptr   r   r   r   2  s   
þzIISCGIHandler.__init__Nr—   r   r   r   r   r     s
    r   )Úutilr   r   r   r[   r   r$   r(   r   Ú__all__r   r   r   Ú__contains__r   r   r   r   r   r	   r
   r   r   r   r   r   Ú<module>   s(    ý<  V2