3 §˜‚]6ã@sÌdZddlmZddlmZddlmZGdd„deƒZdd„Zd d „Zd d „Z d d„Z d'dd„Z d(dd„Z d)dd„Z d*dd„Zd+dd„Zd,dd„Zdd„Ze eeedœZed d!d"gƒZd#d$„Zd%d&„ZdS)-z3Module containing the validation logic for rfc3986.é)Ú exceptions)Úmisc)Ú normalizersc@sneZdZdZedddddddgƒZd d „Zd d „Zd d„Zdd„Z dd„Z dd„Z dd„Z dd„Z dd„ZdS)Ú Validatora�Object used to configure validation of all objects in rfc3986. .. versionadded:: 1.0 Example usage:: >>> from rfc3986 import api, validators >>> uri = api.uri_reference('https://github.com/') >>> validator = validators.Validator().require_presence_of( ... 'scheme', 'host', 'path', ... ).allow_schemes( ... 'http', 'https', ... ).allow_hosts( ... '127.0.0.1', 'github.com', ... ) >>> validator.validate(uri) >>> invalid_uri = rfc3986.uri_reference('imap://mail.google.com') >>> validator.validate(invalid_uri) Traceback (most recent call last): ... rfc3986.exceptions.MissingComponentError: ('path was required but missing', URIReference(scheme=u'imap', authority=u'mail.google.com', path=None, query=None, fragment=None), ['path']) ÚschemeÚuserinfoÚhostÚportÚpathÚqueryÚfragmentcCsDtƒ|_tƒ|_tƒ|_d|_ddddddddœ|_|jjƒ|_dS)z#Initialize our default validations.TF)rrrr r r r N)ÚsetÚallowed_schemesÚ allowed_hostsÚ allowed_portsÚallow_passwordÚrequired_componentsÚcopyÚvalidated_components)Úself©rúF/tmp/pip-build-el9acr48/urllib3/urllib3/packages/rfc3986/validators.pyÚ__init__:s zValidator.__init__cGs$x|D]}|jjtj|ƒƒqW|S)a Require the scheme to be one of the provided schemes. .. versionadded:: 1.0 :param schemes: Schemes, without ``://`` that are allowed. :returns: The validator instance. :rtype: Validator )rÚaddrZnormalize_scheme)rÚschemesrrrrÚ allow_schemesKs zValidator.allow_schemescGs$x|D]}|jjtj|ƒƒqW|S)zðRequire the host to be one of the provided hosts. .. versionadded:: 1.0 :param hosts: Hosts that are allowed. :returns: The validator instance. :rtype: Validator )rrrZnormalize_host)rÚhostsrrrrÚ allow_hosts[s zValidator.allow_hostscGsBx<|D]4}t|dd�}d|ko(dknr|jj|ƒqW|S)zðRequire the port to be one of the provided ports. .. versionadded:: 1.0 :param ports: Ports that are allowed. :returns: The validator instance. :rtype: Validator é )Úbaseéiÿÿ)Úintrr)rZportsr Zport_intrrrÚ allow_portsks  zValidator.allow_portscCs d|_|S)z®Allow passwords to be present in the URI. .. versionadded:: 1.0 :returns: The validator instance. :rtype: Validator T)r)rrrrÚallow_use_of_password}s zValidator.allow_use_of_passwordcCs d|_|S)z¶Prevent passwords from being included in the URI. .. versionadded:: 1.0 :returns: The validator instance. :rtype: Validator F)r)rrrrÚforbid_use_of_passwordŠs z Validator.forbid_use_of_passwordcGsNdd„|Dƒ}x$|D]}||jkrtdj|ƒƒ‚qW|jjdd„|Dƒƒ|S)aACheck the validity of the components provided. This can be specified repeatedly. .. versionadded:: 1.1 :param components: Names of components from :attr:`Validator.COMPONENT_NAMES`. :returns: The validator instance. :rtype: Validator cSsg|] }|jƒ‘qSr)Úlower)Ú.0Úcrrrú ¥sz/Validator.check_validity_of..z"{}" is not a valid componentcSsi|] }d|“qS)Tr)r&Ú componentrrrú «sz/Validator.check_validity_of..)ÚCOMPONENT_NAMESÚ ValueErrorÚformatrÚupdate)rÚ componentsr)rrrÚcheck_validity_of—s    zValidator.check_validity_ofcGsNdd„|Dƒ}x$|D]}||jkrtdj|ƒƒ‚qW|jjdd„|Dƒƒ|S)a3Require the components provided. This can be specified repeatedly. .. versionadded:: 1.0 :param components: Names of components from :attr:`Validator.COMPONENT_NAMES`. :returns: The validator instance. :rtype: Validator cSsg|] }|jƒ‘qSr)r%)r&r'rrrr(¾sz1Validator.require_presence_of..z"{}" is not a valid componentcSsi|] }d|“qS)Tr)r&r)rrrr*Äsz1Validator.require_presence_of..)r+r,r-rr.)rr/r)rrrÚrequire_presence_of°s    zValidator.require_presence_ofcCs€|jst|ƒdd„|jjƒDƒ}dd„|jjƒDƒ}|rDt||ƒ|rRt||ƒt|j|dƒt|j |dƒt|j |dƒdS)aƒCheck a URI for conditions specified on this validator. .. versionadded:: 1.0 :param uri: Parsed URI to validate. :type uri: rfc3986.uri.URIReference :raises MissingComponentError: When a required component is missing. :raises UnpermittedComponentError: When a component is not one of those allowed. :raises PasswordForbidden: When a password is present in the userinfo component but is not permitted by configuration. :raises InvalidComponentsError: When a component was found to be invalid. cSsg|]\}}|r|‘qSrr)r&r)Úrequiredrrrr(àsz&Validator.validate..cSsg|]\}}|r|‘qSrr)r&r)r2rrrr(åsrrr N) rÚcheck_passwordrÚitemsrÚ ensure_required_components_existÚensure_components_are_validÚ ensure_one_ofrrr)rÚurirrrrrÚvalidateÉs  zValidator.validateN)Ú__name__Ú __module__Ú __qualname__Ú__doc__Ú frozensetr+rrrr"r#r$r0r1r9rrrrrs$  rcCs8|j}|sdS|jddƒ}t|ƒdkr*dStj|ƒ‚dS)z4Assert that there is no password present in the uri.Nú:r)rÚsplitÚlenrZPasswordForbidden)r8rÚ credentialsrrrr3ós  r3cCs0t||ƒ}|dk r,|r,||kr,tj|||ƒ‚dS)z=Assert that the uri's attribute is one of the allowed values.N)ÚgetattrrZUnpermittedComponentError)Zallowed_valuesr8Ú attributeÚvaluerrrr7þs r7cs.t‡fdd„|Dƒƒ}|r*tjˆf|žŽ‚dS)z;Assert that all required components are present in the URI.csg|]}tˆ|ƒdkr|‘qS)N)rC)r&r))r8rrr( sz4ensure_required_components_exist..N)ÚsortedrZMissingComponentError)r8rZmissing_componentsr)r8rr5s   r5cCs(|r|dk o|j|ƒS|dkp&|j|ƒS)a Determine if a value is valid based on the provided matcher. :param str value: Value to validate. :param matcher: Compiled regular expression to use to validate the value. :param require: Whether or not the value is required. N)Úmatch)rEZmatcherÚrequirerrrÚis_valids  rINFcCs(t|tj|ƒ}|r$|dk r$t||ƒS|S)anDetermine if the authority string is valid. :param str authority: The authority to validate. :param str host: (optional) The host portion of the authority to validate. :param bool require: (optional) Specify if authority must not be None. :returns: ``True`` if valid, ``False`` otherwise :rtype: bool N)rIrZSUBAUTHORITY_MATCHERÚ host_is_valid)Ú authorityrrHÚ validatedrrrÚauthority_is_valid$s  rMcCsZt|tj|ƒ}|r.|dk r.tjj|ƒr.t|ƒS|rV|dk rVtjj|ƒrVtjj|ƒdk S|S)aDetermine if the host string is valid. :param str host: The host to validate. :param bool require: (optional) Specify if host must not be None. :returns: ``True`` if valid, ``False`` otherwise :rtype: bool N)rIrZ HOST_MATCHERZ IPv4_MATCHERrGÚvalid_ipv4_host_addressZ IPv6_MATCHERZIPv6_NO_RFC4007_MATCHER)rrHrLrrrrJ8s rJcCst|tj|ƒS)a+Determine if the scheme is valid. :param str scheme: The scheme string to validate. :param bool require: (optional) Set to ``True`` to require the presence of a scheme. :returns: ``True`` if the scheme is valid. ``False`` otherwise. :rtype: bool )rIrZSCHEME_MATCHER)rrHrrrÚscheme_is_validLs rOcCst|tj|ƒS)a+Determine if the path component is valid. :param str path: The path string to validate. :param bool require: (optional) Set to ``True`` to require the presence of a path. :returns: ``True`` if the path is valid. ``False`` otherwise. :rtype: bool )rIrZ PATH_MATCHER)r rHrrrÚ path_is_valid[s rPcCst|tj|ƒS)a0Determine if the query component is valid. :param str query: The query string to validate. :param bool require: (optional) Set to ``True`` to require the presence of a query. :returns: ``True`` if the query is valid. ``False`` otherwise. :rtype: bool )rIrZ QUERY_MATCHER)r rHrrrÚquery_is_validjs rQcCst|tj|ƒS)a?Determine if the fragment component is valid. :param str fragment: The fragment string to validate. :param bool require: (optional) Set to ``True`` to require the presence of a fragment. :returns: ``True`` if the fragment is valid. ``False`` otherwise. :rtype: bool )rIrZFRAGMENT_MATCHER)r rHrrrÚfragment_is_validys rRcCstdd„|jdƒDƒƒS)z4Determine if the given host is a valid IPv4 address.cSs,g|]$}dt|dd�ko"dkn‘qS)r r)réÿ)r!)r&Zbyterrrr(Œsz+valid_ipv4_host_address..Ú.)Úallr@)rrrrrNˆsrN)rr r r rrr cCs‚y |jƒ}Wntjk r"dSX|dkr8t|dƒS|dkrDdSyt|dƒ}Wntk rhdSXd|ko|dkSS)z4Determine if the userinfo, host, and port are valid.Frr Tr iÿÿ)Zauthority_inforZInvalidAuthorityrJr!Ú TypeError)r8r)Zsubauthority_dictr rrrÚsubauthority_component_is_valid™s  rWcCsltgƒ}xJ|D]B}|tkr0t||ƒs|j|ƒqt|}|t||ƒƒs|j|ƒqW|rhtj|f|žŽ‚dS)z0Assert that all components are valid in the URI.N)r Ú_SUBAUTHORITY_VALIDATORSrWrÚ_COMPONENT_VALIDATORSrCrZInvalidComponentsError)r8rZinvalid_componentsr)Z validatorrrrr6±s   r6)NF)F)F)F)F)F)r=ÚrrrÚobjectrr3r7r5rIrMrJrOrPrQrRrNrYr rXrWr6rrrrÚs.   _