wala-rust

Unnamed repository; edit this file 'description' to name the repository.
Info | Log | Files | Refs | README | LICENSE

commit b26b80c16ae2fa1317f41f2c30da47c695fcc5c9
parent f3355d9f9ce8b3282384650e2c0845de2e89ada0
Author: lash <dev@holbrook.no>
Date:   Thu, 23 Jun 2022 19:09:26 +0000

Add reader return for get method processing

Diffstat:
Msrc/auth/mod.rs | 6++++++
Msrc/main.rs | 31+++++++++++++++++++++++++++----
Msrc/record.rs | 22++++++++++++++++++----
Msrc/request.rs | 22+++++++++++++++++++---
4 files changed, 70 insertions(+), 11 deletions(-)

diff --git a/src/auth/mod.rs b/src/auth/mod.rs @@ -29,6 +29,12 @@ impl AuthResult { } } +impl fmt::Debug for AuthResult { + fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { + fmt.write_str(&hex::encode(&self.identity)) + } +} + #[derive(Debug)] pub struct AuthSpecError; diff --git a/src/main.rs b/src/main.rs @@ -60,6 +60,12 @@ impl fmt::Display for NoAuthError { fn exec_response(mut req: Request, r: RequestResult) { let res_status: StatusCode; match r.typ { + RequestResultType::Found => { + res_status = StatusCode(200); + }, + RequestResultType::Changed => { + res_status = StatusCode(200); + }, RequestResultType::WriteError => { res_status = StatusCode(500); }, @@ -69,16 +75,34 @@ fn exec_response(mut req: Request, r: RequestResult) { RequestResultType::InputError => { res_status = StatusCode(400); }, + RequestResultType::RecordError => { + res_status = StatusCode(404); + }, _ => { res_status = StatusCode(500); }, } match r.v { Some(v) => { - req.respond(Response::from_string(v)); + let mut res = Response::from_string(v); + res = res.with_status_code(res_status); + req.respond(res); + return; }, None => { - req.respond(Response::empty(res_status)); + match r.f { + Some(v) => { + let mut res = Response::from_file(v); + res = res.with_status_code(res_status); + req.respond(res); + return; + }, + None => { + let res = Response::empty(res_status); + req.respond(res); + return; + }, + } } } } @@ -123,7 +147,6 @@ fn auth_from_headers(req: &Request) -> Option<AuthSpec> { for h in req.headers() { let k = &h.field; if k.equiv("Authorization") { - //is_auth = true; let v = &h.value; let r = AuthSpec::from_str(v.as_str()); match r { @@ -202,7 +225,7 @@ fn main() { let mut path = base_path.clone(); - let url = String::from(req.url()); + let url = String::from(&req.url()[1..]); let method = req.method().clone(); let expected_size = match req.body_length() { Some(v) => { diff --git a/src/record.rs b/src/record.rs @@ -33,10 +33,10 @@ pub enum RequestResultType { RecordError, } -#[derive(Debug)] pub struct RequestResult { pub typ: RequestResultType, pub v: Option<String>, + pub f: Option<File>, } impl fmt::Display for RequestResult { @@ -45,6 +45,13 @@ impl fmt::Display for RequestResult { } } +impl fmt::Debug for RequestResult { + fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { + //fmt.write_str(format_args!("{:?}", RequestResultType)); + write!(fmt, "{:?}", self.typ) + } +} + impl Error for RequestResult { fn description(&self) -> &str { match &self.v { @@ -80,6 +87,12 @@ impl FromStr for ResourceKey { } } +impl fmt::Display for ResourceKey { + fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { + fmt.write_str(&hex::encode(&self.v)) + } +} + impl ResourceKey { pub fn pointer_for(&self, subject: &AuthResult) -> Vec<u8> { let mut h = Sha256::new(); @@ -116,6 +129,7 @@ pub fn put_immutable(path: &Path, mut f: impl Read, expected_size: usize) -> Res let err = RequestResult{ typ: RequestResultType::ReadError, v: None, + f: None, }; return Err(err); }, @@ -127,6 +141,7 @@ pub fn put_immutable(path: &Path, mut f: impl Read, expected_size: usize) -> Res let err = RequestResult{ typ: RequestResultType::ReadError, v: None, + f: None, }; return Err(err); } @@ -141,6 +156,7 @@ pub fn put_immutable(path: &Path, mut f: impl Read, expected_size: usize) -> Res let err = RequestResult{ typ: RequestResultType::WriteError, v: None, + f: None, }; return Err(err); } @@ -177,7 +193,7 @@ pub fn put_mutable(pointer: Vec<u8>, path: &Path, mut f: impl Read, expected_siz } } -pub fn get(pointer: Vec<u8>, path: &Path) -> Option<impl Read> { +pub fn get(pointer: Vec<u8>, path: &Path) -> Option<File> { //{ impl Read> { let path_canon = match path.canonicalize() { Ok(v) => { v @@ -193,8 +209,6 @@ pub fn get(pointer: Vec<u8>, path: &Path) -> Option<impl Read> { _ => {}, } None - - } #[cfg(test)] diff --git a/src/request.rs b/src/request.rs @@ -19,6 +19,9 @@ use crate::auth::{ }; use std::io::Read; +use log::{ + debug, +}; pub fn process_method(method: &Method, url: String, mut f: impl Read, expected_size: usize, path: &Path, auth_result: AuthResult) -> RequestResult { match method { @@ -27,11 +30,13 @@ pub fn process_method(method: &Method, url: String, mut f: impl Read, expected_s return RequestResult{ typ: RequestResultType::AuthError, v: None, + f: None, }; } if auth_result.active() { let res: RequestResult; let rk = ResourceKey::from_str(url.as_str()).unwrap(); + debug!("authenticated as {:?} using mutable key {} -> {}", auth_result, &url, &rk); let ptr = rk.pointer_for(&auth_result); match put_mutable(ptr, path, f, expected_size) { Ok(v) => { @@ -39,6 +44,7 @@ pub fn process_method(method: &Method, url: String, mut f: impl Read, expected_s res = RequestResult{ typ: RequestResultType::Changed, v: Some(digest_hex), + f: None, }; }, Err(e) => { @@ -46,11 +52,13 @@ pub fn process_method(method: &Method, url: String, mut f: impl Read, expected_s res = RequestResult { typ: RequestResultType::RecordError, v: Some(String::from(err_str)), + f: None, }; }, }; return res; } else { + debug!("immutable put"); let res: RequestResult; match put_immutable(path, f, expected_size) { Ok(v) => { @@ -58,6 +66,7 @@ pub fn process_method(method: &Method, url: String, mut f: impl Read, expected_s res = RequestResult{ typ: RequestResultType::Changed, v: Some(digest_hex), + f: None, }; }, Err(e) => { @@ -65,6 +74,7 @@ pub fn process_method(method: &Method, url: String, mut f: impl Read, expected_s res = RequestResult { typ: RequestResultType::RecordError, v: Some(String::from(err_str)), + f: None, }; }, }; @@ -78,26 +88,31 @@ pub fn process_method(method: &Method, url: String, mut f: impl Read, expected_s return RequestResult { typ: RequestResultType::InputError, v: Some(String::from(err_str)), + f: None, }; }, Ok(v) => { v }, }; + let full_path_buf = path.join(&url); + debug!("url {} resolved to {:?}", &url, &full_path_buf); match get_record(digest, full_path_buf.as_path()) { Some(v) => { - // CONTINUE FROM HERE, v IS Read return RequestResult { typ: RequestResultType::Found, - v: Some(String::new()), + v: None, //Some(String::new()), + f: Some(v), }; }, None => { + debug!("nooonn"); return RequestResult { typ: RequestResultType::RecordError, - v: Some(String::from(&url)), + v: Some(String::new()), + f: None, }; }, }; @@ -107,6 +122,7 @@ pub fn process_method(method: &Method, url: String, mut f: impl Read, expected_s RequestResult { typ: RequestResultType::InputError, v: Some(String::new()), + f: None, } }