Normalize domains containing Unicode characters.
This commit is contained in:
parent
c0623ca419
commit
d2f3f40cba
|
@ -19,6 +19,7 @@ url = "1.6"
|
|||
hex = "0.3"
|
||||
base64 = "0.10"
|
||||
pathdiff = "0.1"
|
||||
idna = "0.1"
|
||||
|
||||
[lib]
|
||||
name = "hagrid_database"
|
||||
|
|
|
@ -12,6 +12,7 @@ use std::str::FromStr;
|
|||
extern crate failure;
|
||||
use failure::Error;
|
||||
use failure::Fallible as Result;
|
||||
extern crate idna;
|
||||
#[macro_use] extern crate log;
|
||||
extern crate parking_lot;
|
||||
use parking_lot::MutexGuard;
|
||||
|
|
|
@ -52,6 +52,11 @@ impl FromStr for Email {
|
|||
|
||||
fn from_str(s: &str) -> Result<Email> {
|
||||
let (localpart, domain) = parse2822address(s)?;
|
||||
|
||||
// Normalize Unicode in domains.
|
||||
let domain = idna::domain_to_ascii(domain)
|
||||
.map_err(|e| failure::format_err!(
|
||||
"punycode conversion failed: {:?}", e))?;
|
||||
Ok(Email(format!("{}@{}", localpart, domain)))
|
||||
}
|
||||
}
|
||||
|
@ -167,5 +172,7 @@ mod tests {
|
|||
assert_eq!(c("Foo Bar <foo@example.org>").as_str(), "foo@example.org");
|
||||
assert_eq!(c("\"Foo Bar\" <foo@example.org>").as_str(),
|
||||
"foo@example.org");
|
||||
assert_eq!(c("foo@👍.example.org").as_str(),
|
||||
"foo@xn--yp8h.example.org");
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue