
0,ƒY—  ã               @   sL  d  Z  d d l m 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 d l m Z 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)Úcolor_style)Úget_wsgi_application)Úsix)Ú
uri_to_iri)Úimport_string)ÚsocketserverÚ
WSGIServerÚWSGIRequestHandlerc              C   s¥   d d l  m }  t |  d ƒ } | d k r2 t ƒ  Sy t | ƒ SWn^ t k
 r  } z> d d | d | i } 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   ú>/tmp/pip-build-2gsypclq/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                s;   | j  d d ƒ r t j |  _ t t |  ƒ j | | Ž  d  S)NÚipv6F)Úpopr   ÚAF_INET6Úaddress_familyÚsuperr   Ú__init__)Úselfr!   Úkwargs)Ú	__class__r   r   r+   F   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/   K   s    zWSGIServer.server_bindc                s@   t  ƒ  r# t j j d | f ƒ n t t |  ƒ j | | ƒ d  S)Nz- Broken pipe from %s
)r$   r   ÚstderrÚwriter*   r   Úhandle_error)r,   ÚrequestÚclient_address)r.   r   r   r2   P   s    	zWSGIServer.handle_error)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Úrequest_queue_sizer+   r/   r2   r   r   )r.   r   r   A   s
   c                   s"   e  Z d  Z ‡  f d d †  Z ‡  S)ÚServerHandlerc                s    t  ƒ  s t t |  ƒ j ƒ  d  S)N)r$   r*   r:   r2   )r,   )r.   r   r   r2   Y   s    	zServerHandler.handle_error)r5   r6   r7   r2   r   r   )r.   r   r:   X   s   r:   c                   sX   e  Z d  Z ‡  f d d †  Z d d „  Z d d „  Z ‡  f d d †  Z d	 d
 „  Z ‡  S)r   c                s)   t  ƒ  |  _ t t |  ƒ j | | Ž  d  S)N)r   Ústyler*   r   r+   )r,   r!   r-   )r.   r   r   r+   a   s    zWSGIRequestHandler.__init__c             C   s   |  j  d S)Nr   )r4   )r,   r   r   r   Úaddress_stringe   s    z!WSGIRequestHandler.address_stringc             G   sx  d |  j  ƒ  } y | d | | 7} Wn t k
 rD | d 7} Yn X| d d d k rn |  j j | ƒ } nö | d d d k r— |  j j | ƒ } nÍ | d d k r¼ |  j j | ƒ } n¨ | d d d	 k rå |  j j | ƒ } n | d d
 k r
|  j j | ƒ } nZ | d d d k rR| d j t	 d ƒ ƒ r=d } |  j j
 | ƒ } n |  j j | ƒ } t j j | ƒ d  S)Nz[%s] z%s
Ú
é   r   Ú2Ú1Z304Ú3Z404Ú4zzOYou're accessing the development server over HTTPS, but it only supports HTTP.
)Zlog_date_time_stringÚUnicodeDecodeErrorr;   ZHTTP_SUCCESSZ	HTTP_INFOZHTTP_NOT_MODIFIEDZHTTP_REDIRECTZHTTP_NOT_FOUNDÚ
startswithÚstrZHTTP_BAD_REQUESTZHTTP_SERVER_ERRORr   r0   r1   )r,   Úformatr!   r   r   r   r   Úlog_messagei   s*    zWSGIRequestHandler.log_messagec                s®   x3 |  j  j ƒ  D]" \ } } d | k r |  j  | =q Wt t |  ƒ j ƒ  } |  j } d | k rs | j d ƒ d } 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ÚenvrM   )r.   r   r   rL   Š   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_requestlineÚlenZrequestlineZrequest_versionÚcommandZ
send_errorZparse_requestr:   ÚwfileZ
get_stderrrL   Zrequest_handlerÚrunÚserverZget_app)r,   Úhandlerr   r   r   Úhandle¡   s    			$	zWSGIRequestHandler.handle)r5   r6   r7   r+   r<   rG   rL   r^   r   r   )r.   r   r   _   s
   !Fc             C   s{   |  | f } | r6 t  t d ƒ t j t f i  ƒ } n t } | | t d | ƒ} | r` d | _ | j | ƒ | j ƒ  d  S)Nr   r&   T)	ÚtyperE   r   ÚThreadingMixInr   r   Údaemon_threadsZset_appÚserve_forever)ÚaddrÚportZwsgi_handlerr&   Ú	threadingÚserver_addressZ	httpd_clsZhttpdr   r   r   r[   ¶   s    $	r[   )r   r   ) r8   Ú
__future__r   r   r   Úwsgirefr   Zdjango.core.exceptionsr   Zdjango.core.handlers.wsgir   r   Zdjango.core.management.colorr   Zdjango.core.wsgir   Zdjango.utilsr	   Zdjango.utils.encodingr
   Zdjango.utils.module_loadingr   Zdjango.utils.six.movesr   Ú__all__r   r$   r   Úobjectr:   r   r[   r   r   r   r   Ú<module>   s&    W