o
    <g'                     @   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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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)deque)simple_server)ImproperlyConfigured)LimitedStream)get_wsgi_application)connections)import_string)
WSGIServerWSGIRequestHandlerzdjango.serverc               
   C   sV   ddl m}  t| d}|du rt S zt|W S  ty* } ztd| |d}~w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err r   O/var/www/html/venv/lib/python3.10/site-packages/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_error8   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|i | d S r   )socketAF_INET6address_familyr!   super__init__)selfr    r!   argskwargs	__class__r   r   r&   I   s   zWSGIServer.__init__c                    s(   t  rtd| d S t || d S )Nz- Broken pipe from %s)r   loggerinfor%   handle_error)r'   requestclient_addressr*   r   r   r.   O   s   zWSGIServer.handle_error)__name__
__module____qualname____doc__request_queue_sizer&   r.   __classcell__r   r   r*   r   r	   D   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|i | || _d S r   )r%   r&   r8   )r'   r8   r(   r)   r*   r   r   r&   [   s   
zThreadedWSGIServer.__init__c                    s4   | j r| j  D ]\}}|t|< qt || d S r   )r8   itemsr   r%   process_request_thread)r'   r/   r0   aliasconnr*   r   r   r:   a   s   
z)ThreadedWSGIServer.process_request_threadc                 C   s   t   d S r   )r   	close_allr'   r   r   r   _close_connectionsi   s   z%ThreadedWSGIServer._close_connectionsc                    s   |    t | d S r   )r?   r%   close_request)r'   r/   r*   r   r   r@   m   s   z ThreadedWSGIServer.close_request)
r1   r2   r3   r4   daemon_threadsr&   r:   r?   r@   r6   r   r   r*   r   r7   V   s    r7   c                       sD   e Zd ZdZ fddZ fddZ fddZ fdd	Z  ZS )
ServerHandlerz1.1c              	      sR   z	t |d}W n ttfy   d}Y nw t jt|||||fi | 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&   u   s   
zServerHandler.__init__c                    s   t    | jd dkrd| jv rt| jd dkr| jd= | jd dkr0d| jvr0d| jd< nt| jjtj	s=d| jd< | j
ddkrKd| j_d S d S )NREQUEST_METHODHEADzContent-Length0close
ConnectionT)r%   cleanup_headersrK   headersstr
isinstancerequest_handlerserversocketserverThreadingMixInrE   close_connectionr>   r*   r   r   rR      s   



zServerHandler.cleanup_headersc                    s   |     t   d S r   )	get_stdinreadr%   rP   r>   r*   r   r   rP      s   zServerHandler.closec                    s`   | j d dkr)zt| jdd | js|   W |   d S W |   d S |   w t   d S )NrM   rN   r   )maxlen)rK   r   resultheaders_sentsend_headersrP   r%   finish_responser>   r*   r   r   ra      s   
zServerHandler.finish_response)	r1   r2   r3   http_versionr&   rR   rP   ra   r6   r   r   r*   r   rB   r   s    rB   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   )r0   r>   r   r   r   address_string   s   
z!WSGIRequestHandler.address_stringc                 G   s   | j |  d}|d d dkr$|d dr$d|d< tjd|d	 d S |d  rPt|d d
krPt|d }||d< |dkrDtj}n|dkrLtj}ntj	}ntj	}||g|R d|i d S )N)r/   server_time   r   4zi  status_codezNYou're accessing the development server over HTTPS, but it only supports HTTP.)extra   i  rh   )
r/   log_date_time_string
startswithr,   errorisdigitlenrD   warningr-   )r'   formatr(   rh   rg   levelr   r   r   log_message   s*   zWSGIRequestHandler.log_messagec                    s&   | j D ]
}d|v r| j |= qt  S )Nr   )rS   r%   get_environ)r'   kr*   r   r   rs      s
   

zWSGIRequestHandler.get_environc              	   C   sP   d| _ |   | j s|   | j r
z
| jtj W d S  ttfy'   Y d S w )NT)rZ   handle_one_request
connectionshutdownr"   SHUT_WRAttributeErrorOSErrorr>   r   r   r   handle   s   zWSGIRequestHandler.handlec                 C   s|   | j d| _t| jdkrd| _d| _d| _| d dS |  s$dS t	| j | j
|  |  }| |_|| j  dS )zDCopy of WSGIRequestHandler.handle() but with different ServerHandleri  i    i  N)rfilereadlineraw_requestlinern   requestlinerequest_versioncommand
send_errorparse_requestrB   wfile
get_stderrrs   rV   runrW   get_app)r'   handlerr   r   r   ru      s   
z%WSGIRequestHandler.handle_one_request)
r1   r2   r3   protocol_versionrc   rr   rs   r{   ru   r6   r   r   r*   r   r
      s    
r
   Fc           
      C   sj   | |f}|rt dtj|fi }n|}||t|d}	|d ur%|t|	d| |r*d|	_|	| |	  d S )Nr	   )r    server_portT)typerX   rY   r
   r   rA   set_appserve_forever)
addrportwsgi_handlerr    	threadingon_bind
server_clsserver_address	httpd_clshttpdr   r   r   r      s   	
r   )r4   loggingr"   rX   r   collectionsr   wsgirefr   django.core.exceptionsr   django.core.handlers.wsgir   django.core.wsgir   	django.dbr   django.utils.module_loadingr   __all__	getLoggerr,   r   r   r	   rY   r7   rB   r
   r   r   r   r   r   <module>   s2    	
?S