U
    aúd¥T  ã                   @   s   d Z ddlmZmZmZ ddlmZ ddlZddlZddl	Z	dddd	d
dgZ
dddddddgZdddddddddddddgZdd „ Zd!d"d#d$d%d&d'd(d)d*h
jZd+d,„ Zd-d„ ZG d.d„ dƒZG d/d„ deƒZG d0d„ deƒZG d1d	„ d	eƒZG d2d
„ d
eƒZdS )3z/Base classes for server/gateway implementationsé   )ÚFileWrapperÚguess_schemeÚis_hop_by_hop)ÚHeadersé    NÚBaseHandlerÚSimpleHandlerÚBaseCGIHandlerÚ
CGIHandlerÚIISCGIHandlerÚread_environZMonZTueZWedZThuZFriZSatZSunZJanZFebZMarZAprZMayZJunZJulZAugZSepZOctZNovZDecc           
   	   C   s:   t  | ¡\	}}}}}}}}}	dt| |t| ||||f S )Nz#%s, %02d %3s %4d %02d:%02d:%02d GMT)ÚtimeÚgmtimeÚ_weekdaynameÚ
_monthname)
Z	timestampZyearZmonthZdayZhhZmmZssZwdÚyÚz© r   ú&/usr/lib/python3.8/wsgiref/handlers.pyÚformat_date_time   s          ÿr   ÚSCRIPT_NAMEÚ	PATH_INFOZQUERY_STRINGZREQUEST_METHODZ	AUTH_TYPEZCONTENT_TYPEZCONTENT_LENGTHZHTTPSZREMOTE_USERZREMOTE_IDENTc                 C   s6   t | ƒp4|  d¡p4|  d¡p4|  d¡o4t| dd … ƒS )NZHTTP_ZSSL_Z	REDIRECT_é	   )Ú_is_requestÚ
startswithÚ_needs_transcode)Úkr   r   r   r      s    ÿr   c                  C   sð   t  ¡ } d}zd d|¡ W n tk
r4   d}Y nX i }tj ¡ D ]¦\}}t|ƒrât jdkrÐtj 	dd¡ 
¡ }| d¡rŒ| d¡ d¡}qâ| d	¡r˜qâ| d
¡r¼d|kr¼| d¡ d¡}qâ| | d¡ d¡}n| | |¡ d¡}|||< qD|S )z'Read environment, fixing HTTP variablesÚsurrogateescapeÚ zutf-8ÚreplaceZwin32ÚSERVER_SOFTWAREzmicrosoft-iis/ú
iso-8859-1zapache/zsimplehttp/zpython/3)ÚsysÚgetfilesystemencodingÚencodeÚLookupErrorÚosÚenvironÚitemsr   ÚplatformÚgetÚlowerr   Údecode)ÚencZescr'   r   ÚvZsoftwarer   r   r   r   "   s0    

	

ÿþ
c                   @   s"  e Zd ZdZdZdZdZdZdZdZ	dZ
eƒ ZeZeZdZdZdgZd	Zd ZZdZdZd
Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zd;dd„Z dd„ Z!dd„ Z"dd„ Z#dd „ Z$d!d"„ Z%d#d$„ Z&d%d&„ Z'd'd(„ Z(d)d*„ Z)d+d,„ Z*d-d.„ Z+d/d0„ Z,d1d2„ Z-d3d4„ Z.d5d6„ Z/d7d8„ Z0d9d:„ Z1dS )<r   z+Manage the invocation of a WSGI application)r   r   TFz1.0Nz500 Internal Server Error)zContent-Typez
text/plains;   A server error occurred.  Please contact the administrator.r   c              
   C   st   z$|   ¡  || j| jƒ| _|  ¡  W nJ tttfk
r@   Y dS    z|  ¡  W n   |  	¡  ‚ Y nX Y nX dS )zInvoke the applicationN)
Úsetup_environr'   Ústart_responseÚresultÚfinish_responseÚConnectionAbortedErrorÚBrokenPipeErrorÚConnectionResetErrorÚhandle_errorÚclose)ÚselfZapplicationr   r   r   Úrun€   s    zBaseHandler.runc                 C   s–   | j  ¡  }| _|  ¡  |  ¡ |d< |  ¡ |d< | j|d< | j|d< |  ¡ |d< | j	|d< | j
|d< | jdk	rx| j|d	< | jr’| jr’| d
| j¡ dS )z&Set up the environment for one requestz
wsgi.inputzwsgi.errorszwsgi.versionzwsgi.run_oncezwsgi.url_schemezwsgi.multithreadzwsgi.multiprocessNzwsgi.file_wrapperr    )Ú
os_environÚcopyr'   Úadd_cgi_varsÚ	get_stdinÚ
get_stderrÚwsgi_versionÚwsgi_run_onceÚ
get_schemeÚwsgi_multithreadÚwsgi_multiprocessÚwsgi_file_wrapperÚorigin_serverÚserver_softwareÚ
setdefault)r8   Úenvr   r   r   r/   ˜   s    





