-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
Showing
22 changed files
with
1,721 additions
and
674 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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?) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
Oops, something went wrong.