2024-06-06 18:04:29 +00:00
|
|
|
use std::str;
|
2024-05-23 18:44:19 +00:00
|
|
|
|
2024-06-06 18:04:29 +00:00
|
|
|
use axum::{extract::Path, RequestExt, RequestPartsExt};
|
|
|
|
|
use bytes::Bytes;
|
2024-07-16 08:05:25 +00:00
|
|
|
use conduit::{err, Result};
|
2024-05-23 18:44:19 +00:00
|
|
|
use http::request::Parts;
|
|
|
|
|
use serde::Deserialize;
|
2024-07-16 08:05:25 +00:00
|
|
|
use service::Services;
|
2024-05-23 18:44:19 +00:00
|
|
|
|
|
|
|
|
#[derive(Deserialize)]
|
|
|
|
|
pub(super) struct QueryParams {
|
|
|
|
|
pub(super) access_token: Option<String>,
|
|
|
|
|
pub(super) user_id: Option<String>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub(super) struct Request {
|
|
|
|
|
pub(super) path: Path<Vec<String>>,
|
|
|
|
|
pub(super) query: QueryParams,
|
|
|
|
|
pub(super) body: Bytes,
|
|
|
|
|
pub(super) parts: Parts,
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-16 08:05:25 +00:00
|
|
|
pub(super) async fn from(services: &Services, request: hyper::Request<axum::body::Body>) -> Result<Request> {
|
2024-05-23 18:44:19 +00:00
|
|
|
let limited = request.with_limited_body();
|
|
|
|
|
let (mut parts, body) = limited.into_parts();
|
|
|
|
|
|
2024-06-06 18:04:29 +00:00
|
|
|
let path: Path<Vec<String>> = parts.extract().await?;
|
2024-07-15 04:19:43 +00:00
|
|
|
let query = parts.uri.query().unwrap_or_default();
|
|
|
|
|
let query =
|
|
|
|
|
serde_html_form::from_str(query).map_err(|e| err!(Request(Unknown("Failed to read query parameters: {e}"))))?;
|
2024-05-23 18:44:19 +00:00
|
|
|
|
2024-07-16 08:05:25 +00:00
|
|
|
let max_body_size = services.globals.config.max_request_size;
|
2024-05-23 18:44:19 +00:00
|
|
|
|
|
|
|
|
let body = axum::body::to_bytes(body, max_body_size)
|
|
|
|
|
.await
|
2024-07-15 04:19:43 +00:00
|
|
|
.map_err(|e| err!(Request(TooLarge("Request body too large: {e}"))))?;
|
2024-05-23 18:44:19 +00:00
|
|
|
|
|
|
|
|
Ok(Request {
|
|
|
|
|
path,
|
|
|
|
|
query,
|
|
|
|
|
body,
|
|
|
|
|
parts,
|
|
|
|
|
})
|
|
|
|
|
}
|