Able to send a particular page now
This commit is contained in:
16
src/files.rs
16
src/files.rs
@@ -90,6 +90,7 @@ fn encode_npon(file_contents: &str, depth: u8) ->
|
||||
let mut index_flag: usize = 0;
|
||||
let mut flag: u8 = 0;
|
||||
let mut backslash_flag: bool = false;
|
||||
let mut temp_string: Vec<u8> = vec![];
|
||||
for (index, character) in file_contents.bytes().enumerate()
|
||||
{
|
||||
if backslash_flag
|
||||
@@ -119,7 +120,7 @@ fn encode_npon(file_contents: &str, depth: u8) ->
|
||||
{
|
||||
40 =>
|
||||
{
|
||||
bytes.push(3); // ( for name start
|
||||
temp_string.push(3); // ( for name start
|
||||
flag = 2;
|
||||
}
|
||||
123 => bytes.push(5),
|
||||
@@ -131,7 +132,7 @@ fn encode_npon(file_contents: &str, depth: u8) ->
|
||||
},
|
||||
39 | 34 =>
|
||||
{
|
||||
bytes.push(7);
|
||||
temp_string.push(7);
|
||||
flag = 1;
|
||||
},
|
||||
_ => (),
|
||||
@@ -144,14 +145,15 @@ fn encode_npon(file_contents: &str, depth: u8) ->
|
||||
{
|
||||
39 | 34 =>
|
||||
{
|
||||
bytes.push(8);
|
||||
temp_string.push(8);
|
||||
bytes.append(&mut temp_string);
|
||||
flag = 0;
|
||||
},
|
||||
92 =>
|
||||
{
|
||||
backslash_flag = true;
|
||||
},
|
||||
_ => bytes.push(character),
|
||||
_ => temp_string.push(character),
|
||||
}
|
||||
continue
|
||||
},
|
||||
@@ -161,14 +163,16 @@ fn encode_npon(file_contents: &str, depth: u8) ->
|
||||
{
|
||||
41 =>
|
||||
{
|
||||
bytes.push(4); // ) for name end
|
||||
temp_string.push(4);
|
||||
bytes.append(&mut temp_string); // ) for name end
|
||||
temp_string = vec![];
|
||||
flag = 0;
|
||||
},
|
||||
92 =>
|
||||
{
|
||||
backslash_flag = true;
|
||||
},
|
||||
_ => bytes.push(character),
|
||||
_ => temp_string.push(character),
|
||||
}
|
||||
continue
|
||||
},
|
||||
|
||||
81
src/main.rs
81
src/main.rs
@@ -20,6 +20,7 @@ lazy_static! {
|
||||
// TODO, make 'code' an enum
|
||||
// TODO, safely modify bytes in thread.
|
||||
// TODO, Makes paths more consistent.
|
||||
// TODO, Have timers to see how inefficient my code is
|
||||
fn main() -> io::Result<()>
|
||||
{
|
||||
#![warn(clippy::pedantic, clippy::perf)]
|
||||
@@ -54,9 +55,15 @@ fn main() -> io::Result<()>
|
||||
}
|
||||
}
|
||||
);
|
||||
let mut bytes: Vec<u8> = vec![];
|
||||
|
||||
for stream in listener.incoming() {
|
||||
let bytes = rx.recv().unwrap();
|
||||
let bytes_result = rx.try_recv();
|
||||
bytes = match bytes_result
|
||||
{
|
||||
Ok(bytes_data) => bytes_data,
|
||||
Err(error) => bytes,
|
||||
};
|
||||
let stream = stream.unwrap();
|
||||
handle_connection(stream, &bytes);
|
||||
}
|
||||
@@ -72,13 +79,79 @@ fn handle_connection(mut stream: TcpStream, bytes: &[u8])
|
||||
.map(|result| result.unwrap())
|
||||
.take_while(|line| !line.is_empty())
|
||||
.collect();
|
||||
println!("REQUEST: {:?}", notp_request);
|
||||
if let Err(e) = stream.write_all(bytes)
|
||||
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
|
||||
if let Err(e) = stream.write_all(&page_bytes)
|
||||
{
|
||||
eprintln!("Failed to send response: {}", e);
|
||||
}
|
||||
else
|
||||
{
|
||||
println!("Sent response: {:?}", bytes);
|
||||
println!("Sent response");
|
||||
}
|
||||
}
|
||||
|
||||
// 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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user