o
    fss                     @   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  m	Z
 ddlm  mZ ddlm  mZ ddlm  mZ ddlm  mZ e  d ZddlmZ z
ddlmZmZ W n eyl   ddlmZmZ Y nw G dd dZG d	d
 d
ejZG dd de
j Z!dd Z"e#dej$Z%dd Z&d(ddZ'dd Z(e#dZ)dd Z*e+dkrUe,ej-dk re.dej-d   dS ddl/m  m0Z0 ddl1m  m2Z2 ddl3m  m4Z4 ddl5m  m6Z6 ddl7m  m8Z8 ddl9m  m:Z: e:;dZ<e=ej>Z?e0@dddZAe2Bej-d ZCe44eCZDe!deEej-d e<e?ZFeFGeD e6j6e6Hej-d d e<e?d!ZIe88dd"ZJd#ej-v rOd$d% ZKddlLZLeLMd&d' dS e
N  dS dS ))z>$Id: http_server.py,v 1.12 2004/04/21 15:11:44 akuchling Exp $    N)as_bytes   )counter)unquote
splitqueryc                   @   s  e Zd ZdZe 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 Zdd Zd|ddZdd Zdd Ze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eZd+d, Zd-d. Z d/d0 Z!i d1d2d3d4dd5d6d7d8d9d:d;d<d=d>d?d@dAdBdCdDdEdFdGdHdIdJdKdLdMdNdOdPdQi dRdSdTdUdVdWdXdYdZd[d\d]d^d_d`dadbdcdddedfdgdhdidjdkdldmdndodpdqdrdsdtdudvdwZ"dx#dyZ$dzd{ Z%dS )~http_request      Nc                 G   s^   |\| _ | _| _| _| _| _g | _dt t	t

 d| _g | _tj | _d | _i | _d S )Nz	Medusa/%s)ServerDate)channelrequestcommanduriversionheaderoutgoingVERSION_STRING	http_datebuild_http_datetimereply_headers _http_request__reply_header_listr   request_counter	incrementrequest_number
_split_uri_header_cacheselfargs r!   P/var/www/html/venv/lib/python3.10/site-packages/supervisor/medusa/http_server.py__init__9   s   

zhttp_request.__init__c                 C   s   || j |< d S Nr   )r   keyvaluer!   r!   r"   __setitem__P      zhttp_request.__setitem__c                 C   s
   | j | S r$   r%   r   r&   r!   r!   r"   __getitem__S      
zhttp_request.__getitem__c                 C   
   || j v S r$   r%   r*   r!   r!   r"   __contains__V   r,   zhttp_request.__contains__c                 C   r-   r$   r%   r*   r!   r!   r"   has_keyY   r,   zhttp_request.has_keyc                 C   s8   dd | j  D }d| | jg| d }t|S )Nc                 S      g | ]}d | qS %s: %sr!   ).0itemr!   r!   r"   
<listcomp>]       z3http_request.build_reply_header.<locals>.<listcomp>


)r   itemsjoinresponse
reply_coder   )r   header_itemsresultr!   r!   r"   build_reply_header\   s   zhttp_request.build_reply_headerc                 C   s   | j ||f dS )z$ Adds a header to the reply headers N)r   append)r   namer'   r!   r!   r"   
add_headerm   s   zhttp_request.add_headerc                 C   s   | j   g | jdd< dS )z Clears the reply header list N)r   clearr   r   r!   r!   r"   clear_headersq   s   
zhttp_request.clear_headersc                 C   s   d}|| j v r|du s| j | |kr| j |= d}g }|dur-||f| jv r,||fg}d}n| jD ]}|d |kr?|| d}q0|sW|du rKd| }nd||f }td| |D ]}| j| qYdS )z Removes the specified header.
        If a value is provided, the name and
        value must match to remove the header.
        If the value is None, removes all headers
        with that name.r   Nr	   z%sr2   zHeader '%s' not found)r   r   r@   LookupErrorremove)r   rA   r'   found_itremoved_headershsearch_valuer!   r!   r"   remove_headery   s2   




zhttp_request.remove_headerc                 C   sT   | j dd }dd |D }| j D ]\}}||vr'|||f || q|S )zP Get the tuple of headers that will be used
        for generating reply headersNc                 S   s   g | ]\}}|qS r!   r!   )r3   nvr!   r!   r"   r5      r6   z2http_request.get_reply_headers.<locals>.<listcomp>)r   r   r9   r@   )r   header_tuplesheader_namesrM   rN   r!   r!   r"   get_reply_headers   s   
zhttp_request.get_reply_headersc                 C   s6   |   }| | jg}|dd |D 7 }d|d S )zE Gets the reply header (including status and
        additional crlf)c                 S   r0   r1   r!   )r3   rJ   r!   r!   r"   r5      r6   z6http_request.get_reply_header_text.<locals>.<listcomp>r7   r8   )rQ   r;   r<   r:   )r   rO   headersr!   r!   r"   get_reply_header_text   s   z"http_request.get_reply_header_textz#([^;?#]*)(;[^?#]*)?(\?[^#]*)?(#.*)?c                 C   sB   | j d u r| j| j}| t| jkrtd| | _ | j S )Nz
Broken URI)r   
path_regexmatchr   endlen
ValueErrorgroups)r   mr!   r!   r"   	split_uri   s   

zhttp_request.split_uric                 C   s8   | j D ]}||}| t|kr||  S qdS N )r   rU   rV   rW   group)r   head_regr^   linerZ   r!   r!   r"   get_header_with_regex   s   

z"http_request.get_header_with_regexc                 C   sv   |  }| j}||vr7|d }t|}| jD ]}|d |   |kr0||d  }|||< |  S qd ||< d S || S )Nz: )lowerr   rW   r   )r   r   hcrJ   hlr`   rr!   r!   r"   
get_header   s   
zhttp_request.get_headerc                 C   s.   | j r| j | d S | dt| d d S )Nz*Dropping %d bytes of incoming request datawarning)	collectorcollect_incoming_datalog_inforW   r   datar!   r!   r"   ri      s   
z"http_request.collect_incoming_datac                 C   s$   | j r
| j   d S | dd d S )Nz-Unexpected end-of-record for incoming requestrg   )rh   found_terminatorrj   rD   r!   r!   r"   rm      s   zhttp_request.found_terminatorc                 C   s>   t |tr	t|}t |trtj|t|d}| j| d S )N)buffer_size)	
isinstancestrr   bytes	producerssimple_producerrW   r   r@   )r   thingr!   r!   r"   push	  s
   

