o
    ˜¨Êhk"  ã                   @   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	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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)Ú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   úN/var/www/html/env/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_error7   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%   H   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-   N   s   zWSGIServer.handle_error)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Úrequest_queue_sizer%   r-   Ú__classcell__r   r   r)   r   r   C   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%   r7   )r&   r7   r'   r(   r)   r   r   r%   Z   s   
zThreadedWSGIServer.__init__c                    s4   | j r| j  ¡ D ]\}}|t|< qtƒ  ||¡ d S r   )r7   Úitemsr   r$   Úprocess_request_thread)r&   r.   r/   ÚaliasÚconnr)   r   r   r9   `   s   
z)ThreadedWSGIServer.process_request_threadc                 C   s   t  ¡  d S r   )r   Ú	close_all©r&   r   r   r   Ú_close_connectionsh   s   z%ThreadedWSGIServer._close_connectionsc                    s   |   ¡  tƒ  |¡ d S r   )r>   r$   Úclose_request)r&   r.   r)   r   r   r?   l   s   z ThreadedWSGIServer.close_request)
r0   r1   r2   r3   Údaemon_threadsr%   r9   r>   r?   r5   r   r   r)   r   r6   U   s    r6   c                       s8   e Zd Z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%   t   s   ÿÿ
ÿzServerHandler.__init__c                    sZ   t ƒ  ¡  d| jvrd| jd< nt| jjtjƒsd| jd< | j d¡dkr+d| j_	d S d S )NzContent-LengthÚcloseÚ
ConnectionT)
r$   Úcleanup_headersÚheadersÚ
isinstanceÚrequest_handlerÚserverÚsocketserverÚThreadingMixInrD   Úclose_connectionr=   r)   r   r   rN   ƒ   s   


ÿzServerHandler.cleanup_headersc                    s   |   ¡  ¡  tƒ  ¡  d S r   )Ú	get_stdinÚ_read_limitedr$   rL   r=   r)   r   r   rL   ’   s   zServerHandler.close)r0   r1   r2   Úhttp_versionr%   rN   rL   r5   r   r   r)   r   rA   q   s
    rA   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   )r/   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�  r^   )
r.   Úlog_date_time_stringÚ
startswithr+   ÚerrorÚisdigitÚlenrC   Úwarningr,   )r&   Úformatr'   r^   r]   Úlevelr   r   r   Úlog_messagež   s*   þýzWSGIRequestHandler.log_messagec                    s&   | j D ]
}d|v r| j |= qtƒ  ¡ S )Nr   )rO   r$   Úget_environ)r&   Úkr)   r   r   ri   ½   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)rU   Ú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_requestlinerd   ÚrequestlineÚrequest_versionÚcommandÚ
send_errorÚparse_requestrA   ÚwfileÚ
get_stderrri   rQ   ÚrunrR   Úget_app)r&   Úhandlerr   r   r   rk   Ò   s   
ÿz%WSGIRequestHandler.handle_one_request)
r0   r1   r2   Úprotocol_versionrY   rh   ri   rq   rk   r5   r   r   r)   r   r	   —   s    
r	   Fc           	      C   sR   | |f}|rt dtj|fi ƒ}n|}||t|d�}|rd|_| |¡ | ¡  d S )Nr   )r   T)ÚtyperS   rT   r	   r@   Úset_appÚserve_forever)	ÚaddrÚportÚwsgi_handlerr   Ú	threadingÚ
server_clsÚserver_addressÚ	httpd_clsÚhttpdr   r   r   r}   æ   s   
r}   )r3   Úloggingr!   rS   r   Úwsgirefr   Údjango.core.exceptionsr   Údjango.core.handlers.wsgir   Údjango.core.wsgir   Ú	django.dbr   Údjango.utils.module_loadingr   Ú__all__Ú	getLoggerr+   r   r   r   rT   r6   rA   r	   r}   r   r   r   r   Ú<module>   s(    	
&O