Cleanup
This commit is contained in:
12
src/error.rs
12
src/error.rs
@ -1,7 +1,6 @@
|
|||||||
use std::borrow::Borrow;
|
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
|
|
||||||
#[derive(Copy, Clone)]
|
#[derive(Copy, Clone, Debug)]
|
||||||
pub enum Kind {
|
pub enum Kind {
|
||||||
InvalidEndpointError,
|
InvalidEndpointError,
|
||||||
JsonParseError,
|
JsonParseError,
|
||||||
@ -11,12 +10,13 @@ pub enum Kind {
|
|||||||
IoError,
|
IoError,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Copy, Clone)]
|
#[derive(Copy, Clone, Debug)]
|
||||||
pub enum Class {
|
pub enum Class {
|
||||||
ClientError,
|
ClientError,
|
||||||
ServerError,
|
ServerError,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
pub struct Error {
|
pub struct Error {
|
||||||
kind: Kind,
|
kind: Kind,
|
||||||
msg: Option<String>,
|
msg: Option<String>,
|
||||||
@ -89,12 +89,6 @@ impl fmt::Display for Error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl fmt::Debug for Error {
|
|
||||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
||||||
self.fmt(f)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl From<std::io::Error> for Error {
|
impl From<std::io::Error> for Error {
|
||||||
fn from(error: std::io::Error) -> Self {
|
fn from(error: std::io::Error) -> Self {
|
||||||
Error {
|
Error {
|
||||||
|
@ -113,7 +113,7 @@ fn endpoint_handler(
|
|||||||
client.server_keep_alive = false;
|
client.server_keep_alive = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
client.stream.write_all(buf.as_bytes());
|
client.stream.write_all(buf.as_bytes()).unwrap();
|
||||||
};
|
};
|
||||||
|
|
||||||
let length = req.find_header("Content-Length");
|
let length = req.find_header("Content-Length");
|
||||||
@ -141,7 +141,7 @@ fn endpoint_handler(
|
|||||||
};
|
};
|
||||||
|
|
||||||
let mut buf = [0; 8192];
|
let mut buf = [0; 8192];
|
||||||
client.stream.read_exact(&mut buf[..length]);
|
client.stream.read_exact(&mut buf[..length]).unwrap();
|
||||||
|
|
||||||
// TODO decompress
|
// TODO decompress
|
||||||
let input = match serde_json::from_slice(&buf[..length]) {
|
let input = match serde_json::from_slice(&buf[..length]) {
|
||||||
@ -160,6 +160,6 @@ fn endpoint_handler(
|
|||||||
res.add_header("Content-Type", "application/json; charset=utf-8");
|
res.add_header("Content-Type", "application/json; charset=utf-8");
|
||||||
|
|
||||||
res.status(200);
|
res.status(200);
|
||||||
res.send(&mut client.stream);
|
res.send(&mut client.stream).unwrap();
|
||||||
client.stream.write_all(buf.as_bytes());
|
client.stream.write_all(buf.as_bytes()).unwrap();
|
||||||
}
|
}
|
||||||
|
@ -262,7 +262,7 @@ impl Response {
|
|||||||
|
|
||||||
stream.write_all(header.as_bytes())?;
|
stream.write_all(header.as_bytes())?;
|
||||||
if let Some(buf) = buf {
|
if let Some(buf) = buf {
|
||||||
stream.write_all(buf.as_bytes());
|
stream.write_all(buf.as_bytes())?;
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
@ -8,11 +8,11 @@ pub fn connection_handler(client: &mut http::HttpStream, req: &http::Request) {
|
|||||||
} else {
|
} else {
|
||||||
res.status(405);
|
res.status(405);
|
||||||
res.add_header("Allow", "GET");
|
res.add_header("Allow", "GET");
|
||||||
res.send(&mut client.stream);
|
res.send(&mut client.stream).unwrap();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO implement websocket
|
// TODO implement websocket
|
||||||
res.status(501);
|
res.status(501);
|
||||||
res.send(&mut client.stream);
|
res.send(&mut client.stream).unwrap();
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user