a
    ´i3  ã                   @   s2  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ƒ�LZej ¡ Zeded ded dƒ ddlZe d¡ e ¡  W d  ƒ n1 �s$0    Y  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 | ¡ nt | ¡ 0 d S )Nr   é   r   )Úrequest_handlerZlog_requestÚstatusÚsplitZ
bytes_sentr   Úclose©Úself© r   ú-/usr/lib64/python3.9/wsgiref/simple_server.pyr       s
    ÿzServerHandler.closeN)Ú__name__Ú
__module__Ú__qualname__Úsoftware_versionZ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 )	NZSERVER_NAMEzCGI/1.1ZGATEWAY_INTERFACEZSERVER_PORTÚ ÚREMOTE_HOSTÚCONTENT_LENGTHZSCRIPT_NAME)Úbase_environZserver_nameÚstrZserver_port)r   Úenvr   r   r   r   5   s    

zWSGIServer.setup_environc                 C   s   | j S ©N©Úapplicationr   r   r   r   Úget_app?   s    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&   r'   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   sP  | j j ¡ }| j|d< | j|d< | j|d< d| jv rH| j dd¡\}}n| jd }}tj	 
|d¡|d< ||d	< |  ¡ }|| jd
 krŒ||d< | jd
 |d< | j d¡d u rº| j ¡ |d< n| jd |d< | j d¡}|rà||d< | j ¡ D ]`\}}| dd¡ ¡ }| ¡ }||v �rqêd| |v �r>|d|   d| 7  < qê||d| < qê|S )NZSERVER_PROTOCOLZSERVER_SOFTWAREZREQUEST_METHODú?r   r   z
iso-8859-1Z	PATH_INFOZQUERY_STRINGr   r   ZREMOTE_ADDRzcontent-typeZCONTENT_TYPEzcontent-lengthr   ú-Ú_ZHTTP_ú,)Úserverr    ÚcopyÚrequest_versionÚserver_versionÚcommandÚpathr   ÚurllibÚparseÚunquoteZaddress_stringZclient_addressZheadersÚgetZget_content_typeÚitemsÚreplaceÚupperÚstrip)r   r"   r2   Ú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   s    zWSGIRequestHandler.get_stderrc                 C   s€   | j  d¡| _t| jƒdkr<d| _d| _d| _|  d¡ dS |  ¡ sHdS t	| j | j
|  ¡ |  ¡ dd�}| |_| | j ¡ ¡ dS )zHandle a single HTTP requesti  i   r   iž  NF)Zmultithread)ZrfileÚreadlineZraw_requestlineÚlenZrequestliner/   r1   Z
send_errorZparse_requestr   ZwfilerC   r@   r   Úrunr-   r&   )r   Úhandlerr   r   r   Úhandles   s    
þzWSGIRequestHandler.handleN)r   r   r   Ú__version__r0   r@   rC   rH   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� q8|ddgƒ | ¡  d¡gS )	Nr   )ÚStringIOzHello world!)Úfileú=z200 OK)zContent-Typeztext/plain; charset=utf-8zutf-8)ÚiorJ   ÚprintÚsortedr7   ÚreprÚgetvalueÚencode)ÚenvironZstart_responserJ   ÚstdoutÚhr>   r?   r   r   r   r   Š   s    
r   c                 C   s   || |f|ƒ}|  |¡ |S )zACreate a new WSGI server listening on `host` and `port` for `app`)r'   )r<   ÚportZappZserver_classZhandler_classr-   r   r   r   r	   –   s    
r	   Ú__main__r   i@  zServing HTTP onrV   r   z...zhttp://localhost:8000/xyz?abc) r(   Zhttp.serverr   r   rA   Úurllib.parser3   Zwsgiref.handlersr   Úplatformr   rI   Ú__all__r0   Úversionr   Úsys_versionr   r   r   r   r   r	   r   ZhttpdÚsocketÚgetsocknameÚsarN   Z
webbrowserÚopenZhandle_requestr   r   r   r   Ú<module>   s.   Cÿ
	


