
    FdD                         d dl Z d dlZd dlmZmZ d dlmZmZ d dlmZ dedef         defdZ	d	e
defd
Zd	e
de
fdZd	e
defdZdS )    N)AnyCallable)urljoinurlparse)r   func.returnc                 d     t          j                   dt          dt          f fd            }|S )zqThe method decorator validate_url is used to validate urls for any command that requires
    a url as an argumenturlr   c                 :   t          j        d|           st          d          t          |           st          d          t	          |           rt          d          t          |           dk    rt          d           t          |           g|R i |S )a'  Check if the URL is valid using a basic check, urllib check, and local file check

        Args:
            url (str): The URL to check

        Returns:
            the result of the wrapped function

        Raises:
            ValueError if the url fails any of the validation tests
        z
^https?://zInvalid URL formatz"Missing Scheme or Network locationz#Access to local files is restrictedi  zURL is too long)rematch
ValueErroris_valid_urlcheck_local_file_accesslensanitize_url)r
   argskwargsr   s      PC:\Users\Administrator\Downloads\Auto-GPT-master\autogpt\url_utils\validators.pywrapperzvalidate_url.<locals>.wrapper   s     xs++ 	31222C   	CABBB"3'' 	DBCCCs88d??.///tL%%7777777    )	functoolswrapsstrr   )r   r   s   ` r   validate_urlr   	   sM     _T8S 8c 8 8 8 8 8 84 Nr   r
   c                 z    	 t          |           }t          |j        |j        g          S # t          $ r Y dS w xY w)zCheck if the URL is valid

    Args:
        url (str): The URL to check

    Returns:
        bool: True if the URL is valid, False otherwise
    F)r   allschemenetlocr   )r
   results     r   r   r   +   sK    #FM6=1222   uus   ), 
::c                 p    t          |           }|j         |j         d|j         }t	          | |          S )ztSanitize the URL

    Args:
        url (str): The URL to sanitize

    Returns:
        str: The sanitized URL
    ?)r   pathparamsqueryr   )r
   
parsed_urlreconstructed_urls      r   r   r   ;   sA     #J%?RJ,=RR
@PRR3)***r   c                 B     g d}t           fd|D                       S )zCheck if the URL is a local file

    Args:
        url (str): The URL to check

    Returns:
        bool: True if the URL is a local file, False otherwise
    )zfile:///zfile://localhost/zfile://localhostzhttp://localhostzhttp://localhost/zhttps://localhostzhttps://localhost/zhttp://2130706433zhttp://2130706433/zhttps://2130706433zhttps://2130706433/zhttp://127.0.0.1/zhttp://127.0.0.1zhttps://127.0.0.1/zhttps://127.0.0.1zhttps://0.0.0.0/zhttps://0.0.0.0zhttp://0.0.0.0/zhttp://0.0.0.0zhttp://0000zhttp://0000/zhttps://0000zhttps://0000/c              3   B   K   | ]}                     |          V  d S )N)
startswith).0prefixr
   s     r   	<genexpr>z*check_local_file_access.<locals>.<genexpr>k   s/      CC&s~~f%%CCCCCCr   )any)r
   local_prefixess   ` r   r   r   I   s9      N2 CCCCNCCCCCCr   )r   r   typingr   r   urllib.parser   r   requests.compatr   r   boolr   r   r    r   r   <module>r5      s        				                 * * * * * * * * # # # # # #xS) c    Dc d     +c +c + + + +"D "D "D "D "D "D "D "Dr   