common.rs 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /* This file is part of DarkFi (https://dark.fi)
  2. *
  3. * Copyright (C) 2020-2025 Dyne.org foundation
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU Affero General Public License as
  7. * published by the Free Software Foundation, either version 3 of the
  8. * License, or (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU Affero General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Affero General Public License
  16. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. */
  18. use std::{io, time::Duration};
  19. use log::error;
  20. use smol::io::{AsyncReadExt, AsyncWriteExt, BufReader, ReadHalf, WriteHalf};
  21. use super::jsonrpc::*;
  22. use crate::net::transport::PtStream;
  23. pub(super) const INIT_BUF_SIZE: usize = 4096; // 4K
  24. pub(super) const MAX_BUF_SIZE: usize = 1024 * 1024 * 16; // 16M
  25. pub(super) const READ_TIMEOUT: Duration = Duration::from_secs(30);
  26. /// Internal read function that reads from the active stream into a buffer.
  27. /// Performs HTTP POST request parsing. Returns the request body length.
  28. pub(super) async fn http_read_from_stream_request(
  29. reader: &mut BufReader<ReadHalf<Box<dyn PtStream>>>,
  30. buf: &mut Vec<u8>,
  31. ) -> io::Result<usize> {
  32. let mut total_read = 0;
  33. // Intermediate buffer we use to read byte-by-byte.
  34. let mut tmpbuf = [0_u8];
  35. while total_read < MAX_BUF_SIZE {
  36. buf.resize(total_read + INIT_BUF_SIZE, 0u8);
  37. match reader.read(&mut tmpbuf).await {
  38. Ok(0) if total_read == 0 => return Err(io::ErrorKind::ConnectionAborted.into()),
  39. Ok(0) => break, // Finished reading
  40. Ok(_) => {
  41. // Copy the read byte to the destination buffer.
  42. buf[total_read] = tmpbuf[0];
  43. total_read += 1;
  44. // In HTTP, when we reach '\r\n\r\n' we know we've read the headers.
  45. // The rest is the body. Headers should contain Content-Length which
  46. // tells us the remaining amount of bytes to read.
  47. if total_read > 4 && buf[total_read - 4..total_read] == [b'\r', b'\n', b'\r', b'\n']
  48. {
  49. break
  50. }
  51. }
  52. Err(e) => return Err(e),
  53. }
  54. }
  55. // Here we parse the HTTP for correctness and find Content-Length
  56. let mut headers = [httparse::EMPTY_HEADER; 8];
  57. let mut req = httparse::Request::new(&mut headers);
  58. let _body_offset = match req.parse(buf) {
  59. Ok(v) => v.unwrap(), // TODO: This should check httparse::Status::is_partial()
  60. Err(e) => {
  61. error!("[RPC] Failed parsing HTTP request: {e}");
  62. return Err(io::ErrorKind::InvalidData.into())
  63. }
  64. };
  65. let mut content_length: usize = 0;
  66. for header in headers {
  67. if header.name.to_lowercase() == "content-length" {
  68. let s = String::from_utf8_lossy(header.value);
  69. content_length = match s.parse() {
  70. Ok(v) => v,
  71. Err(_) => return Err(io::ErrorKind::InvalidData.into()),
  72. };
  73. }
  74. }
  75. if content_length == 0 || content_length > MAX_BUF_SIZE {
  76. return Err(io::ErrorKind::InvalidData.into())
  77. }
  78. // Now we know the request body size. Read it into the buffer.
  79. buf.clear();
  80. buf.resize(content_length, 0_u8);
  81. reader.read(buf).await?;
  82. assert!(buf.len() == content_length);
  83. Ok(content_length)
  84. }
  85. /// Internal read function that reads from the active stream into a buffer.
  86. /// Performs HTTP POST response parsing. Returns the response body length.
  87. pub(super) async fn http_read_from_stream_response(
  88. reader: &mut BufReader<ReadHalf<Box<dyn PtStream>>>,
  89. buf: &mut Vec<u8>,
  90. ) -> io::Result<usize> {
  91. let mut total_read = 0;
  92. // Intermediate buffer we use to read byte-by-byte.
  93. let mut tmpbuf = [0_u8];
  94. while total_read < MAX_BUF_SIZE {
  95. buf.resize(total_read + INIT_BUF_SIZE, 0u8);
  96. match reader.read(&mut tmpbuf).await {
  97. Ok(0) if total_read == 0 => return Err(io::ErrorKind::ConnectionAborted.into()),
  98. Ok(0) => break, // Finished reading
  99. Ok(_) => {
  100. // Copy the read byte to the destination buffer.
  101. buf[total_read] = tmpbuf[0];
  102. total_read += 1;
  103. // In HTTP, when we reach '\r\n\r\n' we know we've read the headers.
  104. // The rest is the body. Headers should contain Content-Length which
  105. // tells us the remaining amount of bytes to read.
  106. if total_read > 4 && buf[total_read - 4..total_read] == [b'\r', b'\n', b'\r', b'\n']
  107. {
  108. break
  109. }
  110. }
  111. Err(e) => return Err(e),
  112. }
  113. }
  114. // Here we parse the HTTP for correctness and find Content-Length
  115. let mut headers = [httparse::EMPTY_HEADER; 8];
  116. let mut resp = httparse::Response::new(&mut headers);
  117. let _body_offset = match resp.parse(buf) {
  118. Ok(v) => v.unwrap(), // TODO: This should check httparse::Status::is_partial()
  119. Err(e) => {
  120. error!("[RPC] Failed parsing HTTP response: {e}");
  121. return Err(io::ErrorKind::InvalidData.into())
  122. }
  123. };
  124. let mut content_length: usize = 0;
  125. for header in headers {
  126. if header.name.to_lowercase() == "content-length" {
  127. let s = String::from_utf8_lossy(header.value);
  128. content_length = match s.parse() {
  129. Ok(v) => v,
  130. Err(_) => return Err(io::ErrorKind::InvalidData.into()),
  131. };
  132. }
  133. }
  134. if content_length == 0 || content_length > MAX_BUF_SIZE {
  135. return Err(io::ErrorKind::InvalidData.into())
  136. }
  137. // Now we know the response body size. Read it into the buffer.
  138. buf.clear();
  139. buf.resize(content_length, 0_u8);
  140. reader.read(buf).await?;
  141. assert!(buf.len() == content_length);
  142. Ok(content_length)
  143. }
  144. /// Internal read function that reads from the active stream into a buffer.
  145. /// Reading stops upon reaching CRLF or LF, or when `MAX_BUF_SIZE` is reached.
  146. pub(super) async fn read_from_stream(
  147. reader: &mut BufReader<ReadHalf<Box<dyn PtStream>>>,
  148. buf: &mut Vec<u8>,
  149. ) -> io::Result<usize> {
  150. let mut total_read = 0;
  151. // Intermediate buffer we use to read byte-by-byte.
  152. let mut tmpbuf = [0_u8];
  153. while total_read < MAX_BUF_SIZE {
  154. buf.resize(total_read + INIT_BUF_SIZE, 0u8);
  155. match reader.read(&mut tmpbuf).await {
  156. Ok(0) if total_read == 0 => return Err(io::ErrorKind::ConnectionAborted.into()),
  157. Ok(0) => break, // Finished reading
  158. Ok(_) => {
  159. // When we reach '\n', pop a possible '\r' from the buffer and bail.
  160. if tmpbuf[0] == b'\n' {
  161. if buf[total_read - 1] == b'\r' {
  162. buf.pop();
  163. total_read -= 1;
  164. }
  165. break
  166. }
  167. // Copy the read byte to the destination buffer.
  168. buf[total_read] = tmpbuf[0];
  169. total_read += 1;
  170. }
  171. Err(e) => return Err(e),
  172. }
  173. }
  174. // Truncate buffer to actual data size
  175. buf.truncate(total_read);
  176. Ok(total_read)
  177. }
  178. /// Internal write function that writes a JSON-RPC object to the active stream.
  179. /// Sent as an HTTP response.
  180. pub(super) async fn http_write_to_stream(
  181. writer: &mut WriteHalf<Box<dyn PtStream>>,
  182. object: &JsonResult,
  183. ) -> io::Result<()> {
  184. let (status_line, object_str) = match object {
  185. JsonResult::Notification(v) => ("HTTP/1.1 200 OK", v.stringify().unwrap()),
  186. JsonResult::Response(v) => ("HTTP/1.1 200 OK", v.stringify().unwrap()),
  187. JsonResult::Error(v) => ("HTTP/1.1 400 Bad Request", v.stringify().unwrap()),
  188. JsonResult::Request(v) => ("POST /json_rpc HTTP/1.1", v.stringify().unwrap()),
  189. _ => unreachable!(),
  190. };
  191. let length = object_str.len();
  192. let data = format!("{status_line}\r\nContent-Length: {length}\r\nContent-Type: application/json\r\n\r\n{object_str}");
  193. writer.write_all(data.as_bytes()).await?;
  194. writer.flush().await?;
  195. Ok(())
  196. }
  197. /// Internal write function that writes a JSON-RPC object to the active stream.
  198. pub(super) async fn write_to_stream(
  199. writer: &mut WriteHalf<Box<dyn PtStream>>,
  200. object: &JsonResult,
  201. ) -> io::Result<()> {
  202. let object_str = match object {
  203. JsonResult::Notification(v) => v.stringify().unwrap(),
  204. JsonResult::Response(v) => v.stringify().unwrap(),
  205. JsonResult::Error(v) => v.stringify().unwrap(),
  206. JsonResult::Request(v) => v.stringify().unwrap(),
  207. _ => unreachable!(),
  208. };
  209. // As we're a line-based protocol, we append CRLF to the end of the JSON string.
  210. for i in [object_str.as_bytes(), b"\r\n"] {
  211. writer.write_all(i).await?
  212. }
  213. writer.flush().await?;
  214. Ok(())
  215. }