1use crate::response::Error;
2use bytes::{BufMut, Bytes, BytesMut};
3use serde_json::Value;
4
5pub fn split_bytes(bytes: Bytes) -> Result<Value, Error> {
6 let mut vec: Vec<u8> = Vec::new();
7
8 for (i, v) in (&bytes).iter().enumerate() {
9 if v == &13 || v == &10 {
10 vec.push(v.clone())
11 } else {
12 if vec == [13, 10, 13, 10] {
13 let b = Bytes::from(&bytes[i..]);
14 return serde_json::from_slice(&b[..]).or(Err(Error::ParseError(None)));
15 } else {
16 if vec.len() > 0 {
17 vec.clear();
18 }
19 }
20 }
21 }
22 println!("!!!!!! error parse");
23
24 return Err(Error::ParseError(None));
25}
26
27pub fn generate_request_headers(host: String, length: usize) -> BytesMut {
28 let mut headers = BytesMut::with_capacity(length + 200);
29 headers.put("POST / HTTP/1.1\r\n");
30 headers.put("Host: ");
31 headers.put(host);
32 headers.put("\r\n");
33 headers.put("Content-Type: application/json\r\n");
34 headers.put("Content-Length: ");
35 headers.put(length.to_string());
36 headers.put("\r\n\r\n");
37 headers
38}
39
40pub fn generate_response_headers(length: usize) -> BytesMut {
41 let mut headers = BytesMut::with_capacity(length + 200);
42 headers.put("HTTP/1.1 200 OK\r\n");
43 headers.put("Server: Tachion JSON-RPC\r\n");
44 headers.put("Content-Type: application/json\r\n");
45 headers.put("Connection: Closed\r\n");
46 headers.put("Content-Length: ");
47 headers.put(length.to_string());
48 headers.put("\r\n\r\n");
49 headers
50}