zhttp_request.pushc                 C   s    | j | }|| _d| j||f S )NzHTTP/%s %d %s)	responsesr<   r   )r   codemessager!   r!   r"   r;     s   
zhttp_request.responsec                 C   sR   || _ | j| }| j||d }t|}t|| d< d| d< | | |   d S )N)rw   rx   Content-Lengthz	text/htmlzContent-Type)r<   rv   DEFAULT_ERROR_MESSAGEr   rW   ru   done)r   rw   rx   sr!   r!   r"   error  s   

zhttp_request.errorc                 C   s>  t t| j }d}d}| jdkr$|dkr!d| vrd}n9d| d< n4d}n1| jdkrN|d	kr0d}n%d| vrMd
| v rA| d
 dks@d}n| jrKd| d
< d}n
d}n| jdu rUd}t|  }|rbd	| d< |rut	t
| j}t
||g}n| jd| t
| j}| jtt|| j d| j_|r| j  dS dS )z;finalize this transaction - send output to the http channelr   z1.0z
keep-alivery   r	   z
Keep-Alive
Connectionz1.1closezTransfer-EncodingchunkedN)rf   
CONNECTIONr   rb   r   use_chunkedrr   rs   rS   chunked_producercomposite_producerr   insertr   push_with_producerglobbing_producerhooked_producerlogcurrent_requestclose_when_done)r   
connectionclose_itwrap_in_chunkingoutgoing_headeroutgoing_producerr!   r!   r"   r{   (  s`   	




