Error handling, I should make it better though
This commit is contained in:
@@ -44,12 +44,12 @@ pub fn watch_files(file_list: &[PathBuf]) -> u8
|
||||
Err(e) =>
|
||||
{
|
||||
println!("watch error: {:?}", e);
|
||||
return 2
|
||||
return 1
|
||||
}
|
||||
}
|
||||
}
|
||||
println!("Watching files...");
|
||||
1
|
||||
2
|
||||
}
|
||||
|
||||
pub fn read_file(file_name: &str, depth: u8) ->
|
||||
|
||||
80
src/main.rs
80
src/main.rs
@@ -7,11 +7,12 @@ use std::
|
||||
sync::mpsc,
|
||||
thread,
|
||||
path::PathBuf,
|
||||
time::Duration,
|
||||
};
|
||||
use lazy_static::lazy_static;
|
||||
|
||||
|
||||
mod files;
|
||||
mod pages;
|
||||
|
||||
lazy_static! {
|
||||
static ref ROOT_DIR: PathBuf = fs::canonicalize(PathBuf::from("./root")).unwrap();
|
||||
@@ -44,13 +45,20 @@ fn main() -> io::Result<()>
|
||||
loop
|
||||
{
|
||||
let code = files::watch_files(&file_list);
|
||||
if code == 0
|
||||
// Pause, because often the metadata, name and contents are all registered at the
|
||||
// same time, resulting in multiple re-encodes
|
||||
thread::sleep(Duration::from_millis(100));
|
||||
match code
|
||||
{
|
||||
0 =>
|
||||
{
|
||||
println!("Re encoding index");
|
||||
|
||||
(bytes, file_list) = files::read_file(INDEX_FILE, 0);
|
||||
file_list.push(ROOT_DIR.join(INDEX_FILE));
|
||||
tx.send(bytes).unwrap();
|
||||
},
|
||||
error => println!("Watch error: {}", error),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -62,7 +70,7 @@ fn main() -> io::Result<()>
|
||||
bytes = match bytes_result
|
||||
{
|
||||
Ok(bytes_data) => bytes_data,
|
||||
Err(error) => bytes,
|
||||
Err(_) => bytes,
|
||||
};
|
||||
let stream = stream.unwrap();
|
||||
handle_connection(stream, &bytes);
|
||||
@@ -80,7 +88,7 @@ fn handle_connection(mut stream: TcpStream, bytes: &[u8])
|
||||
.take_while(|line| !line.is_empty())
|
||||
.collect();
|
||||
println!("REQUEST: {:?}", notp_request); // TODO format requests in npon
|
||||
let page_bytes = get_page(bytes, ¬p_request[0]); // first request line is the page atm
|
||||
let page_bytes = pages::get_page(bytes, ¬p_request[0]); // first request line is the page atm
|
||||
if let Err(e) = stream.write_all(&page_bytes)
|
||||
{
|
||||
eprintln!("Failed to send response: {}", e);
|
||||
@@ -91,67 +99,3 @@ fn handle_connection(mut stream: TcpStream, bytes: &[u8])
|
||||
}
|
||||
}
|
||||
|
||||
// TODO these functions are so shit
|
||||
// Finds the index of the first occurrence of search_bytes in bytes.
|
||||
fn find_pattern(bytes: &[u8], search_bytes: &[u8]) -> Option<usize>
|
||||
{
|
||||
bytes.windows(search_bytes.len())
|
||||
.position(|window| window == search_bytes)
|
||||
}
|
||||
// Finds the closing brace of the opening brace passed as a param
|
||||
fn find_closing_brace(bytes: &[u8], opening_brace_index: usize) -> Option<usize>
|
||||
{
|
||||
let mut depth: i8 = 0;
|
||||
for i in opening_brace_index..bytes.len()-1
|
||||
{
|
||||
if bytes[i] == 5 {
|
||||
depth += 1; }
|
||||
else if bytes[i] == 6
|
||||
{
|
||||
depth -= 1;
|
||||
if depth == 0 { return Some(i) }
|
||||
}
|
||||
//println!("{}\t{}", bytes[i], depth);
|
||||
}
|
||||
None
|
||||
}
|
||||
fn get_page(bytes: &[u8], page: &str) -> Vec<u8>
|
||||
{
|
||||
let mut page_bytes: Vec<u8> = vec![1];
|
||||
let search_pages_bytes: Vec<u8> = vec![3, 112, 97, 103, 101, 115, 4];
|
||||
let mut search_bytes: Vec<u8> = vec![3];
|
||||
search_bytes.append(&mut page.as_bytes().to_vec());
|
||||
search_bytes.push(4);
|
||||
|
||||
let mut opening_page_index: usize = 0;
|
||||
let mut closing_page_index: usize = 0;
|
||||
let mut opening_index: usize = 0;
|
||||
let mut closing_index: usize = 0;
|
||||
|
||||
if let Some(opening_index) = find_pattern(&bytes, &search_pages_bytes)
|
||||
{
|
||||
if let Some(closing_index) = find_closing_brace(&bytes, opening_index+search_pages_bytes.len())
|
||||
{
|
||||
//println!("found pages: {} to {}", opening_index, closing_index);
|
||||
if let Some(opening_page_index) = find_pattern(&bytes[opening_index..closing_index+1], &search_bytes)
|
||||
{
|
||||
//println!("{:?}", &bytes[opening_index+opening_page_index..]);
|
||||
if let Some(closing_page_index) = find_closing_brace
|
||||
(
|
||||
&bytes[opening_index+opening_page_index..],
|
||||
search_bytes.len()
|
||||
)
|
||||
{
|
||||
//println!("found page: {} to {}", opening_index+opening_page_index, opening_index+closing_page_index);
|
||||
//println!("found page: {:?}", &bytes[opening_index+opening_page_index..opening_index+opening_page_index+closing_page_index+1]);
|
||||
page_bytes.append(&mut bytes[opening_page_index+opening_index..opening_page_index+closing_page_index+opening_index+1].to_vec());
|
||||
page_bytes.push(2);
|
||||
return page_bytes
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
page_bytes.push(10); // Page not found error message
|
||||
page_bytes.push(2);
|
||||
page_bytes
|
||||
}
|
||||
|
||||
60
src/pages.rs
Normal file
60
src/pages.rs
Normal file
@@ -0,0 +1,60 @@
|
||||
// TODO these functions are so shit
|
||||
/// Finds the index of the first occurrence of search_bytes in bytes.
|
||||
fn find_pattern(bytes: &[u8], search_bytes: &[u8]) -> Option<usize>
|
||||
{
|
||||
bytes.windows(search_bytes.len())
|
||||
.position(|window| window == search_bytes)
|
||||
}
|
||||
/// Finds the closing brace of the opening brace passed as a param
|
||||
fn find_closing_brace(bytes: &[u8], opening_brace_index: usize) -> Option<usize>
|
||||
{
|
||||
let mut depth: i8 = 0;
|
||||
for i in opening_brace_index..bytes.len()-1
|
||||
{
|
||||
if bytes[i] == 5 {
|
||||
depth += 1; }
|
||||
else if bytes[i] == 6
|
||||
{
|
||||
depth -= 1;
|
||||
if depth == 0 { return Some(i) }
|
||||
}
|
||||
//println!("{}\t{}", bytes[i], depth);
|
||||
}
|
||||
None
|
||||
}
|
||||
/// Searches the bytes for a particular page
|
||||
pub fn get_page(bytes: &[u8], page: &str) -> Vec<u8>
|
||||
{
|
||||
let mut page_bytes: Vec<u8> = vec![1];
|
||||
let search_pages_bytes: Vec<u8> = vec![3, 112, 97, 103, 101, 115, 4];
|
||||
let mut search_bytes: Vec<u8> = vec![3];
|
||||
search_bytes.append(&mut page.as_bytes().to_vec());
|
||||
search_bytes.push(4);
|
||||
|
||||
if let Some(opening_index) = find_pattern(&bytes, &search_pages_bytes)
|
||||
{
|
||||
if let Some(closing_index) = find_closing_brace(&bytes, opening_index+search_pages_bytes.len())
|
||||
{
|
||||
//println!("found pages: {} to {}", opening_index, closing_index);
|
||||
if let Some(opening_page_index) = find_pattern(&bytes[opening_index..closing_index+1], &search_bytes)
|
||||
{
|
||||
//println!("{:?}", &bytes[opening_index+opening_page_index..]);
|
||||
if let Some(closing_page_index) = find_closing_brace
|
||||
(
|
||||
&bytes[opening_index+opening_page_index..],
|
||||
search_bytes.len()
|
||||
)
|
||||
{
|
||||
//println!("found page: {} to {}", opening_index+opening_page_index, opening_index+closing_page_index);
|
||||
//println!("found page: {:?}", &bytes[opening_index+opening_page_index..opening_index+opening_page_index+closing_page_index+1]);
|
||||
page_bytes.append(&mut bytes[opening_page_index+opening_index..opening_page_index+closing_page_index+opening_index+1].to_vec());
|
||||
page_bytes.push(2);
|
||||
return page_bytes
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
page_bytes.push(10); // Page not found error message
|
||||
page_bytes.push(2);
|
||||
page_bytes
|
||||
}
|
||||
Reference in New Issue
Block a user