a
    LcòfÐ&  ã                   @   sö   d Z ddlZddlZddlZddlZddlmZ ddlmZ ddl	m
Z
 ddlmZ ddlmZ ddlmZ dd	lmZ d
Ze d¡Zdd„ Zdd„ ZG dd„ dejƒZG dd„ dejeƒZG dd„ dejƒZG dd„ dejƒZdddefdd„ZdS )a0  
HTTP server that implements the Python WSGI protocol (PEP 333, rev 1.21).

Based on wsgiref.simple_server which is part of the standard library since 2.5.

This is a simple server for use in testing or debugging Django apps. It hasn't
been reviewed for security issues. DON'T USE IT FOR PRODUCTION USE!
é    N)Údeque)Úsimple_server)ÚImproperlyConfigured)ÚLimitedStream)Úget_wsgi_application)Úconnections)Úimport_string)Ú
WSGIServerÚWSGIRequestHandlerzdjango.serverc               
   C   sf   ddl m}  t| dƒ}|du r$tƒ S z
t|ƒW S  ty` } ztd| ƒ|‚W Y d}~n
d}~0 0 dS )aK  
    Load and return the WSGI application as configured by the user in
    ``settings.WSGI_APPLICATION``. With the default ``startproject`` layout,
    this will be the ``application`` object in ``projectname/wsgi.py``.

    This function, and the ``WSGI_APPLICATION`` setting itself, are only useful
    for Django's internal server (runserver); external WSGI servers should just
    be configured to point to the correct application object directly.

    If settings.WSGI_APPLICATION is not set (is ``None``), return
    whatever ``django.core.wsgi.get_wsgi_application`` returns.
    r   )ÚsettingsZWSGI_APPLICATIONNzBWSGI application '%s' could not be loaded; Error importing module.)Zdjango.confr   Úgetattrr   r   ÚImportErrorr   )r   Zapp_pathÚerr© r   úY/home/httpd/docs/test/DocsMgr/lib/python3.9/site-packages/django/core/servers/basehttp.pyÚget_internal_wsgi_application   s    

ÿÿýr   c                  C   s   t  ¡ \} }}t| tttfƒS ©N)ÚsysÚexc_infoÚ
issubclassÚBrokenPipeErrorÚConnectionAbortedErrorÚConnectionResetError)Úexc_typeÚ_r   r   r   Úis_broken_pipe_error8   s    ýþr   c                       s8   e Zd ZdZdZdddœ‡ fdd„
Z‡ fdd	„Z‡  ZS )
r	   z7BaseHTTPServer that implements the Python WSGI protocolé
   FT)Úipv6Úallow_reuse_addressc                   s(   |rt j| _|| _tƒ j|i |¤Ž d S r   )ÚsocketÚAF_INET6Úaddress_familyr   ÚsuperÚ__init__)Úselfr   r   ÚargsÚkwargs©Ú	__class__r   r   r#   I   s    zWSGIServer.__init__c                    s&   t ƒ rt d|¡ ntƒ  ||¡ d S )Nz- Broken pipe from %s)r   ÚloggerÚinfor"   Úhandle_error)r$   ÚrequestÚclient_addressr'   r   r   r+   O   s    zWSGIServer.handle_error)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Úrequest_queue_sizer#   r+   Ú__classcell__r   r   r'   r   r	   D   s   r	   c                       sJ   e Zd ZdZdZddœ‡ fdd„
Z‡ fdd„Zd	d
„ Z‡ fdd„Z‡  Z	S )ÚThreadedWSGIServerz$A threaded version of the WSGIServerTN)Úconnections_overridec                   s   t ƒ j|i |¤Ž || _d S r   )r"   r#   r5   )r$   r5   r%   r&   r'   r   r   r#   [   s    zThreadedWSGIServer.__init__c                    s4   | j r"| j  ¡ D ]\}}|t|< qtƒ  ||¡ d S r   )r5   Úitemsr   r"   Úprocess_request_thread)r$   r,   r-   ÚaliasÚconnr'   r   r   r7   a   s    
z)ThreadedWSGIServer.process_request_threadc                 C   s   t  ¡  d S r   )r   Z	close_all©r$   r   r   r   Ú_close_connectionsi   s    z%ThreadedWSGIServer._close_connectionsc                    s   |   ¡  tƒ  |¡ d S r   )r;   r"   Úclose_request)r$   r,   r'   r   r   r<   m   s    z ThreadedWSGIServer.close_request)
r.   r/   r0   r1   Údaemon_threadsr#   r7   r;   r<   r3   r   r   r'   r   r4   V   s   r4   c                       sD   e Zd ZdZ‡ fdd„Z‡ fdd„Z‡ fdd„Z‡ fdd	„Z‡  ZS )
ÚServerHandlerz1.1c              	      sR   zt | d¡ƒ}W n ttfy,   d}Y n0 tƒ jt||ƒ|||fi |¤Ž dS )a#  
        Use a LimitedStream so that unread request data will be ignored at
        the end of the request. WSGIRequest uses a LimitedStream but it
        shouldn't discard the data since the upstream servers usually do this.
        This fix applies only for testserver/runserver.
        ÚCONTENT_LENGTHr   N)ÚintÚgetÚ
ValueErrorÚ	TypeErrorr"   r#   r   )r$   ÚstdinÚstdoutÚstderrÚenvironr&   Úcontent_lengthr'   r   r   r#   u   s    
ÿÿzServerHandler.__init__c                    s„   t ƒ  ¡  | jd dkr*d| jv r*| jd= | jd dkrNd| jvrNd| jd< nt| jjtjƒshd| jd< | j 	d¡dkr€d| j_
d S )NÚREQUEST_METHODÚHEADzContent-LengthÚcloseÚ
ConnectionT)r"   Úcleanup_headersrG   ÚheadersÚ
isinstanceÚrequest_handlerÚserverÚsocketserverÚThreadingMixInrA   Úclose_connectionr:   r'   r   r   rM   „   s    
ÿþÿþ
zServerHandler.cleanup_headersc                    s   |   ¡  ¡  tƒ  ¡  d S r   )Z	get_stdinÚreadr"   rK   r:   r'   r   r   rK   ›   s    zServerHandler.closec                    sR   | j d dkrDz(t| jdd� | js,|  ¡  W |  ¡  qN|  ¡  0 n
tƒ  ¡  d S )NrI   rJ   r   )Úmaxlen)rG   r   ÚresultZheaders_sentÚsend_headersrK   r"   Úfinish_responser:   r'   r   r   rY   Ÿ   s    
zServerHandler.finish_response)	r.   r/   r0   Úhttp_versionr#   rM   rK   rY   r3   r   r   r'   r   r>   r   s
   r>   c                       s@   e Zd ZdZdd„ Zdd„ Z‡ fdd„Zdd	„ Zd
d„ Z‡  Z	S )r
   zHTTP/1.1c                 C   s
   | j d S )Nr   )r-   r:   r   r   r   Úaddress_string³   s    z!WSGIRequestHandler.address_stringc                 G   sÀ   | j |  ¡ dœ}|d d dkrH|d  d¡rHd|d< tjd|d	� d S |d  ¡ r t|d ƒd
kr t|d ƒ}||d< |dkrˆtj}q¦|dkr˜tj}q¦tj	}ntj	}||g|¢R d|iŽ d S )N)r,   Zserver_timeé   r   Ú4ziô  Ústatus_codezNYou're accessing the development server over HTTPS, but it only supports HTTP.)Úextraé   i�  r_   )
r,   Zlog_date_time_stringÚ
startswithr)   ÚerrorÚisdigitÚlenr@   Úwarningr*   )r$   Úformatr%   r_   r^   Úlevelr   r   r   Úlog_message·   s*    þýzWSGIRequestHandler.log_messagec                    s&   | j D ]}d|v r| j |= qtƒ  ¡ S )Nr   )rN   r"   Úget_environ)r$   Úkr'   r   r   ri   Ö   s    

zWSGIRequestHandler.get_environc              	   C   sL   d| _ |  ¡  | j s|  ¡  qz| j tj¡ W n ttfyF   Y n0 d S )NT)rT   Úhandle_one_requestÚ
connectionÚshutdownr   ÚSHUT_WRÚAttributeErrorÚOSErrorr:   r   r   r   Úhandleá   s    
zWSGIRequestHandler.handlec                 C   s|   | j  d¡| _t| jƒdkr<d| _d| _d| _|  d¡ dS |  ¡ sHdS t	| j | j
|  ¡ |  ¡ ƒ}| |_| | j ¡ ¡ dS )zDCopy of WSGIRequestHandler.handle() but with different ServerHandleri  i   Ú iž  N)ÚrfileÚreadlineZraw_requestlinerd   ZrequestlineZrequest_versionÚcommandZ
send_errorZparse_requestr>   ÚwfileZ
get_stderrri   rP   ÚrunrQ   Zget_app)r$   Úhandlerr   r   r   rk   ë   s    
ÿz%WSGIRequestHandler.handle_one_request)
r.   r/   r0   Zprotocol_versionr[   rh   ri   rq   rk   r3   r   r   r'   r   r
   °   s   
r
   Fc           
      C   sj   | |f}|r t dtj|fi ƒ}n|}||t|d�}	|d urJ|t|	d|ƒƒ |rTd|	_|	 |¡ |	 ¡  d S )Nr	   )r   Zserver_portT)ÚtyperR   rS   r
   r   r=   Zset_appÚserve_forever)
ÚaddrÚportZwsgi_handlerr   Ú	threadingZon_bindZ
server_clsÚserver_addressZ	httpd_clsZhttpdr   r   r   rw   ÿ   s    	
rw   )r1   Úloggingr   rR   r   Úcollectionsr   Úwsgirefr   Zdjango.core.exceptionsr   Zdjango.core.handlers.wsgir   Zdjango.core.wsgir   Z	django.dbr   Zdjango.utils.module_loadingr   Ú__all__Ú	getLoggerr)   r   r   r	   rS   r4   r>   r
   rw   r   r   r   r   Ú<module>   s0   	
>Sù