use std::collections::BTreeMap;
use std::sync::Arc;
use axum::Router;
use axum::body::{Body, Bytes};
use axum::extract::{Request, State};
use axum::response::Response;
use axum::routing::any;
use http::{StatusCode, Uri};
use crate::api::{TmfRequest, TmfResponse};
use super::handler::TmfHandler;
use super::store::ResourceStore;
pub fn router<S>(handler: TmfHandler<S>) -> Router
where
S: ResourceStore + 'static,
{
Router::new()
.route("/{*path}", any(serve))
.route("/", any(serve))
.with_state(Arc::new(handler))
}
async fn serve<S>(State(handler): State<Arc<TmfHandler<S>>>, request: Request) -> Response
where
S: ResourceStore + 'static,
{
const MAX_BODY: usize = 8 * 1024 * 1024;
let (parts, body) = request.into_parts();
let url = absolute_url(handler.base_url(), &parts.uri);
if let Some(refused) = handler.refusal(&parts.method, &url) {
return into_response(&refused);
}
let Ok(body) = axum::body::to_bytes(body, MAX_BODY).await else {
return into_response(&super::handler::error_response(
StatusCode::PAYLOAD_TOO_LARGE,
"41301",
"Request body is too large",
));
};
let request = TmfRequest {
method: parts.method.clone(),
url,
query: parse_query(parts.uri.query()),
headers: parts.headers,
body: (!body.is_empty()).then_some(body),
};
into_response(&handler.handle(&request).await)
}
fn absolute_url(base_url: &str, uri: &Uri) -> String {
let path = uri.path().trim_start_matches('/');
if path.is_empty() {
base_url.to_owned()
} else {
format!("{base_url}/{path}")
}
}
fn parse_query(query: Option<&str>) -> BTreeMap<String, String> {
let Some(query) = query else {
return BTreeMap::new();
};
let mut params: BTreeMap<String, String> = BTreeMap::new();
for pair in query.split('&').filter(|pair| !pair.is_empty()) {
let (key, value) = match pair.split_once('=') {
Some((key, value)) => (decode(key), decode(value)),
None => (decode(pair), String::new()),
};
match params.entry(key) {
std::collections::btree_map::Entry::Vacant(slot) => {
slot.insert(value);
}
std::collections::btree_map::Entry::Occupied(mut slot) => {
if super::semantics::is_reserved(slot.key()) {
slot.insert(value);
} else {
let widened = format!("{},{value}", slot.get());
slot.insert(widened);
}
}
}
}
params
}
fn decode(raw: &str) -> String {
let bytes = raw.as_bytes();
let mut out = Vec::with_capacity(bytes.len());
let mut index = 0;
while index < bytes.len() {
match bytes[index] {
b'+' => {
out.push(b' ');
index += 1;
}
b'%' if index + 2 < bytes.len() => {
let decoded = std::str::from_utf8(&bytes[index + 1..index + 3])
.ok()
.and_then(|hex| u8::from_str_radix(hex, 16).ok());
if let Some(byte) = decoded {
out.push(byte);
index += 3;
} else {
out.push(bytes[index]);
index += 1;
}
}
byte => {
out.push(byte);
index += 1;
}
}
}
String::from_utf8_lossy(&out).into_owned()
}
fn into_response(response: &TmfResponse) -> Response {
let mut builder = Response::builder().status(response.status);
if let Some(headers) = builder.headers_mut() {
headers.clone_from(&response.headers);
}
builder
.body(Body::from(Bytes::clone(&response.body)))
.unwrap_or_else(|_| {
Response::builder()
.status(StatusCode::INTERNAL_SERVER_ERROR)
.body(Body::empty())
.expect("an empty body is always a valid response")
})
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn a_query_string_is_decoded_including_plus_as_space() {
let parsed = parse_query(Some("name=Basic+Firewall¬e=a%20b&flag"));
assert_eq!(parsed["name"], "Basic Firewall");
assert_eq!(parsed["note"], "a b");
assert_eq!(parsed["flag"], "");
}
#[test]
fn a_malformed_escape_survives_rather_than_failing_the_request() {
assert_eq!(parse_query(Some("a=100%"))["a"], "100%");
assert_eq!(parse_query(Some("a=%zz"))["a"], "%zz");
}
#[test]
fn an_operator_suffix_survives_decoding() {
let parsed = parse_query(Some("orderDate.gte=2026-01-01T00%3A00%3A00Z"));
assert_eq!(parsed["orderDate.gte"], "2026-01-01T00:00:00Z");
}
#[test]
fn the_mount_point_is_put_back_in_front_of_the_path() {
let base = "https://mycsp.com/tmf-api/productCatalogManagement/v5";
assert_eq!(
absolute_url(base, &"/productOffering/7655".parse().unwrap()),
format!("{base}/productOffering/7655")
);
assert_eq!(absolute_url(base, &"/".parse().unwrap()), base);
}
#[test]
fn a_repeated_filter_widens_into_the_comma_list_it_stands_for() {
assert_eq!(
parse_query(Some("state=held&state=pending"))["state"],
"held,pending"
);
assert_eq!(
parse_query(Some("state=held,pending"))["state"],
parse_query(Some("state=held&state=pending"))["state"]
);
}
#[test]
fn a_repeated_reserved_parameter_keeps_the_last() {
assert_eq!(parse_query(Some("limit=20&limit=50"))["limit"], "50");
assert_eq!(parse_query(Some("fields=id&fields=name"))["fields"], "name");
}
}