U
    ©}agÐ&  ã                   @   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   sd   ddl m}  t| dƒ}|dkr$tƒ S z
t|ƒW S  tk
r^ } ztd| ƒ|‚W 5 d}~X Y nX 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   úT/var/www/html/publicvenv/lib/python3.8/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||Ž 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||Ž || _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              	      sP   zt | d¡ƒ}W n ttfk
r.   d}Y nX tƒ jt||ƒ|||f|Ž 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.
        Z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kr*| jd= | jd dkrNd| jk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_headersrF   ÚheadersÚ
isinstanceÚrequest_handlerÚserverÚsocketserverÚThreadingMixInr@   Úclose_connectionr:   r'   r   r   rL   „   s    
ÿþÿþ
zServerHandler.cleanup_headersc                    s   |   ¡  ¡  tƒ  ¡  d S r   )Z	get_stdinÚreadr"   rJ   r:   r'   r   r   rJ   ›   s    zServerHandler.closec                    sJ   | j d dkr<z t| jdd� | js,|  ¡  W 5 |  ¡  X n
tƒ  ¡  d S )NrH   rI   r   )Úmaxlen)rF   rJ   r   ÚresultZheaders_sentÚsend_headersr"   Úfinish_responser:   r'   r   r   rX   Ÿ   s    zServerHandler.finish_response)	r.   r/   r0   Úhttp_versionr#   rL   rJ   rX   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	}||f|ž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|kr| j |= qtƒ  ¡ S )Nr   )rM   r"   Úget_environ)r$   Úkr'   r   r   rh   Ö   s    

zWSGIRequestHandler.get_environc              	   C   sN   d| _ |  ¡  | j s|  ¡  qz| j tj¡ W n ttfk
rH   Y nX d S )NT)rS   Ú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_requestlinerc   ZrequestlineZrequest_versionÚcommandZ
send_errorZparse_requestr>   ÚwfileZ
get_stderrrh   rO   ÚrunrP   Zget_app)r$   Úhandlerr   r   r   rj   ë   s"    
   ÿz%WSGIRequestHandler.handle_one_request)
r.   r/   r0   Zprotocol_versionrZ   rg   rh   rp   rj   r3   r   r   r'   r   r
   °   s   
r
   Fc           
      C   sj   | |f}|r t dtj|fi ƒ}n|}||t|d�}	|d k	rJ|t|	d|ƒƒ |rTd|	_|	 |¡ |	 ¡  d S )Nr	   )r   Zserver_portT)ÚtyperQ   rR   r
   r   r=   Zset_appÚserve_forever)
ÚaddrÚportZwsgi_handlerr   Ú	threadingZon_bindZ
server_clsÚserver_addressZ	httpd_clsZhttpdr   r   r   rv   ÿ   s    	
rv   )r1   Úloggingr   rQ   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	   rR   r4   r>   r
   rv   r   r   r   r   Ú<module>   s0   	
>Sù