U
    mfk"                     @   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-siwebuq3/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_error7   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"   H   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*   N   s    zWSGIServer.handle_error)__name__
__module____qualname____doc__request_queue_sizer"   r*   __classcell__r   r   r&   r   r   C   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"   Z   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_connectionsh   s    z%ThreadedWSGIServer._close_connectionsc                    s   |    t | d S r   )r:   r!   close_request)r#   r+   r&   r   r   r;   l   s    z ThreadedWSGIServer.close_request)
r-   r.   r/   r0   daemon_threadsr"   r6   r:   r;   r2   r   r   r&   r   r3   U   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"   t   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=   q   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  rt|d d
krt|d }||d< |dkrtj}q|dkrtj}qt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  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&   	
&O