zhttp_request.donec           	      C   s   t |}t jr|d rt j}nt j}|dkrd}nd}| }t|d\}}t|d\}}|r6d||f }nd||f }t d|| S )	N   r   r	   i  <   z	-%02d%02dz	+%02d%02dz%d/%b/%Y:%H:%M:%S )r   gmtimedaylightaltzonetimezonedivmodstrftime)	r   whengmttznegrJ   remrZ   offsetr!   r!   r"   log_date_stringt  s   
zhttp_request.log_date_stringc              	   C   sB   | j jj| j jd d| j jd | t | j| j|f  d S )Nr   z%d - - [%s] "%s" %d %d
r	   )	r   serverloggerr   addrr   r   r   r<   )r   rq   r!   r!   r"   r     s   


zhttp_request.logd   Continuee   zSwitching ProtocolsOK   Created   Accepted   zNon-Authoritative Information   z
No Content   zReset Content   zPartial Contenti,  zMultiple Choicesi-  zMoved Permanentlyi.  zMoved Temporarilyi/  z	See Otheri0  zNot Modifiedi1  z	Use Proxy  zBad Requesti  Unauthorizedi  zPayment Requiredi  	Forbidden  z	Not Foundi  zMethod Not Allowedi  zNot Acceptablei  zProxy Authentication Requiredi  zRequest Time-outi  Conflicti  Gonei  zLength Requiredi  zPrecondition Failedi  zRequest Entity Too Largei  zRequest-URI Too Largei  zUnsupported Media Type  zInternal Server Errori  zNot Implementedi  zBad GatewayzService UnavailablezGateway Time-outzHTTP Version not supported)i  i  i  r7   )	z<head>z<title>Error response</title>z</head>z<body>z<h1>Error response</h1>z<p>Error code %(code)d.z<p>Message: %(message)s.z</body>r]   c                 C      d S r$   r!   )r   msglevelr!   r!   r"   rj        zhttp_request.log_infor$   )r   )&__name__
__module____qualname__r<   r   r   r   rh   r#   r(   r+   r.   r/   r?   rB   rE   rL   rQ   rS   recompilerT   r[   ra   rf   ri   rm   ru   r;   r}   	reply_nowr{   r   r   rv   r:   rz   rj   r!   r!   r!   r"   r   &   s    	
(			
	L	
 !"#)r   c                   @   s   e Zd ZdZdZe Zdd Zdd ZdZ	dd	 Z
d
d Z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S )http_channeli   Nc                 C   s`   t j | _t | _tj| | || _	|| _
| d d| _tt | _| j| _|   d S )Ns   

    )r   channel_counterr   channel_numberr   r   asynchat
async_chatr#   r   r   set_terminator	in_bufferintr   creation_time	last_usedcheck_maintenance)r   r   connr   r!   r!   r"   r#     s   
zhttp_channel.__init__c                 C   s&   t j| dd }d|| j| jf S )Nr	   z<%s channel#: %s requests:%s>)r   r   __repr__r   r   )r   arr!   r!   r"   r     s   zhttp_channel.__repr__r   c                 C   s   | j | j s|   d S d S r$   )r   maintenance_intervalmaintenancerD   r!   r!   r"   r     s   zhttp_channel.check_maintenancec                 C   s   |    d S r$   )kill_zombiesrD   r!   r!   r"   r     s   zhttp_channel.maintenancei  c                 C   sH   t t }ttj D ]}|j| jkr!||j |jkr!|	  qd S r$   )
r   r   listasyncore
socket_mapvalues	__class__r   zombie_timeoutr   )r   nowr   r!   r!   r"   r     s   zhttp_channel.kill_zombiesc                 C   s2   t j| |}| jjt| tt | _	|S r$   )
r   r   sendr   	bytes_outr   rW   r   r   r   )r   rl   r>   r!   r!   r"   r     s   zhttp_channel.sendc                 C   sT   zt j| |}| jjt| tt | _	|W S  t
y)   td Y d S w )NzOut of Memory!)r   r   recvr   bytes_inr   rW   r   r   r   MemoryErrorsysexit)r   rn   r>   r!   r!   r"   r     s   zhttp_channel.recvc                 C   s4   t  d d \}}|tu r||tj|  d S )Nr   )r   exc_info
SystemExitr   r   handle_error)r   trN   r!   r!   r"   r     s   zhttp_channel.handle_errorc                 G   r   r$   r!   r   r!   r!   r"   r   $  r   zhttp_channel.logc                 C   s&   | j r| j | d S | j| | _d S r$   )r   ri   r   rk   r!   r!   r"   ri   +  s   z"http_channel.collect_incoming_datac                 C   s  | j r
| j   d S | j}d| _|d}|r'|d s'|dd  }|r'|d r|s/|   d S |d }t|\}}}t|dd  }t|\}}d|v r[|rWt|d | }nt|}t	| |||||}	| j
  | jj  |d u r| dt| d |	d	 d S | jjD ]G}
|
|	rz|	| _ |
|	 W  d S    | jj  t \\}}}}}}| d
||||f d z
|	d W Y  d S    Y Y  d S q|	d d S )Nr   s   
r   r	   %?zBad HTTP request: %sr}   r   z'Server Error: %s, %s: file: %s line: %sr   r   )r   rm   r   splitr   crack_requestjoin_headersr   r   r   r   r   r   total_requestsrj   reprr}   handlersrU   handle_request
exceptionsr   compact_traceback)r   r   linesr   r   r   r   rpathrqueryre   rJ   filefunr`   r   rN   tbinfor!   r!   r"   rm   3  s^   



