U
    5øi "  ã                   @   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	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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)Ú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   ú@/tmp/pip-unpacked-wheel-gcyiijx6/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_error6   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"   D   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*   J   s    zWSGIServer.handle_error)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Úrequest_queue_sizer"   r*   Ú__classcell__r   r   r&   r   r   ?   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"   r4   )r#   r4   r$   r%   r&   r   r   r"   U   s    zThreadedWSGIServer.__init__c                    s4   | j r"| j  ¡ D ]\}}|t|< qtƒ  ||¡ d S r   )r4   Úitemsr   r!   Úprocess_request_thread)r#   r+   r,   ÚaliasÚconnr&   r   r   r6   [   s    
z)ThreadedWSGIServer.process_request_threadc                 C   s   t  ¡  d S r   )r   Z	close_all©r#   r   r   r   Ú_close_connectionsc   s    z%ThreadedWSGIServer._close_connectionsc                    s   |   ¡  tƒ  |¡ d S r   )r:   r!   Úclose_request)r#   r+   r&   r   r   r;   g   s    z ThreadedWSGIServer.close_request)
r-   r.   r/   r0   Údaemon_threadsr"   r6   r:   r;   r2   r   r   r&   r   r3   Q   s   r3   c                       s8   e Zd Z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.
        ÚCONTENT_LENGTHr   N)ÚintÚgetÚ
ValueErrorÚ	TypeErrorr!   r"   r   )r#   ÚstdinÚstdoutÚstderrÚenvironr%   Úcontent_lengthr&   r   r   r"   o   s
    
zServerHandler.__init__c                    sV   t ƒ  ¡  d| jkr d| jd< nt| jjtjƒs:d| jd< | j d¡dkrRd| j_	d S )NzContent-LengthÚcloseÚ
ConnectionT)
r!   Úcleanup_headersÚheadersÚ
isinstanceÚrequest_handlerÚserverÚsocketserverÚThreadingMixInr@   Úclose_connectionr9   r&   r   r   rJ   |   s    


zServerHandler.cleanup_headersc                    s   |   ¡  ¡  tƒ  ¡  d S r   )Z	get_stdinZ_read_limitedr!   rH   r9   r&   r   r   rH   ‹   s    zServerHandler.close)r-   r.   r/   Úhttp_versionr"   rJ   rH   r2   r   r   r&   r   r=   l   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,   r9   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_codezOYou're accessing the development server over HTTPS, but it only supports HTTP.
)Úextraé   i�  rW   )
r+   Zlog_date_time_stringÚ
startswithr(   ÚerrorÚisdigitÚlenr?   Úwarningr)   )r#   Úformatr$   rW   rV   Úlevelr   r   r   Úlog_message—   s*    þþzWSGIRequestHandler.log_messagec                    s&   | j D ]}d|kr| j |= qtƒ  ¡ S )Nr   )rK   r!   Úget_environ)r#   Úkr&   r   r   ra   µ   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)rQ   Úhandle_one_requestÚ
connectionÚshutdownr   ÚSHUT_WRÚAttributeErrorÚOSErrorr9   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_requestliner\   ZrequestlineZrequest_versionÚcommandZ
send_errorZparse_requestr=   ÚwfileZ
get_stderrra   rM   ÚrunrN   Zget_app)r#   Úhandlerr   r   r   rc   Ê   s"    
   ÿz%WSGIRequestHandler.handle_one_request)
r-   r.   r/   Zprotocol_versionrS   r`   ra   ri   rc   r2   r   r   r&   r   r	   �   s   
r	   Fc           	      C   sR   | |f}|r t dtj|fi ƒ}n|}||t|d�}|r<d|_| |¡ | ¡  d S )Nr   )r   T)ÚtyperO   rP   r	   r<   Zset_appÚserve_forever)	ÚaddrÚportZwsgi_handlerr   Ú	threadingZ
server_clsÚserver_addressZ	httpd_clsZhttpdr   r   r   ro   Þ   s    
ro   )r0   Úloggingr   rO   r   Ú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   rP   r3   r=   r	   ro   r   r   r   r   Ú<module>   s&   	
	$N