Add get_hostname function
This commit is contained in:
parent
879e284403
commit
09d025d461
3 changed files with 45 additions and 5 deletions
|
@ -20,6 +20,7 @@ use crate::models::profiles::types::{
|
||||||
use crate::models::users::types::User;
|
use crate::models::users::types::User;
|
||||||
use crate::utils::crypto::{deserialize_private_key, get_public_key_pem};
|
use crate::utils::crypto::{deserialize_private_key, get_public_key_pem};
|
||||||
use crate::utils::files::get_file_url;
|
use crate::utils::files::get_file_url;
|
||||||
|
use crate::utils::urls::get_hostname;
|
||||||
use super::attachments::{
|
use super::attachments::{
|
||||||
attach_extra_field,
|
attach_extra_field,
|
||||||
attach_identity_proof,
|
attach_identity_proof,
|
||||||
|
@ -119,11 +120,8 @@ impl Actor {
|
||||||
pub fn address(
|
pub fn address(
|
||||||
&self,
|
&self,
|
||||||
) -> Result<ActorAddress, ValidationError> {
|
) -> Result<ActorAddress, ValidationError> {
|
||||||
let hostname = url::Url::parse(&self.id)
|
let hostname = get_hostname(&self.id)
|
||||||
.map_err(|_| ValidationError("invalid actor ID"))?
|
.map_err(|_| ValidationError("invalid actor ID"))?;
|
||||||
.host_str()
|
|
||||||
.ok_or(ValidationError("invalid actor ID"))?
|
|
||||||
.to_owned();
|
|
||||||
let actor_address = ActorAddress {
|
let actor_address = ActorAddress {
|
||||||
username: self.preferred_username.clone(),
|
username: self.preferred_username.clone(),
|
||||||
hostname: hostname,
|
hostname: hostname,
|
||||||
|
|
|
@ -5,3 +5,4 @@ pub mod files;
|
||||||
pub mod html;
|
pub mod html;
|
||||||
pub mod id;
|
pub mod id;
|
||||||
pub mod markdown;
|
pub mod markdown;
|
||||||
|
pub mod urls;
|
||||||
|
|
41
src/utils/urls.rs
Normal file
41
src/utils/urls.rs
Normal file
|
@ -0,0 +1,41 @@
|
||||||
|
use url::{Url, ParseError};
|
||||||
|
|
||||||
|
pub fn get_hostname(url: &str) -> Result<String, ParseError> {
|
||||||
|
let hostname = Url::parse(url)?
|
||||||
|
.host_str()
|
||||||
|
.ok_or(ParseError::EmptyHost)?
|
||||||
|
.to_owned();
|
||||||
|
Ok(hostname)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
#[test]
|
||||||
|
fn test_get_hostname() {
|
||||||
|
let url = "https://example.org/objects/1";
|
||||||
|
let hostname = get_hostname(url).unwrap();
|
||||||
|
assert_eq!(hostname, "example.org");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_get_hostname_tor() {
|
||||||
|
let url = "http://2gzyxa5ihm7nsggfxnu52rck2vv4rvmdlkiu3zzui5du4xyclen53wid.onion/objects/1";
|
||||||
|
let hostname = get_hostname(url).unwrap();
|
||||||
|
assert_eq!(hostname, "2gzyxa5ihm7nsggfxnu52rck2vv4rvmdlkiu3zzui5du4xyclen53wid.onion");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_get_hostname_yggdrasil() {
|
||||||
|
let url = "http://[319:3cf0:dd1d:47b9:20c:29ff:fe2c:39be]/objects/1";
|
||||||
|
let hostname = get_hostname(url).unwrap();
|
||||||
|
assert_eq!(hostname, "[319:3cf0:dd1d:47b9:20c:29ff:fe2c:39be]");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_get_hostname_email() {
|
||||||
|
let url = "mailto:user@example.org";
|
||||||
|
let result = get_hostname(url);
|
||||||
|
assert_eq!(result.is_err(), true);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue