o
     i                     @   st   d Z ddlZddlZzddlmZ W n ey!   ddlmZ Y nw dddZedZ	dd	 Z
G d
d deZdS )a]  
Use this in the same way as Python's SimpleHTTPServer:

  python -m RangeHTTPServer [port]

The only difference from SimpleHTTPServer is that RangeHTTPServer supports
'Range:' headers to load portions of files. This is helpful for doing local web
development with genomic data files, which tend to be to large to load into the
browser all at once.
    N)SimpleHTTPRequestHandler @  c                 C   sP   |dur	|  | 	 t||r|d |   n|}| |}|s"dS || q
)zkLike shutil.copyfileobj, but only copy a range of the streams.

    Both start and stop are inclusive.
    N   )seekmintellreadwrite)infileoutfilestartstopbufsizeto_readbuf r   V/home/ubuntu/SoloSpeech/.venv/lib/python3.10/site-packages/RangeHTTPServer/__init__.pycopy_byte_range   s   

r   zbytes=(\d+)-(\d+)?$c                 C   s`   |   dkrdS t| }|std|  dd | D \}}|r,||k r,td|  ||fS )zwReturns the two numbers in 'bytes=123-456' or throws ValueError.

    The last number or both numbers may be None.
     )NNzInvalid byte range %sc                 S   s   g | ]}|o	t |qS r   )int).0xr   r   r   
<listcomp>3   s    z$parse_byte_range.<locals>.<listcomp>)stripBYTE_RANGE_REmatch
ValueErrorgroups)
byte_rangemfirstlastr   r   r   parse_byte_range'   s   
r"   c                   @   s(   e Zd ZdZdd Zdd Zdd ZdS )	RangeRequestHandlerzAdds support for HTTP 'Range' requests to SimpleHTTPRequestHandler

    The approach is to:
    - Override send_head to look for 'Range' and respond appropriately.
    - Override copyfile to only transmit a range when requested.
    c           
   
   C   sd  d| j vrd | _t| S z
t| j d | _W n ty0 } z| dd W Y d }~d S d }~ww | j\}}| | j}d }| 	|}zt
|d}W n tyZ   | dd Y d S w t| }|d }||krr| dd	 d S | d
 | d| |d u s||kr|d }|| d }	| dd|||f  | dt|	 | d| |j |   |S )NRangei  zInvalid byte rangerbi  zFile not found   i  zRequested Range Not Satisfiable   zContent-typer   zContent-Rangezbytes %s-%s/%szContent-LengthzLast-Modified)headersranger   	send_headr"   r   
send_errortranslate_pathpath
guess_typeopenIOErrorosfstatfilenosend_responsesend_headerstrdate_time_stringst_mtimeend_headers)
selfer    r!   r-   fctypefsfile_lenresponse_lengthr   r   r   r*   @   sJ   




zRangeRequestHandler.send_headc                 C   s   |  dd t| S )NzAccept-Rangesbytes)r5   r   r9   )r:   r   r   r   r9   i   s   
zRangeRequestHandler.end_headersc                 C   s0   | j s
t| ||S | j \}}t|||| d S )N)r)   r   copyfiler   )r:   source
outputfiler   r   r   r   r   rB   m   s   
zRangeRequestHandler.copyfileN)__name__
__module____qualname____doc__r*   r9   rB   r   r   r   r   r#   9   s
    )r#   )NNr   )rH   r1   rehttp.serverr   ImportErrorSimpleHTTPServerr   compiler   r"   r#   r   r   r   r   <module>   s    

