Skip to content

Commit

Permalink
Rework xapi-rs crate
Browse files Browse the repository at this point in the history
Reorganize the library
Port to hyper 1.x
Add support for stunnel requests.
Add basic (incomplete) support for SMAPIv3.

Signed-off-by: Teddy Astie <[email protected]>
  • Loading branch information
TSnake41 committed Jan 2, 2025
1 parent b1c8cf0 commit 8f34566
Show file tree
Hide file tree
Showing 22 changed files with 1,721 additions and 674 deletions.
1,152 changes: 1,053 additions & 99 deletions Cargo.lock

Large diffs are not rendered by default.

31 changes: 27 additions & 4 deletions xapi-rs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,23 @@ edition = "2021"
[dependencies]
serde_json = "1.0"
anyhow = "1.0"
hyperlocal = "0.8"
jsonrpc-base = "0.2.0"

http = "1.1"
http-body = "1.0"
http-body-util = "0.1"

[dependencies.serde]
version = "1.0"
features = ["std", "derive"]

[dependencies.uuid]
version = "1.4"
version = "1.10"
features = ["std", "serde"]

# XML-RPC stuff
[dependencies.dxr]
version = "0.6"
version = "0.6.3"
features = ["derive"]
default-features = false # no client/server stuff

Expand All @@ -32,5 +35,25 @@ version = "0.30"
features = ["serialize"]

[dependencies.hyper]
version = "0.14"
version = "1.4"
features = ["full"]
optional = true

[dependencies.hyper-util]
version = "0.1"
features = ["full"]
optional = true

[dependencies.tokio]
version = "1"
features = ["io-util", "net"]
optional = true

[dependencies.reqwest]
version = "0.12"
optional = true

[features]
default = []
http = ["reqwest"]
unix = ["tokio", "hyper", "hyper-util"]
25 changes: 25 additions & 0 deletions xapi-rs/src/http.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
use std::str::FromStr;

use crate::rpc::{
message::{request::RpcRequest, RpcKind},
XcpRpcMethod,
};
use reqwest::{Client, Method, Url};

/// Send a RPC request to the module `name`.
pub async fn send_rpc_to<M: XcpRpcMethod>(
host: Url,
http_method: &str,
rpc_method: &M,
user_agent: &str,
kind: RpcKind,
) -> anyhow::Result<reqwest::Response> {
Ok(Client::builder()
.user_agent(user_agent)
.build()?
.request(Method::from_str(http_method)?, host)
.header("content-type", kind.to_mime())
.body(RpcRequest::new(rpc_method, kind)?.to_body()?)
.send()
.await?)
}
90 changes: 4 additions & 86 deletions xapi-rs/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,90 +1,8 @@
//! XAPI utilities
pub mod rpc;
pub(crate) mod utils;

use std::path::{Path, PathBuf};
#[cfg(feature = "unix")]
pub mod unix;

use crate::rpc::{message::RpcKind, write_method_jsonrpc, write_method_xmlrpc, XcpRpcMethod};
use hyper::{body, Body, Request, Response};

pub const XAPI_SOCKET_PATH: &str = "/var/lib/xcp";
pub const METRICS_SHM_PATH: &str = "/dev/shm/metrics/";

pub use hyper;
pub use hyperlocal;

/// Get the path of the socket of some module.
pub fn get_module_path(name: &str) -> PathBuf {
Path::new(XAPI_SOCKET_PATH).join(name)
}

/// Send a XML-RPC request to the module `name`.
pub async fn send_rpc_to<M: XcpRpcMethod>(
xapi_daemon_path: &Path,
http_method: &str,
rpc_method: &M,
user_agent: &str,
kind: RpcKind,
) -> anyhow::Result<Response<Body>> {
let module_uri = hyperlocal::Uri::new(xapi_daemon_path, "/");

let mut rpc = vec![];

match kind {
RpcKind::XmlRpc => write_method_xmlrpc(&mut rpc, rpc_method)?,
RpcKind::JsonRpc => write_method_jsonrpc(&mut rpc, rpc_method)?,
};

let content_type = match kind {
RpcKind::XmlRpc => "text/xml",
RpcKind::JsonRpc => "application/json-rpc",
};

let request = Request::builder()
.uri(hyper::Uri::from(module_uri))
.method(http_method)
.header("User-agent", user_agent)
.header("content-length", rpc.len())
.header("host", "localhost")
.header("content-type", content_type)
.body(body::Body::from(rpc))?;

Ok(hyper::Client::builder()
.build(hyperlocal::UnixConnector)
.request(request)
.await?)
}

/// Send a XML-RPC request to the module `name`.
pub async fn send_xmlrpc_to<M: XcpRpcMethod>(
xapi_daemon_path: &Path,
http_method: &str,
rpc_method: &M,
user_agent: &str,
) -> anyhow::Result<Response<Body>> {
send_rpc_to(
xapi_daemon_path,
http_method,
rpc_method,
user_agent,
RpcKind::XmlRpc,
)
.await
}

/// Send a JSON-RPC request to the module `name`.
pub async fn send_jsonrpc_to<M: XcpRpcMethod>(
xapi_daemon_path: &Path,
http_method: &str,
rpc_method: &M,
user_agent: &str,
) -> anyhow::Result<Response<Body>> {
send_rpc_to(
xapi_daemon_path,
http_method,
rpc_method,
user_agent,
RpcKind::JsonRpc,
)
.await
}
#[cfg(feature = "http")]
pub mod http;
Loading

0 comments on commit 8f34566

Please sign in to comment.