mirror of
https://github.com/astro/buzzrelay.git
synced 2024-11-21 19:51:00 +00:00
main: consolidate actor uri parsing into Actor::from_uri()
This commit is contained in:
parent
f9a103de9a
commit
316b34c689
2 changed files with 33 additions and 43 deletions
49
src/actor.rs
49
src/actor.rs
|
@ -27,26 +27,39 @@ pub struct Actor {
|
||||||
|
|
||||||
impl Actor {
|
impl Actor {
|
||||||
pub fn from_uri(mut uri: &str) -> Option<Self> {
|
pub fn from_uri(mut uri: &str) -> Option<Self> {
|
||||||
if ! uri.starts_with("https://") {
|
let kind;
|
||||||
|
let host;
|
||||||
|
if uri.starts_with("acct:tag-") {
|
||||||
|
let off = "acct:tag-".len();
|
||||||
|
let Some(at) = uri.find('@') else { return None; };
|
||||||
|
kind = ActorKind::TagRelay(uri[off..at].to_string());
|
||||||
|
host = Arc::new(uri[at + 1..].to_string());
|
||||||
|
} else if uri.starts_with("acct:instance-") {
|
||||||
|
let off = "acct:instance-".len();
|
||||||
|
let Some(at) = uri.find('@') else { return None; };
|
||||||
|
kind = ActorKind::InstanceRelay(uri[off..at].to_string());
|
||||||
|
host = Arc::new(uri[at + 1..].to_string());
|
||||||
|
} else if uri.starts_with("https://") {
|
||||||
|
uri = &uri[8..];
|
||||||
|
|
||||||
|
let parts = uri.split('/').collect::<Vec<_>>();
|
||||||
|
if parts.len() != 3 {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
|
||||||
|
let Ok(topic) = urlencoding::decode(parts[2]) else { return None; };
|
||||||
|
kind = match parts[1] {
|
||||||
|
"tag" =>
|
||||||
|
ActorKind::TagRelay(topic.to_string()),
|
||||||
|
"instance" =>
|
||||||
|
ActorKind::InstanceRelay(topic.to_string()),
|
||||||
|
_ =>
|
||||||
|
return None,
|
||||||
|
};
|
||||||
|
host = Arc::new(parts[0].to_string());
|
||||||
|
} else {
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
uri = &uri[8..];
|
|
||||||
|
|
||||||
let parts = uri.split("/").collect::<Vec<_>>();
|
|
||||||
if parts.len() != 3 {
|
|
||||||
return None;
|
|
||||||
}
|
|
||||||
|
|
||||||
let Ok(topic) = urlencoding::decode(parts[2]) else { return None; };
|
|
||||||
let kind = match parts[1] {
|
|
||||||
"tag" =>
|
|
||||||
ActorKind::TagRelay(topic.to_string()),
|
|
||||||
"instance" =>
|
|
||||||
ActorKind::InstanceRelay(topic.to_string()),
|
|
||||||
_ =>
|
|
||||||
return None,
|
|
||||||
};
|
|
||||||
let host = Arc::new(parts[0].to_string());
|
|
||||||
Some(Actor { host, kind })
|
Some(Actor { host, kind })
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
27
src/main.rs
27
src/main.rs
|
@ -9,7 +9,7 @@ use metrics::increment_counter;
|
||||||
use metrics_util::MetricKindMask;
|
use metrics_util::MetricKindMask;
|
||||||
use metrics_exporter_prometheus::PrometheusBuilder;
|
use metrics_exporter_prometheus::PrometheusBuilder;
|
||||||
use serde_json::json;
|
use serde_json::json;
|
||||||
use std::{net::SocketAddr, sync::Arc, time::Duration, collections::HashMap};
|
use std::{net::SocketAddr, time::Duration, collections::HashMap};
|
||||||
use std::{panic, process};
|
use std::{panic, process};
|
||||||
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};
|
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};
|
||||||
use reqwest::Url;
|
use reqwest::Url;
|
||||||
|
@ -37,7 +37,6 @@ fn track_request(method: &'static str, controller: &'static str, result: &'stati
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn webfinger(
|
async fn webfinger(
|
||||||
axum::extract::State(state): axum::extract::State<State>,
|
|
||||||
Query(params): Query<HashMap<String, String>>,
|
Query(params): Query<HashMap<String, String>>,
|
||||||
) -> Response {
|
) -> Response {
|
||||||
let resource = match params.get("resource") {
|
let resource = match params.get("resource") {
|
||||||
|
@ -47,29 +46,7 @@ async fn webfinger(
|
||||||
return StatusCode::NOT_FOUND.into_response();
|
return StatusCode::NOT_FOUND.into_response();
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
let target = if resource.starts_with("acct:") {
|
let Some(target) = Actor::from_uri(resource) else {
|
||||||
let (target_kind, target_host) =
|
|
||||||
if resource.starts_with("acct:tag-") {
|
|
||||||
let off = "acct:tag-".len();
|
|
||||||
let at = resource.find('@');
|
|
||||||
(actor::ActorKind::TagRelay(resource[off..at.unwrap_or(resource.len())].to_string()),
|
|
||||||
at.map_or_else(|| state.hostname.clone(), |at| Arc::new(resource[at + 1..].to_string())))
|
|
||||||
} else if resource.starts_with("acct:instance-") {
|
|
||||||
let off = "acct:instance-".len();
|
|
||||||
let at = resource.find('@');
|
|
||||||
(actor::ActorKind::InstanceRelay(resource[off..at.unwrap_or(resource.len())].to_string()),
|
|
||||||
at.map_or_else(|| state.hostname.clone(), |at| Arc::new(resource[at + 1..].to_string())))
|
|
||||||
} else {
|
|
||||||
track_request("GET", "webfinger", "not_found");
|
|
||||||
return StatusCode::NOT_FOUND.into_response();
|
|
||||||
};
|
|
||||||
actor::Actor {
|
|
||||||
host: target_host,
|
|
||||||
kind: target_kind,
|
|
||||||
}
|
|
||||||
} else if let Some(target) = Actor::from_uri(resource) {
|
|
||||||
target
|
|
||||||
} else {
|
|
||||||
track_request("GET", "webfinger", "not_found");
|
track_request("GET", "webfinger", "not_found");
|
||||||
return StatusCode::NOT_FOUND.into_response();
|
return StatusCode::NOT_FOUND.into_response();
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in a new issue