Using serde_json
This commit is contained in:
@ -1,10 +1,7 @@
|
||||
use super::Method;
|
||||
use crate::usimp;
|
||||
use crate::websocket;
|
||||
use chrono;
|
||||
use json;
|
||||
use std::borrow::Borrow;
|
||||
use std::sync::{Arc, Mutex};
|
||||
use serde_json;
|
||||
|
||||
pub struct HttpStream {
|
||||
pub stream: super::Stream,
|
||||
@ -53,17 +50,12 @@ fn request_handler(client: &mut super::HttpStream) {
|
||||
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),
|
||||
[endpoint] => match req.method {
|
||||
Method::POST => return endpoint_handler(client, &req, &mut res, endpoint),
|
||||
_ => {
|
||||
res.status(405);
|
||||
res.add_header("Allow", "POST");
|
||||
}
|
||||
},
|
||||
_ => res.status(400),
|
||||
}
|
||||
@ -137,16 +129,7 @@ fn endpoint_handler(
|
||||
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,
|
||||
)
|
||||
}
|
||||
}) {
|
||||
let input = match serde_json::from_slice(&buf[..length]) {
|
||||
Ok(val) => val,
|
||||
Err(e) => {
|
||||
return error(
|
||||
@ -156,10 +139,13 @@ fn endpoint_handler(
|
||||
)
|
||||
}
|
||||
};
|
||||
let output = usimp::endpoint(endpoint, input);
|
||||
|
||||
let buf = match usimp::endpoint(endpoint, input) {
|
||||
Ok(output) => output.to_string() + "\r\n",
|
||||
Err(e) => "{\"status\":\"error\"}\r\n".to_string(),
|
||||
};
|
||||
|
||||
// TODO compress
|
||||
let buf = json::stringify(output) + "\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");
|
||||
|
Reference in New Issue
Block a user