refactor: separate http musikcubed calls into a method
This commit is contained in:
parent
c2425bd4a4
commit
59b0e29f0d
@ -18,7 +18,7 @@ use base64::Engine;
|
|||||||
use futures::{SinkExt, StreamExt};
|
use futures::{SinkExt, StreamExt};
|
||||||
use http::{
|
use http::{
|
||||||
header::{AUTHORIZATION, CACHE_CONTROL, CONTENT_TYPE, RANGE},
|
header::{AUTHORIZATION, CACHE_CONTROL, CONTENT_TYPE, RANGE},
|
||||||
HeaderMap, HeaderName, HeaderValue, Method, Request, Response, StatusCode,
|
HeaderName, HeaderValue, Method, Request, Response, StatusCode,
|
||||||
};
|
};
|
||||||
use hyper::Body;
|
use hyper::Body;
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
@ -179,16 +179,11 @@ async fn get_scoped_music_thumbnail(
|
|||||||
let Some(info) = app.music_info.get(music_id).await else {
|
let Some(info) = app.music_info.get(music_id).await else {
|
||||||
return Err("music id not found".into());
|
return Err("music id not found".into());
|
||||||
};
|
};
|
||||||
let req = Request::builder()
|
app.make_musikcubed_request(
|
||||||
.uri(format!(
|
format!("thumbnail/{}", info.thumbnail_id),
|
||||||
"http://{}:{}/thumbnail/{}",
|
Request::new(Body::empty()),
|
||||||
app.musikcubed_address, app.musikcubed_http_port, info.thumbnail_id
|
)
|
||||||
))
|
.await
|
||||||
.header(AUTHORIZATION, app.musikcubed_auth_header_value.clone())
|
|
||||||
.body(Body::empty())
|
|
||||||
.expect("cant fail");
|
|
||||||
let resp = app.client.request(req).await?;
|
|
||||||
Ok(resp)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn get_scoped_music_file(
|
async fn get_scoped_music_file(
|
||||||
@ -197,19 +192,14 @@ async fn get_scoped_music_file(
|
|||||||
request: Request<Body>,
|
request: Request<Body>,
|
||||||
) -> Result<Response<Body>, AppError> {
|
) -> Result<Response<Body>, AppError> {
|
||||||
let music_id = app.verify_scoped_token(token).await?;
|
let music_id = app.verify_scoped_token(token).await?;
|
||||||
let mut req = Request::builder()
|
let mut req = Request::new(Body::empty());
|
||||||
.uri(format!(
|
|
||||||
"http://{}:{}/audio/external_id/{}",
|
|
||||||
app.musikcubed_address, app.musikcubed_http_port, music_id
|
|
||||||
))
|
|
||||||
.header(AUTHORIZATION, app.musikcubed_auth_header_value.clone())
|
|
||||||
.body(Body::empty())
|
|
||||||
.expect("cant fail");
|
|
||||||
// proxy any range headers
|
// proxy any range headers
|
||||||
if let Some(range) = request.headers().get(RANGE).cloned() {
|
if let Some(range) = request.headers().get(RANGE).cloned() {
|
||||||
req.headers_mut().insert(RANGE, range);
|
req.headers_mut().insert(RANGE, range);
|
||||||
}
|
}
|
||||||
let mut resp = app.client.request(req).await?;
|
let mut resp = app
|
||||||
|
.make_musikcubed_request(format!("audio/external_id/{music_id}"), req)
|
||||||
|
.await?;
|
||||||
if resp.status().is_success() {
|
if resp.status().is_success() {
|
||||||
// add cache header
|
// add cache header
|
||||||
resp.headers_mut()
|
resp.headers_mut()
|
||||||
@ -236,8 +226,16 @@ async fn get_music(
|
|||||||
async fn http(
|
async fn http(
|
||||||
State(app): State<AppState>,
|
State(app): State<AppState>,
|
||||||
Query(auth): Query<Auth>,
|
Query(auth): Query<Auth>,
|
||||||
mut req: Request<Body>,
|
req: Request<Body>,
|
||||||
) -> Result<Response<Body>, AppError> {
|
) -> Result<Response<Body>, AppError> {
|
||||||
|
let maybe_token = auth.token.or_else(|| {
|
||||||
|
req.headers()
|
||||||
|
.get(AUTHORIZATION)
|
||||||
|
.and_then(|h| h.to_str().ok())
|
||||||
|
.and_then(|auth| extract_password_from_basic_auth(auth).ok())
|
||||||
|
});
|
||||||
|
app.verify_token(maybe_token).await?;
|
||||||
|
|
||||||
// remove token from query
|
// remove token from query
|
||||||
let path = req.uri().path();
|
let path = req.uri().path();
|
||||||
let query_map = remove_token_from_query(req.uri().query());
|
let query_map = remove_token_from_query(req.uri().query());
|
||||||
@ -247,47 +245,20 @@ async fn http(
|
|||||||
.unwrap_or_else(String::new);
|
.unwrap_or_else(String::new);
|
||||||
let query_prefix = has_query.then_some("?").unwrap_or("");
|
let query_prefix = has_query.then_some("?").unwrap_or("");
|
||||||
|
|
||||||
// craft new url
|
let mut request = Request::new(Body::empty());
|
||||||
*req.uri_mut() = format!(
|
if let Some(range) = req.headers().get(RANGE).cloned() {
|
||||||
"http://{}:{}{path}{query_prefix}{query}",
|
request.headers_mut().insert(RANGE, range);
|
||||||
app.musikcubed_address, app.musikcubed_http_port
|
}
|
||||||
)
|
|
||||||
.parse()?;
|
|
||||||
|
|
||||||
let maybe_token = auth.token.or_else(|| {
|
|
||||||
req.headers()
|
|
||||||
.get(AUTHORIZATION)
|
|
||||||
.and_then(|h| h.to_str().ok())
|
|
||||||
.and_then(|auth| extract_password_from_basic_auth(auth).ok())
|
|
||||||
});
|
|
||||||
|
|
||||||
app.verify_token(maybe_token).await?;
|
|
||||||
|
|
||||||
// proxy only the headers we need
|
|
||||||
let headers = {
|
|
||||||
let mut headers = HeaderMap::with_capacity(2);
|
|
||||||
let mut proxy_header = |header_name: HeaderName| {
|
|
||||||
if let Some(value) = req.headers().get(&header_name).cloned() {
|
|
||||||
headers.insert(header_name, value);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
// proxy range header
|
|
||||||
proxy_header(RANGE);
|
|
||||||
// add auth
|
|
||||||
headers.insert(AUTHORIZATION, app.musikcubed_auth_header_value.clone());
|
|
||||||
headers
|
|
||||||
};
|
|
||||||
|
|
||||||
*req.headers_mut() = headers;
|
|
||||||
|
|
||||||
let scheme = req.uri().scheme_str().unwrap();
|
|
||||||
let authority = req.uri().authority().unwrap().as_str();
|
|
||||||
tracing::debug!(
|
tracing::debug!(
|
||||||
"proxying request to {scheme}://{authority} with headers {:?}",
|
"proxying request to {}:{} with headers {:?}",
|
||||||
|
app.musikcubed_address,
|
||||||
|
app.musikcubed_http_port,
|
||||||
req.headers()
|
req.headers()
|
||||||
);
|
);
|
||||||
|
|
||||||
Ok(app.client.request(req).await?)
|
app.make_musikcubed_request(format!("{path}{query_prefix}{query}"), request)
|
||||||
|
.await
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn metadata_ws(
|
async fn metadata_ws(
|
||||||
|
20
src/main.rs
20
src/main.rs
@ -186,6 +186,26 @@ impl AppStateInternal {
|
|||||||
Err(AppError::from("Invalid token or token not present")
|
Err(AppError::from("Invalid token or token not present")
|
||||||
.status(http::StatusCode::UNAUTHORIZED))
|
.status(http::StatusCode::UNAUTHORIZED))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async fn make_musikcubed_request(
|
||||||
|
&self,
|
||||||
|
path: impl AsRef<str>,
|
||||||
|
mut req: http::Request<hyper::Body>,
|
||||||
|
) -> Result<http::Response<hyper::Body>, AppError> {
|
||||||
|
*req.uri_mut() = format!(
|
||||||
|
"http://{}:{}/{}",
|
||||||
|
self.musikcubed_address,
|
||||||
|
self.musikcubed_http_port,
|
||||||
|
path.as_ref()
|
||||||
|
)
|
||||||
|
.parse()?;
|
||||||
|
req.headers_mut().insert(
|
||||||
|
http::header::AUTHORIZATION,
|
||||||
|
self.musikcubed_auth_header_value.clone(),
|
||||||
|
);
|
||||||
|
let resp = self.client.request(req).await?;
|
||||||
|
Ok(resp)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Deserialize, Serialize)]
|
#[derive(Clone, Deserialize, Serialize)]
|
||||||
|
Loading…
Reference in New Issue
Block a user