Skip to content
This repository was archived by the owner on Feb 20, 2025. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
463 changes: 451 additions & 12 deletions Cargo.lock

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,7 @@ edition = "2021"
chrono = "0.4.31"
case_insensitive_hashmap = "0.9.0"
percent-encoding = "2.3.1"
# may_minihttp = "0.1.9"
may_minihttp = { git = "https://github.com/Xudong-Huang/may_minihttp", rev = "e35c93cce95fbbb143c5ec3cb26ca1e0cc9308a7"}
bytes = "1.7.1"
may = "0.3.46"
1 change: 1 addition & 0 deletions src/handlers/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ pub const IS_DIR: &str = "Is a dir";
pub const NO_INPUT: &str = "No input";
pub const OUT_OF_FS: &str = "Out of scope";
pub const WRITE_ERROR: &str = "Write error";
pub const TRUNCATE_ERROR: &str = "File truncate error";
pub const OPEN_ERROR: &str = "Open error";
pub const CREATION_ERROR: &str = "File didn't exist, creation caused errors";

Expand Down
67 changes: 34 additions & 33 deletions src/handlers/list.rs
Original file line number Diff line number Diff line change
@@ -1,31 +1,27 @@
use std::io::Write;

use bytes::BufMut;
use case_insensitive_hashmap::CaseInsensitiveHashMap;
use may_minihttp::{Request, Response};
use percent_encoding::percent_decode_str;

use crate::utils_lib::index_of;

