o 3a"@sdZddlZddlZddlZddlZddlmZddlmZddl m Z ddl m Z ddl mZdZed Zd d Zd d ZGdddejZGdddejeZGdddejZGdddejZddefddZdS)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) import_string) WSGIServerWSGIRequestHandlerz django.serverc CsVddlm}t|d}|durtSzt|WSty*}ztd||d}~ww)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)settingsWSGI_APPLICATIONNzBWSGI application '%s' could not be loaded; Error importing module.) django.confr getattrrr ImportErrorr)r app_patherrr>/usr/lib/python3/dist-packages/django/core/servers/basehttp.pyget_internal_wsgi_applications  rcCst\}}}t|tttfSN)sysexc_info issubclassBrokenPipeErrorConnectionAbortedErrorConnectionResetError)exc_type_rrris_broken_pipe_error5s rcs8eZdZdZdZdddfdd Zfdd ZZS) rz7BaseHTTPServer that implements the Python WSGI protocol FT)ipv6allow_reuse_addresscs(|rtj|_||_tj|i|dSr)socketAF_INET6address_familyrsuper__init__)selfrrargskwargs __class__rrr$CszWSGIServer.__init__cs(tr td|dSt||dS)Nz- Broken pipe from %s )rloggerinfor# handle_error)r%requestclient_addressr(rrr,IszWSGIServer.handle_error)__name__ __module__ __qualname____doc__request_queue_sizer$r, __classcell__rrr(rr>s rc@seZdZdZdZdS)ThreadedWSGIServerz$A threaded version of the WSGIServerTN)r/r0r1r2daemon_threadsrrrrr5Psr5csDeZdZdZfddZfddZfddZfdd ZZS) ServerHandlerz1.1c sRz t|d}Wn ttfyd}Ynwtjt|||||fi|dS)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_LENGTHrN)intget ValueError TypeErrorr#r$r)r%stdinstdoutstderrenvironr'content_lengthr(rrr$Xs $zServerHandler.__init__csZtd|jvrd|jd<n t|jjtjsd|jd<|jddkr+d|j_ dSdS)NzContent-Lengthclose ConnectionT) r#cleanup_headersheaders isinstancerequest_handlerserver socketserverThreadingMixInr:close_connectionr%r(rrrDes     zServerHandler.cleanup_headerscs|tdSr) get_stdin _read_limitedr#rBrLr(rrrBts zServerHandler.closecsts tdSdSr)rr#r,rLr(rrr,xszServerHandler.handle_error) r/r0r1 http_versionr$rDrBr,r4rrr(rr7Us   r7cs@eZdZdZddZddZfddZdd Zd d ZZ S) rzHTTP/1.1cCs |jdS)Nr)r.rLrrraddress_strings z!WSGIRequestHandler.address_stringcGs|j|d}|dddkr$|ddr$d|d<tjd|d dS|drPt|dd krPt|d}||d<|dkrDtj}n|d krLtj}ntj }ntj }||g|Rd |idS) N)r- server_timer4zi status_codezOYou're accessing the development server over HTTPS, but it only supports HTTP. )extrairU) r-log_date_time_string startswithr*errorisdigitlenr9warningr+)r%formatr&rUrTlevelrrr log_messages* zWSGIRequestHandler.log_messagecs&|jD] }d|vr |j|=qtS)Nr)rEr# get_environ)r%kr(rrr`s  zWSGIRequestHandler.get_environc CsPd|_||js||jr z |jtjWdSttfy'YdSw)NT)rKhandle_one_request connectionshutdownr SHUT_WRAttributeErrorOSErrorrLrrrhandleszWSGIRequestHandler.handlecCs||jd|_t|jdkrd|_d|_d|_|ddS|s$dSt |j|j | | }||_ ||jdS)zDCopy of WSGIRequestHandler.handle() but with different ServerHandleriiiN)rfilereadlineraw_requestliner[ requestlinerequest_versioncommand send_error parse_requestr7wfile get_stderrr`rGrunrHget_app)r%handlerrrrrbs z%WSGIRequestHandler.handle_one_request) r/r0r1protocol_versionrPr_r`rhrbr4rrr(rr~s   rFc CsR||f}|rtdtj|fi}n|}||t|d}|rd|_|||dS)Nr)rT)typerIrJrr6set_app serve_forever) addrport wsgi_handlerr threading server_clsserver_address httpd_clshttpdrrrrts  rt)r2loggingr rIrwsgirefrdjango.core.exceptionsrdjango.core.handlers.wsgirdjango.core.wsgirdjango.utils.module_loadingr__all__ getLoggerr*rrrrJr5r7rrtrrrrs&        )N