Add really basic request responses
This commit is contained in:
@ -1,24 +1,74 @@
|
||||
use json;
|
||||
use chrono;
|
||||
use crate::usimp;
|
||||
use super::Method;
|
||||
|
||||
pub struct HttpStream {
|
||||
stream: super::Stream,
|
||||
request_num: u32,
|
||||
client_keep_alive: bool,
|
||||
server_keep_alive: bool,
|
||||
}
|
||||
|
||||
pub fn connection_handler(client: super::Stream) {
|
||||
let mut client = super::HttpStream {
|
||||
stream: client,
|
||||
request_num: 0,
|
||||
client_keep_alive: true,
|
||||
server_keep_alive: true,
|
||||
};
|
||||
|
||||
while client.request_num < super::REQUESTS_PER_CONNECTION {
|
||||
while client.request_num < super::REQUESTS_PER_CONNECTION
|
||||
&& client.client_keep_alive
|
||||
&& client.server_keep_alive
|
||||
{
|
||||
request_handler(&mut client);
|
||||
client.request_num += 1;
|
||||
}
|
||||
}
|
||||
|
||||
fn request_handler(client: &mut super::HttpStream) {
|
||||
let mut res = super::Response::new();
|
||||
res.add_header("Server", "Locutus");
|
||||
res.add_header("Date", chrono::Utc::now().format("%a, %d %b %Y %H:%M:%S GMT").to_string().as_str());
|
||||
|
||||
let req = super::parser::parse_request(&mut client.stream).unwrap();
|
||||
println!("{} {}", req.method, req.uri);
|
||||
|
||||
let mut res = super::Response::new();
|
||||
let doc = "<!DOCTYPE html><html><head><title>Locutus Server</title></head><body><h1>Hello World! :D</h1></body></html>";
|
||||
res.add_header("Content-Length", doc.len().to_string().as_str());
|
||||
res.add_header("Content-Type", "text/html; charset=utf-8");
|
||||
|
||||
if !req.uri.starts_with("/")
|
||||
|| req.uri.contains("/./")
|
||||
|| req.uri.contains("/../")
|
||||
|| req.uri.ends_with("/..")
|
||||
{
|
||||
res.status(400);
|
||||
} else if req.uri.contains("/.") {
|
||||
res.status(404);
|
||||
} else if req.uri.eq("/") {
|
||||
res.status(200);
|
||||
res.add_header("Content-Length", doc.len().to_string().as_str());
|
||||
res.add_header("Content-Type", "text/html; charset=utf-8");
|
||||
} else if req.uri.starts_with("/_usimp/") {
|
||||
let parts: Vec<&str> = req.uri.split('/').collect();
|
||||
match parts[2..] {
|
||||
["entity", entity] => res.status(501),
|
||||
[func] => {
|
||||
match usimp::is_valid(func) {
|
||||
true => match req.method {
|
||||
Method::POST => res.status(200),
|
||||
_ => res.status(405),
|
||||
},
|
||||
false => res.status(400),
|
||||
}
|
||||
},
|
||||
_ => res.status(400),
|
||||
}
|
||||
} else {
|
||||
res.status(404);
|
||||
}
|
||||
|
||||
res.send(&mut client.stream).unwrap();
|
||||
client.stream.write_all(doc.as_bytes()).unwrap();
|
||||
client.server_keep_alive = false;
|
||||
}
|
||||
|
Reference in New Issue
Block a user