Echo/reply working
This commit is contained in:
@ -53,8 +53,29 @@ 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 => {
|
||||||
// TODO
|
let mut buf = [0; 8192];
|
||||||
res.status(200)
|
|
||||||
|
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);
|
||||||
|
@ -160,6 +160,21 @@ pub struct Response {
|
|||||||
header_fields: Vec<HeaderField>,
|
header_fields: Vec<HeaderField>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Request {
|
||||||
|
pub fn find_header(&self, header_name: &str) -> Option<String> {
|
||||||
|
for field in &self.header_fields {
|
||||||
|
if field
|
||||||
|
.name
|
||||||
|
.to_lowercase()
|
||||||
|
.eq(header_name.to_ascii_lowercase().as_str())
|
||||||
|
{
|
||||||
|
return Some(field.value.clone());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl Response {
|
impl Response {
|
||||||
pub fn new() -> Response {
|
pub fn new() -> Response {
|
||||||
Response {
|
Response {
|
||||||
|
@ -1,7 +1,25 @@
|
|||||||
use json;
|
use json;
|
||||||
|
|
||||||
|
static ENDPOINTS: [(&str, fn(json::JsonValue) -> json::JsonValue); 1] = [("echo", echo)];
|
||||||
|
|
||||||
pub fn is_valid_endpoint(endpoint: &str) -> bool {
|
pub fn is_valid_endpoint(endpoint: &str) -> bool {
|
||||||
|
for (name, _func) in &ENDPOINTS {
|
||||||
|
if endpoint.eq(*name) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
false
|
false
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn endpoint(endpoint: &str, input: json::object::Object) {}
|
pub fn endpoint(endpoint: &str, input: json::JsonValue) -> json::JsonValue {
|
||||||
|
for (name, func) in &ENDPOINTS {
|
||||||
|
if endpoint.eq(*name) {
|
||||||
|
return func(input);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
panic!("invalid endpoint, check with is_valid_endpoint")
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn echo(input: json::JsonValue) -> json::JsonValue {
|
||||||
|
input
|
||||||
|
}
|
||||||
|
@ -5,7 +5,6 @@ pub fn connection_handler(client: &mut http::HttpStream, req: &http::Request) {
|
|||||||
let mut res = http::Response::new();
|
let mut res = http::Response::new();
|
||||||
|
|
||||||
if let http::Method::GET = req.method {
|
if let http::Method::GET = req.method {
|
||||||
res.status(501);
|
|
||||||
} else {
|
} else {
|
||||||
res.status(405);
|
res.status(405);
|
||||||
res.add_header("Allow", "GET");
|
res.add_header("Allow", "GET");
|
||||||
@ -13,5 +12,7 @@ pub fn connection_handler(client: &mut http::HttpStream, req: &http::Request) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO implement websocket
|
||||||
|
res.status(501);
|
||||||
res.send(&mut client.stream);
|
res.send(&mut client.stream);
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user