mirror of
https://gitlab.computer.surgery/matrix/grapevine.git
synced 2025-12-17 15:51:23 +01:00
Log curl command line for all requests at trace
This commit is contained in:
parent
b0f33207fe
commit
5c4062742f
3 changed files with 103 additions and 19 deletions
60
src/utils.rs
60
src/utils.rs
|
|
@ -306,6 +306,66 @@ impl<'a> TryFrom<&'a MxcUri> for MxcData<'a> {
|
|||
}
|
||||
}
|
||||
|
||||
fn curlify_args<T>(req: &http::Request<T>) -> Option<Vec<String>> {
|
||||
let mut args =
|
||||
vec!["curl".to_owned(), "-X".to_owned(), req.method().to_string()];
|
||||
|
||||
for (name, val) in req.headers() {
|
||||
args.extend([
|
||||
"-H".to_owned(),
|
||||
format!("{name}: {}", val.to_str().ok()?),
|
||||
]);
|
||||
}
|
||||
|
||||
let fix_uri = || {
|
||||
if req.uri().scheme().is_some() {
|
||||
return None;
|
||||
}
|
||||
if req.uri().authority().is_some() {
|
||||
return None;
|
||||
}
|
||||
let mut parts = req.uri().clone().into_parts();
|
||||
|
||||
parts.scheme = Some(http::uri::Scheme::HTTPS);
|
||||
|
||||
let host =
|
||||
req.headers().get(http::header::HOST)?.to_str().ok()?.to_owned();
|
||||
parts.authority =
|
||||
Some(http::uri::Authority::from_maybe_shared(host).ok()?);
|
||||
|
||||
http::uri::Uri::from_parts(parts).ok()
|
||||
};
|
||||
|
||||
let uri = if let Some(new_uri) = fix_uri() {
|
||||
Cow::Owned(new_uri)
|
||||
} else {
|
||||
Cow::Borrowed(req.uri())
|
||||
};
|
||||
|
||||
args.push(uri.to_string());
|
||||
|
||||
Some(args)
|
||||
}
|
||||
|
||||
pub(crate) fn curlify<T>(req: &http::Request<T>) -> Option<String> {
|
||||
let args = curlify_args(req)?;
|
||||
|
||||
Some(
|
||||
args.into_iter()
|
||||
.map(|arg| {
|
||||
if arg.chars().all(|c| {
|
||||
c.is_alphanumeric() || ['-', '_', ':', '/'].contains(&c)
|
||||
}) {
|
||||
arg
|
||||
} else {
|
||||
format!("'{}'", arg.replace('\'', "\\'"))
|
||||
}
|
||||
})
|
||||
.collect::<Vec<_>>()
|
||||
.join(" "),
|
||||
)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::utils::dbg_truncate_str;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue