U
    ÕCd£%  ã                   @   sT   d dl mZ d dlZddlmZ e e¡Zdd„ ZG dd„ dƒZ	d	d
„ Z
e
ƒ e	_dS )é    )ÚdequeNé   )ÚInvalidTableIndexc                 C   s   dt | ƒ t |ƒ S )aW  
    Calculates the size of a single entry

    This size is mostly irrelevant to us and defined
    specifically to accommodate memory management for
    lower level implementations. The 32 extra bytes are
    considered the "maximum" overhead that would be
    required to represent each entry in the table.

    See RFC7541 Section 4.1
    é    )Úlen)ÚnameÚvalue© r	   ú//tmp/pip-unpacked-wheel-eus5hmsv/hpack/table.pyÚtable_entry_size   s    r   c                   @   sj   e Zd ZdZdZdZeeƒZdd„ Zdd„ Z	dd	„ Z
d
d„ Zdd„ Zedd„ ƒZejdd„ ƒZdd„ ZdS )ÚHeaderTablezø
    Implements the combined static and dynamic header table

    The name and value arguments for all the functions
    should ONLY be byte strings (b'') however this is not
    strictly enforced in the interface.

    See RFC7541 Section 2.3
    i   )=)s
   :authorityó    )ó   :methods   GET)r   s   POST)ó   :pathó   /)r   s   /index.html)ó   :schemes   http)r   s   https)ó   :statuss   200)r   s   204)r   s   206)r   s   304)r   s   400)r   s   404)r   s   500)s   accept-charsetr   )s   accept-encodings   gzip, deflate)s   accept-languager   )s   accept-rangesr   )s   acceptr   )s   access-control-allow-originr   )s   ager   )s   allowr   )s   authorizationr   )s   cache-controlr   )s   content-dispositionr   )s   content-encodingr   )s   content-languager   )s   content-lengthr   )s   content-locationr   )s   content-ranger   )s   content-typer   )s   cookier   )s   dater   )s   etagr   )s   expectr   )s   expiresr   )s   fromr   )s   hostr   )s   if-matchr   )s   if-modified-sincer   )s   if-none-matchr   )s   if-ranger   )s   if-unmodified-sincer   )s   last-modifiedr   )s   linkr   )s   locationr   )s   max-forwardsr   )s   proxy-authenticater   )s   proxy-authorizationr   )s   ranger   )s   refererr   )s   refreshr   )s   retry-afterr   )s   serverr   )s
   set-cookier   )s   strict-transport-securityr   )s   transfer-encodingr   )s
   user-agentr   )s   varyr   )s   viar   )s   www-authenticater   c                 C   s    t j| _d| _d| _tƒ | _d S )Nr   F)r   ÚDEFAULT_SIZEÚ_maxsizeÚ_current_sizeÚresizedr   Údynamic_entries©Úselfr	   r	   r
   Ú__init__l   s    zHeaderTable.__init__c                 C   sZ   |}|d8 }d|krJ|t jk r(t j| S |t j8 }|t| jƒk rJ| j| S td| ƒ‚dS )an  
        Returns the entry specified by index

        Note that the table is 1-based ie an index of 0 is
        invalid.  This is due to the fact that a zero value
        index signals that a completely unindexed header
        follows.

        The entry will either be from the static table or
        the dynamic table depending on the value of index.
        r   r   zInvalid table index %dN)r   ÚSTATIC_TABLE_LENGTHÚSTATIC_TABLEr   r   r   )r   ÚindexZoriginal_indexr	   r	   r
   Úget_by_indexr   s    



zHeaderTable.get_by_indexc                 C   s   d| j | j| jf S )NzHeaderTable(%d, %s, %r))r   r   r   r   r	   r	   r
   Ú__repr__Š   s
    ýzHeaderTable.__repr__c                 C   sP   t ||ƒ}|| jkr&| j ¡  d| _n&| j ||f¡ |  j|7  _|  ¡  dS )z•
        Adds a new entry to the table

        We reduce the table size if the entry will make the
        table size greater than maxsize.
        r   N)r   r   r   Úclearr   Ú
appendleftÚ_shrink)r   r   r   Úsizer	   r	   r
   Úadd‘   s    


zHeaderTable.addc           
      C   s    d}t j |¡}|rB|d  |¡}|dk	r4|||fS |d |df}t jd }t| jƒD ]D\}\}}	||krV|	|kr„|| ||	f  S |dkrV|| |df}qV|S )a)  
        Searches the table for the entry specified by name
        and value

        Returns one of the following:
            - ``None``, no match at all
            - ``(index, name, None)`` for partial matches on name only.
            - ``(index, name, value)`` for perfect matches.
        Nr   r   )r   ÚSTATIC_TABLE_MAPPINGÚgetr   Ú	enumerater   )
r   r   r   ÚpartialÚheader_name_search_resultr   ÚoffsetÚiÚnÚvr	   r	   r
   Úsearch£   s    


zHeaderTable.searchc                 C   s   | j S )N)r   r   r	   r	   r
   ÚmaxsizeÀ   s    zHeaderTable.maxsizec                 C   s\   t |ƒ}t d|| j¡ | j}|| _||k| _|dkrH| j ¡  d| _n||krX|  ¡  d S )Nz#Resizing header table to %d from %dr   )	ÚintÚlogÚdebugr   r   r   r    r   r"   )r   ZnewmaxZoldmaxr	   r	   r
   r/   Ä   s    

c                 C   sF   | j }|| jkr<| j ¡ \}}|t||ƒ8 }t d||¡ q|| _ dS )zE
        Shrinks the dynamic table to be at or below maxsize
        z%Evicting %s: %s from the header tableN)r   r   r   Úpopr   r1   r2   )r   Zcursizer   r   r	   r	   r
   r"   Ñ   s    
zHeaderTable._shrinkN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r   r   r   r$   r.   Úpropertyr/   Úsetterr"   r	   r	   r	   r
   r      s   @

r   c                  C   s>   i } t tjdƒD ](\}\}}|  ||i f¡}||d |< q| S )zÐ
    Build static table mapping from header name to tuple with next structure:
    (<minimal index of header>, <mapping from header value to it index>).

    static_table_mapping used for hash searching.
    r   )r'   r   r   Ú
setdefault)Zstatic_table_mappingr   r   r   r)   r	   r	   r
   Ú_build_static_table_mappingÝ   s
    r;   )Úcollectionsr   ÚloggingÚ
exceptionsr   Ú	getLoggerr4   r1   r   r   r;   r%   r	   r	   r	   r
   Ú<module>   s   
 D