zBaseHandler.setup_environc                 C   sd   z2|   ¡ r|  ¡ s0| jD ]}|  |¡ q|  ¡  W n$   t| jdƒrP| j ¡  ‚ Y n
X |  ¡  dS )a>  Send any iterable data, then close self and the iterable

        Subclasses intended for use in asynchronous servers will
        want to redefine this method, such that it sets up callbacks
        in the event loop to iterate over the data, and to call
        'self.close()' once the response is finished.
        r7   N)Úresult_is_fileÚsendfiler1   ÚwriteÚfinish_contentÚhasattrr7   ©r8   Údatar   r   r   r2   ­   s    

zBaseHandler.finish_responsec                 C   s
   t | jƒS )z Return the URL scheme being used)r   r'   ©r8   r   r   r   rA   Ç   s    zBaseHandler.get_schemec              
   C   sJ   zt | jƒ}W n tttfk
r(   Y nX |dkrFt| jƒ| jd< dS dS )z@Compute Content-Length or switch to chunked encoding if possibler   úContent-LengthN)Úlenr1   Ú	TypeErrorÚAttributeErrorÚNotImplementedErrorÚstrÚ
bytes_sentÚheaders)r8   Zblocksr   r   r   Úset_content_lengthÌ   s    zBaseHandler.set_content_lengthc                 C   s   d| j kr|  ¡  dS )zqMake any necessary header changes or defaults

        Subclasses can extend this to add other defaults.
        rQ   N)rX   rY   rP   r   r   r   Úcleanup_headersÙ   s    
zBaseHandler.cleanup_headersc                 C   sì   |r2z$| j r&|d |d ƒ |d ¡‚W 5 d}X n| jdk	rDtdƒ‚|| _|  |¡| _|  |d¡}t|ƒdksvtdƒ‚|dd	…  ¡ sŽtd
ƒ‚|d	 dks¢tdƒ‚|D ]>\}}|  |d¡}|  |d¡}t	|ƒr¦td|› d|› dƒ‚q¦| j
S )z4'start_response()' callable as specified by PEP 3333Nr   r   é   zHeaders already set!ZStatusé   z$Status must be at least 4 charactersé   z(Status message must begin w/3-digit codeú z+Status message must have a space after codezHeader namezHeader valuezHop-by-hop header, 'z: z', not allowed)Úheaders_sentÚwith_tracebackrX   ÚAssertionErrorÚstatusÚheaders_classÚ_convert_string_typerR   Úisdigitr   rK   )r8   rb   rX   Úexc_infoÚnameÚvalr   r   r   r0   á   s(    

ÿzBaseHandler.start_responsec                 C   s(   t |ƒtkr|S td |t|ƒ¡ƒ‚dS )zConvert/check value type.z!{0} must be of type str (got {1})N)ÚtyperV   ra   ÚformatÚrepr)r8   ÚvalueÚtitler   r   r   rd   þ   s
    ÿz BaseHandler._convert_string_typec                 C   s’   | j rx|  ¡ rŽ|  d| j| jf  d¡¡ d| jkrP|  dtt ¡ ƒ  d¡¡ | j	rŽd| jkrŽ|  d| j	  d¡¡ n|  d| j  d¡¡ dS )	z6Transmit version/status/date/server, via self._write()zHTTP/%s %s
r!   ZDatez
Date: %s
ZServerzServer: %s
zStatus: %s
N)
rE   Úclient_is_modernÚ_writeÚhttp_versionrb   r$   rX   r   r   rF   rP   r   r   r   Úsend_preamble  s    
ÿzBaseHandler.send_preamblec                 C   sf   t |ƒtkstdƒ‚| js$tdƒ‚n,| js>t|ƒ| _|  ¡  n|  jt|ƒ7  _|  |¡ |  	¡  dS )z+'write()' callable as specified by PEP 3333z)write() argument must be a bytes instancezwrite() before start_response()N)
ri   Úbytesra   rb   r_   rR   rW   Úsend_headersro   Ú_flushrN   r   r   r   rK     s    ÿ



