{
+ if a.contains("text/html") {
+ return Ok(Response::new(
+ 200,
+ "text/html",
+ fs::read("static/200.html").unwrap_or("NOT FOUND".into()),
+ ));
+ } else if a.contains("application/json") {
+ let j = json!({ "msg": "OK", "status": 200 });
+ return Ok(Response::new(
+ 200,
+ "application/json",
+ serde_json::to_vec(&j)?,
+ ));
+ }
+
+ Ok(Response::new(200, "text", "Success".into()))
+ }
+
+ match status {
+ HTTPResponse::OK(content_opt) => {
+ if let Some(content) = content_opt {
+ response_ok_content(content)
+ } else {
+ response_ok_no_content(accept.unwrap_or("text"))
+ }
+ }
+ HTTPResponse::InvalidMethod => {
+ if accept.unwrap_or("").contains("text/html") {
+ Ok(Response::new(
+ 405,
+ "text/html",
+ fs::read("static/405.html")
+ .unwrap_or_else(|_| b"405 Invalid Method".to_vec()),
+ ))
+ } else {
+ Ok(Response::new(
+ 405,
+ "text/plain",
+ b"405 Invalid Method".to_vec(),
+ ))
+ }
+ }
+ HTTPResponse::BadRequest => {
+ if accept.unwrap_or("").contains("text/html") {
+ Ok(Response::new(
+ 400,
+ "text/html",
+ fs::read("static/400.html").unwrap_or_else(|_| b"400 Bad Request".to_vec()),
+ ))
+ } else {
+ Ok(Response::new(
+ 400,
+ "text/plain",
+ b"400 Bad Request".to_vec(),
+ ))
+ }
+ }
+ HTTPResponse::InternalServerError => {
+ if accept.unwrap_or("").contains("text/html") {
+ Ok(Response::new(
+ 500,
+ "text/html",
+ fs::read("static/500.html")
+ .unwrap_or_else(|_| b"500 Internal Server Error".to_vec()),
+ ))
+ } else {
+ Ok(Response::new(
+ 500,
+ "text/plain",
+ b"500 Internal Server Error".to_vec(),
+ ))
+ }
+ }
+ }
+ }
+
+ pub fn response(&mut self, status: HTTPResponse) {
+ let accept = self.headers.get("Accept").map(|s| s.as_str());
+ let resp = Self::make_response(status, accept).unwrap();
+ if let Err(e) = self.stream.as_mut().unwrap().write_all(&resp.to_bytes()) {
+ eprintln!("Error writing response to stream: {}", e);
+ }
+ }
+}
+
+impl fmt::Display for HTTPRequest {
+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+ // Write the HTTP method and version
+ writeln!(f, "Method: {}", self.method)?;
+ writeln!(f, "HTTP Version: {}", self.http_version)?;
+
+ // Write the stream status
+ writeln!(
+ f,
+ "Stream: {}",
+ if self.stream.is_some() {
+ "Connected"
+ } else {
+ "Disconnected"
+ }
+ )?;
+
+ // Write the headers
+ writeln!(f, "Headers:")?;
+ if self.headers.is_empty() {
+ writeln!(f, " (none)")?;
+ } else {
+ for (key, value) in &self.headers {
+ writeln!(f, " {}: {}", key, value)?;
+ }
+ }
+
+ Ok(())
+ }
+}
+#[derive(Debug)]
+pub enum HTTPParseError {
+ InvalidStatusLine,
+ InvalidRequestLine,
+ // MissingFields,
+ MissingContentLength,
+}
+impl fmt::Display for HTTPParseError {
+ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+ match self {
+ HTTPParseError::InvalidRequestLine => write!(f, "Invalid request line"),
+ // HTTPParseError::MissingFields => write!(f, "Missing required fields"),
+ HTTPParseError::InvalidStatusLine => write!(f, "Invalid status line"),
+ HTTPParseError::MissingContentLength => {
+ write!(f, "Missing content-length field in headers")
+ }
+ }
+ }
+}
+impl std::error::Error for HTTPParseError {}
diff --git a/src/node/resolve_requests/mod.rs b/src/node/resolve_requests/mod.rs
new file mode 100644
index 0000000..b4cdc8f
--- /dev/null
+++ b/src/node/resolve_requests/mod.rs
@@ -0,0 +1,4 @@
+pub mod endpoints;
+pub mod errors;
+pub mod helpers;
+pub mod methods;
diff --git a/src/node/static/200.html b/src/node/static/200.html
new file mode 100644
index 0000000..3382bd5
--- /dev/null
+++ b/src/node/static/200.html
@@ -0,0 +1,26 @@
+
+
+
+
+ Success
+
+
+
+ Success!
+ Your request was completed successfully.
+
+
diff --git a/src/node/static/400.html b/src/node/static/400.html
new file mode 100644
index 0000000..cc7e866
--- /dev/null
+++ b/src/node/static/400.html
@@ -0,0 +1,26 @@
+
+
+
+
+ 400 Bad Request
+
+
+
+ 400 - Bad Request
+ The server couldn't understand your request. Please check the syntax and try again.
+
+
diff --git a/src/node/static/404.html b/src/node/static/404.html
new file mode 100644
index 0000000..956761d
--- /dev/null
+++ b/src/node/static/404.html
@@ -0,0 +1,26 @@
+
+
+
+
+ 404 Not Found
+
+
+
+ 404 - Page Not Found
+ The page you're looking for doesn't exist or has been moved.
+
+
diff --git a/src/node/static/405.html b/src/node/static/405.html
new file mode 100644
index 0000000..e4348b5
--- /dev/null
+++ b/src/node/static/405.html
@@ -0,0 +1,26 @@
+
+
+
+
+ 405 Method Not Allowed
+
+
+
+ 405 - Method Not Allowed
+ The method used in the request is not supported for this resource.
+
+
diff --git a/src/node/static/500.html b/src/node/static/500.html
new file mode 100644
index 0000000..a043f66
--- /dev/null
+++ b/src/node/static/500.html
@@ -0,0 +1,26 @@
+
+
+
+
+ 500 Internal Server Error
+
+
+
+500 - Internal Server Error
+Oops! Something went wrong on the server.
+
+
diff --git a/src/node/static/fav.ico b/src/node/static/fav.ico
new file mode 100644
index 0000000..29543c5
Binary files /dev/null and b/src/node/static/fav.ico differ
diff --git a/src/node/static/fav.png b/src/node/static/fav.png
new file mode 100644
index 0000000..b36ffd2
Binary files /dev/null and b/src/node/static/fav.png differ
diff --git a/src/node/static/index.html b/src/node/static/index.html
new file mode 100644
index 0000000..9550a9b
--- /dev/null
+++ b/src/node/static/index.html
@@ -0,0 +1,87 @@
+
+
+
+
+
+ Cryptocoin Node Dashboard
+
+
+
+
+
Cryptocoin Node Dashboard
+