rdserver
: Use mediatype
to process Accept
header
This commit is contained in:
parent
f223ce64fe
commit
79e604eb96
1 changed files with 21 additions and 17 deletions
|
@ -1,9 +1,10 @@
|
||||||
|
use std::iter::IntoIterator;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use axum::Extension;
|
use axum::Extension;
|
||||||
use axum::extract::Path;
|
use axum::extract::Path;
|
||||||
use axum::http::{HeaderMap, Response, StatusCode};
|
use axum::http::{HeaderMap, Response, StatusCode};
|
||||||
use axum_extra::extract::Query;
|
use axum_extra::extract::Query;
|
||||||
use mediatype::MediaTypeBuf;
|
use mediatype::{MediaTypeBuf, MediaTypeList};
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
use acrate_database::diesel::GroupedBy;
|
use acrate_database::diesel::GroupedBy;
|
||||||
use acrate_database::diesel_async::{AsyncConnection, AsyncPgConnection};
|
use acrate_database::diesel_async::{AsyncConnection, AsyncPgConnection};
|
||||||
|
@ -20,8 +21,6 @@ pub struct WebfingerQuery {
|
||||||
pub rel: Vec<String>,
|
pub rel: Vec<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[allow(unused_variables)] // Inspection seems to be broken on this function. Idk why...
|
|
||||||
#[axum::debug_handler]
|
|
||||||
pub async fn webfinger_handler(
|
pub async fn webfinger_handler(
|
||||||
Path(path): Path<String>,
|
Path(path): Path<String>,
|
||||||
Query(WebfingerQuery {resource, rel}): Query<WebfingerQuery>,
|
Query(WebfingerQuery {resource, rel}): Query<WebfingerQuery>,
|
||||||
|
@ -38,13 +37,13 @@ pub async fn webfinger_handler(
|
||||||
|
|
||||||
log::debug!("Rel is: {rel:#?}");
|
log::debug!("Rel is: {rel:#?}");
|
||||||
|
|
||||||
let accept = headers.get("Accept")
|
let accept = headers.get("Accept");
|
||||||
.map(|v| v.to_str())
|
|
||||||
.filter(Result::is_ok)
|
|
||||||
.map(|v| v.unwrap())
|
|
||||||
.unwrap_or("application/json")
|
|
||||||
.to_string();
|
|
||||||
log::debug!("Accept is: {accept:#?}");
|
log::debug!("Accept is: {accept:#?}");
|
||||||
|
let accept = accept
|
||||||
|
.map(|h| h.to_str())
|
||||||
|
.unwrap_or(Ok("*/*"))
|
||||||
|
.map(|h| MediaTypeList::new(h))
|
||||||
|
.map_err(|_| StatusCode::BAD_REQUEST)?;
|
||||||
|
|
||||||
let mut response = Response::new("".to_string());
|
let mut response = Response::new("".to_string());
|
||||||
|
|
||||||
|
@ -118,21 +117,26 @@ pub async fn webfinger_handler(
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
for mime in accept.split(",") {
|
for media_type in accept.into_iter() {
|
||||||
|
let media_type = match media_type {
|
||||||
|
Err(e) => {
|
||||||
|
log::debug!("Skipping error while parsing media type: {e:?}");
|
||||||
|
continue;
|
||||||
|
},
|
||||||
|
Ok(media_type) => media_type
|
||||||
|
};
|
||||||
|
|
||||||
{
|
{
|
||||||
let headers = response.headers_mut();
|
let headers = response.headers_mut();
|
||||||
headers.insert(
|
headers.insert(
|
||||||
"Content-Type",
|
"Content-Type",
|
||||||
mime.parse().map_err(|_| StatusCode::BAD_REQUEST)?
|
media_type.to_string()
|
||||||
|
.parse()
|
||||||
|
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
let (mime, _params) = match mime.trim().split_once(";") {
|
match media_type.essence().to_string().to_ascii_lowercase().as_str() {
|
||||||
Some((mime, params)) => (mime, Some(params)),
|
|
||||||
None => (mime, None),
|
|
||||||
};
|
|
||||||
|
|
||||||
match mime {
|
|
||||||
"*/*" | "application/json" | "application/jrd+json" => {
|
"*/*" | "application/json" | "application/jrd+json" => {
|
||||||
let aliases = aliases
|
let aliases = aliases
|
||||||
.into_iter()
|
.into_iter()
|
||||||
|
|
Loading…
Reference in a new issue