
v^                 @   s3  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 Z e j d
  Z d d   Z d d   Z Gd d   d e j  Z Gd d   d e j e  Z Gd d   d e j  Z Gd d	   d	 e j  Z d d e 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)import_string
WSGIServerWSGIRequestHandlerzdjango.serverc              C   s   d d l  m }  t |  d  } | d k r2 t   Sy t |  SWn8 t k
 rz } z t d |  |  WYd d } ~ Xn Xd 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-build-8lau8j11/django/django/core/servers/basehttp.pyget_internal_wsgi_application   s    r   c              C   s"   t  j   \ }  } } t |  t  S)N)sysexc_info
issubclassBrokenPipeError)exc_type_r   r   r   is_broken_pipe_error5   s    r   c                   sL   e  Z d  Z d Z d Z d d d d   f d d  Z   f d	 d
   Z   S)r   z7BaseHTTPServer that implements the Python WSGI protocol
   ipv6Fallow_reuse_addressTc               s2   | r t  j |  _ | |  _ t   j | |   d  S)N)socketAF_INET6address_familyr   super__init__)selfr   r   argskwargs)	__class__r   r   r   ?   s    	zWSGIServer.__init__c                s3   t    r t j d |  n t   j | |  d  S)Nz- Broken pipe from %s
)r   loggerinfor   handle_error)r   requestclient_address)r"   r   r   r%   E   s    	zWSGIServer.handle_error)__name__
__module____qualname____doc__request_queue_sizer   r%   r   r   )r"   r   r   :   s   c               @   s   e  Z d  Z d Z d Z d S)ThreadedWSGIServerz$A threaded version of the WSGIServerTN)r(   r)   r*   r+   daemon_threadsr   r   r   r   r-   L   s   r-   c                   s^   e  Z d  Z d Z   f d d   Z   f d d   Z   f d d   Z   f d d	   Z   S)
ServerHandlerz1.1c                sc   y t  | j d   } Wn t t f k
 r9 d } Yn Xt   j t | |  | | | |  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!   Zcontent_length)r"   r   r   r   T   s
    zServerHandler.__init__c                sy   t    j   d |  j k r, d |  j d <n% t |  j j t j  sQ d |  j d <|  j j d  d k ru d |  j _	 d  S)NzContent-Lengthclose
ConnectionT)
r   cleanup_headersheaders
isinstancerequest_handlerserversocketserverThreadingMixInr2   close_connection)r   )r"   r   r   r;   a   s    zServerHandler.cleanup_headersc                s!   |  j    j   t   j   d  S)N)Z	get_stdinZ_read_limitedr   r9   )r   )r"   r   r   r9   p   s    zServerHandler.closec                s   t    s t   j   d  S)N)r   r   r%   )r   )r"   r   r   r%   t   s    	zServerHandler.handle_error)r(   r)   r*   http_versionr   r;   r9   r%   r   r   )r"   r   r/   Q   s
   r/   c                   sX   e  Z d  Z d Z d d   Z d d   Z   f d d   Z d d	   Z d
 d   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   d |  j  d |  j   i } | d d d k rc | d j d  rc d | d <t j d	 d
 | d  S| d j   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	 } | | | d
 | d  S)Nr&   Zserver_time   r   4zi  status_codezOYou're accessing the development server over HTTPS, but it only supports HTTP.
extra   i  )
r&   Zlog_date_time_string
startswithr#   errorisdigitlenr1   warningr$   )r   formatr    rH   rG   levelr   r   r   log_message   s&    	
&
	zWSGIRequestHandler.log_messagec                s7   x' |  j  D] } d | k r
 |  j  | =q
 Wt   j   S)Nr   )r<   r   get_environ)r   k)r"   r   r   rR      s    zWSGIRequestHandler.get_environc             C   sc   d |  _  |  j   x |  j  s, |  j   q Wy |  j j t j  Wn t t f k
 r^ Yn Xd  S)NT)rB   handle_one_request
connectionshutdownr   SHUT_WRAttributeErrorOSError)r   r   r   r   handle   s    	
zWSGIRequestHandler.handlec             C   s   |  j  j d  |  _ t |  j  d k rV d |  _ d |  _ d |  _ |  j d  d S|  j   sf d St	 |  j  |  j
 |  j   |  j    } |  | _ | j |  j j    d S)zDCopy of WSGIRequestHandler.handle() but with different ServerHandleri  i    i  N)rfilereadlineZraw_requestlinerM   ZrequestlineZrequest_versioncommandZ
send_errorZparse_requestr/   wfileZ
get_stderrrR   r>   runr?   Zget_app)r   handlerr   r   r   rT      s    			$	z%WSGIRequestHandler.handle_one_request)	r(   r)   r*   Zprotocol_versionrD   rQ   rR   rZ   rT   r   r   )r"   r   r   z   s   
Fc       	      C   su   |  | f } | r0 t  d t j | f i   } n | } | | t d | } | rZ d | _ | j |  | j   d  S)Nr   r   T)typer@   rA   r   r.   Zset_appserve_forever)	addrportZwsgi_handlerr   	threadingZ
server_clsserver_addressZ	httpd_clsZhttpdr   r   r   r`      s    	r`   )z
WSGIServerzWSGIRequestHandler)r+   loggingr   r@   r   wsgirefr   Zdjango.core.exceptionsr   Zdjango.core.handlers.wsgir   Zdjango.core.wsgir   Zdjango.utils.module_loadingr   __all__	getLoggerr#   r   r   r   rA   r-   r/   r   r`   r   r   r   r   <module>   s$   )N