scream for 3 hours

This commit is contained in:
abbie 2024-01-24 00:11:08 +00:00
parent 6411f84c5c
commit 287c1dd5f4
Signed by: threeoh6000
GPG key ID: 801FE4AD456E922C
3 changed files with 62 additions and 20 deletions

2
Cargo.lock generated
View file

@ -19,5 +19,3 @@ dependencies = [
[[package]]
name = "packeteer"
version = "0.5.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5425d35ba9635d531e8d6e0a4bd9b0974decbb146e7f7f69fffb071e44bcb647"

View file

@ -5,4 +5,4 @@ edition = "2018"
[dependencies]
frostwalker = "0.1.1"
packeteer = { version = "0.5.2", features = ["http1"] }
packeteer = { version = "0.5.2", features = ["http1"], path = "../packeteer" }

View file

@ -1,4 +1,5 @@
use std::io::{Write, BufReader, BufRead, Read};
use std::time::Duration;
use std::thread;
use std::net::{TcpListener, TcpStream};
use std::fs;
@ -69,8 +70,11 @@ fn process_cgi(filename: String, post_data: Option<String>) -> Option<Vec<u8>> {
.stdout(Stdio::piped())
.spawn()
.unwrap();
let mut stdin = child.stdin.take().unwrap();
stdin.write_all(post_data.unwrap().as_bytes()).unwrap();
let mut stdin = child.stdin.take().expect("stdin error");
thread::spawn(move || {
stdin.write_all(post_data.unwrap().as_bytes()).expect("stdin error");
});
output = child.wait_with_output();
} else if query == "".to_string() && post_data.is_some() {
let mut child = Command::new(format!("./{}", script))
@ -82,8 +86,11 @@ fn process_cgi(filename: String, post_data: Option<String>) -> Option<Vec<u8>> {
.stdout(Stdio::piped())
.spawn()
.unwrap();
let mut stdin = child.stdin.take().unwrap();
stdin.write_all(post_data.unwrap().as_bytes()).unwrap();
let mut stdin = child.stdin.take().expect("stdin error");
thread::spawn(move || {
stdin.write_all(post_data.unwrap().as_bytes()).expect("stdin error");
});
output = child.wait_with_output();
}
else if query != "" {
@ -267,7 +274,7 @@ fn generate_index(directory: String) -> String {
}
fn get_page(filename: String, settings: Settings) -> GetPageResult {
fn get_page(filename: String, settings: Settings, post_data: Option<String>) -> GetPageResult {
// The loaded page should be left immutable as it does
// not need to be modified by the server.
@ -302,7 +309,12 @@ fn get_page(filename: String, settings: Settings) -> GetPageResult {
return resultstruct;
}
let result = process_cgi(filename, None);
let result;
if post_data.is_some() {
result = process_cgi(filename, Some(post_data.unwrap()));
} else {
result = process_cgi(filename, None);
}
if result.is_some() {
resultstruct.contents = result.unwrap();
resultstruct.iscgi = true;
@ -323,7 +335,12 @@ fn get_page(filename: String, settings: Settings) -> GetPageResult {
return resultstruct;
}
let result = process_cgi(filename, None);
let result;
if post_data.is_some() {
result = process_cgi(filename, Some(post_data.unwrap()));
} else {
result = process_cgi(filename, None);
}
if result.is_some() {
resultstruct.contents = result.unwrap();
resultstruct.iscgi = true;
@ -376,6 +393,10 @@ fn grab_time() -> String{
fn process_request(request: Vec<u8>, settings: Settings) -> Resource {
if request.clone().contains(&(0 as u8)) {
let resource = Resource { contents: "<!DOCTYPE HTML><html><body><h1>400 Bad Request</h1><p>The request you sent appears to be malformed.</p></body></html>".to_string().into_bytes(), status_code: 400, mime: "text/html".to_string(), iscgi: false };
return resource;
}
let input = String::from_utf8_lossy(&request).to_string();
let prerequest = construct_request(&input);
let request;
@ -389,10 +410,9 @@ fn process_request(request: Vec<u8>, settings: Settings) -> Resource {
let mut index = String::new();
let output;
if &request.method == "GET" {
if &request.method == "GET" || &request.method == "POST" && !input.contains("\0") {
if settings.logging {
println!("Stream sent GET request.");
println!("Stream sent valid request.");
}
if request.location.segments.len() != 0 {
if request.location.segments[0] == ".." || request.location.segments[0] == "." {
@ -430,7 +450,12 @@ fn process_request(request: Vec<u8>, settings: Settings) -> Resource {
return resource;
}
let contents = process_cgi_with_path(cgipathraw, segclone, cgiscript, None);
let contents;
if &request.method == "GET" {
contents = process_cgi_with_path(cgipathraw, segclone, cgiscript, None);
} else {
contents = process_cgi_with_path(cgipathraw, segclone, cgiscript, Some(request.body.clone()));
}
if contents.is_some() {
let resource = Resource { contents: contents.unwrap(), status_code: 200, mime: "text/html".to_string(), iscgi: true };
return resource;
@ -468,7 +493,13 @@ fn process_request(request: Vec<u8>, settings: Settings) -> Resource {
}
// Did you want to see chars.as_str().to_string()?
let rescontents = get_page(output.to_string(), settings.clone());
let rescontents;
if &request.method == "GET" {
rescontents = get_page(output.to_string(), settings.clone(), None);
} else {
rescontents = get_page(output.to_string(), settings.clone(), Some(request.body.clone()));
}
let mut resource = Resource { contents: rescontents.contents, status_code: 200, mime: detect_media_type(output.split("?").collect::<Vec<&str>>()[0].to_string()), iscgi: false };
if rescontents.is500 {
resource.status_code = 500;
@ -499,13 +530,26 @@ fn serve(mut stream: TcpStream, settings: Settings) {
println!("Stream thread created.");
}
let mut request = Vec::new();
let mut reader = BufReader::new(&mut stream);
stream.set_read_timeout(Some(Duration::new(1, 0))).expect("can't timeout set");
let mut request = [0; 262144];
let mut reader = BufReader::with_capacity(4096, &mut stream);
reader
.read_until(b'\n', &mut request)
.read(&mut request)
.expect("Failed to read from stream!");
let resource = process_request(request, settings.clone());
let mut req = Vec::new();
for x in request {
if x == 0 {
break;
}
req.push(x);
}
if req.last().unwrap() != &10 {
req.push(10);
}
let resource = process_request(req, settings.clone());
let mut response_constructed = generate_response(resource.status_code, "");
response_constructed.headers.push(generate_kvheader("Content-Type", &resource.mime));