Some error handling
This commit is contained in:
@ -31,7 +31,8 @@ pub fn connection_handler(client: super::Stream) {
|
|||||||
fn request_handler(client: &mut super::HttpStream) {
|
fn request_handler(client: &mut super::HttpStream) {
|
||||||
let mut res = super::Response::new();
|
let mut res = super::Response::new();
|
||||||
|
|
||||||
let req = super::parser::parse_request(&mut client.stream).unwrap();
|
match super::parser::parse_request(&mut client.stream) {
|
||||||
|
Ok(Some(req)) => {
|
||||||
println!("{} {}", req.method, req.uri);
|
println!("{} {}", req.method, req.uri);
|
||||||
|
|
||||||
if !req.uri.starts_with("/")
|
if !req.uri.starts_with("/")
|
||||||
@ -53,29 +54,7 @@ fn request_handler(client: &mut super::HttpStream) {
|
|||||||
[endpoint] => match usimp::is_valid_endpoint(endpoint) {
|
[endpoint] => match usimp::is_valid_endpoint(endpoint) {
|
||||||
true => match req.method {
|
true => match req.method {
|
||||||
Method::POST => {
|
Method::POST => {
|
||||||
let mut buf = [0; 8192];
|
return endpoint_handler(client, &req, &mut res, endpoint)
|
||||||
|
|
||||||
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.status(405);
|
||||||
@ -89,7 +68,99 @@ fn request_handler(client: &mut super::HttpStream) {
|
|||||||
} else {
|
} else {
|
||||||
res.status(404);
|
res.status(404);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
res.send(&mut client.stream).unwrap();
|
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;
|
client.server_keep_alive = false;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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());
|
||||||
|
}
|
||||||
|
@ -97,6 +97,7 @@ pub struct Status {
|
|||||||
message: String,
|
message: String,
|
||||||
desc: &'static str,
|
desc: &'static str,
|
||||||
class: StatusClass,
|
class: StatusClass,
|
||||||
|
info: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Status {
|
impl Status {
|
||||||
@ -108,6 +109,7 @@ impl Status {
|
|||||||
message: msg.to_string(),
|
message: msg.to_string(),
|
||||||
desc,
|
desc,
|
||||||
class: class.clone(),
|
class: class.clone(),
|
||||||
|
info: None,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -124,6 +126,7 @@ impl Status {
|
|||||||
message: message.to_string(),
|
message: message.to_string(),
|
||||||
desc: status.desc,
|
desc: status.desc,
|
||||||
class: status.class,
|
class: status.class,
|
||||||
|
info: None,
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
Status {
|
Status {
|
||||||
@ -131,6 +134,7 @@ impl Status {
|
|||||||
message: message.to_string(),
|
message: message.to_string(),
|
||||||
desc: "",
|
desc: "",
|
||||||
class: StatusClass::from_code(status_code),
|
class: StatusClass::from_code(status_code),
|
||||||
|
info: None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -188,6 +192,10 @@ impl Response {
|
|||||||
self.status = Status::from_code(status_code).unwrap()
|
self.status = Status::from_code(status_code).unwrap()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn error_info(&mut self, info: String) {
|
||||||
|
self.status.info = Some(info);
|
||||||
|
}
|
||||||
|
|
||||||
pub fn add_header(&mut self, name: &str, value: &str) {
|
pub fn add_header(&mut self, name: &str, value: &str) {
|
||||||
self.header_fields.push(HeaderField {
|
self.header_fields.push(HeaderField {
|
||||||
name: String::from(name),
|
name: String::from(name),
|
||||||
@ -266,7 +274,7 @@ impl Response {
|
|||||||
doc.replace("{code}", self.status.code.to_string().as_str())
|
doc.replace("{code}", self.status.code.to_string().as_str())
|
||||||
.replace("{message}", self.status.message.as_str())
|
.replace("{message}", self.status.message.as_str())
|
||||||
.replace("{desc}", self.status.desc)
|
.replace("{desc}", self.status.desc)
|
||||||
.replace("{info}", "") // TODO info string
|
.replace("{info}", self.status.info.as_ref().unwrap_or(&String::new()).as_str())
|
||||||
.as_str(),
|
.as_str(),
|
||||||
)
|
)
|
||||||
.replace("{{", "{")
|
.replace("{{", "{")
|
||||||
|
@ -1,12 +1,15 @@
|
|||||||
use crate::http;
|
use crate::http;
|
||||||
use crate::http::Status;
|
use crate::http::Status;
|
||||||
|
|
||||||
pub fn parse_request(stream: &mut http::Stream) -> Result<http::Request, String> {
|
pub fn parse_request(stream: &mut http::Stream) -> Result<Option<http::Request>, String> {
|
||||||
let mut buf = [0; 4096];
|
let mut buf = [0; 4096];
|
||||||
let size = stream.peek(&mut buf).unwrap();
|
let size = stream.peek(&mut buf).unwrap();
|
||||||
|
if size == 0 {
|
||||||
|
return Ok(None)
|
||||||
|
}
|
||||||
|
|
||||||
let mut parser = Parser::new_request_parser(&buf[..size]);
|
let mut parser = Parser::new_request_parser(&buf[..size]);
|
||||||
let header_size = parser.parse().unwrap();
|
let header_size = parser.parse()?;
|
||||||
|
|
||||||
let mut header_fields = Vec::new();
|
let mut header_fields = Vec::new();
|
||||||
for (name, value) in parser.headers {
|
for (name, value) in parser.headers {
|
||||||
@ -25,7 +28,7 @@ pub fn parse_request(stream: &mut http::Stream) -> Result<http::Request, String>
|
|||||||
|
|
||||||
stream.read_exact(&mut buf[..header_size]).unwrap();
|
stream.read_exact(&mut buf[..header_size]).unwrap();
|
||||||
|
|
||||||
Ok(request)
|
Ok(Some(request))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn parse_response(stream: &mut http::Stream) -> Result<http::Response, String> {
|
pub fn parse_response(stream: &mut http::Stream) -> Result<http::Response, String> {
|
||||||
|
Reference in New Issue
Block a user