zhttp_channel.found_terminatorc                 C   s:   | j rdS t| jr| j }t|dr|  S dS d S )Nr	   stalled)ac_out_bufferrW   producer_fifofirsthasattrr   )r   pr!   r!   r"   writable_for_proxyz  s   



zhttp_channel.writable_for_proxy)r   r   r   ac_out_buffer_sizer   r   r   r#   r   r   r   r   r   r   r   r   r   r   ri   rm   r   r!   r!   r!   r"   r     s$    	Gr   c                   @   sd   e Zd Zde ZeZd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S )http_serverzHTTP Server (V%s)Nc                 C   sJ  || _ || _tj|  | tjtj g | _	|st
tj}|   | ||f | d | j \}}|sE| dd tt }z
t|d | _W n tjyb   | dd || _Y nw || _t | _t | _t | _t | _t | _|st
tj}|rt
||| _
nt
 || _
| dt!t"#t"" | j|f  d S )Ni   zComputing default hostnamerg   r   zCannot do reverse lookupz2Medusa (V%s) started at %s
	Hostname: %s
	Port:%d
)$ipportr   
dispatcherr#   create_socketsocketAF_INETSOCK_STREAMr   r   file_loggerr   stdoutset_reuse_addrbindlistengetsocknamerj   gethostbynamegethostnamegethostbyaddrserver_namer}   server_portr   total_clientsr   r   r   r   resolving_loggerunresolving_loggerr   r   ctime)r   r   r   resolverlogger_objecthostr!   r!   r"   r#     sP   

