Struct Uts46
pub struct Uts46 { /* private fields */ }
An implementation of UTS #46.
Implementations
impl Uts46
const fn new() -> SelfConstructor using data compiled into the binary.
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 usingAsciiDenyList::URLhere.hyphens- The UTS 46 CheckHyphens flag. Most callers are probably the best off by usingHyphens::Allowhere.dns_length- The UTS 46 VerifyDNSLength flag.
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_interfaceinstead.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 usingAsciiDenyList::URLhere.hyphens- The UTS 46 CheckHyphens flag. Most callers are probably the best off by usingHyphens::Allowhere.
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_unicodeclosure. 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_unicodeis 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 istrue, the label passes through as Unicode. If the return value isfalse, 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 usingAsciiDenyList::URLhere.hyphens- The UTS 46 CheckHyphens flag. Most callers are probably the best off by usingHyphens::Allowhere.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 istrueiff 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 toprocess, and the closure may cache computations derived from the second and third argument (hence theFnMuttype).
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, andUts46::to_user_interfaceare built on to allow support for output types other thanCow<'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 usingAsciiDenyList::URLhere.hyphens- The UTS 46 CheckHyphens flag. Most callers are probably the best off by usingHyphens::Allowhere.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 istrueiff 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 toprocess, and the closure may cache computations derived from the second and third argument (hence theFnMuttype). To perform the ToASCII operation,|_, _, _| falsemust be passed as the closure. To perform the ToUnicode operation,|_, _, _| truemust 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 andsinkreceived 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 whenoutput_as_unicodealways returnsfalse, in which case the ToASCII form ends up insinkalready. Ifascii_sinkreceives no output and the return value isOk(ProcessingSuccess::WroteToSink), use the output received bysinkalso as the ToASCII result.
Return value
Ok(ProcessingSuccess::Passthrough)- The caller must treatunsafe { core::str::from_utf8_unchecked(domain_name) }as the output. (This return value asserts that callingcore::str::from_utf8_unchecked(domain_name)is safe.)Ok(ProcessingSuccess::WroteToSink)- The caller must treat was was written tosinkas the output. If another sink was passed asascii_sinkbut it did not receive output, the caller must treat what was written tosinkalso as the ToASCII output. Otherwise, ifascii_sinkreceived output, the caller must treat what was written toascii_sinkas 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. Iferror_policywasErrorPolicy::MarkErrors, the output written tosinkmay be displayed to the user as an illustration of where the error was or the errors were.Err(ProcessingError::SinkError)- Eithersinkorascii_sinkreturnedcore::fmt::Error. The partial output written tosinkascii_sinkmust not be used. IfWnever returnscore::fmt::Error, this method never returnsErr(ProcessingError::SinkError).
Safety-usable invariant
If the return value is
Ok(ProcessingSuccess::Passthrough),domain_nameis ASCII andcore::str::from_utf8_unchecked(domain_name)is safe. (Note: Other return values do not imply thatdomain_namewasn'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
impl Default for Uts46
fn default() -> Self
Auto Trait Implementations
impl Freeze for Uts46
impl RefUnwindSafe for Uts46
impl Send for Uts46
impl Sync for Uts46
impl Unpin for Uts46
impl UnsafeUnpin for Uts46
impl UnwindSafe for Uts46
Blanket Implementations
impl<T> Any for Uts46
where
T: 'static + ?Sized,
fn type_id(&self) -> TypeId
impl<T> Borrow<T> for Uts46
where
T: ?Sized,
fn borrow(&self) -> &T
impl<T> BorrowMut<T> for Uts46
where
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
impl<T> From<T> for Uts46
fn from(t: T) -> TReturns the argument unchanged.
impl<T, U> Into<U> for Uts46
where
U: From<T>,
fn into(self) -> UCalls
U::from(self).That is, this conversion is whatever the implementation of
[From]<T> for Uchooses to do.
impl<T, U> TryFrom<U> for Uts46
where
U: Into<T>,
type Error = never;fn try_from(value: U) -> Result<T, never>
impl<T, U> TryInto<U> for Uts46
where
U: TryFrom<T>,
type Error = <U as TryFrom<T>>::Error;fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>