0.7.3: Fully unbreak CGI implementation so that it works properly AND remove all compilation warnings.

This commit is contained in:
abbie 2024-01-15 16:36:11 +00:00
parent 68b7c1f34e
commit fbbb97a98d
Signed by: threeoh6000
GPG key ID: 801FE4AD456E922C
3 changed files with 17 additions and 18 deletions

View file

@ -1 +1 @@
9 10

View file

@ -1,6 +1,6 @@
[package] [package]
name = "herb" name = "herb"
version = "0.7.2" version = "0.7.3"
edition = "2018" edition = "2018"
[dependencies] [dependencies]

View file

@ -6,7 +6,7 @@ use std::fs::File;
use std::string::{String}; use std::string::{String};
use std::process::Command; use std::process::Command;
use packeteer::http1::*; use packeteer::http1::*;
use packeteer::{generate_kvheader, unwrap_url_into_segments, Url}; use packeteer::{generate_kvheader, unwrap_url_into_segments};
struct Resource { struct Resource {
contents: Vec<u8>, contents: Vec<u8>,
@ -64,31 +64,30 @@ fn process_cgi(filename: String) -> Option<Vec<u8>> {
} }
fn process_cgi_with_path(filename: String, segments: Vec<String>, location: i32) -> Option<Vec<u8>> { fn process_cgi_with_path(filename: String, segments: Vec<String>, location: i32) -> Option<Vec<u8>> {
let query: String; let mut query: String = "".to_string();
let script = filename; let script = filename;
if segments[segments.len()-1].contains("?") {
let vars = segments[segments.len()-1].to_string().find("?").unwrap();
query = segments[segments.len()-1][vars..].to_string();
} else {
query = "".to_string();
}
let mut i = 0; let mut i = 0;
let mut path = "".to_string(); let mut path = "".to_string();
for seg in segments { for seg in segments {
if !(i <= location) { if !(i <= location) && !(seg.contains("?")) {
if path == "".to_string() { if path == "".to_string() {
path = format!("/{}", seg); path = format!("/{}", seg);
} else { } else {
path = format!("{}/{}", path, seg); path = format!("{}/{}", path, seg);
} }
} }
if !(i <= location) && seg.contains("?") {
let split: Vec<&str> = seg.as_str().split("?").collect();
query = split[split.len()-1].to_string();
path = format!("{}/{}", path, split[0].to_string());
}
i = i + 1; i = i + 1;
} }
path = format!("{}/", path); path = format!("{}/", path);
println!("{}", path);
let output; let output;
if query != "".to_string() { if query != "".to_string() {
output = Command::new(format!("./{}", script)) output = Command::new(format!("./{}", script))
@ -320,11 +319,11 @@ fn process_request(request: Vec<u8>) -> Resource {
} }
let contents = process_cgi_with_path(cgipathraw, segclone, cgiscript); let contents = process_cgi_with_path(cgipathraw, segclone, cgiscript);
if contents.is_some() { if contents.is_some() {
let mut resource = Resource { contents: contents.unwrap(), status_code: 200, mime: "text/html".to_string(), iscgi: true }; let resource = Resource { contents: contents.unwrap(), status_code: 200, mime: "text/html".to_string(), iscgi: true };
return resource; return resource;
} else { } else {
let newcontents = "<!DOCTYPE HTML><html><body><h1>502 Bad Gateway</h1><p>The server is unable to complete your request due to an error.</p></body></html>".to_string().into_bytes(); let newcontents = "<!DOCTYPE HTML><html><body><h1>502 Bad Gateway</h1><p>The server is unable to complete your request due to an error.</p></body></html>".to_string().into_bytes();
let mut resource = Resource { contents: newcontents, status_code: 502, mime: "text/html".to_string(), iscgi: false }; let resource = Resource { contents: newcontents, status_code: 502, mime: "text/html".to_string(), iscgi: false };
return resource; return resource;
} }
} }
@ -339,9 +338,9 @@ fn process_request(request: Vec<u8>) -> Resource {
if dir == true { if dir == true {
index += &path; index += &path;
index += "/index.html"; index += "/index.html";
output = index.as_str().clone(); output = index.as_str();
} else { } else {
output = path.as_str().clone(); output = path.as_str();
} }
} }
} else { } else {
@ -394,7 +393,7 @@ fn serve(mut stream: TcpStream) {
} }
if resource.iscgi { if resource.iscgi {
let mut header = "HTTP/1.1 200 OK\r\n".to_string().into_bytes(); let header = "HTTP/1.1 200 OK\r\n".to_string().into_bytes();
stream.write(header.as_slice()).unwrap(); stream.write(header.as_slice()).unwrap();
stream.write(&resource.contents).unwrap(); stream.write(&resource.contents).unwrap();
stream.flush().unwrap(); stream.flush().unwrap();