
    +{Bi0&                     v   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d	lmZ dd
lmZ dZ ej,                  d      Zd Zd Z G d dej4                        Z G d dej6                  e      Z G d dej:                        Z G d dej<                        ZdddefdZy)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)deque)simple_server)ImproperlyConfigured)LimitedStream)get_wsgi_application)connections)log_message)import_string)
WSGIServerWSGIRequestHandlerzdjango.serverc                      ddl m}  t        | d      }|
t               S 	 t	        |      S # t
        $ r}t        d|z        |d}~ww xY w)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   )settingsWSGI_APPLICATIONNzBWSGI application '%s' could not be loaded; Error importing module.)django.confr   getattrr   r
   ImportErrorr   )r   app_patherrs      Q/var/www/python/venv/lib/python3.12/site-packages/django/core/servers/basehttp.pyget_internal_wsgi_applicationr      sb     %x!34H#%%X&& "&(01
 	s   
+ 	AAAc                  h    t        j                         \  } }}t        | t        t        t
        f      S N)sysexc_info
issubclassBrokenPipeErrorConnectionAbortedErrorConnectionResetError)exc_type_s     r   is_broken_pipe_errorr!   9   s1    \\^NHa" 	
     c                   8     e Zd ZdZdZddd fd
Z fdZ xZS )r   z7BaseHTTPServer that implements the Python WSGI protocol
   FT)ipv6allow_reuse_addressc                `    |rt         j                  | _        || _        t	        |   |i | y r   )socketAF_INET6address_familyr&   super__init__)selfr%   r&   argskwargs	__class__s        r   r,   zWSGIServer.__init__J   s,    "(//D#6 $)&)r"   c                 h    t               rt        j                  d|       y t        |   ||       y )Nz- Broken pipe from %s)r!   loggerinfor+   handle_error)r-   requestclient_addressr0   s      r   r4   zWSGIServer.handle_errorP   s'    !KK/@G .9r"   )__name__
__module____qualname____doc__request_queue_sizer,   r4   __classcell__r0   s   @r   r   r   E   s     A#(d *: :r"   r   c                   F     e Zd ZdZdZdd fd
Z fdZd Z fdZ xZ	S )	ThreadedWSGIServerz$A threaded version of the WSGIServerTN)connections_overridec                2    t        |   |i | || _        y r   )r+   r,   r@   )r-   r@   r.   r/   r0   s       r   r,   zThreadedWSGIServer.__init__\   s    $)&)$8!r"   c                     | j                   r+| j                   j                         D ]  \  }}|t        |<    t        |   ||       y r   )r@   itemsr   r+   process_request_thread)r-   r5   r6   aliasconnr0   s        r   rD   z)ThreadedWSGIServer.process_request_threadb   sI    $$  $88>>@ *t%)E"*&w?r"   c                 ,    t        j                          y r   )r   	close_allr-   s    r   _close_connectionsz%ThreadedWSGIServer._close_connectionsj   s    r"   c                 D    | j                          t        | 	  |       y r   )rJ   r+   close_request)r-   r5   r0   s     r   rL   z ThreadedWSGIServer.close_requestn   s    !g&r"   )
r7   r8   r9   r:   daemon_threadsr,   rD   rJ   rL   r<   r=   s   @r   r?   r?   W   s(    .N37 9@ ' 'r"   r?   c                   @     e Zd ZdZ fdZ fdZ fdZ fdZ xZS )ServerHandlerz1.1c                     	 t        |j                  d            }t	        |   t        ||      |||fi | y# t        t        f$ r d}Y 1w xY w)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_lengthr0   s          r   r,   zServerHandler.__init__v   s^    	 -=!>?N 	%0&&'	
MS	
 I& 	N	s   ; AAc                    t         |           | j                  d   dk(  r6d| j                  v r(t	        | j                  d         dk(  r| j                  d= | j                  d   dk7  rd| j                  vrd| j                  d<   n=t        | j                  j                  t        j                        sd| j                  d<   | j                  j                  d      dk(  rd| j                  _        y y )NREQUEST_METHODHEADzContent-Length0close
ConnectionT)r+   cleanup_headersrY   headersstr
isinstancerequest_handlerserversocketserverThreadingMixInrS   close_connectionr-   r0   s    r   ra   zServerHandler.cleanup_headers   s    !LL)*f4 DLL0DLL!123s:-.
 LL)*f4 4)0DLL&D00779T9TU)0DLL& <<L)W448D  1 5r"   c                 ^    | j                         j                          t        |           y r   )	get_stdinreadr+   r_   rj   s    r   r_   zServerHandler.close   s    r"   c                     | j                   d   dk(  rE	 t        | j                  d       | j                  s| j	                          | j                          y t        |           y # | j                          w xY w)Nr\   r]   r   )maxlen)rY   r   resultheaders_sentsend_headersr_   r+   finish_responserj   s    r   rs   zServerHandler.finish_response   s`    <<()V3
