use reqwest::Url;
use crate::http_source::constants::{DOT_SEGMENTS, NEXT_RELATION, RELATION_PARAMETER};
use serde_json::{Map, Value};
pub(crate) fn request_url(base: &Url, path: &str) -> Result<Url, String> {
let text = match path.starts_with("http://") || path.starts_with("https://") {
true => path.to_owned(),
false => format!(
"{}/{}",
base.as_str().trim_end_matches('/'),
path.trim_start_matches('/')
),
};
Url::parse(&text).map_err(|error| format!("{path} is not a valid URL: {error}"))
}
pub(crate) fn same_origin(base: &Url, url: &Url) -> bool {
base.origin() == url.origin()
}
pub(crate) fn placeholders(path: &str) -> Vec<String> {
path.split('{')
.skip(1)
.filter_map(|part| part.split_once('}'))
.map(|(name, _)| name.to_owned())
.collect()
}
pub(crate) fn filled_path(path: &str, key: &Map<String, Value>) -> Result<String, String> {
placeholders(path)
.iter()
.try_fold(path.to_owned(), |filled, name| {
let value = match key.get(name) {
Some(Value::String(text)) => text.clone(),
Some(Value::Null) | None => {
return Err(format!(
"fan-out path {path} needs {name}, which the key {} lacks",
Value::Object(key.clone())
));
}
Some(other) => other.to_string(),
};
Ok(filled.replace(&format!("{{{name}}}"), &encoded(&value)))
})
}
pub(crate) fn body_placeholders(body: &Value) -> Vec<String> {
let mut names: Vec<String> = Vec::new();
let mut pending: Vec<&Value> = vec![body];
while let Some(value) = pending.pop() {
match value {
Value::String(text) => {
for name in text_placeholders(text) {
if !names.contains(&name) {
names.push(name);
}
}
}
Value::Array(items) => pending.extend(items.iter().rev()),
Value::Object(fields) => pending.extend(fields.values().rev()),
Value::Null | Value::Bool(_) | Value::Number(_) => {}
}
}
names
}
fn text_placeholders(text: &str) -> Vec<String> {
let mut names = placeholders(text);
names.retain(|name| {
name.starts_with(|first: char| first.is_ascii_alphabetic() || first == '_')
&& name.chars().all(|c| c.is_ascii_alphanumeric() || c == '_')
});
names
}
pub(crate) fn filled_body(
body: &Value,
keys: &[&Map<String, Value>],
batched: bool,
) -> Result<Value, String> {
match body {
Value::String(text) => filled_text(text, keys, batched),
Value::Array(items) => {
let mut filled: Vec<Value> = Vec::with_capacity(items.len());
for item in items {
filled.push(filled_body(item, keys, batched)?);
}
Ok(Value::Array(filled))
}
Value::Object(fields) => {
let mut filled = Map::new();
for (name, value) in fields {
filled.insert(name.clone(), filled_body(value, keys, batched)?);
}
Ok(Value::Object(filled))
}
Value::Null | Value::Bool(_) | Value::Number(_) => Ok(body.clone()),
}
}
fn filled_text(text: &str, keys: &[&Map<String, Value>], batched: bool) -> Result<Value, String> {
let names = text_placeholders(text);
let Some(first) = names.first() else {
return Ok(Value::String(text.to_owned()));
};
if names.len() == 1 && text == format!("{{{first}}}") {
let mut values: Vec<Value> = Vec::with_capacity(keys.len());
for key in keys {
values.push(key_value(key, first)?);
}
return Ok(match (batched, values.pop()) {
(false, Some(value)) => value,
(true, Some(last)) => {
values.push(last);
Value::Array(values)
}
(_, None) => Value::Array(Vec::new()),
});
}
if batched {
return Err(format!(
"json_body value {text:?} holds a placeholder within other text; a batched fan-out fills only a value that is just the placeholder, such as \"{{id}}\", with the list of its keys"
));
}
let mut filled = text.to_owned();
for name in &names {
let value = match keys.first() {
Some(key) => key_value(key, name)?,
None => Value::Null,
};
let shown = match value {
Value::String(value) => value,
other => other.to_string(),
};
filled = filled.replace(&format!("{{{name}}}"), &shown);
}
Ok(Value::String(filled))
}
fn key_value(key: &Map<String, Value>, name: &str) -> Result<Value, String> {
match key.get(name) {
Some(Value::Null) | None => Err(format!(
"json_body needs {name}, which the key {} lacks",
Value::Object(key.clone())
)),
Some(value) => Ok(value.clone()),
}
}
pub(crate) fn encoded(text: &str) -> String {
if DOT_SEGMENTS.contains(&text) {
return text.replace('.', "%2E");
}
text.bytes()
.map(|byte| match byte {
b'A'..=b'Z' | b'a'..=b'z' | b'0'..=b'9' | b'-' | b'.' | b'_' | b'~' => {
char::from(byte).to_string()
}
_ => format!("%{byte:02X}"),
})
.collect()
}
pub(crate) fn link_header_next(header: &str) -> Option<String> {
header.split('<').skip(1).find_map(next_target)
}
fn next_target(link: &str) -> Option<String> {
let (target, parameters) = link.split_once('>')?;
let next = parameters.split([';', ',']).any(is_next_relation);
next.then(|| target.trim().to_owned())
}
fn is_next_relation(parameter: &str) -> bool {
let relations = parameter
.split_once('=')
.filter(|(key, _)| key.trim().eq_ignore_ascii_case(RELATION_PARAMETER))
.map(|(_, value)| value.trim().trim_matches('"'));
relations.is_some_and(|relations| {
relations
.split_whitespace()
.any(|relation| relation.eq_ignore_ascii_case(NEXT_RELATION))
})
}
pub(crate) fn redacted(text: &str, secrets: &[String]) -> String {
secrets
.iter()
.filter(|secret| !secret.is_empty())
.fold(text.to_owned(), |text, secret| {
text.replace(secret.as_str(), "***")
})
}