o
    éT•j3  ã                   @   s4  d Z ddlmZmZ ddlZddlZddlmZ ddl	m
Z
 dZg d¢Zde Ze
ƒ d	 ej ¡ d  Zed
 e ZG dd„ deƒZG dd„ deƒZG dd„ deƒZdd„ Zeefdd„Zedkr˜eddeƒ�'Zej ¡ Zeded ded dƒ ddlZe d¡ e ¡  W d  ƒ dS 1 s‘w   Y  dS dS )a!  BaseHTTPServer that implements the Python WSGI protocol (PEP 3333)

This is both an example of how WSGI can be implemented, and a basis for running
simple web applications on a local machine, such as might be done when testing
or debugging an application.  It has not been reviewed for security issues,
however, and we strongly recommend that you use a "real" web server for
production use.

For example usage, see the 'if __name__=="__main__"' block at the end of the
module.  See also the BaseHTTPServer module docs for other API information.
é    )ÚBaseHTTPRequestHandlerÚ
HTTPServerN)ÚSimpleHandler)Úpython_implementationz0.2)Ú
WSGIServerÚWSGIRequestHandlerÚdemo_appÚmake_serverúWSGIServer/ú/ú c                   @   s   e Zd ZeZdd„ ZdS )ÚServerHandlerc              	   C   s<   z| j  | j dd¡d | j¡ W t | ¡ d S t | ¡ w )Nr   é   r   )Úrequest_handlerÚlog_requestÚstatusÚsplitÚ
bytes_sentr   Úclose©Úself© r   ú,/usr/lib/python3.10/wsgiref/simple_server.pyr       s
   ÿzServerHandler.closeN)Ú__name__Ú
__module__Ú__qualname__Úsoftware_versionÚserver_softwarer   r   r   r   r   r      s    r   c                   @   s4   e Zd ZdZdZdd„ Zdd„ Zdd„ Zd	d
„ ZdS )r   z7BaseHTTPServer that implements the Python WSGI protocolNc                 C   s   t  | ¡ |  ¡  dS )z.Override server_bind to store the server name.N)r   Úserver_bindÚsetup_environr   r   r   r   r   0   s   
zWSGIServer.server_bindc                 C   sF   i  }| _ | j|d< d|d< t| jƒ|d< d|d< d|d< d|d< d S )	NÚSERVER_NAMEzCGI/1.1ÚGATEWAY_INTERFACEÚSERVER_PORTÚ ÚREMOTE_HOSTÚCONTENT_LENGTHÚSCRIPT_NAME)Úbase_environÚserver_nameÚstrÚserver_port)r   Úenvr   r   r   r   5   s   

zWSGIServer.setup_environc                 C   s   | j S ©N©Úapplicationr   r   r   r   Úget_app?   ó   zWSGIServer.get_appc                 C   s
   || _ d S r,   r-   )r   r.   r   r   r   Úset_appB   s   
zWSGIServer.set_app)	r   r   r   Ú__doc__r.   r   r   r/   r1   r   r   r   r   r   *   s    
r   c                   @   s,   e Zd Zde Zdd„ Zdd„ Zdd„ ZdS )	r   r
   c                 C   sL  | j j ¡ }| j|d< | j|d< | j|d< d| jv r$| j dd¡\}}n| jd}}tj	 
|d¡|d< ||d	< |  ¡ }|| jd
 krF||d< | jd
 |d< | j d¡d u r]| j ¡ |d< n| jd |d< | j d¡}|rp||d< | j ¡ D ].\}}| dd¡ ¡ }| ¡ }||v rŠqud| |v r�|d|   d| 7  < qu||d| < qu|S )NÚSERVER_PROTOCOLÚSERVER_SOFTWAREÚREQUEST_METHODú?r   r#   z
iso-8859-1Ú	PATH_INFOÚQUERY_STRINGr   r$   ÚREMOTE_ADDRzcontent-typeÚCONTENT_TYPEzcontent-lengthr%   ú-Ú_ÚHTTP_ú,)Úserverr'   ÚcopyÚrequest_versionÚserver_versionÚcommandÚpathr   ÚurllibÚparseÚunquoteÚaddress_stringÚclient_addressÚheadersÚgetÚget_content_typeÚitemsÚreplaceÚupperÚstrip)r   r+   rD   ÚqueryÚhostÚlengthÚkÚvr   r   r   Úget_environK   s6   



zWSGIRequestHandler.get_environc                 C   s   t jS r,   )ÚsysÚstderrr   r   r   r   Ú
get_stderrp   r0   zWSGIRequestHandler.get_stderrc                 C   s€   | j  d¡| _t| jƒdkrd| _d| _d| _|  d¡ dS |  ¡ s$dS t	| j | j
|  ¡ |  ¡ dd�}| |_| | j ¡ ¡ dS )zHandle a single HTTP requesti  i   r#   iž  NF)Úmultithread)ÚrfileÚreadlineÚraw_requestlineÚlenÚrequestlinerA   rC   Ú
send_errorÚparse_requestr   ÚwfilerY   rV   r   Úrunr?   r/   )r   Úhandlerr   r   r   Úhandles   s   
þzWSGIRequestHandler.handleN)r   r   r   Ú__version__rB   rV   rY   re   r   r   r   r   r   G   s
    %r   c                 C   sr   ddl m} |ƒ }td|d� t|d� t|  ¡ ƒ}|D ]\}}t|dt|ƒ|d� q|ddgƒ | ¡  d¡gS )	Nr   )ÚStringIOzHello world!)Úfileú=z200 OK)zContent-Typeztext/plain; charset=utf-8zutf-8)Úiorg   ÚprintÚsortedrM   ÚreprÚgetvalueÚencode)ÚenvironÚstart_responserg   ÚstdoutÚhrT   rU   r   r   r   r   Š   s   
r   c                 C   s   || |f|ƒ}|  |¡ |S )zACreate a new WSGI server listening on `host` and `port` for `app`)r1   )rR   ÚportÚappÚserver_classÚhandler_classr?   r   r   r   r	   –   s   
r	   Ú__main__r#   i@  zServing HTTP onrt   r   z...zhttp://localhost:8000/xyz?abc) r2   Úhttp.serverr   r   rW   Úurllib.parserE   Úwsgiref.handlersr   Úplatformr   rf   Ú__all__rB   Úversionr   Úsys_versionr   r   r   r   r   r	   r   ÚhttpdÚsocketÚgetsocknameÚsark   Ú
webbrowserÚopenÚhandle_requestr   r   r   r   Ú<module>   s4    C
ÿ	


"ûÿ