Able to send a particular page now

This commit is contained in:
2025-11-30 00:30:16 +00:00
parent a26fe3d986
commit aa4cf930b9
2 changed files with 87 additions and 10 deletions

View File

@@ -90,6 +90,7 @@ fn encode_npon(file_contents: &str, depth: u8) ->
let mut index_flag: usize = 0; let mut index_flag: usize = 0;
let mut flag: u8 = 0; let mut flag: u8 = 0;
let mut backslash_flag: bool = false; let mut backslash_flag: bool = false;
let mut temp_string: Vec<u8> = vec![];
for (index, character) in file_contents.bytes().enumerate() for (index, character) in file_contents.bytes().enumerate()
{ {
if backslash_flag if backslash_flag
@@ -119,7 +120,7 @@ fn encode_npon(file_contents: &str, depth: u8) ->
{ {
40 => 40 =>
{ {
bytes.push(3); // ( for name start temp_string.push(3); // ( for name start
flag = 2; flag = 2;
} }
123 => bytes.push(5), 123 => bytes.push(5),
@@ -131,7 +132,7 @@ fn encode_npon(file_contents: &str, depth: u8) ->
}, },
39 | 34 => 39 | 34 =>
{ {
bytes.push(7); temp_string.push(7);
flag = 1; flag = 1;
}, },
_ => (), _ => (),
@@ -144,14 +145,15 @@ fn encode_npon(file_contents: &str, depth: u8) ->
{ {
39 | 34 => 39 | 34 =>
{ {
bytes.push(8); temp_string.push(8);
bytes.append(&mut temp_string);
flag = 0; flag = 0;
}, },
92 => 92 =>
{ {
backslash_flag = true; backslash_flag = true;
}, },
_ => bytes.push(character), _ => temp_string.push(character),
} }
continue continue
}, },
@@ -161,14 +163,16 @@ fn encode_npon(file_contents: &str, depth: u8) ->
{ {
41 => 41 =>
{ {
bytes.push(4); // ) for name end temp_string.push(4);
bytes.append(&mut temp_string); // ) for name end
temp_string = vec![];
flag = 0; flag = 0;
}, },
92 => 92 =>
{ {
backslash_flag = true; backslash_flag = true;
}, },
_ => bytes.push(character), _ => temp_string.push(character),
} }
continue continue
}, },

View File

@@ -20,6 +20,7 @@ lazy_static! {
// TODO, make 'code' an enum // TODO, make 'code' an enum
// TODO, safely modify bytes in thread. // TODO, safely modify bytes in thread.
// TODO, Makes paths more consistent. // TODO, Makes paths more consistent.
// TODO, Have timers to see how inefficient my code is
fn main() -> io::Result<()> fn main() -> io::Result<()>
{ {
#![warn(clippy::pedantic, clippy::perf)] #![warn(clippy::pedantic, clippy::perf)]
@@ -54,9 +55,15 @@ fn main() -> io::Result<()>
} }
} }
); );
let mut bytes: Vec<u8> = vec![];
for stream in listener.incoming() { 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(); let stream = stream.unwrap();
handle_connection(stream, &bytes); handle_connection(stream, &bytes);
} }
@@ -72,13 +79,79 @@ fn handle_connection(mut stream: TcpStream, bytes: &[u8])
.map(|result| result.unwrap()) .map(|result| result.unwrap())
.take_while(|line| !line.is_empty()) .take_while(|line| !line.is_empty())
.collect(); .collect();
println!("REQUEST: {:?}", notp_request); println!("REQUEST: {:?}", notp_request); // TODO format requests in npon
if let Err(e) = stream.write_all(bytes) let page_bytes = get_page(bytes, &notp_request[0]); // first request line is the page atm
if let Err(e) = stream.write_all(&page_bytes)
{ {
eprintln!("Failed to send response: {}", e); eprintln!("Failed to send response: {}", e);
} }
else 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
}