nodeinfo
: Split off Content-Type
parameters before checking the MIME type
This commit is contained in:
parent
de5fdf26f3
commit
d964f1befc
1 changed files with 33 additions and 11 deletions
|
@ -2,6 +2,7 @@
|
|||
//!
|
||||
//! [RFC 6415]: https://datatracker.ietf.org/doc/html/rfc6415
|
||||
|
||||
use reqwest::header::HeaderValue;
|
||||
use serde::{Serialize, Deserialize};
|
||||
|
||||
/// A [host-meta document].
|
||||
|
@ -60,15 +61,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(reqwest::header::CONTENT_TYPE)
|
||||
.ok_or(ContentTypeMissing)?
|
||||
.eq("application/json")
|
||||
.then_some(())
|
||||
.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 +114,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(reqwest::header::CONTENT_TYPE)
|
||||
.ok_or(ContentTypeMissing)?
|
||||
.eq("application/xrd+json")
|
||||
.then_some(())
|
||||
.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 +316,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()),
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue