Http responses contain body
This commit is contained in:
@ -1,13 +1,14 @@
|
||||
use super::Method;
|
||||
use crate::usimp;
|
||||
use crate::websocket;
|
||||
use chrono;
|
||||
use json;
|
||||
|
||||
pub struct HttpStream {
|
||||
stream: super::Stream,
|
||||
request_num: u32,
|
||||
client_keep_alive: bool,
|
||||
server_keep_alive: bool,
|
||||
pub stream: super::Stream,
|
||||
pub request_num: u32,
|
||||
pub client_keep_alive: bool,
|
||||
pub server_keep_alive: bool,
|
||||
}
|
||||
|
||||
pub fn connection_handler(client: super::Stream) {
|
||||
@ -29,20 +30,10 @@ pub fn connection_handler(client: super::Stream) {
|
||||
|
||||
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 doc = "<!DOCTYPE html><html><head><title>Locutus Server</title></head><body><h1>Hello World! :D</h1></body></html>";
|
||||
|
||||
if !req.uri.starts_with("/")
|
||||
|| req.uri.contains("/./")
|
||||
|| req.uri.contains("/../")
|
||||
@ -53,16 +44,22 @@ fn request_handler(client: &mut super::HttpStream) {
|
||||
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.eq("/_usimp/websocket") {
|
||||
return websocket::connection_handler(client, &req);
|
||||
} 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) {
|
||||
[endpoint] => match usimp::is_valid_endpoint(endpoint) {
|
||||
true => match req.method {
|
||||
Method::POST => res.status(200),
|
||||
_ => res.status(405),
|
||||
Method::POST => {
|
||||
// TODO
|
||||
res.status(200)
|
||||
}
|
||||
_ => {
|
||||
res.status(405);
|
||||
res.add_header("Allow", "POST");
|
||||
}
|
||||
},
|
||||
false => res.status(400),
|
||||
},
|
||||
@ -73,6 +70,5 @@ fn request_handler(client: &mut super::HttpStream) {
|
||||
}
|
||||
|
||||
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