Some error handling
This commit is contained in:
@ -31,65 +31,136 @@ pub fn connection_handler(client: super::Stream) {
|
||||
fn request_handler(client: &mut super::HttpStream) {
|
||||
let mut res = super::Response::new();
|
||||
|
||||
let req = super::parser::parse_request(&mut client.stream).unwrap();
|
||||
println!("{} {}", req.method, req.uri);
|
||||
match super::parser::parse_request(&mut client.stream) {
|
||||
Ok(Some(req)) => {
|
||||
println!("{} {}", req.method, req.uri);
|
||||
|
||||
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);
|
||||
} 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),
|
||||
[endpoint] => match usimp::is_valid_endpoint(endpoint) {
|
||||
true => match req.method {
|
||||
Method::POST => {
|
||||
let mut buf = [0; 8192];
|
||||
|
||||
let length = req.find_header("Content-Length");
|
||||
let length: usize = length.unwrap().parse().unwrap();
|
||||
|
||||
client.stream.read_exact(&mut buf[..length]);
|
||||
|
||||
// TODO decompress
|
||||
let input =
|
||||
json::parse(std::str::from_utf8(&buf[..length]).unwrap()).unwrap();
|
||||
let output = usimp::endpoint(endpoint, input);
|
||||
|
||||
// TODO compress
|
||||
let buf = output.to_string() + "\n";
|
||||
let length = buf.as_bytes().len();
|
||||
res.add_header("Content-Length", length.to_string().as_str());
|
||||
res.add_header("Content-Type", "application/json; charset=utf-8");
|
||||
|
||||
res.status(200);
|
||||
res.send(&mut client.stream);
|
||||
client.stream.write_all(buf.as_bytes());
|
||||
|
||||
return;
|
||||
}
|
||||
_ => {
|
||||
res.status(405);
|
||||
res.add_header("Allow", "POST");
|
||||
}
|
||||
},
|
||||
false => res.status(400),
|
||||
},
|
||||
_ => res.status(400),
|
||||
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);
|
||||
} 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),
|
||||
[endpoint] => match usimp::is_valid_endpoint(endpoint) {
|
||||
true => match req.method {
|
||||
Method::POST => {
|
||||
return endpoint_handler(client, &req, &mut res, endpoint)
|
||||
}
|
||||
_ => {
|
||||
res.status(405);
|
||||
res.add_header("Allow", "POST");
|
||||
}
|
||||
},
|
||||
false => res.status(400),
|
||||
},
|
||||
_ => res.status(400),
|
||||
}
|
||||
} else {
|
||||
res.status(404);
|
||||
}
|
||||
}
|
||||
Ok(None) => {
|
||||
client.client_keep_alive = false;
|
||||
return;
|
||||
}
|
||||
Err(e) => {
|
||||
res.status(400);
|
||||
res.error_info(format!("Unable to parser header: {}", &e));
|
||||
println!("Unable to parser header: {}", &e);
|
||||
client.server_keep_alive = false;
|
||||
}
|
||||
} else {
|
||||
res.status(404);
|
||||
}
|
||||
|
||||
res.send(&mut client.stream).unwrap();
|
||||
if let Err(e) = res.send(&mut client.stream) {
|
||||
println!("Unable to send: {}", e);
|
||||
client.server_keep_alive = false;
|
||||
}
|
||||
client.server_keep_alive = false;
|
||||
}
|
||||
|
||||
fn endpoint_handler(
|
||||
client: &mut super::HttpStream,
|
||||
req: &super::Request,
|
||||
res: &mut super::Response,
|
||||
endpoint: &str,
|
||||
) {
|
||||
let mut buf = [0; 8192];
|
||||
|
||||
let mut error = |code: u16, err_str: &str, client: &mut super::HttpStream| {
|
||||
println!("{}", err_str);
|
||||
res.status(code);
|
||||
res.error_info(err_str.to_string());
|
||||
|
||||
if let Err(e) = res.send(&mut client.stream) {
|
||||
println!("Unable to send: {}", e);
|
||||
client.server_keep_alive = false;
|
||||
}
|
||||
};
|
||||
|
||||
let length = req.find_header("Content-Length");
|
||||
let length: usize = match match length {
|
||||
Some(length) => length,
|
||||
None => {
|
||||
return error(
|
||||
400,
|
||||
"Unable to parse header: Content-Length missing",
|
||||
client,
|
||||
)
|
||||
},
|
||||
}
|
||||
.parse()
|
||||
{
|
||||
Ok(length) => length,
|
||||
Err(e) => {
|
||||
return error(
|
||||
400,
|
||||
format!("Unable to parse Content-Length: {}", &e).as_str(),
|
||||
client,
|
||||
)
|
||||
},
|
||||
};
|
||||
|
||||
client.stream.read_exact(&mut buf[..length]);
|
||||
|
||||
// TODO decompress
|
||||
let input = match json::parse(match std::str::from_utf8(&buf[..length]) {
|
||||
Ok(source) => source,
|
||||
Err(e) => {
|
||||
return error(
|
||||
400,
|
||||
format!("Unable to parse payload: {}", &e).as_str(),
|
||||
client,
|
||||
)
|
||||
},
|
||||
}) {
|
||||
Ok(val) => val,
|
||||
Err(e) => {
|
||||
return error(
|
||||
400,
|
||||
format!("Unable to parse JSON: {}", &e).as_str(),
|
||||
client,
|
||||
)
|
||||
},
|
||||
};
|
||||
let output = usimp::endpoint(endpoint, input);
|
||||
|
||||
// TODO compress
|
||||
let buf = output.to_string() + "\r\n";
|
||||
let length = buf.as_bytes().len();
|
||||
res.add_header("Content-Length", length.to_string().as_str());
|
||||
res.add_header("Content-Type", "application/json; charset=utf-8");
|
||||
|
||||
res.status(200);
|
||||
res.send(&mut client.stream);
|
||||
client.stream.write_all(buf.as_bytes());
|
||||
}
|
||||
|
Reference in New Issue
Block a user