zBaseHandler.writec                 C   s   dS )a  Platform-specific file transmission

        Override this method in subclasses to support platform-specific
        file transmission.  It is only called if the application's
        return iterable ('self.result') is an instance of
        'self.wsgi_file_wrapper'.

        This method should return a true value if it was able to actually
        transmit the wrapped file-like object using a platform-specific
        approach.  It should return a false value if normal iteration
        should be used instead.  An exception can be raised to indicate
        that transmission was attempted, but failed.

        NOTE: this method should call 'self.send_headers()' if
        'self.headers_sent' is false and it is going to attempt direct
        transmission of the file.
        Fr   rP   r   r   r   rJ   )  s    zBaseHandler.sendfilec                 C   s"   | j s| j dd¡ |  ¡  n dS )z.Ensure headers and content have both been sentrQ   Ú0N)r_   rX   rG   rs   rP   r   r   r   rL   >  s    
zBaseHandler.finish_contentc              	   C   sF   zt| j dƒr| j  ¡  W 5 d | _  | _ | _| _d| _d| _X dS )z„Close the iterable (if needed) and reset all instance vars

        Subclasses may want to also drop the client connection.
        Nr   Fr7   )r1   rX   rb   r'   rW   r_   rM   r7   rP   r   r   r   r7   H  s     zBaseHandler.closec                 C   s8   |   ¡  d| _| jr|  ¡ r4|  ¡  |  t| jƒ¡ dS )z1Transmit headers to the client, via self._write()TN)rZ   r_   rE   rn   rq   ro   rr   rX   rP   r   r   r   rs   U  s
    zBaseHandler.send_headersc                 C   s   | j }|dk	ot| j|ƒS )z@True if 'self.result' is an instance of 'self.wsgi_file_wrapper'N)rD   Ú
isinstancer1   )r8   Úwrapperr   r   r   rI   ^  s    zBaseHandler.result_is_filec                 C   s   | j d  ¡ dkS )z,True if client can accept status and headersZSERVER_PROTOCOLzHTTP/0.9)r'   ÚupperrP   r   r   r   rn   d  s    zBaseHandler.client_is_modernc                 C   sJ   z>ddl m} |  ¡ }||d |d |d | j|ƒ | ¡  W 5 d}X dS )zLog the 'exc_info' tuple in the server log

        Subclasses may override to retarget the output or change its format.
        Nr   )Úprint_exceptionr   r[   )Ú	tracebackry   r>   Útraceback_limitÚflush)r8   rf   ry   Ústderrr   r   r   Úlog_exceptioni  s       þzBaseHandler.log_exceptionc                 C   s2   |   t ¡ ¡ | js.|  | j| j¡| _|  ¡  dS )z>Log current error, and send error output to client if possibleN)	r~   r"   rf   r_   Úerror_outputr'   r0   r1   r2   rP   r   r   r   r6   y  s    zBaseHandler.handle_errorc                 C   s$   || j | jdd… t ¡ ƒ | jgS )aZ  WSGI mini-app to create error output

        By default, this just uses the 'error_status', 'error_headers',
        and 'error_body' attributes to generate an output page.  It can
        be overridden in a subclass to dynamically generate diagnostics,
        choose an appropriate message for the user's preferred language, etc.

        Note, however, that it's not recommended from a security perspective to
        spit out diagnostics to any old user; ideally, you should have to do
        something special to enable diagnostic output, which is why we don't
        include any here!
        N)Úerror_statusÚerror_headersr"   rf   Ú
error_body)r8   r'   r0   r   r   r   r     s    zBaseHandler.error_outputc                 C   s   t ‚dS )a  Override in subclass to buffer data for send to client

        It's okay if this method actually transmits the data; BaseHandler
        just separates write and flush operations for greater efficiency
        when the underlying system actually has such a distinction.
        N©rU   rN   r   r   r   ro   ”  s    zBaseHandler._writec                 C   s   t ‚dS )z¬Override in subclass to force sending of recent '_write()' calls

        It's okay if this method is a no-op (i.e., if '_write()' actually
        sends the data.
        Nrƒ   rP   r   r   r   rt     s    zBaseHandler._flushc                 C   s   t ‚dS )z4Override in subclass to return suitable 'wsgi.input'Nrƒ   rP   r   r   r   r=   ¥  s    zBaseHandler.get_stdinc                 C   s   t ‚dS )z5Override in subclass to return suitable 'wsgi.errors'Nrƒ   rP   r   r   r   r>   ©  s    zBaseHandler.get_stderrc                 C   s   t ‚dS )z>Override in subclass to insert CGI variables in 'self.environ'Nrƒ   rP   r   r   r   r<   ­  s    zBaseHandler.add_cgi_vars)N)2Ú__name__Ú
__module__Ú__qualname__Ú__doc__r?   rB   rC   r@   rE   rp   rF   r   r:   r   rD   r   rc   r{   r€   r   r‚   rb   r1   r_   rX   rW   r9   r/   r2   rA   rY   rZ   r0   rd   rq   rK   rJ   rL   r7   rs   rI   rn   r~   r6   r   ro   rt   r=   r>   r<   r   r   r   r   r   ^   sV   

		c                   @   sB   e Zd ZdZddd„Zdd„ Zdd	„ Zd
d„ Zdd„ Zdd„ Z	dS )r   aq  Handler that's just initialized with streams, environment, etc.

    This handler subclass is intended for synchronous HTTP/1.0 origin servers,
    and handles sending the entire response output, given the correct inputs.

    Usage::

        handler = SimpleHandler(
            inp,out,err,env, multithread=False, multiprocess=True
        )
        handler.run(app)TFc                 C   s(   || _ || _|| _|| _|| _|| _d S ©N)ÚstdinÚstdoutr}   Úbase_envrB   rC   )r8   r‰   rŠ   r}   r'   ÚmultithreadÚmultiprocessr   r   r   Ú__init__¿  s    zSimpleHandler.__init__c                 C   s   | j S rˆ   )r‰   rP   r   r   r   r=   É  s    zSimpleHandler.get_stdinc                 C   s   | j S rˆ   )r}   rP   r   r   r   r>   Ì  s    zSimpleHandler.get_stderrc                 C   s   | j  | j¡ d S rˆ   )r'   Úupdater‹   rP   r   r   r   r<   Ï  s    zSimpleHandler.add_cgi_varsc                 C   s^   | j  |¡}|d ks |t|ƒkr$d S ddlm} |dtƒ ||d … }|sLqZ| j  |¡}q:d S )Nr   )Úwarnz9SimpleHandler.stdout.write() should not do partial writes)rŠ   rK   rR   Úwarningsr   ÚDeprecationWarning)r8   rO   r1   r   r   r   r   ro   Ò  s    ÿzSimpleHandler._writec                 C   s   | j  ¡  | j j| _d S rˆ   )rŠ   r|   rt   rP   r   r   r   rt   ß  s    
zSimpleHandler._flushN)TF)
r„   r…   r†   r‡   rŽ   r=   r>   r<   ro   rt   r   r   r   r   r   ²  s      ÿ

c                   @   s   e Zd ZdZdZdS )r	   aÚ  CGI-like systems using input/output/error streams and environ mapping

    Usage::

        handler = BaseCGIHandler(inp,out,err,env)
        handler.run(app)

    This handler class is useful for gateway protocols like ReadyExec and
    FastCGI, that have usable input/output/error streams and an environment
    mapping.  It's also the base class for CGIHandler, which just uses
    sys.stdin, os.environ, and so on.

    The constructor also takes keyword arguments 'multithread' and
    'multiprocess' (defaulting to 'True' and 'False' respectively) to control
    the configuration sent to the application.  It sets 'origin_server' to
    False (to enable CGI-like output), and assumes that 'wsgi.run_once' is
    False.
    FN)r„   r…   r†   r‡   rE   r   r   r   r   r	   ä  s   c                   @   s    e Zd ZdZdZi Zdd„ ZdS )r
   aé  CGI-based invocation via sys.stdin/stdout/stderr and os.environ

    Usage::

        CGIHandler().run(app)

    The difference between this class and BaseCGIHandler is that it always
    uses 'wsgi.run_once' of 'True', 'wsgi.multithread' of 'False', and
    'wsgi.multiprocess' of 'True'.  It does not take any initialization
    parameters, but always uses 'sys.stdin', 'os.environ', and friends.

    If you need to override any of these parameters, use BaseCGIHandler
    instead.
    Tc              	   C   s(   t j| tjjtjjtjtƒ ddd d S )NFT©rŒ   r   )r	   rŽ   r"   r‰   ÚbufferrŠ   r}   r   rP   r   r   r   rŽ     s         þzCGIHandler.__init__N©r„   r…   r†   r‡   r@   r:   rŽ   r   r   r   r   r
   ü  s   c                   @   s    e Zd ZdZdZi Zdd„ ZdS )r   a  CGI-based invocation with workaround for IIS path bug

    This handler should be used in preference to CGIHandler when deploying on
    Microsoft IIS without having set the config allowPathInfo option (IIS>=7)
    or metabase allowPathInfoForScriptMappings (IIS<7).
    Tc              	   C   sj   t ƒ }| dd¡}| dd¡}|d  |d ¡rD|t|ƒd … |d< tj| tjjtj	jtj
|ddd d S )Nr   r   r   ú/FTr“   )r   r*   r   rR   r	   rŽ   r"   r‰   r”   rŠ   r}   )r8   r'   ÚpathZscriptr   r   r   rŽ   2  s         þzIISCGIHandler.__init__Nr•   r   r   r   r   r     s   )r‡   Úutilr   r   r   rX   r   r"   r&   r   Ú__all__r   r   r   Ú__contains__r   r   r   r   r   r	   r
   r   r   r   r   r   Ú<module>   s\       þ          þ        þ<  V2