Compare commits

..

6 commits

2 changed files with 115 additions and 41 deletions

View file

@ -2,31 +2,43 @@
//!
//! [RFC 6415]: https://datatracker.ietf.org/doc/html/rfc6415
use serde::{Serialize, Deserialize};
use reqwest::header::HeaderValue;
use serde::Deserialize;
/// A [host-meta document].
/// A [host-meta document] in JRD representation.
///
/// [host-meta document]: https://datatracker.ietf.org/doc/html/rfc6415
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "PascalCase")]
#[derive(Debug, Clone, Deserialize)]
pub struct HostMetaDocument {
/// The resource this document refers to.
#[serde(alias = "Subject")]
pub subject: Option<String>,
/// Links established between the [`Self::subject`] and other resources.
#[serde(alias = "Link")]
pub links: Vec<HostMetaLink>,
}
/// 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
#[derive(Debug, Clone, Serialize, Deserialize)]
#[derive(Debug, Clone, Deserialize)]
pub struct HostMetaLink {
/// The kind of relation established by the subject with the attached resource.
#[serde(alias = "@rel")]
pub rel: String,
/// The resource put in relation with the subject resource.
pub href: String,
#[serde(alias = "@href")]
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 {
@ -60,15 +72,21 @@ impl HostMetaDocument {
.await
.map_err(Request)?;
log::trace!("Checking headers of the response...");
response
log::trace!("Checking `Content-Type` of the response...");
let content_type = response
.headers()
.get("Content-Type")
.ok_or(ContentTypeMissing)?
.eq("application/json")
.then_some(())
.get(reqwest::header::CONTENT_TYPE)
.ok_or(ContentTypeMissing)?;
log::trace!("Extracting MIME type from the `Content-Type` header...");
let mime_type = extract_mime_from_content_type(content_type)
.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...");
let data = response.json::<Self>()
.await
@ -107,15 +125,21 @@ impl HostMetaDocument {
.await
.map_err(Request)?;
log::trace!("Checking headers of the response...");
response
log::trace!("Checking `Content-Type` of the response...");
let content_type = response
.headers()
.get("Content-Type")
.ok_or(ContentTypeMissing)?
.eq("application/xrd+json")
.then_some(())
.get(reqwest::header::CONTENT_TYPE)
.ok_or(ContentTypeMissing)?;
log::trace!("Extracting MIME type from the `Content-Type` header...");
let mime_type = extract_mime_from_content_type(content_type)
.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...");
let data = response.text()
.await
@ -303,3 +327,12 @@ pub enum HostMetaGetJRDError {
/// The document failed to be parsed as JSON by [`reqwest`].
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,26 +1,67 @@
#![allow(non_snake_case)]
fn make_client() -> reqwest::Client {
let crate_name = env!("CARGO_PKG_NAME");
let crate_version = env!("CARGO_PKG_VERSION");
let crate_repository = env!("CARGO_PKG_REPOSITORY");
let user_agent = format!("{crate_name}/{crate_version} ({crate_repository})");
reqwest::Client::builder()
.user_agent(user_agent)
.build()
.expect("reqwest client to build")
}
macro_rules! test {
($id:ident, $url:literal) => {
test!($id, $url,);
};
($id:ident, $url:literal, $($tag:meta),*) => {
mod $id {
use acrate_nodeinfo::*;
use super::*;
#[tokio::test]
async fn discover_hostmeta__junimo_party() {
let client = reqwest::Client::new();
let base: reqwest::Url = "https://junimo.party".parse()
$(#[$tag])*
async fn test_hostmeta() {
let client = make_client();
let base: reqwest::Url = $url.parse()
.expect("a valid URL");
HostMetaDocument::discover_hostmeta(&client, base)
let doc = HostMetaDocument::discover_hostmeta(&client, base)
.await
.expect("host-meta discovery to succeed");
println!("{doc:#?}");
}
#[tokio::test]
async fn discover_nodeinfo__junimo_party() {
let client = reqwest::Client::new();
let base: reqwest::Url = "https://junimo.party".parse()
$(#[$tag])*
async fn test_nodeinfo() {
let client = make_client();
let base: reqwest::Url = $url.parse()
.expect("a valid URL");
HostMetaDocument::discover_nodeinfo(&client, base)
let doc = HostMetaDocument::discover_nodeinfo(&client, base)
.await
.expect("nodeinfo discovery to succeed");
.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");