pub static BASE: &str = "/api/directory";
pub static METHOD: &str = "GET";
pub fn handle_list(
incoming_req: crate::http_lib::RequestDataToSet,
incoming_req: Request,
mount_point: &str,
) -> crate::http_lib::ResponseDataToSet {
let mut method = "GET".to_string();
let mut path = "/".to_string();
crate::http_lib::extract_path_and_method(
incoming_req.method_and_path.as_str(),
&mut path,
&mut method,
);
response: &mut Response,
) -> std::io::Result<()> {
let mut method = incoming_req.method();
let mut path = incoming_req.path();
response.header("Access-Control-Allow-Origin: *");

let search_pos_opt = index_of(&path, "?");
if search_pos_opt.is_none() {
return crate::http_lib::ResponseDataToSet {
base: crate::http_lib::BasicHTTPDataToSet {
headers: CaseInsensitiveHashMap::new(),
data: "wrong".as_bytes().to_vec(),
},
code: 500,
msg: "Server error".to_string(),
};
response.status_code(500, "Server error");
return Ok(());
}
let search_pos = search_pos_opt.unwrap();
let search = &path[search_pos..];
Expand All @@ -43,7 +39,7 @@ pub fn handle_list(
literal_path = percent_decode_str(value).decode_utf8().unwrap().to_string();
}
let mut error_reason = crate::handlers::errors::OK;
let mut final_data = [].to_vec();
// let mut final_data = [].to_vec();
if !literal_path.is_empty() {
if literal_path.starts_with('.') {
// idk if there is any side effect of this...
Expand All @@ -58,7 +54,22 @@ pub fn handle_list(
} else if !super::is_path_within_mount_point(joined_path_buf, mount_point) {
error_reason = crate::handlers::errors::OUT_OF_FS;
} else {
final_data.extend(
// final_data.extend(
// std::fs::read_dir(joined_path_buf)
// .unwrap()
// .filter_map(|entry| {
// entry.ok().and_then(|e| {
// let path = e.path();
// path.file_name().and_then(|n| n.to_str().map(String::from))
// })
// })
// .collect::<Vec<String>>()
// .join("\n")
// .as_bytes()
// .to_vec(),
// );
let mut w = response.body_mut().writer();
w.write_all(
std::fs::read_dir(joined_path_buf)
.unwrap()
.filter_map(|entry| {
Expand All @@ -69,26 +80,16 @@ pub fn handle_list(
})
.collect::<Vec<String>>()
.join("\n")
.as_bytes()
.to_vec(),
.as_bytes(),
);
return Ok(());
}
} else {
error_reason = crate::handlers::errors::GENERAL_FAILURE;
}
} else {
error_reason = crate::handlers::errors::NO_INPUT;
}
crate::http_lib::ResponseDataToSet {
base: crate::http_lib::BasicHTTPDataToSet {
headers: super::all_origins(), // very unsafe, TODO
data: final_data,
},
code: if error_reason == crate::handlers::errors::OK {
200
} else {
400
},
msg: error_reason.to_string(),
}
response.status_code(400, error_reason);
Ok(())
}
81 changes: 50 additions & 31 deletions src/handlers/read.rs
Original file line number Diff line number Diff line change
@@ -1,31 +1,34 @@
use bytes::BufMut;
use case_insensitive_hashmap::CaseInsensitiveHashMap;
use may_minihttp::{Request, Response};
use percent_encoding::percent_decode_str;
use std::io::{Read, Write};

use crate::utils_lib::index_of;

pub static BASE: &str = "/api/file";
pub static METHOD: &str = "GET";
pub fn handle_read(
incoming_req: crate::http_lib::RequestDataToSet,
incoming_req: Request,
mount_point: &str,
) -> crate::http_lib::ResponseDataToSet {
let mut method = "GET".to_string();
let mut path = "/".to_string();
crate::http_lib::extract_path_and_method(
incoming_req.method_and_path.as_str(),
&mut path,
&mut method,
);
response: &mut Response,
) -> std::io::Result<()> {
let mut method = incoming_req.method();
let mut path = incoming_req.path();
response.header("Access-Control-Allow-Origin: *");

let search_pos_opt = index_of(&path, "?");
if search_pos_opt.is_none() {
return crate::http_lib::ResponseDataToSet {
base: crate::http_lib::BasicHTTPDataToSet {
headers: CaseInsensitiveHashMap::new(),
data: "wrong".as_bytes().to_vec(),
},
code: 500,
msg: "Server error".to_string(),
};
response.status_code(500, "Server error");
return Ok(());
// return crate::http_lib::ResponseDataToSet {
// base: crate::http_lib::BasicHTTPDataToSet {
// headers: CaseInsensitiveHashMap::new(),
// data: "wrong".as_bytes().to_vec(),
// },
// code: 500,
// msg: "Server error".to_string(),
// };
}
let search_pos = search_pos_opt.unwrap();
let search = &path[search_pos..];
Expand All @@ -43,7 +46,7 @@ pub fn handle_read(
literal_path = percent_decode_str(value).decode_utf8().unwrap().to_string();
}
let mut error_reason = crate::handlers::errors::OK;
let mut final_data = [].to_vec();
// let mut final_data = [].to_vec();
if !literal_path.is_empty() {
if literal_path.starts_with('.') {
// idk if there is any side effect of this...
Expand All @@ -58,24 +61,40 @@ pub fn handle_read(
} else if !super::is_path_within_mount_point(joined_path_buf, mount_point) {
error_reason = crate::handlers::errors::OUT_OF_FS;
} else {
final_data.extend(std::fs::read(joined_path_buf).unwrap().to_vec());
// final_data.extend(std::fs::read(joined_path_buf).unwrap().to_vec());
// let w = response.body_mut();
let mut file = std::fs::File::open(joined_path_buf)?;
// let _n = file.read_to_end(w.to_vec().as_mut())?;
let mut w = response.body_mut().writer();
let mut chunk = [0; 1024];
loop {
match file.read(&mut chunk)? {
0 => break, // End of file
n => {
w.write_all(&chunk[..n])?;
}
}
}
return Ok(());
}
} else {
error_reason = crate::handlers::errors::GENERAL_FAILURE;
}
} else {
error_reason = crate::handlers::errors::NO_INPUT;
}
crate::http_lib::ResponseDataToSet {
base: crate::http_lib::BasicHTTPDataToSet {
headers: super::all_origins(), // very unsafe, TODO
data: final_data,
},
code: if error_reason == crate::handlers::errors::OK {
200
} else {
400
},
msg: error_reason.to_string(),
}
response.status_code(400, error_reason);
Ok(())
// crate::http_lib::ResponseDataToSet {
// base: crate::http_lib::BasicHTTPDataToSet {
// headers: super::all_origins(), // very unsafe, TODO
// data: final_data,
// },
// code: if error_reason == crate::handlers::errors::OK {
// 200
// } else {
// 400
// },
// msg: error_reason.to_string(),
// }
}
54 changes: 22 additions & 32 deletions src/handlers/stat.rs
Original file line number Diff line number Diff line change
@@ -1,30 +1,27 @@
use crate::utils_lib::{self, index_of};
use std::io::Write;

use bytes::BufMut;
use case_insensitive_hashmap::CaseInsensitiveHashMap;
use may_minihttp::{Request, Response};
use percent_encoding::percent_decode_str;

use crate::utils_lib::{self, index_of};

pub static BASE: &str = "/api/stat";
pub static METHOD: &str = "GET";
pub fn handle_stat(
incoming_req: crate::http_lib::RequestDataToSet,
incoming_req: Request,
mount_point: &str,
) -> crate::http_lib::ResponseDataToSet {
let mut method = "GET".to_string();
let mut path = "/".to_string();
crate::http_lib::extract_path_and_method(
incoming_req.method_and_path.as_str(),
&mut path,
&mut method,
);
response: &mut Response,
) -> std::io::Result<()> {
let mut method = incoming_req.method();
let mut path = incoming_req.path();
response.header("Access-Control-Allow-Origin: *");

let search_pos_opt = index_of(&path, "?");
if search_pos_opt.is_none() {
return crate::http_lib::ResponseDataToSet {
base: crate::http_lib::BasicHTTPDataToSet {
headers: CaseInsensitiveHashMap::new(),
data: "wrong".as_bytes().to_vec(),
},
code: 500,
msg: "Server error".to_string(),
};
response.status_code(500, "Server error");
return Ok(());
}
let search_pos = search_pos_opt.unwrap();
let search = &path[search_pos..];
Expand All @@ -42,7 +39,7 @@ pub fn handle_stat(
literal_path = percent_decode_str(value).decode_utf8().unwrap().to_string();
}
let mut error_reason = crate::handlers::errors::OK;
let mut final_data = Vec::new();
// let mut final_data = Vec::new();
if !literal_path.is_empty() {
if literal_path.starts_with('.') {
// idk if there is any side effect of this...
Expand Down Expand Up @@ -114,24 +111,17 @@ pub fn handle_stat(
out_raw.insert("ctime", true_creation_time.to_string());
out_raw.insert("mtime", true_modification_time.to_string());
out_raw.insert("atime", true_access_time.to_string());
final_data.extend(utils_lib::create_options(out_raw).as_bytes());
let mut w = response.body_mut().writer();
// final_data.extend(utils_lib::create_options(out_raw).as_bytes());
w.write_all(utils_lib::create_options(out_raw).as_bytes())?;
return Ok(());
}
} else {
error_reason = crate::handlers::errors::GENERAL_FAILURE;
}
} else {
error_reason = crate::handlers::errors::NO_INPUT;
}
crate::http_lib::ResponseDataToSet {
base: crate::http_lib::BasicHTTPDataToSet {
headers: super::all_origins(), // very unsafe, TODO
data: final_data,
},
code: if error_reason == crate::handlers::errors::OK {
200
} else {
400
},
msg: error_reason.to_string(),
}
response.status_code(400, error_reason);
Ok(())
}
Loading