wala-rust

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

commit cc25d1cebe19be1082ca37b07cf2bde4d5536e77
parent 68b6d94110ba3c2ee68768608dc751503e017f30
Author: lash <dev@holbrook.no>
Date:   Sun,  3 Jul 2022 07:58:44 +0000

Add content type return

Diffstat:
MCargo.toml | 1+
Msrc/main.rs | 15+++++++++++++++
Msrc/meta.rs | 59+++++++++++++++++++++++++++++++++++++++++++++++++----------
Msrc/record.rs | 6++++++
Msrc/request.rs | 22++++++++++++++++++++--
5 files changed, 91 insertions(+), 12 deletions(-)

diff --git a/Cargo.toml b/Cargo.toml @@ -13,6 +13,7 @@ sha2 = "^0.10" hex = "^0.4" tempfile = "^3.3.0" mime = "^0.3.13" +ascii = "^1.0.0" [dependencies.tree_magic] version = "^0.2.3" diff --git a/src/main.rs b/src/main.rs @@ -5,8 +5,10 @@ use tiny_http::{ Response, StatusCode, Header, + HeaderField, Method, }; +use ascii::AsciiString; use mime::Mime; use std::net::{Ipv4Addr, SocketAddrV4}; use std::str::FromStr; @@ -104,6 +106,16 @@ fn exec_response(req: Request, r: RequestResult) { match r.f { Some(v) => { let mut res = Response::from_file(v); + match r.m { + Some(v) => { + let h = Header{ + field: HeaderField::from_str("Content-Type").unwrap(), + value: AsciiString::from_ascii(v.as_ref()).unwrap(), + }; + res.add_header(h); + }, + _ => {}, + } res = res.with_status_code(res_status); req.respond(res); return; @@ -317,6 +329,9 @@ fn main() { let digest = hex::decode(&digest_hex).unwrap(); process_meta(&req, &path, digest); }, + RequestResultType::Found => { + + }, _ => {}, } diff --git a/src/meta.rs b/src/meta.rs @@ -1,10 +1,19 @@ -use std::fs::File; -use std::path::Path; +use std::fs::{ + File, + read, +}; +use std::path::{ + Path, + PathBuf, +}; use std::io::Write; +use std::str::FromStr; use mime::Mime; +use log::{debug, error}; -pub fn register_type(path: &Path, digest: Vec<u8>, typ: Mime) -> Result<(), std::io::Error> { + +fn meta_path(path: &Path, digest: Vec<u8>) -> Result<PathBuf, std::io::Error> { let digest_hex = hex::encode(digest); let fp = path.join(digest_hex); @@ -18,13 +27,43 @@ pub fn register_type(path: &Path, digest: Vec<u8>, typ: Mime) -> Result<(), std: }; path_canon.set_extension("meta"); - match File::create(path_canon) { - Ok(mut f) => { - f.write(typ.as_ref().as_bytes()); - } - Err(e) => { - return Err(e); - } + Ok(path_canon) +} + +pub fn register_type(path: &Path, digest: Vec<u8>, typ: Mime) -> Result<(), std::io::Error> { + match meta_path(path, digest) { + Ok(v) => { + match File::create(v) { + Ok(mut f) => { + f.write(typ.as_ref().as_bytes()); + } + Err(e) => { + return Err(e); + } + }; + }, + _ => {}, }; Ok(()) } + +pub fn get_type(path: &Path, digest: Vec<u8>) -> Option<Mime> { + let digest_hex = hex::encode(&digest); + match meta_path(path, digest) { + Ok(v) => { + match read(v) { + Ok(r) => { + let mime_str = String::from_utf8(r).unwrap(); + debug!("content type {} retrieved for {}", &mime_str, &digest_hex); + let mime = Mime::from_str(mime_str.as_str()).unwrap(); + return Some(mime); + }, + Err(e) => { + debug!("meta type file not found for {}: {}", &digest_hex, e); + }, + }; + }, + _ => {}, + }; + None +} diff --git a/src/record.rs b/src/record.rs @@ -20,6 +20,8 @@ use crate::auth::AuthResult; use tiny_http::Request; use tempfile::NamedTempFile; +use mime::Mime; + use log::{debug, info, error}; #[derive(Debug, PartialEq)] @@ -37,6 +39,7 @@ pub struct RequestResult { pub typ: RequestResultType, pub v: Option<String>, pub f: Option<File>, + pub m: Option<Mime>, } impl fmt::Display for RequestResult { @@ -130,6 +133,7 @@ pub fn put_immutable(path: &Path, mut f: impl Read, expected_size: usize) -> Res typ: RequestResultType::ReadError, v: None, f: None, + m: None, }; return Err(err); }, @@ -142,6 +146,7 @@ pub fn put_immutable(path: &Path, mut f: impl Read, expected_size: usize) -> Res typ: RequestResultType::ReadError, v: None, f: None, + m: None, }; return Err(err); } @@ -157,6 +162,7 @@ pub fn put_immutable(path: &Path, mut f: impl Read, expected_size: usize) -> Res typ: RequestResultType::WriteError, v: None, f: None, + m: None, }; return Err(err); } diff --git a/src/request.rs b/src/request.rs @@ -19,6 +19,8 @@ use crate::auth::{ }; use std::io::Read; +use crate::meta::get_type as get_meta_type; + use log::{ debug, error, @@ -32,6 +34,7 @@ pub fn process_method(method: &Method, url: String, mut f: impl Read, expected_s typ: RequestResultType::AuthError, v: None, f: None, + m: None, }; } if auth_result.active() { @@ -46,6 +49,7 @@ pub fn process_method(method: &Method, url: String, mut f: impl Read, expected_s typ: RequestResultType::Changed, v: Some(digest_hex), f: None, + m: None, }; }, Err(e) => { @@ -55,6 +59,7 @@ pub fn process_method(method: &Method, url: String, mut f: impl Read, expected_s typ: RequestResultType::RecordError, v: Some(String::from(err_str)), f: None, + m: None, }; }, }; @@ -69,6 +74,7 @@ pub fn process_method(method: &Method, url: String, mut f: impl Read, expected_s typ: RequestResultType::Changed, v: Some(digest_hex), f: None, + m: None, }; }, Err(e) => { @@ -77,6 +83,7 @@ pub fn process_method(method: &Method, url: String, mut f: impl Read, expected_s typ: RequestResultType::RecordError, v: Some(String::from(err_str)), f: None, + m: None, }; }, }; @@ -91,6 +98,7 @@ pub fn process_method(method: &Method, url: String, mut f: impl Read, expected_s typ: RequestResultType::InputError, v: Some(String::from(err_str)), f: None, + m: None, }; }, Ok(v) => { @@ -101,13 +109,21 @@ pub fn process_method(method: &Method, url: String, mut f: impl Read, expected_s let full_path_buf = path.join(&url); debug!("url {} resolved to {:?}", &url, &full_path_buf); - match get_record(digest, full_path_buf.as_path()) { + match get_record(digest.clone(), full_path_buf.as_path()) { Some(v) => { - return RequestResult { + let mut res = RequestResult { typ: RequestResultType::Found, v: None, //Some(String::new()), f: Some(v), + m: None, + }; + match get_meta_type(path, digest) { + Some(v) => { + res.m = Some(v); + }, + _ => {}, }; + return res; }, None => { debug!("nooonn"); @@ -115,6 +131,7 @@ pub fn process_method(method: &Method, url: String, mut f: impl Read, expected_s typ: RequestResultType::RecordError, v: Some(String::new()), f: None, + m: None, }; }, }; @@ -125,6 +142,7 @@ pub fn process_method(method: &Method, url: String, mut f: impl Read, expected_s typ: RequestResultType::InputError, v: Some(String::new()), f: None, + m: None, } }