common.rs 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. /* This file is part of DarkFi (https://dark.fi)
  2. *
  3. * Copyright (C) 2020-2026 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 smol::io::{AsyncReadExt, AsyncWriteExt, BufReader, ReadHalf, WriteHalf};
  20. use tracing::error;
  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\n\r\n" {
  48. break
  49. }
  50. }
  51. Err(e) => return Err(e),
  52. }
  53. }
  54. // Here we parse the HTTP for correctness and find Content-Length
  55. let mut headers = [httparse::EMPTY_HEADER; 8];
  56. let mut req = httparse::Request::new(&mut headers);
  57. match req.parse(buf) {
  58. Ok(httparse::Status::Complete(_)) => { /* Do nothing */ }
  59. Ok(httparse::Status::Partial) => {
  60. error!("[RPC] Failed parsing HTTP request: partial headers");
  61. return Err(io::ErrorKind::InvalidData.into())
  62. }
  63. Err(e) => {
  64. error!("[RPC] Failed parsing HTTP request: {e}");
  65. return Err(io::ErrorKind::InvalidData.into())
  66. }
  67. }
  68. let mut content_length: usize = 0;
  69. for header in headers {
  70. if header.name.to_lowercase() == "content-length" {
  71. let s = String::from_utf8_lossy(header.value);
  72. content_length = match s.parse() {
  73. Ok(v) => v,
  74. Err(_) => return Err(io::ErrorKind::InvalidData.into()),
  75. };
  76. }
  77. }
  78. if content_length == 0 || content_length > MAX_BUF_SIZE {
  79. return Err(io::ErrorKind::InvalidData.into())
  80. }
  81. // Now we know the request body size. Read it into the buffer.
  82. buf.clear();
  83. buf.resize(content_length, 0_u8);
  84. reader.read_exact(buf).await?;
  85. Ok(content_length)
  86. }
  87. /// Internal read function that reads from the active stream into a buffer.
  88. /// Performs HTTP POST response parsing. Returns the response body length.
  89. pub(super) async fn http_read_from_stream_response(
  90. reader: &mut BufReader<ReadHalf<Box<dyn PtStream>>>,
  91. buf: &mut Vec<u8>,
  92. ) -> io::Result<usize> {
  93. let mut total_read = 0;
  94. // Intermediate buffer we use to read byte-by-byte.
  95. let mut tmpbuf = [0_u8];
  96. while total_read < MAX_BUF_SIZE {
  97. buf.resize(total_read + INIT_BUF_SIZE, 0u8);
  98. match reader.read(&mut tmpbuf).await {
  99. Ok(0) if total_read == 0 => return Err(io::ErrorKind::ConnectionAborted.into()),
  100. Ok(0) => break, // Finished reading
  101. Ok(_) => {
  102. // Copy the read byte to the destination buffer.
  103. buf[total_read] = tmpbuf[0];
  104. total_read += 1;
  105. // In HTTP, when we reach '\r\n\r\n' we know we've read the headers.
  106. // The rest is the body. Headers should contain Content-Length which
  107. // tells us the remaining amount of bytes to read.
  108. if total_read > 4 && buf[total_read - 4..total_read] == *b"\r\n\r\n" {
  109. break
  110. }
  111. }
  112. Err(e) => return Err(e),
  113. }
  114. }
  115. // Here we parse the HTTP for correctness and find Content-Length
  116. let mut headers = [httparse::EMPTY_HEADER; 8];
  117. let mut resp = httparse::Response::new(&mut headers);
  118. match resp.parse(buf) {
  119. Ok(httparse::Status::Complete(_)) => { /* Do nothing */ }
  120. Ok(httparse::Status::Partial) => {
  121. error!("[RPC] Failed parsing HTTP response: partial headers");
  122. return Err(io::ErrorKind::InvalidData.into())
  123. }
  124. Err(e) => {
  125. error!("[RPC] Failed parsing HTTP response: {e}");
  126. return Err(io::ErrorKind::InvalidData.into())
  127. }
  128. }
  129. let mut content_length: usize = 0;
  130. for header in headers {
  131. if header.name.to_lowercase() == "content-length" {
  132. let s = String::from_utf8_lossy(header.value);
  133. content_length = match s.parse() {
  134. Ok(v) => v,
  135. Err(_) => return Err(io::ErrorKind::InvalidData.into()),
  136. };
  137. }
  138. }
  139. if content_length == 0 || content_length > MAX_BUF_SIZE {
  140. return Err(io::ErrorKind::InvalidData.into())
  141. }
  142. // Now we know the response body size. Read it into the buffer.
  143. buf.clear();
  144. buf.resize(content_length, 0_u8);
  145. reader.read_exact(buf).await?;
  146. Ok(content_length)
  147. }
  148. /// Internal read function that reads from the active stream into a buffer.
  149. /// Reading stops upon reaching CRLF or LF, or when `MAX_BUF_SIZE` is reached.
  150. pub(super) async fn read_from_stream(
  151. reader: &mut BufReader<ReadHalf<Box<dyn PtStream>>>,
  152. buf: &mut Vec<u8>,
  153. ) -> io::Result<usize> {
  154. let mut total_read = 0;
  155. // Intermediate buffer we use to read byte-by-byte.
  156. let mut tmpbuf = [0_u8];
  157. while total_read < MAX_BUF_SIZE {
  158. buf.resize(total_read + INIT_BUF_SIZE, 0u8);
  159. match reader.read(&mut tmpbuf).await {
  160. Ok(0) if total_read == 0 => return Err(io::ErrorKind::ConnectionAborted.into()),
  161. Ok(0) => break, // Finished reading
  162. Ok(_) => {
  163. // When we reach '\n', pop a possible '\r' from the buffer and bail.
  164. if tmpbuf[0] == b'\n' {
  165. if total_read > 0 && buf[total_read - 1] == b'\r' {
  166. buf.pop();
  167. total_read -= 1;
  168. }
  169. buf.truncate(total_read);
  170. return Ok(total_read)
  171. }
  172. // Copy the read byte to the destination buffer.
  173. buf[total_read] = tmpbuf[0];
  174. total_read += 1;
  175. }
  176. Err(e) => return Err(e),
  177. }
  178. }
  179. // Loop exited without finding \n.
  180. if total_read >= MAX_BUF_SIZE {
  181. return Err(io::Error::new(
  182. io::ErrorKind::InvalidData,
  183. "line exceeded maximum buf size without terminator",
  184. ))
  185. }
  186. // Truncate buffer to actual data size
  187. buf.truncate(total_read);
  188. Ok(total_read)
  189. }
  190. /// Internal write function that writes a JSON-RPC object to the active stream.
  191. /// Sent as an HTTP response.
  192. pub(super) async fn http_write_to_stream(
  193. writer: &mut WriteHalf<Box<dyn PtStream>>,
  194. object: &JsonResult,
  195. ) -> io::Result<()> {
  196. let (status_line, object_str) = match object {
  197. JsonResult::Notification(v) => ("HTTP/1.1 200 OK", v.stringify().unwrap()),
  198. JsonResult::Response(v) => ("HTTP/1.1 200 OK", v.stringify().unwrap()),
  199. JsonResult::Error(v) => ("HTTP/1.1 400 Bad Request", v.stringify().unwrap()),
  200. JsonResult::Request(v) => ("POST /json_rpc HTTP/1.1", v.stringify().unwrap()),
  201. _ => unreachable!(),
  202. };
  203. let length = object_str.len();
  204. let data = format!("{status_line}\r\nContent-Length: {length}\r\nContent-Type: application/json\r\n\r\n{object_str}");
  205. writer.write_all(data.as_bytes()).await?;
  206. writer.flush().await?;
  207. Ok(())
  208. }
  209. /// Internal write function that writes a JSON-RPC object to the active stream.
  210. pub(super) async fn write_to_stream(
  211. writer: &mut WriteHalf<Box<dyn PtStream>>,
  212. object: &JsonResult,
  213. ) -> io::Result<()> {
  214. let object_str = match object {
  215. JsonResult::Notification(v) => v.stringify().unwrap(),
  216. JsonResult::Response(v) => v.stringify().unwrap(),
  217. JsonResult::Error(v) => v.stringify().unwrap(),
  218. JsonResult::Request(v) => v.stringify().unwrap(),
  219. _ => unreachable!(),
  220. };
  221. // As we're a line-based protocol, we append CRLF to the end of the JSON string.
  222. for i in [object_str.as_bytes(), b"\r\n"] {
  223. writer.write_all(i).await?
  224. }
  225. writer.flush().await?;
  226. Ok(())
  227. }