From 73d5d4721f6d3ca40905e34ec9cac31eb0f026ba Mon Sep 17 00:00:00 2001 From: Mayuki Sawatari Date: Tue, 17 Dec 2024 16:16:03 +0900 Subject: [PATCH] Fix for building on Windows --- native/yaha_native/Cargo.toml | 2 ++ native/yaha_native/src/binding.rs | 8 +++++++- native/yaha_native/src/context.rs | 31 +++++++++++++++++++++++++------ 3 files changed, 34 insertions(+), 7 deletions(-) diff --git a/native/yaha_native/Cargo.toml b/native/yaha_native/Cargo.toml index e612833..4e9244c 100644 --- a/native/yaha_native/Cargo.toml +++ b/native/yaha_native/Cargo.toml @@ -41,6 +41,8 @@ http-body-util = "0.1.1" tokio-stream = "0.1.15" futures-channel = "0.3.30" futures-util = "0.3.30" + +[target.'cfg(unix)'.dependencies] hyperlocal = "0.9.1" [features] diff --git a/native/yaha_native/src/binding.rs b/native/yaha_native/src/binding.rs index d1d7362..7eb8973 100644 --- a/native/yaha_native/src/binding.rs +++ b/native/yaha_native/src/binding.rs @@ -335,6 +335,7 @@ pub extern "C" fn yaha_client_config_http2_initial_max_send_streams( .http2_initial_max_send_streams(initial); } +#[cfg(unix)] #[no_mangle] pub extern "C" fn yaha_client_config_unix_domain_socket_path( ctx: *mut YahaNativeContext, @@ -509,7 +510,12 @@ pub extern "C" fn yaha_request_begin( (req_ctx.seq, builder.body(body).unwrap()) }; - if ctx.tcp_client.is_none() && ctx.uds_client.is_none() { + #[cfg(unix)] + let client_is_none = ctx.tcp_client.is_none() && ctx.uds_client.is_none(); + #[cfg(not(unix))] + let client_is_none = ctx.tcp_client.is_none(); + + if client_is_none { LAST_ERROR.with(|v| { *v.borrow_mut() = Some("The client has not been built. You need to build it before sending the request. ".to_string()); }); diff --git a/native/yaha_native/src/context.rs b/native/yaha_native/src/context.rs index 6742e1d..71d742f 100644 --- a/native/yaha_native/src/context.rs +++ b/native/yaha_native/src/context.rs @@ -2,7 +2,6 @@ use std::{ num::NonZeroIsize, sync::{Arc, Mutex}, time::Duration, - path::PathBuf, }; use futures_channel::mpsc::Sender; use http_body_util::combinators::BoxBody; @@ -24,7 +23,9 @@ use hyper_rustls::ConfigBuilderExt; use hyper_rustls::HttpsConnector; #[cfg(feature = "native")] use hyper_tls::HttpsConnector; +#[cfg(unix)] use hyperlocal::UnixConnector; + use rustls::pki_types::{CertificateDer, PrivateKeyDer}; use tokio_util::sync::CancellationToken; @@ -67,8 +68,11 @@ pub struct YahaNativeContextInternal<'a> { pub on_status_code_and_headers_receive: OnStatusCodeAndHeadersReceive, pub on_receive: OnReceive, pub on_complete: OnComplete, + + #[cfg(unix)] pub uds_client: Option>>, - pub uds_socket_path: Option, + #[cfg(unix)] + pub uds_socket_path: Option, } impl YahaNativeContextInternal<'_> { @@ -85,7 +89,6 @@ impl YahaNativeContextInternal<'_> { YahaNativeContextInternal { runtime: runtime_handle, tcp_client: None, - uds_client: None, client_builder: Some(Client::builder(TokioExecutor::new())), skip_certificate_verification: None, server_certificate_verification_handler: None, @@ -97,6 +100,9 @@ impl YahaNativeContextInternal<'_> { on_status_code_and_headers_receive, on_receive, on_complete, + #[cfg(unix)] + uds_client: None, + #[cfg(unix)] uds_socket_path: None, } } @@ -105,9 +111,17 @@ impl YahaNativeContextInternal<'_> { let mut builder = self.client_builder.take().unwrap(); builder.timer(TokioTimer::new()); - if self.uds_socket_path.is_some() { - self.uds_client = Some(builder.build(UnixConnector)); - } else { + #[cfg(unix)] + { + if self.uds_socket_path.is_some() { + self.uds_client = Some(builder.build(UnixConnector)); + } else { + let https = self.new_connector(); + self.tcp_client = Some(builder.build(https)); + } + } + #[cfg(not(unix))] + { let https = self.new_connector(); self.tcp_client = Some(builder.build(https)); } @@ -193,6 +207,7 @@ impl YahaNativeContextInternal<'_> { https } + #[cfg(unix)] pub fn request(&self, mut req: Request>) -> ResponseFuture { // Precondition (`uds_client` or `tcp_client` is set) ensured by `Self::build_client` and `yaha_request_begin` if let Some(uds_socket_path) = &self.uds_socket_path { @@ -210,6 +225,10 @@ impl YahaNativeContextInternal<'_> { self.tcp_client.as_ref().unwrap().request(req) } } + #[cfg(not(unix))] + pub fn request(&self, req: Request>) -> ResponseFuture { + self.tcp_client.as_ref().unwrap().request(req) + } } #[cfg(feature = "rustls")]