Compare commits
6 commits
707d6d3d54
...
9abf1812a8
Author | SHA1 | Date | |
---|---|---|---|
9abf1812a8 | |||
6ca0b5368a | |||
ee2f386cf0 | |||
e5f52a760a | |||
d964f1befc | |||
de5fdf26f3 |
2 changed files with 115 additions and 41 deletions
|
@ -2,31 +2,43 @@
|
||||||
//!
|
//!
|
||||||
//! [RFC 6415]: https://datatracker.ietf.org/doc/html/rfc6415
|
//! [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
|
/// [host-meta document]: https://datatracker.ietf.org/doc/html/rfc6415
|
||||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
#[derive(Debug, Clone, 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, Serialize, Deserialize)]
|
#[derive(Debug, Clone, 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.
|
||||||
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 {
|
impl HostMetaDocument {
|
||||||
|
@ -60,15 +72,21 @@ impl HostMetaDocument {
|
||||||
.await
|
.await
|
||||||
.map_err(Request)?;
|
.map_err(Request)?;
|
||||||
|
|
||||||
log::trace!("Checking headers of the response...");
|
log::trace!("Checking `Content-Type` of the response...");
|
||||||
response
|
let content_type = response
|
||||||
.headers()
|
.headers()
|
||||||
.get("Content-Type")
|
.get(reqwest::header::CONTENT_TYPE)
|
||||||
.ok_or(ContentTypeMissing)?
|
.ok_or(ContentTypeMissing)?;
|
||||||
.eq("application/json")
|
|
||||||
.then_some(())
|
log::trace!("Extracting MIME type from the `Content-Type` header...");
|
||||||
|
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
|
||||||
|
@ -107,15 +125,21 @@ impl HostMetaDocument {
|
||||||
.await
|
.await
|
||||||
.map_err(Request)?;
|
.map_err(Request)?;
|
||||||
|
|
||||||
log::trace!("Checking headers of the response...");
|
log::trace!("Checking `Content-Type` of the response...");
|
||||||
response
|
let content_type = response
|
||||||
.headers()
|
.headers()
|
||||||
.get("Content-Type")
|
.get(reqwest::header::CONTENT_TYPE)
|
||||||
.ok_or(ContentTypeMissing)?
|
.ok_or(ContentTypeMissing)?;
|
||||||
.eq("application/xrd+json")
|
|
||||||
.then_some(())
|
log::trace!("Extracting MIME type from the `Content-Type` header...");
|
||||||
|
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
|
||||||
|
@ -303,3 +327,12 @@ pub enum HostMetaGetJRDError {
|
||||||
/// 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()),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -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 acrate_nodeinfo::*;
|
||||||
|
use super::*;
|
||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn discover_hostmeta__junimo_party() {
|
$(#[$tag])*
|
||||||
let client = reqwest::Client::new();
|
async fn test_hostmeta() {
|
||||||
let base: reqwest::Url = "https://junimo.party".parse()
|
let client = make_client();
|
||||||
|
|
||||||
|
let base: reqwest::Url = $url.parse()
|
||||||
.expect("a valid URL");
|
.expect("a valid URL");
|
||||||
|
|
||||||
HostMetaDocument::discover_hostmeta(&client, base)
|
let doc = HostMetaDocument::discover_hostmeta(&client, base)
|
||||||
.await
|
.await
|
||||||
.expect("host-meta discovery to succeed");
|
.expect("host-meta discovery to succeed");
|
||||||
|
|
||||||
|
println!("{doc:#?}");
|
||||||
}
|
}
|
||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn discover_nodeinfo__junimo_party() {
|
$(#[$tag])*
|
||||||
let client = reqwest::Client::new();
|
async fn test_nodeinfo() {
|
||||||
let base: reqwest::Url = "https://junimo.party".parse()
|
let client = make_client();
|
||||||
|
|
||||||
|
let base: reqwest::Url = $url.parse()
|
||||||
.expect("a valid URL");
|
.expect("a valid URL");
|
||||||
|
|
||||||
HostMetaDocument::discover_nodeinfo(&client, base)
|
let doc = HostMetaDocument::discover_nodeinfo(&client, base)
|
||||||
.await
|
.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");
|
||||||
|
|
Loading…
Reference in a new issue