o
    hrH                  
   @  s  d Z ddlmZ ddlZddlZddlZddlZddlmZ dZ	dZ
dZed	Zd
dd eddD Zd
dd eddD Zd
dd eddD Zd
dd eddD Zd
dd eddD Zd
dd eddD ZedjddddddZedjddddZededededededededd Zed!Zed"ZG d#d$ d$ejZdFdGd*d+ZdHd-d.ZdId3d4Z dJd:d;Z!dKd<d=Z"dLd?d@Z#dMdBdCZ$dMdDdEZ%dS )Na  
An implementation of `urlparse` that provides URL validation and normalization
as described by RFC3986.

We rely on this implementation rather than the one in Python's stdlib, because:

* It provides more complete URL validation.
* It properly differentiates between an empty querystring and an absent querystring,
  to distinguish URLs with a trailing '?'.
* It handles scheme, hostname, port, and path normalization.
* It supports IDNA hostnames, normalizing them to their encoded form.
* The API supports passing individual components, as well as the complete URL string.

Previously we relied on the excellent `rfc3986` package to handle URL parsing and
validation, but this module provides a simpler alternative, with less indirection
required.
    )annotationsN   )
InvalidURLi   zBABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._~z!$&'()*+,;=z%[A-Fa-f0-9]{2} c                 C     g | ]
}|d vrt |qS ))    "   <   >   `   chr.0i r   c/var/www/html/construction_image-detection-poc/venv/lib/python3.10/site-packages/httpx/_urlparse.py
<listcomp>,       r   r      c                 C  r   ))r   r   #   r	   r
   r   r   r   r   r   r   2   r   c                 C  r   ))	r   r   r   r	   r
   ?   r   {   }   r   r   r   r   r   r   8   s
    c                 C  r   )r   r   r   r	   r
   r   r   r   r   /   :   ;   =   @   [   \   ]   ^   |   r   r   r   r   r   r   C       c                 C  r   r   r   r   r   r   r   r   M   r%   c                 C  r   ))r   r   r   r	   r
   r   r   r   r   r   r   r   r   r    r!   r"   r#   r$   r   r   r   r   r   r   Z   r%   z(?:(?P<scheme>{scheme}):)?(?://(?P<authority>{authority}))?(?P<path>{path})(?:\?(?P<query>{query}))?(?:#(?P<fragment>{fragment}))?z([a-zA-Z][a-zA-Z0-9+.-]*)?z[^/?#]*z[^?#]*z[^#]*z.*scheme	authoritypathqueryfragmentzA(?:(?P<userinfo>{userinfo})@)?(?P<host>{host}):?(?P<port>{port})?z(\[.*\]|[^:@]*))userinfohostportz[^@]*z(\[.*\]|[^:]*))r'   r(   r)   r*   r+   r,   r-   r.   z ^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$z^\[.*\]$c                   @  sv   e Zd ZU ded< ded< ded< ded< ded< ded	< ded
< edddZedddZdddZdddZdS )ParseResultstrr'   r,   r-   
int | Noner.   r)   
str | Noner*   r+   returnc                 C  sV   d | jr| j dndd| jv rd| j dn| j| jd ur'd| j gS dgS )Nr   @:[])joinr,   r-   r.   selfr   r   r   r(      s   zParseResult.authorityc                 C  sB   d d| jv rd| j dn| j| jd urd| j gS dgS )Nr   r5   r6   r7   )r8   r-   r.   r9   r   r   r   netloc   s   zParseResult.netlockwargsc                 K  s:   |s| S | j | j| j| j| jd}|| tdi |S )Nr&   r   r   )r'   r(   r)   r*   r+   updateurlparse)r:   r<   defaultsr   r   r   	copy_with   s   
zParseResult.copy_withc                 C  sl   | j }d| jr| j dnd|rd| nd| j| jd ur$d| j nd| jd ur2d| j gS dgS )Nr   r5   //?#)r(   r8   r'   r)   r*   r+   )r:   r(   r   r   r   __str__   s   zParseResult.__str__N)r3   r0   )r<   r2   r3   r/   )	__name__
__module____qualname____annotations__propertyr(   r;   rA   rE   r   r   r   r   r/      s   
 	
r/   urlr0   r<   r2   r3   c           "      K  s  t | tkr
tdtdd | D r.tdd | D }| |}d|d| d}t|d|v rC|d }t|tr?t|n||d< d	|v rZ|	d	pMd
}|
d\|d< }|d< d|v sbd|v rt|	dd
pjd
td}t|	dd
pvd
td}	|	r| d|	 n||d< d|v r|	dpd
}
|

