
 XO                 @   sW  d  Z  d d l m 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 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 j d  Z d d   Z d d   Z Gd d   d e j e  Z Gd d   d e j e  Z Gd d   d e j e  Z d d 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!
    )unicode_literalsN)simple_server)ImproperlyConfigured)
ISO_8859_1UTF_8)get_wsgi_application)six)
uri_to_iri)import_string)socketserver
WSGIServerWSGIRequestHandlerzdjango.serverc              C   s   d d l  m }  t |  d  } | d k r2 t   Sy t |  SWn` t k
 r } z@ d i | d 6| d 6} t j t t |  t	 j
   d  WYd d } ~ Xn Xd S)	aU  
    Loads and returns 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``), we just return
    whatever ``django.core.wsgi.get_wsgi_application`` returns.
    r   )settingsZWSGI_APPLICATIONNz\WSGI application '%(app_path)s' could not be loaded; Error importing module: '%(exception)s'app_path	exception   )Zdjango.confr   getattrr   r
   ImportErrorr   reraiser   sysexc_info)r   r   emsg r   J/home/ubuntu/projects/ifolica/build/django/django/core/servers/basehttp.pyget_internal_wsgi_application   s    r   c              C   s?   t  j   d  d  \ }  } t |  t j  o> | j d d k S)Nr   r       )r   r   
issubclasssocketerrorargs)exc_type	exc_valuer   r   r   is_broken_pipe_error>   s    r#   c                   sR   e  Z d  Z d Z d Z   f d d   Z   f d d   Z   f d d   Z   S)	r   z7BaseHTTPServer that implements the Python WSGI protocol
   c                sS   | j  d d  r! t j |  _ n  | j  d d  |  _ t t |   j | |   d  S)Nipv6Fallow_reuse_addressT)popr   AF_INET6address_familyr&   superr   __init__)selfr    kwargs)	__class__r   r   r+   H   s    zWSGIServer.__init__c                s!   t  t |   j   |  j   d S)z.Override server_bind to store the server name.N)r*   r   server_bindZsetup_environ)r,   )r.   r   r   r/   N   s    zWSGIServer.server_bindc                s9   t    r t j d |  n t t |   j | |  d  S)Nz- Broken pipe from %s
)r#   loggerinfor*   r   handle_error)r,   requestclient_address)r.   r   r   r2   S   s    	zWSGIServer.handle_error)__name__
__module____qualname____doc__request_queue_sizer+   r/   r2   r   r   )r.   r   r   C   s
   c                   s"   e  Z d  Z   f d d   Z   S)ServerHandlerc                s#   t    s t t |   j   n  d  S)N)r#   r*   r:   r2   )r,   )r.   r   r   r2   \   s    	zServerHandler.handle_error)r5   r6   r7   r2   r   r   )r.   r   r:   [   s   r:   c                   sF   e  Z d  Z d d   Z d d   Z   f d d   Z d d   Z   S)	r   c             C   s   |  j  d S)Nr   )r4   )r,   r   r   r   address_stringc   s    z!WSGIRequestHandler.address_stringc             G   s
  i |  j  d 6|  j   d 6} | d d d k rn | d j t d   rn d | d <t j d	 d
 | d  Sn  | 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)Nr3   Zserver_time   r   4zi  status_codezOYou're accessing the development server over HTTPS, but it only supports HTTP.
extra   i  )r3   Zlog_date_time_string
startswithstrr0   r   isdigitlenintwarningr1   )r,   formatr    r?   r>   levelr   r   r   log_messageg   s(    

&
	zWSGIRequestHandler.log_messagec                s   x6 |  j  j   D]% \ } } d | k r |  j  | =q q Wt t |   j   } |  j } d | k ry | j d  d } n  t |  j t	  } t
 j r | j t  n | | d <| S)N_?r   Z	PATH_INFO)headersitemsr*   r   get_environpath	partitionr	   encoder   r   PY3decoder   )r,   kvenvrO   )r.   r   r   rN      s    	"zWSGIRequestHandler.get_environc             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)z<Copy of WSGIRequestHandler, but with different ServerHandleri  i    i  N)rfilereadlineZraw_requestlinerD   ZrequestlineZrequest_versioncommandZ
send_errorZparse_requestr:   wfileZ
get_stderrrN   Zrequest_handlerrunserverZget_app)r,   handlerr   r   r   handle   s    			$	zWSGIRequestHandler.handle)r5   r6   r7   r;   rI   rN   r_   r   r   )r.   r   r   b   s   Fc             C   s~   |  | f } | r6 t  t d  t j t f i   } n t } | | t d | } | rc d | _ n  | j |  | j   d  S)Nr   r%   T)	typerB   r   ThreadingMixInr   r   daemon_threadsZset_appserve_forever)addrportZwsgi_handlerr%   	threadingserver_addressZ	httpd_clsZhttpdr   r   r   r\      s    $r\   )z
WSGIServerzWSGIRequestHandler)!r8   
__future__r   loggingr   r   Zwsgirefr   Zdjango.core.exceptionsr   Zdjango.core.handlers.wsgir   r   Zdjango.core.wsgir   Zdjango.utilsr   Zdjango.utils.encodingr	   Zdjango.utils.module_loadingr
   Zdjango.utils.six.movesr   __all__	getLoggerr0   r   r#   r   objectr:   r   r\   r   r   r   r   <module>   s(    O