dkk!, ((%%'

G#% 

s   3A( (A:)	r7   r8   r9   http_versionr,   ra   r_   rs   r<   r=   s   @r   rO   rO   s   s!    L
90& &r"   rO   c                   :     e Zd ZdZd Zd Z fdZd Zd Z xZ	S )r   zHTTP/1.1c                      | j                   d   S )Nr   )r6   rI   s    r   address_stringz!WSGIRequestHandler.address_string   s    ""1%%r"   c                    |d   d   dk(  r|d   j                  d      rd}d}d}n5|d   j                         r t        |d         dk(  rt        |d         }nd }t	        t
        |g|| j                  || j                         d	 y )
N   r   4zzNYou're accessing the development server over HTTPS, but it only supports HTTP.i      )r5   status_codeserver_time)
startswithisdigitlenrR   r	   r2   r5   log_date_time_string)r-   formatr.   r}   s       r   r	   zWSGIRequestHandler.log_message   s    71:a!3!3J!?!  KD!W__3tAw<1#4d1g,KK	
 	
 LL#113	
r"   c                 f    | j                   D ]  }d|v s| j                   |=  t        | 	         S )Nr    )rb   r+   get_environ)r-   kr0   s     r   r   zWSGIRequestHandler.get_environ   s:    
  	$AaxLLO	$ w"$$r"   c                    d| _         | j                          | j                   s| j                          | j                   s	 | j                  j                  t        j
                         y # t        t        f$ r Y y w xY w)NT)ri   handle_one_request
connectionshutdownr(   SHUT_WRAttributeErrorOSErrorrI   s    r   handlezWSGIRequestHandler.handle   sh     $!''##% ''	OO$$V^^4( 		s   )A, ,A>=A>c                    | j                   j                  d      | _        t        | j                        dkD  r'd| _        d| _        d| _        | j                  d       y| j                         syt        | j                   | j                  | j                         | j                               }| |_        |j                  | j                  j!                                y)zV
        Copy of WSGIRequestHandler.handle() but with different ServerHandler
        i  i    i  N)rfilereadlineraw_requestliner   requestlinerequest_versioncommand
send_errorparse_requestrO   wfile
get_stderrr   re   runrf   get_app)r-   handlers     r   r   z%WSGIRequestHandler.handle_one_request   s      $zz2259t##$u,!D#%D DLOOC !!#JJ

DOO$5t7G7G7I
 #'DKK'')*r"   )
r7   r8   r9   protocol_versionrw   r	   r   r   r   r<   r=   s   @r   r   r      s"    !&
.	%+r"   r   Fc                     | |f}|rt        dt        j                  |fi       }n|} ||t        |      }	| |t	        |	d|             |rd|	_        |	j                  |       |	j                          y )Nr   )r%   server_portT)typerg   rh   r   r   rM   set_appserve_forever)
addrportwsgi_handlerr%   	threadingon_bind
server_clsserver_address	httpd_clshttpds
             r   r   r      sz     D\N(C(CZ'PRTU		n&8tDE}d34  $	MM,	r"   ) r:   loggingr(   rg   r   collectionsr   wsgirefr   django.core.exceptionsr   django.core.handlers.wsgir   django.core.wsgir   	django.dbr   django.utils.logr	   django.utils.module_loadingr
   __all__	getLoggerr2   r   r!   r   rh   r?   rO   r   r   r{   r"   r   <module>r      s       
  ! 7 3 1 ! ( 5
.			?	+8	:)) :$'44j '8<&M// <&~F+99 F+Z 
r"   