idna::uts46

Struct Uts46

source
pub struct Uts46 { /* private fields */ }
Expand description

An implementation of UTS #46.

Implementations§

source§

impl Uts46

source

pub const fn new() -> Self

Constructor using data compiled into the binary.

source

pub fn to_ascii<'a>( &self, domain_name: &'a [u8], ascii_deny_list: AsciiDenyList, hyphens: Hyphens, dns_length: DnsLength, ) -> Result<Cow<'a, str>, Errors>

Performs the ToASCII operation from UTS #46 with the options indicated.

§Arguments
  • domain_name - The input domain name as UTF-8 bytes. (The UTF-8ness is checked by this method and input that is not well-formed UTF-8 is treated as an error. If you already have a &str, call .as_bytes() on it.)
  • ascii_deny_list - What ASCII deny list, if any, to apply. The UTS 46 UseSTD3ASCIIRules flag or the WHATWG URL Standard forbidden domain code point processing is handled via this argument. Most callers are probably the best off by using AsciiDenyList::URL here.
  • hyphens - The UTS 46 CheckHyphens flag. Most callers are probably the best off by using Hyphens::Allow here.
  • dns_length - The UTS 46 VerifyDNSLength flag.
source

pub fn to_unicode<'a>( &self, domain_name: &'a [u8], ascii_deny_list: AsciiDenyList, hyphens: Hyphens, ) -> (Cow<'a, str>, Result<(), Errors>)

Performs the ToUnicode operation from UTS #46 according to the options given. When there are errors, there is still output, which may be rendered user, even through the output must not be used in networking protocols. Errors are denoted by U+FFFD REPLACEMENT CHARACTERs in the output. (That is, if the second item of the return tuple is Err, the first item of the return tuple is guaranteed to contain at least one U+FFFD.)

Most applications probably shouldn’t use this method and should be using Uts46::to_user_interface instead.

§Arguments
  • domain_name - The input domain name as UTF-8 bytes. (The UTF-8ness is checked by this method and input that is not well-formed UTF-8 is treated as an error. If you already have a &str, call .as_bytes() on it.)
  • ascii_deny_list - What ASCII deny list, if any, to apply. The UTS 46 UseSTD3ASCIIRules flag or the WHATWG URL Standard forbidden domain code point processing is handled via this argument. Most callers are probably the best off by using AsciiDenyList::URL here.
  • hyphens - The UTS 46 CheckHyphens flag. Most callers are probably the best off by using Hyphens::Allow here.
source

pub fn to_user_interface<'a, OutputUnicode: FnMut(&[char], &[char], bool) -> bool>( &self, domain_name: &'a [u8], ascii_deny_list: AsciiDenyList, hyphens: Hyphens, output_as_unicode: OutputUnicode, ) -> (Cow<'a, str>, Result<(), Errors>)

Performs the ToUnicode operation from UTS #46 according to options given with some error-free Unicode labels output according to ToASCII instead as decided by application policy implemented via the output_as_unicode closure. The purpose is to convert user-visible domains to the Unicode form in general but to render potentially misleading labels as Punycode.

This is an imperfect security mechanism, because the Punycode form itself may be resemble a user-recognizable name. However, since this mechanism is common practice, this API provides support for The the mechanism.

ASCII labels always pass through as ASCII and labels with errors always pass through as Unicode. For non-erroneous labels that contain at least one non-ASCII character (implies non-empty), output_as_unicode is called with the Unicode form of the label, the TLD (potentially empty), and a flag indicating whether the domain name as a whole is a bidi domain name. If the return value is true, the label passes through as Unicode. If the return value is false, the label is converted to Punycode.

When there are errors, there is still output, which may be rendered user, even through the output must not be used in networking protocols. Errors are denoted by U+FFFD REPLACEMENT CHARACTERs in the output. (That is, if the second item of the return tuple is Err, the first item of the return tuple is guaranteed to contain at least one U+FFFD.) Labels that contain errors are not converted to Punycode.

§Arguments
  • domain_name - The input domain name as UTF-8 bytes. (The UTF-8ness is checked by this method and input that is not well-formed UTF-8 is treated as an error. If you already have a &str, call .as_bytes() on it.)
  • ascii_deny_list - What ASCII deny list, if any, to apply. The UTS 46 UseSTD3ASCIIRules flag or the WHATWG URL Standard forbidden domain code point processing is handled via this argument. Most callers are probably the best off by using AsciiDenyList::URL here.
  • hyphens - The UTS 46 CheckHyphens flag. Most callers are probably the best off by using Hyphens::Allow here.
  • output_as_unicode - A closure for deciding if a label should be output as Unicode (as opposed to Punycode). The first argument is the label for which a decision is needed (always non-empty slice). The second argument is the TLD (potentially empty). The third argument is true iff the domain name as a whole is a bidi domain name. Only non-erroneous labels that contain at least one non-ASCII character are passed to the closure as the first argument. The second and third argument values are guaranteed to remain the same during a single call to process, and the closure may cache computations derived from the second and third argument (hence the FnMut type).
source

pub fn process<W: Write + ?Sized, OutputUnicode: FnMut(&[char], &[char], bool) -> bool>( &self, domain_name: &[u8], ascii_deny_list: AsciiDenyList, hyphens: Hyphens, error_policy: ErrorPolicy, output_as_unicode: OutputUnicode, sink: &mut W, ascii_sink: Option<&mut W>, ) -> Result<ProcessingSuccess, ProcessingError>

The lower-level function that Uts46::to_ascii, Uts46::to_unicode, and Uts46::to_user_interface are built on to allow support for output types other than Cow<'a, str> (e.g. string types in a non-Rust programming language).

§Arguments
  • domain_name - The input domain name as UTF-8 bytes. (The UTF-8ness is checked by this method and input that is not well-formed UTF-8 is treated as an error. If you already have a &str, call .as_bytes() on it.)
  • ascii_deny_list - What ASCII deny list, if any, to apply. The UTS 46 UseSTD3ASCIIRules flag or the WHATWG URL Standard forbidden domain code point processing is handled via this argument. Most callers are probably the best off by using AsciiDenyList::URL here.
  • hyphens - The UTS 46 CheckHyphens flag. Most callers are probably the best off by using Hyphens::Allow here.
  • error_policy - Whether to fail fast or to produce output that may be rendered for the user to examine in case of errors.
  • output_as_unicode - A closure for deciding if a label should be output as Unicode (as opposed to Punycode). The first argument is the label for which a decision is needed (always non-empty slice). The second argument is the TLD (potentially empty). The third argument is true iff the domain name as a whole is a bidi domain name. Only non-erroneous labels that contain at least one non-ASCII character are passed to the closure as the first argument. The second and third argument values are guaranteed to remain the same during a single call to process, and the closure may cache computations derived from the second and third argument (hence the FnMut type). To perform the ToASCII operation, |_, _, _| false must be passed as the closure. To perform the ToUnicode operation, |_, _, _| true must be passed as the closure. A more complex closure may be used to prepare a domain name for display in a user interface so that labels are converted to the Unicode form in general but potentially misleading labels are converted to the Punycode form.
  • sink - The object that receives the output (in the non-passthrough case).
  • ascii_sink - A second sink that receives the ToASCII form only if there were no errors and sink received at least one character of non-ASCII output. The purpose of this argument is to enable a user interface display form of the domain and the ToASCII form of the domain to be computed efficiently together. This argument is useless when output_as_unicode always returns false, in which case the ToASCII form ends up in sink already. If ascii_sink receives no output and the return value is Ok(ProcessingSuccess::WroteToSink), use the output received by sink also as the ToASCII result.
§Return value
  • Ok(ProcessingSuccess::Passthrough) - The caller must treat unsafe { core::str::from_utf8_unchecked(domain_name) } as the output. (This return value asserts that calling core::str::from_utf8_unchecked(domain_name) is safe.)
  • Ok(ProcessingSuccess::WroteToSink) - The caller must treat was was written to sink as the output. If another sink was passed as ascii_sink but it did not receive output, the caller must treat what was written to sink also as the ToASCII output. Otherwise, if ascii_sink received output, the caller must treat what was written to ascii_sink as the ToASCII output.
  • Err(ProcessingError::ValidityError) - The input was in error and must not be used for DNS lookup or otherwise in a network protocol. If error_policy was ErrorPolicy::MarkErrors, the output written to sink may be displayed to the user as an illustration of where the error was or the errors were.
  • Err(ProcessingError::SinkError) - Either sink or ascii_sink returned core::fmt::Error. The partial output written to sink ascii_sink must not be used. If W never returns core::fmt::Error, this method never returns Err(ProcessingError::SinkError).
§Safety-usable invariant

If the return value is Ok(ProcessingSuccess::Passthrough), domain_name is ASCII and core::str::from_utf8_unchecked(domain_name) is safe. (Note: Other return values do not imply that domain_name wasn’t ASCII!)

§Security considerations

Showing labels whose Unicode form might mislead the user as Punycode instead is an imperfect security mechanism, because the Punycode form itself may be resemble a user-recognizable name. However, since this mechanism is common practice, this API provides support for the the mechanism.

Punycode processing is quadratic, so to avoid denial of service, this method imposes length limits on Punycode treating especially long inputs as being in error. These limits are well higher than the DNS length limits and are not more restrictive than the limits imposed by ICU4C.

Trait Implementations§

source§

impl Default for Uts46

source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl Freeze for Uts46

§

impl RefUnwindSafe for Uts46

§

impl !Send for Uts46

§

impl !Sync for Uts46

§

impl Unpin for Uts46

§

impl UnwindSafe for Uts46

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

source§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
source§

impl<T> ErasedDestructor for T
where T: 'static,

source§

impl<T> MaybeSendSync for T