U
    ʼbD                     @   sF  d dl Z ddlmZmZmZ ddlT ddlT ddlmZm	Z	m
Z
mZ dgZe edZe dZd	d
 Zdd Ze e	dZdd Ze e
dZdd ZG dd dZe edZG dd deZG dd deZdd Zeefeeefeee feee!feee"feee#feee!feee"feee#fee$eeedi
Z%dS )    N   )LocalProtocolErrorRemoteProtocolErrorvalidate)*)header_fieldrequest_linestatus_linechunk_headerREADERSasciis   [ \t]+c                 c   s   t | }d }|D ]d}t|}|rb|d kr2tdt|tsDt|}|d7 }||| d  7 }q|d k	rp|V  |}q|d k	r|V  d S )Nz%continuation line at start of headers    )iterobs_fold_rematchr   
isinstance	bytearrayend)linesitlastliner    r   0/tmp/pip-unpacked-wheel-rko__zic/h11/_readers.py_obsolete_line_fold    s$    

r   c                 c   s2   t | D ]$}ttt|}|d |d fV  qd S )N
field_nameZfield_value)r   r   header_field_rebytes)r   r   matchesr   r   r   _decode_header_lines4   s    r   c                 C   sR   |   }|d krd S |s tdtt|d }tf tt|dd  dd|S )Nzno request line receivedr   r   TheadersZ_parsed)maybe_extract_linesr   r   request_line_reRequestlistr   )bufr   r   r   r   r   maybe_read_from_IDLE_client?   s    r'   c                 C   s   |   }|d krd S |s tdtt|d }|d d krBd|d< t|d  }|d< |dk rbtnt}|f tt|dd  dd	|S )
Nzno response line receivedr   reason    status_code   r   Tr    )	r"   r   r   status_line_reintZInformationalResponseResponser%   r   )r&   r   r   r*   class_r   r   r   $maybe_read_from_SEND_RESPONSE_serverL   s    r0   c                   @   s$   e Zd Zdd Zdd Zdd ZdS )ContentLengthReaderc                 C   s   || _ || _d S N)_length
_remaining)selflengthr   r   r   __init__^   s    zContentLengthReader.__init__c                 C   sD   | j dkrt S || j }|d kr(d S |  j t|8  _ t|dS )Nr   data)r4   EndOfMessagemaybe_extract_at_mostlenDatar5   r&   r9   r   r   r   __call__b   s    
zContentLengthReader.__call__c                 C   s   t d| j| j | jd S )Nz]peer closed connection without sending complete message body (received {} bytes, expected {}))r   formatr3   r4   r5   r   r   r   read_eofk   s    
 zContentLengthReader.read_eofN__name__
__module____qualname__r7   r?   rB   r   r   r   r   r1   ]   s   	r1   c                   @   s$   e Zd Zdd Zdd Zdd ZdS )ChunkedReaderc                 C   s   d| _ d| _d| _d S )Nr   F)_bytes_in_chunk_bytes_to_discard_reading_trailerrA   r   r   r   r7   u   s    zChunkedReader.__init__c                 C   sB  | j r,| }|d krd S ttt|dS | jdkrn|| j}|d krNd S |  jt|8  _| jdkrnd S | jdks|t| j	dkr|
d}|d krd S tt|}tt|d dd| _	| j	dkrd| _ | |S d}nd}| j	dkst|| j	}|d krd S |  j	t|8  _	| j	dkr0d	| _d}nd}t|||d
S )N)r!   r   s   

chunk_size   )baseTF   )r9   chunk_start	chunk_end)rJ   r"   r:   r%   r   rI   r;   r<   AssertionErrorrH   Zmaybe_extract_until_nextr   chunk_header_rer-   r   r=   )r5   r&   r   r9   r
   r   rO   rP   r   r   r   r?   }   sD    






zChunkedReader.__call__c                 C   s   t dd S )NzVpeer closed connection without sending complete message body (incomplete chunked read))r   rA   r   r   r   rB      s    zChunkedReader.read_eofNrC   r   r   r   r   rG   t   s   +rG   c                   @   s   e Zd Zdd Zdd ZdS )Http10Readerc                 C   s    | d}|d krd S t|dS )Niɚ;r8   )r;   r=   r>   r   r   r   r?      s    
zHttp10Reader.__call__c                 C   s   t  S r2   )r:   rA   r   r   r   rB      s    zHttp10Reader.read_eofN)rD   rE   rF   r?   rB   r   r   r   r   rS      s   rS   c                 C   s   | rt dd S )NzGot data when expecting EOF)r   )r&   r   r   r   expect_nothing   s    rT   )chunkedzcontent-lengthzhttp/1.0)&reZ_utilr   r   r   _stateZ_eventsZ_abnfr   r   r	   r
   __all__compileencoder   r   r   r   r#   r'   r,   r0   r1   rR   objectrG   rS   rT   ZCLIENTZIDLEZSERVERZSEND_RESPONSEZDONEZ
MUST_CLOSEZCLOSEDZ	SEND_BODYr   r   r   r   r   <module>   sT   
	:
         