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(path: String, host: String, length: usize) -> BytesMut {
28 let mut headers = BytesMut::with_capacity(length + 200);
29 headers.put("POST ");
30 headers.put(path);
31 headers.put(" HTTP/1.1\r\n");
32 headers.put("Host: ");
33 headers.put(host);
34 headers.put("\r\n");
35 headers.put("Content-Type: application/json\r\n");
36 headers.put("Content-Length: ");
37 headers.put(length.to_string());
38 headers.put("\r\n\r\n");
39 headers
40}
41
42pub fn generate_response_headers(length: usize) -> BytesMut {
43 let mut headers = BytesMut::with_capacity(length + 200);
44 headers.put("HTTP/1.1 200 OK\r\n");
45 headers.put("Server: JSON-RPC\r\n");
46 headers.put("Content-Type: application/json\r\n");
47 headers.put("Connection: Closed\r\n");
48 headers.put("Content-Length: ");
49 headers.put(length.to_string());
50 headers.put("\r\n\r\n");
51 headers
52}