U
    /v¶h]  ã                   @  s    d dl mZ d dlZd dlmZ ddlmZ ddlm	Z	 ddl
mZ dd	d
dœdd„Zdddddddœdd„Zddddddddœdd„Zdddddœdd„ZdS )é    )ÚannotationsN)Úquoteé   )Ú
_plain_int)ÚSecurityError)Ú
uri_to_iriz
str | Nonezt.Iterable[str]Úbool)ÚhostnameÚtrusted_listÚreturnc              	   C  sÖ   | sdS z|   d¡d  d¡ d¡} W n tk
r<   Y dS X t|tƒrN|g}|D ]~}| d¡rr|dd… }d	}nd}z|  d¡d  d¡ d¡}W n tk
r¬   Y  dS X || ksÊ|rR|  d|› �¡rR d	S qRdS )
zôCheck if a host matches a list of trusted names.

    :param hostname: The name to check.
    :param trusted_list: A list of valid names to match. If a name
        starts with a dot it will match all subdomains.

    .. versionadded:: 0.9
    Fú:r   ÚidnaÚasciiÚ.é   NT)Ú	partitionÚencodeÚdecodeÚUnicodeEncodeErrorÚ
isinstanceÚstrÚ
startswithÚendswith)r	   r
   ÚrefZsuffix_match© r   ú9/tmp/pip-unpacked-wheel-c3xdgso7/werkzeug/sansio/utils.pyÚhost_is_trusted   s(    	


r   r   ztuple[str, int | None] | Nonezt.Iterable[str] | None)ÚschemeÚhost_headerÚserverÚtrusted_hostsr   c                 C  s¤   d}|dk	r|}n.|dk	r@|d }|d dk	r@|› d|d › �}| dkr`|  d¡r`|dd… }n| d	kr~|  d
¡r~|dd… }|dk	r t||ƒs td|›d�ƒ‚|S )a$  Return the host for the given parameters.

    This first checks the ``host_header``. If it's not present, then
    ``server`` is used. The host will only contain the port if it is
    different than the standard port for the protocol.

    Optionally, verify that the host is trusted using
    :func:`host_is_trusted` and raise a
    :exc:`~werkzeug.exceptions.SecurityError` if it is not.

    :param scheme: The protocol the request used, like ``"https"``.
    :param host_header: The ``Host`` header value.
    :param server: Address of the server. ``(host, port)``, or
        ``(path, None)`` for unix sockets.
    :param trusted_hosts: A list of trusted host names.

    :return: Host, with port if necessary.
    :raise ~werkzeug.exceptions.SecurityError: If the host is not
        trusted.
    Ú Nr   r   r   >   ÚhttpÚwsz:80éýÿÿÿ>   ÚwssÚhttpsz:443éüÿÿÿzHost z is not trusted.)r   r   r   )r   r   r   r    Úhostr   r   r   Úget_host1   s    
r)   zbytes | None)r   r(   Ú	root_pathÚpathÚquery_stringr   c                 C  s¨   | d|g}|dkr*|  d¡ td |¡ƒS |  t| d¡dd�¡ |  d¡ |dkrbtd |¡ƒS |  t| d¡dd�¡ |rš|  d¡ |  t|dd�¡ td |¡ƒS )	aƒ  Recreate the URL for a request. If an optional part isn't
    provided, it and subsequent parts are not included in the URL.

    The URL is an IRI, not a URI, so it may contain Unicode characters.
    Use :func:`~werkzeug.urls.iri_to_uri` to convert it to ASCII.

    :param scheme: The protocol the request used, like ``"https"``.
    :param host: The host the request was made to. See :func:`get_host`.
    :param root_path: Prefix that the application is mounted under. This
        is prepended to ``path``.
    :param path: The path part of the URL after ``root_path``.
    :param query_string: The portion of the URL after the "?".
    z://Nú/r!   z!$&'()*+,/:;=@%)Úsafeú?z!$&'()*+,/:;=?@%)Úappendr   Újoinr   ÚrstripÚlstrip)r   r(   r*   r+   r,   Úurlr   r   r   Úget_current_urla   s    



r5   z
int | None)Úhttp_content_lengthÚhttp_transfer_encodingr   c                 C  s@   |dks| dkrdS zt dt| ƒƒW S  tk
r:   Y dS X dS )a¥  Return the ``Content-Length`` header value as an int. If the header is not given
    or the ``Transfer-Encoding`` header is ``chunked``, ``None`` is returned to indicate
    a streaming request. If the value is not an integer, or negative, 0 is returned.

    :param http_content_length: The Content-Length HTTP header.
    :param http_transfer_encoding: The Transfer-Encoding HTTP header.

    .. versionadded:: 2.2
    ÚchunkedNr   )Úmaxr   Ú
ValueError)r6   r7   r   r   r   Úget_content_lengthŒ   s    r;   )NN)NNN)NN)Ú
__future__r   ÚtypingÚtÚurllib.parser   Ú	_internalr   Ú
exceptionsr   Úurlsr   r   r)   r5   r;   r   r   r   r   Ú<module>   s    )  ü3   û,  þ