refactor(backend): improve error handling with thiserror and secure file deletion
This commit is contained in:
@@ -1,22 +1,17 @@
|
||||
use bytes::Bytes;
|
||||
use std::collections::HashMap;
|
||||
use thiserror::Error;
|
||||
use tokio::io::{AsyncReadExt, AsyncWriteExt};
|
||||
use tokio::net::UnixStream;
|
||||
|
||||
#[derive(Debug)]
|
||||
#[derive(Error, Debug)]
|
||||
pub enum ScgiError {
|
||||
#[allow(dead_code)]
|
||||
Io(std::io::Error),
|
||||
#[allow(dead_code)]
|
||||
#[error("IO Error: {0}")]
|
||||
Io(#[from] std::io::Error),
|
||||
#[error("Protocol Error: {0}")]
|
||||
Protocol(String),
|
||||
}
|
||||
|
||||
impl From<std::io::Error> for ScgiError {
|
||||
fn from(err: std::io::Error) -> Self {
|
||||
ScgiError::Io(err)
|
||||
}
|
||||
}
|
||||
|
||||
pub struct ScgiRequest {
|
||||
headers: HashMap<String, String>,
|
||||
body: Vec<u8>,
|
||||
@@ -46,23 +41,18 @@ impl ScgiRequest {
|
||||
|
||||
pub fn encode(&self) -> Vec<u8> {
|
||||
let mut headers_data = Vec::new();
|
||||
|
||||
// SCGI Spec: The first header must be "CONTENT_LENGTH"
|
||||
// The second header must be "SCGI" with value "1"
|
||||
|
||||
// We handle CONTENT_LENGTH and SCGI explicitly first
|
||||
|
||||
let content_len = self.body.len().to_string();
|
||||
headers_data.extend_from_slice(b"CONTENT_LENGTH");
|
||||
headers_data.push(0);
|
||||
headers_data.extend_from_slice(content_len.as_bytes());
|
||||
headers_data.push(0);
|
||||
|
||||
|
||||
headers_data.extend_from_slice(b"SCGI");
|
||||
headers_data.push(0);
|
||||
headers_data.extend_from_slice(b"1");
|
||||
headers_data.push(0);
|
||||
|
||||
// Add remaining headers (excluding the ones we just added if they exist in the map)
|
||||
for (k, v) in &self.headers {
|
||||
if k == "CONTENT_LENGTH" || k == "SCGI" {
|
||||
continue;
|
||||
@@ -86,10 +76,7 @@ impl ScgiRequest {
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn send_request(
|
||||
socket_path: &str,
|
||||
request: ScgiRequest,
|
||||
) -> Result<Bytes, ScgiError> {
|
||||
pub async fn send_request(socket_path: &str, request: ScgiRequest) -> Result<Bytes, ScgiError> {
|
||||
let mut stream = UnixStream::connect(socket_path).await?;
|
||||
let data = request.encode();
|
||||
stream.write_all(&data).await?;
|
||||
@@ -97,9 +84,6 @@ pub async fn send_request(
|
||||
let mut response = Vec::new();
|
||||
stream.read_to_end(&mut response).await?;
|
||||
|
||||
// The response is usually HTTP-like: headers\r\n\r\nbody
|
||||
// We strictly want the body for XML-RPC
|
||||
// Find double newline
|
||||
let double_newline = b"\r\n\r\n";
|
||||
if let Some(pos) = response
|
||||
.windows(double_newline.len())
|
||||
@@ -107,40 +91,6 @@ pub async fn send_request(
|
||||
{
|
||||
Ok(Bytes::from(response.split_off(pos + double_newline.len())))
|
||||
} else {
|
||||
// Fallback: rTorrent sometimes sends raw XML without headers if configured poorly,
|
||||
// but SCGI usually implies headers.
|
||||
// If we don't find headers, maybe it's all body?
|
||||
// But usually there's at least "Status: 200 OK"
|
||||
// Let's return everything if we can't find the split, or error.
|
||||
// For now, assume everything is body if no headers found might be unsafe,
|
||||
// but valid for simple XML-RPC dumping.
|
||||
Ok(Bytes::from(response))
|
||||
Ok(Bytes::from(response))
|
||||
}
|
||||
}
|
||||
pub async fn system_call(
|
||||
socket_path: &str,
|
||||
method: &str,
|
||||
params: Vec<&str>,
|
||||
) -> Result<String, ScgiError> {
|
||||
// Construct XML-RPC payload manually for simplicity
|
||||
// <methodCall><methodName>method</methodName><params><param><value><string>val</string></value></param>...</params></methodCall>
|
||||
let mut xml = String::from("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
|
||||
xml.push_str(&format!("<methodCall><methodName>{}</methodName><params>", method));
|
||||
for param in params {
|
||||
// Use CDATA for safety with special chars in magnet links
|
||||
xml.push_str(&format!("<param><value><string><![CDATA[{}]]></string></value></param>", param));
|
||||
}
|
||||
xml.push_str("</params></methodCall>");
|
||||
|
||||
tracing::debug!("Sending XML-RPC Payload: {}", xml);
|
||||
|
||||
let req = ScgiRequest::new().body(xml.clone().into_bytes());
|
||||
let response_bytes = send_request(socket_path, req).await?;
|
||||
let response_str = String::from_utf8_lossy(&response_bytes).to_string();
|
||||
|
||||
// Ideally parse the response, but for actions we just check if it executed without SCGI error
|
||||
// rTorrent usually returns <value><i8>0</i8></value> for success or fault.
|
||||
// For now, returning the raw string is fine for debugging/logging in main.
|
||||
|
||||
Ok(response_str)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user