
    i
                         d Z ddlZddlmZ ddlmZmZ deddfdZ	dd	e
fd
ZdededefdZd Zdd	e
ddfdZdd	e
deddfdZdZdefdZdS )z)
Hermes Web UI -- HTTP helper functions.
    N)Path)
IMAGE_EXTSMD_EXTSbodyreturnc                 r      fd|D             }|r%t          dd                    |                     dS )zHPhase D: Validate required fields. Raises ValueError with clean message.c                 p    g | ]2}                     |                               |          d k    0|3S )r   )get).0fr   s     (/home/ubuntu/hermes-webui/api/helpers.py
<listcomp>zrequire.<locals>.<listcomp>   s<    IIIQIq8H8Hq8H8H8H    zMissing required field(s): z, N)
ValueErrorjoin)r   fieldsmissings   `  r   requirer   	   sT    IIII&IIIG MKtyy7I7IKKLLLM Mr     statusc                 *    t          | d|i|          S )z#Return a clean JSON error response.error)r   )j)handlermsgr   s      r   badr      s    WwnV4444r   root	requestedc                     | |z                                   }|                    |                                             |S )zEResolve a relative path inside root, raising ValueError on traversal.)resolverelative_to)r   r   resolveds      r   safe_resolver#      s9    y ))++H(((Or   c                     |                      dd           |                      dd           |                      dd           dS )z'Add security headers to every response.zX-Content-Type-OptionsnosniffzX-Frame-OptionsDENYzReferrer-Policyzsame-originN)send_header)r   s    r   _security_headersr(      sM    0)<<<)6222)=99999r      c                    t          j        |dd                              d          }|                     |           |                     dd           |                     dt          t          |                               |                     dd	           t          |            |                                  | j	        
                    |           d
S )zSend a JSON response.F   )ensure_asciiindentutf-8Content-Typezapplication/json; charset=utf-8Content-LengthCache-Controlno-storeN)_jsondumpsencodesend_responser'   strlenr(   end_headerswfilewrite)r   payloadr   r   s       r   r   r   #   s    ;wU1===DDWMMD&!!!(IJJJ(#c$ii..999444gMr   text/plain; charset=utf-8content_typec                    t          |t                    r|n!t          |                              d          }|                     |           |                     d|           |                     dt          t          |                               |                     dd           t          |            |                                  | j	        
                    |           dS )z#Send a plain text or HTML response.r.   r/   r0   r1   r2   N)
isinstancebytesr7   r5   r6   r'   r8   r(   r9   r:   r;   )r   r<   r   r>   r   s        r   trB   /   s     %00R77c'll6I6I'6R6RD&!!!555(#c$ii..999444gMr   i  @c                 (   t          | j                            dd                    }|t          k    rt	          d| dt           d          |r| j                            |          nd}	 t          j        |          S # t          $ r i cY S w xY w)z9Read and JSON-parse a POST request body (capped at 20MB).r0   r   zRequest body too large (z bytes, max )s   {})
intheadersr
   MAX_BODY_BYTESr   rfilereadr3   loads	Exception)r   lengthraws      r   	read_bodyrN   >   s    $$%5q99::FYFYYYYYZZZ(.
9'-

V
$
$
$EC{3   			s   .B BB)r   )r)   )r)   r=   )__doc__jsonr3   pathlibr   
api.configr   r   dictr   rE   r   r7   r#   r(   r   rB   rG   rN    r   r   <module>rU      sI              * * * * * * * *M$ MD M M M M5 5c 5 5 5 5
t      : : :	 	 	D 	 	 	 		 	 	s 	[_ 	 	 	 	 "	$ 	 	 	 	 	 	r   