commit 43591de8d23a1c5e8c26acf7ae86fd5a39a1285e
parent e93b7a823cda4f02bcb2b14fdd88ce251852a957
Author: lash <dev@holbrook.no>
Date: Sun, 18 Sep 2022 13:18:50 +0000
Add trace puts to spool dir
Diffstat:
6 files changed, 89 insertions(+), 2 deletions(-)
diff --git a/Cargo.toml b/Cargo.toml
@@ -42,3 +42,4 @@ pgpauth = ["pgp", "base64"]
dev = []
magic = ["tree_magic"]
meta = []
+trace = []
diff --git a/src/lib.rs b/src/lib.rs
@@ -47,3 +47,7 @@ pub mod record;
#[cfg(feature = "meta")]
/// Store and serve MIME metadata for content.
pub mod meta;
+
+#[cfg(feature = "trace")]
+/// Log all successful requests to spool directory
+pub mod trace;
diff --git a/src/main.rs b/src/main.rs
@@ -11,7 +11,10 @@ use mime::Mime;
use std::net::{Ipv4Addr, SocketAddrV4};
use std::str::FromStr;
use std::path::{PathBuf, Path};
-use std::fs::File;
+use std::fs::{
+ File,
+ create_dir_all,
+};
use std::error::Error;
use std::fmt;
use std::io::{
@@ -39,10 +42,13 @@ use wala::response::{
preflight_response,
};
+#[cfg(feature = "trace")]
+use wala::trace::trace_request;
+
mod arg;
use arg::Settings;
-use log::{info, error};
+use log::{info, error, warn};
use tempfile::tempfile;
@@ -219,6 +225,19 @@ fn main() {
let settings = Settings::from_args();
let base_path = settings.dir.as_path();
+
+ #[cfg(feature = "trace")]
+ let spool_path = base_path.join("spool");
+ let mut spool_ok = false;
+ match create_dir_all(&spool_path) {
+ Ok(v) => {
+ spool_ok = true;
+ },
+ Err(e) => {
+ warn!("spool directory could not be created: {:?}", e);
+ },
+ };
+
info!("Using data dir: {:?}", &base_path);
let ip_addr = Ipv4Addr::from_str(&settings.host).unwrap();
@@ -301,7 +320,11 @@ fn main() {
_ => {},
}
+ #[cfg(feature="trace")]
+ trace_request(&spool_path, &result);
+
exec_response(req, result);
+
}
}
diff --git a/src/record.rs b/src/record.rs
@@ -55,6 +55,8 @@ pub struct RequestResult {
pub m: Option<Mime>,
/// Contains the file name to use for download request requesting a filename.
pub n: Option<String>,
+ /// Contains the authentication result
+ pub a: Option<AuthResult>,
}
impl fmt::Display for RequestResult {
@@ -163,6 +165,7 @@ pub fn put_immutable(path: &Path, mut f: impl Read, expected_size: usize) -> Res
f: None,
m: None,
n: None,
+ a: None,
};
return Err(err);
},
@@ -177,6 +180,7 @@ pub fn put_immutable(path: &Path, mut f: impl Read, expected_size: usize) -> Res
f: None,
m: None,
n: None,
+ a: None,
};
return Err(err);
}
@@ -194,6 +198,7 @@ pub fn put_immutable(path: &Path, mut f: impl Read, expected_size: usize) -> Res
f: None,
m: None,
n: None,
+ a: None,
};
return Err(err);
}
diff --git a/src/request.rs b/src/request.rs
@@ -50,6 +50,7 @@ pub fn process_method(method: &Method, url: String, mut f: impl Read, expected_s
f: None,
m: None,
n: None,
+ a: None,
};
}
if auth_result.active() {
@@ -66,6 +67,7 @@ pub fn process_method(method: &Method, url: String, mut f: impl Read, expected_s
f: None,
m: None,
n: None,
+ a: Some(auth_result),
};
},
Err(e) => {
@@ -77,6 +79,7 @@ pub fn process_method(method: &Method, url: String, mut f: impl Read, expected_s
f: None,
m: None,
n: None,
+ a: None,
};
},
};
@@ -93,6 +96,7 @@ pub fn process_method(method: &Method, url: String, mut f: impl Read, expected_s
f: None,
m: None,
n: None,
+ a: None,
};
},
Err(e) => {
@@ -103,6 +107,7 @@ pub fn process_method(method: &Method, url: String, mut f: impl Read, expected_s
f: None,
m: None,
n: None,
+ a: None,
};
},
};
@@ -119,6 +124,7 @@ pub fn process_method(method: &Method, url: String, mut f: impl Read, expected_s
f: None,
m: None,
n: None,
+ a: None,
};
},
Ok(v) => {
@@ -137,6 +143,7 @@ pub fn process_method(method: &Method, url: String, mut f: impl Read, expected_s
f: Some(v),
m: None,
n: None,
+ a: None,
};
// match get_meta_type(path, &digest) {
// Some(v) => {
@@ -164,6 +171,7 @@ pub fn process_method(method: &Method, url: String, mut f: impl Read, expected_s
f: None,
m: None,
n: None,
+ a: None,
};
},
};
@@ -176,6 +184,7 @@ pub fn process_method(method: &Method, url: String, mut f: impl Read, expected_s
f: None,
m: None,
n: None,
+ a: None,
}
}
diff --git a/src/trace.rs b/src/trace.rs
@@ -0,0 +1,45 @@
+use std::path::Path;
+use std::fs::File;
+use std::io::Write;
+
+use hex;
+
+use log::{debug};
+
+use crate::record::{
+ RequestResult,
+ RequestResultType,
+};
+
+
+pub fn trace_request(p: &Path, res: &RequestResult) {
+ if (res.typ != RequestResultType::Changed) {
+ return;
+ }
+ let mut rf = String::new();
+ match &res.v {
+ Some(v) => {
+ rf.push_str(v);
+ },
+ None => {
+ },
+ };
+ if rf.len() == 0 {
+ return;
+ }
+ let mut content = String::new();
+ match &res.a {
+ Some(auth) => {
+ if auth.active() {
+ let identity = hex::encode(&auth.identity);
+ content.push_str(&identity);
+ //content.push('\t');
+ //content.push_str("foo");
+ }
+ },
+ None => {},
+ }
+ let fp = p.join(rf);
+ let mut f = File::create(fp).unwrap();
+ f.write(content.as_ref());
+}