zhttp_server.__init__c                 C   s   dS Nr   r!   rD   r!   r!   r"   writable  r   zhttp_server.writablec                 C   r   r$   r!   rD   r!   r!   r"   handle_read  r   zhttp_server.handle_readc                 C   s   | j S r$   )	acceptingrD   r!   r!   r"   readable  s   zhttp_server.readablec                 C   r   r$   r!   rD   r!   r!   r"   handle_connect  r   zhttp_server.handle_connectc                 C   sn   | j   z|  \}}W n  tjy   | dd Y d S  ty-   | dd Y d S w | | || d S )Nz+warning: server accept() threw an exceptionrg   z*warning: server accept() threw EWOULDBLOCK)r  r   acceptr  r}   rj   	TypeErrorchannel_class)r   r   r   r!   r!   r"   handle_accept  s   
zhttp_server.handle_acceptr   c                 C   s&   |r
| j | d S | j d| d S r  )r   r@   r   )r   handlerbackr!   r!   r"   install_handler  s   zhttp_server.install_handlerc                 C   s   | j | d S r$   )r   rG   )r   r"  r!   r!   r"   remove_handler  s   zhttp_server.remove_handlerc                    s   ddl m   fdd}dd tt| jD }| jr'| j t| j  }nd}t	
t	d| j d	| j d
| j d| j d| j d| d|| j  d|| j  d| j dg
g| t	dg S )Nr   english_bytesc                    s   d  | S r\   )r:   )rM   r&  r!   r"   
nice_bytes  r)   z&http_server.status.<locals>.nice_bytesc                 S   s   g | ]}|r|qS r!   r!   )r3   _fr!   r!   r"   r5     r6   z&http_server.status.<locals>.<listcomp>g        z<h2>%s</h2>z!<br>Listening on: <b>Host:</b> %sz<b>Port:</b> %dz#<p><ul><li>Total <b>Clients:</b> %sz<b>Requests:</b> %sz<b>Requests/Client:</b> %.1fz<li>Total <b>Bytes In:</b> %sz<b>Bytes Out:</b> %sz<li>Total <b>Exceptions:</b> %sz!</ul><p><b>Extension List</b><ul>z</ul>)supervisor.medusa.utilr'  mapmaybe_statusr   r  r   as_longfloatrr   r   lines_producerSERVER_IDENTr  r   r   r   r   rs   )r   r(  handler_statsratior!   r&  r"   status  s6   
zhttp_server.status)NN)r   )r   r   r   r   r0  r   r   r#   r  r  r  r  r!  r$  r%  r3  r!   r!   r!   r"   r     s    
6
r   c                 C   s   t | dr	|  S d S )Nr3  )r   r3  )rt   r!   r!   r"   r,    s   
r,  zConnection: (.*)c                 C   sV   g }t t| D ] }| | d dv r!|d | | dd   |d< q|| |  q|S )Nr   z 	r   r	   )rangerW   r@   )rR   re   ir!   r!   r"   r     s   r   r	   c                 C   s:   |D ]}|  |}|r| t|kr||  S qdS r\   )rU   rV   rW   r^   )r_   r   r^   r`   rZ   r!   r!   r"   rf   $  s   
rf   c                 C   s4   |D ]}|  |}|r| t|kr|  S qdS r\   )rU   rV   rW   )r_   r   r`   rZ   r!   r!   r"   get_header_match+  s   
r6  z%([^ ]+) ([^ ]+)(( HTTP/([0-9.]+))$|$)c                 C   sR   t | }|r'| t| kr'|dr|d}nd }|d|d|fS dS )N      r	   r   )NNN)REQUESTrU   rV   rW   r^   )re   rZ   r   r!   r!   r"   r   4  s   

r   __main__zusage: %s <root> <port>z	127.0.0.1fnordi'  r]   iU  )r   r  r  ia  z-pc                   C   s$   zt   W d S  ty   Y d S w r$   )r   loopKeyboardInterruptr!   r!   r!   r"   profile_loopX  s
   r>  zprofile_loop()zprofile.out)r	   )ORCS_IDr   r  r   r   supervisor.compatr   supervisor.medusa.asyncore_25medusaasyncore_25r   supervisor.medusa.asynchat_25asynchat_25r   supervisor.medusa.http_dater   supervisor.medusa.producersrr   supervisor.medusa.loggerr   r   r   supervisor.medusa.counterr   urllibr   r   ImportErrorurllib.parser   r   r   r  r   r,  r   
IGNORECASEr   r   rf   r6  r9  r   r   rW   argvprintsupervisor.medusa.monitormonitorsupervisor.medusa.filesysfilesys!supervisor.medusa.default_handlerdefault_handlersupervisor.medusa.ftp_server
ftp_serversupervisor.medusa.chat_serverchat_serversupervisor.medusa.resolverr  caching_resolverrsr  r  lgsecure_monitor_servermsos_filesystemfsdhr   hsr$  dummy_authorizerftpcsr>  profilerunr<  r!   r!   r!   r"   <module>   s|      / = 
	




