mirror of
https://github.com/fawney19/Aether.git
synced 2026-10-07 18:07:47 +08:00
74 lines
2.2 KiB
Rust
74 lines
2.2 KiB
Rust
use axum::http::Uri;
|
|||
|
|
|
||
|
|
use crate::gateway::headers::header_value_str;
|
||
|
|
use crate::gateway::{AppState, GatewayError};
|
||
|
|
|
||
|
|
use super::{resolve_control_route, GatewayControlDecision};
|
||
|
|
|
||
|
|
#[derive(Debug, Clone)]
|
||
|
|
pub(crate) struct GatewayPublicRequestContext {
|
||
|
|
pub(crate) trace_id: String,
|
||
|
|
pub(crate) request_method: http::Method,
|
||
|
|
pub(crate) request_path: String,
|
||
|
|
pub(crate) request_query_string: Option<String>,
|
||
|
|
pub(crate) request_content_type: Option<String>,
|
||
|
|
pub(crate) host_header: Option<String>,
|
||
|
|
pub(crate) control_decision: Option<GatewayControlDecision>,
|
||
|
|
}
|
||
|
|
|
||
|
|
impl GatewayPublicRequestContext {
|
||
|
|
pub(crate) fn from_request_parts(
|
||
|
|
trace_id: impl Into<String>,
|
||
|
|
method: &http::Method,
|
||
|
|
uri: &Uri,
|
||
|
|
headers: &http::HeaderMap,
|
||
|
|
control_decision: Option<GatewayControlDecision>,
|
||
|
|
) -> Self {
|
||
|
|
let request_path = if uri.path().starts_with('/') {
|
||
|
|
uri.path().to_string()
|
||
|
|
} else {
|
||
|
|
format!("/{}", uri.path())
|
||
|
|
};
|
||
|
|
let request_query_string = uri.query().map(ToOwned::to_owned);
|
||
|
|
|
||
|
|
Self {
|
||
|
|
trace_id: trace_id.into(),
|
||
|
|
request_method: method.clone(),
|
||
|
|
request_path,
|
||
|
|
request_query_string,
|
||
|
|
request_content_type: header_value_str(headers, http::header::CONTENT_TYPE.as_str()),
|
||
|
|
host_header: header_value_str(headers, http::header::HOST.as_str()),
|
||
|
|
control_decision,
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
pub(crate) fn request_path_and_query(&self) -> String {
|
||
|
|
if let Some(query) = self
|
||
|
|
.request_query_string
|
||
|
|
.as_deref()
|
||
|
|
.filter(|value| !value.is_empty())
|
||
|
|
{
|
||
|
|
format!("{}?{query}", self.request_path)
|
||
|
|
} else {
|
||
|
|
self.request_path.clone()
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
pub(crate) async fn resolve_public_request_context(
|
||
|
|
state: &AppState,
|
||
|
|
method: &http::Method,
|
||
|
|
uri: &Uri,
|
||
|
|
headers: &http::HeaderMap,
|
||
|
|
trace_id: &str,
|
||
|
|
) -> Result<GatewayPublicRequestContext, GatewayError> {
|
||
|
|
let control_decision = resolve_control_route(state, method, uri, headers, trace_id).await?;
|
||
|
|
Ok(GatewayPublicRequestContext::from_request_parts(
|
||
|
|
trace_id,
|
||
|
|
method,
|
||
|
|
uri,
|
||
|
|
headers,
|
||
|
|
control_decision,
|
||
|
|
))
|
||
|
|
}
|