d\|d< }|d< |sd |d< d|v r|dpd
}d|v r|dr|dsd| d|d< | D ]O\}}|d urt |tkrtd| dtdd |D rtdd |D }||}d| d|d| d}t|t| |std| dqt| }|d us%J | }|d|d p3d
}|d |d  p>d
}|d|d pId
}|d|d }|d!|d! }t|}|d usfJ | }|d|d ptd
}|d|d pd
}|d|d }| }t|td}t|}t||}|d
k}|d
kp|d
kp|d u}t|||d" |s|rt|}t|td}|d u rd nt|td} |d u rd nt|t d}!t!|||||| |!S )#NzURL too longc                 s  "    | ]}|  o|  V  qd S Nisasciiisprintabler   charr   r   r   	<genexpr>        zurlparse.<locals>.<genexpr>c                 s  $    | ]}|  r| s|V  qd S rM   rN   rQ   r   r   r   rS      s   " z.Invalid non-printable ASCII character in URL, z at position .r.   r;   r   r5   r-   usernamepasswordsafer,   raw_pathrC   r)   r*   r6   r7   zURL component 'z
' too longc                 s  rL   rM   rN   rQ   r   r   r   rS     rT   c                 s  rU   rM   rN   rQ   r   r   r   rS     s    
z-Invalid non-printable ASCII character in URL z component, zInvalid URL component ''r'   r(   r+   )
has_schemehas_authority)"lenMAX_URL_LENGTHr   anynextfind
isinstanceintr0   pop	partitionquoteUSERNAME_SAFEPASSWORD_SAFEget
startswithendswithitemsCOMPONENT_REGEX	fullmatch	URL_REGEXmatch	groupdictAUTHORITY_REGEXlowerUSERINFO_SAFEencode_hostnormalize_portvalidate_pathnormalize_path	PATH_SAFE
QUERY_SAFE	FRAG_SAFEr/   )"rK   r<   rR   idxerrorr.   r;   _rW   rX   r[   	seperatorr-   keyvalue	url_matchurl_dictr'   r(   r)   r*   fragauthority_matchauthority_dictr,   parsed_schemeparsed_userinfoparsed_hostparsed_portr]   r^   parsed_pathparsed_queryparsed_fragr   r   r   r?      s   





r?   r-   c                 C  s   | sdS t | r!zt|  W | S  tjy    td| w t| rGzt| dd  W n tjy@   td| w | dd S |  rWd}t	| 
 t| dS zt| 
 dW S  tjyq   td	| w )
Nr   zInvalid IPv4 address: r   zInvalid IPv6 address: z"`{}%|\rY   asciizInvalid IDNA hostname: )IPv4_STYLE_HOSTNAMErr   	ipaddressIPv4AddressAddressValueErrorr   IPv6_STYLE_HOSTNAMEIPv6AddressrO   rh   ru   
SUB_DELIMSidnaencodedecode	IDNAError)r-   WHATWG_SAFEr   r   r   rw   \  s0   

	rw   r.   str | int | Noner'   r1   c                 C  sd   | d u s| dkr
d S zt | }W n ty   td| w dddddd|}||kr0d S |S )Nr   zInvalid port:    P   i  )ftphttphttpswswss)re   
ValueErrorr   rk   )r.   r'   port_as_intdefault_portr   r   r   rx     s   
rx   r)   r]   boolr^   Nonec                 C  sR   |r| r|  dstd|s#|s%|  drtd|  dr'tddS dS dS )z
    Path validation rules that depend on if the URL contains
    a scheme or authority component.

    See https://datatracker.ietf.org/doc/html/rfc3986.html#section-3.3
    /z7For absolute URLs, path must be empty or begin with '/'rB   z3Relative URLs cannot have a path starting with '//'r5   z2Relative URLs cannot have a path starting with ':'N)rl   r   )r)   r]   r^   r   r   r   ry     s   

ry   c                 C  sv   d| vr| S |  d}d|vrd|vr| S g }|D ]}|dkr q|dkr0|r/|dgkr/|  q|| qd|S )z
    Drop "." and ".." segments from a URL path.

    For example:

        normalize_path("/path/./to/somewhere/..") == "/path/to"
    rV   r   z..r   )splitrf   appendr8   )r)   
componentsoutput	componentr   r   r   rz     s   	

rz   stringc                 C  s   d dd | dD S )Nr   c                 S  s   g | ]}d |dqS )%02Xr   )r   byter   r   r   r     s    zPERCENT.<locals>.<listcomp>zutf-8)r8   r   )r   r   r   r   PERCENT  s   r   rZ   c                   s.   t |  |  s| S d fdd| D S )z1
    Use percent-encoding to quote a string.
    r   c                   s    g | ]}| v r
|nt |qS r   )r   rQ   NON_ESCAPED_CHARSr   r   r     s     z#percent_encoded.<locals>.<listcomp>)UNRESERVED_CHARACTERSrstripr8   )r   rZ   r   r   r   percent_encoded  s   
r   c           
      C  s   g }d}t t| D ]*}| | }}|d}||kr-| || }|t||d || |}q
|t| krJ| |d }	|t|	|d d	|S )a  
    Use percent-encoding to quote a string, omitting existing '%xx' escape sequences.

    See: https://www.rfc-editor.org/rfc/rfc3986#section-2.1

    * `string`: The string to be percent-escaped.
    * `safe`: A string containing characters that may be treated as safe, and do not
        need to be escaped. Unreserved characters are always treated as safe.
        See: https://www.rfc-editor.org/rfc/rfc3986#section-2.3
    r   rY   Nr   )
refinditerPERCENT_ENCODED_REGEXstartendgroupr   r   r_   r8   )
r   rZ   partscurrent_positionrr   start_positionend_positionmatched_textleading_texttrailing_textr   r   r   rh     s   


rh   r=   )rK   r0   r<   r2   r3   r/   )r-   r0   r3   r0   )r.   r   r'   r0   r3   r1   )r)   r0   r]   r   r^   r   r3   r   )r)   r0   r3   r0   )r   r0   r3   r0   )r   r0   rZ   r0   r3   r0   )&__doc__
__future__r   r   r   typingr   _exceptionsr   r`   r   r   compiler   r8   ranger}   r|   r{   ri   rj   rv   formatrq   rt   ro   r   r   
NamedTupler/   r?   rw   rx   ry   rz   r   r   rh   r   r   r   r   <module>   s    



7 

/



