Compare commits

..

No commits in common. "9abf1812a82e07390fdb07cc895df967bbf55a7a" and "707d6d3d543d9e51a7b03a52f5193681b366d062" have entirely different histories.

2 changed files with 41 additions and 115 deletions

View file

@ -2,43 +2,31 @@
//! //!
//! [RFC 6415]: https://datatracker.ietf.org/doc/html/rfc6415 //! [RFC 6415]: https://datatracker.ietf.org/doc/html/rfc6415
use reqwest::header::HeaderValue; use serde::{Serialize, Deserialize};
use serde::Deserialize;
/// A [host-meta document] in JRD representation. /// A [host-meta document].
/// ///
/// [host-meta document]: https://datatracker.ietf.org/doc/html/rfc6415 /// [host-meta document]: https://datatracker.ietf.org/doc/html/rfc6415
#[derive(Debug, Clone, Deserialize)] #[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "PascalCase")]
pub struct HostMetaDocument { pub struct HostMetaDocument {
/// The resource this document refers to. /// The resource this document refers to.
#[serde(alias = "Subject")]
pub subject: Option<String>, pub subject: Option<String>,
/// Links established between the [`Self::subject`] and other resources. /// Links established between the [`Self::subject`] and other resources.
#[serde(alias = "Link")]
pub links: Vec<HostMetaLink>, pub links: Vec<HostMetaLink>,
} }
/// A [host-meta Link Element], which puts the subject resource in relation with another. /// A [host-meta Link Element], which puts the subject resource in relation with another.
/// ///
/// [host-meta Link Element]: https://datatracker.ietf.org/doc/html/rfc6415#section-3.1.1 /// [host-meta Link Element]: https://datatracker.ietf.org/doc/html/rfc6415#section-3.1.1
#[derive(Debug, Clone, Deserialize)] #[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HostMetaLink { pub struct HostMetaLink {
/// The kind of relation established by the subject with the attached resource. /// The kind of relation established by the subject with the attached resource.
#[serde(alias = "@rel")]
pub rel: String, pub rel: String,
/// The resource put in relation with the subject resource. /// The resource put in relation with the subject resource.
#[serde(alias = "@href")] pub href: String,
pub href: Option<String>,
/// Template to fill to get the resource put in relation with the subject resource.
#[serde(alias = "@template")]
pub template: Option<String>,
/// The `Content-Type` of the resource put in relation.
#[serde(alias = "@type")]
pub r#type: Option<String>,
} }
impl HostMetaDocument { impl HostMetaDocument {
@ -72,21 +60,15 @@ impl HostMetaDocument {
.await .await
.map_err(Request)?; .map_err(Request)?;
log::trace!("Checking `Content-Type` of the response..."); log::trace!("Checking headers of the response...");
let content_type = response response
.headers() .headers()
.get(reqwest::header::CONTENT_TYPE) .get("Content-Type")
.ok_or(ContentTypeMissing)?; .ok_or(ContentTypeMissing)?
.eq("application/json")
log::trace!("Extracting MIME type from the `Content-Type` header..."); .then_some(())
let mime_type = extract_mime_from_content_type(content_type)
.ok_or(ContentTypeInvalid)?; .ok_or(ContentTypeInvalid)?;
log::trace!("Ensuring MIME type of `{mime_type}` is acceptable for JRD parsing...");
if mime_type != "application/json" {
return Err(ContentTypeInvalid)
}
log::trace!("Attempting to parse response as JSON..."); log::trace!("Attempting to parse response as JSON...");
let data = response.json::<Self>() let data = response.json::<Self>()
.await .await
@ -125,21 +107,15 @@ impl HostMetaDocument {
.await .await
.map_err(Request)?; .map_err(Request)?;
log::trace!("Checking `Content-Type` of the response..."); log::trace!("Checking headers of the response...");
let content_type = response response
.headers() .headers()
.get(reqwest::header::CONTENT_TYPE) .get("Content-Type")
.ok_or(ContentTypeMissing)?; .ok_or(ContentTypeMissing)?
.eq("application/xrd+json")
log::trace!("Extracting MIME type from the `Content-Type` header..."); .then_some(())
let mime_type = extract_mime_from_content_type(content_type)
.ok_or(ContentTypeInvalid)?; .ok_or(ContentTypeInvalid)?;
log::trace!("Ensuring MIME type of `{mime_type}` is acceptable for JRD parsing...");
if mime_type != "application/xrd+xml" {
return Err(ContentTypeInvalid)
}
log::trace!("Attempting to parse response as text..."); log::trace!("Attempting to parse response as text...");
let data = response.text() let data = response.text()
.await .await
@ -326,13 +302,4 @@ pub enum HostMetaGetJRDError {
ContentTypeInvalid, ContentTypeInvalid,
/// The document failed to be parsed as JSON by [`reqwest`]. /// The document failed to be parsed as JSON by [`reqwest`].
Parse(reqwest::Error), Parse(reqwest::Error),
} }
/// Extract the MIME type from the value of the `Content-Type` header.
fn extract_mime_from_content_type(value: &HeaderValue) -> Option<String> {
let value = value.to_str().ok()?;
match value.split_once("; ") {
None => Some(value.to_string()),
Some((mime, _)) => Some(mime.to_string()),
}
}

View file

@ -1,67 +1,26 @@
fn make_client() -> reqwest::Client { #![allow(non_snake_case)]
let crate_name = env!("CARGO_PKG_NAME");
let crate_version = env!("CARGO_PKG_VERSION"); use acrate_nodeinfo::*;
let crate_repository = env!("CARGO_PKG_REPOSITORY");
let user_agent = format!("{crate_name}/{crate_version} ({crate_repository})");
#[tokio::test]
reqwest::Client::builder() async fn discover_hostmeta__junimo_party() {
.user_agent(user_agent) let client = reqwest::Client::new();
.build() let base: reqwest::Url = "https://junimo.party".parse()
.expect("reqwest client to build") .expect("a valid URL");
HostMetaDocument::discover_hostmeta(&client, base)
.await
.expect("host-meta discovery to succeed");
} }
macro_rules! test { #[tokio::test]
($id:ident, $url:literal) => { async fn discover_nodeinfo__junimo_party() {
test!($id, $url,); let client = reqwest::Client::new();
}; let base: reqwest::Url = "https://junimo.party".parse()
($id:ident, $url:literal, $($tag:meta),*) => { .expect("a valid URL");
mod $id {
use acrate_nodeinfo::*;
use super::*;
#[tokio::test] HostMetaDocument::discover_nodeinfo(&client, base)
$(#[$tag])* .await
async fn test_hostmeta() { .expect("nodeinfo discovery to succeed");
let client = make_client();
let base: reqwest::Url = $url.parse()
.expect("a valid URL");
let doc = HostMetaDocument::discover_hostmeta(&client, base)
.await
.expect("host-meta discovery to succeed");
println!("{doc:#?}");
}
#[tokio::test]
$(#[$tag])*
async fn test_nodeinfo() {
let client = make_client();
let base: reqwest::Url = $url.parse()
.expect("a valid URL");
let doc = HostMetaDocument::discover_nodeinfo(&client, base)
.await
.expect("host-meta discovery to succeed");
println!("{doc:#?}");
}
}
};
} }
test!(akkoma, "https://junimo.party");
test!(mastodon, "https://mastodon.social");
test!(misskey, "https://misskey.io");
test!(iceshrimpnet, "https://ice.frieren.quest");
test!(gotosocial, "https://alpha.polymaths.social");
test!(bridgyfed, "https://fed.brid.gy", ignore = "Returns application/jrd+json");
test!(threads, "https://threads.net", ignore = "Not implemented on their end");