Skip to content

Commit

Permalink
Fix for building on Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
mayuki committed Dec 17, 2024
1 parent 4644ed8 commit 73d5d47
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 7 deletions.
2 changes: 2 additions & 0 deletions native/yaha_native/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
8 changes: 7 additions & 1 deletion native/yaha_native/src/binding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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());
});
Expand Down
31 changes: 25 additions & 6 deletions native/yaha_native/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -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<Client<UnixConnector, BoxBody<Bytes, hyper::Error>>>,
pub uds_socket_path: Option<PathBuf>,
#[cfg(unix)]
pub uds_socket_path: Option<std::path::PathBuf>,
}

impl YahaNativeContextInternal<'_> {
Expand All @@ -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,
Expand All @@ -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,
}
}
Expand All @@ -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));
}
Expand Down Expand Up @@ -193,6 +207,7 @@ impl YahaNativeContextInternal<'_> {
https
}

#[cfg(unix)]
pub fn request(&self, mut req: Request<BoxBody<Bytes, hyper::Error>>) -> 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 {
Expand All @@ -210,6 +225,10 @@ impl YahaNativeContextInternal<'_> {
self.tcp_client.as_ref().unwrap().request(req)
}
}
#[cfg(not(unix))]
pub fn request(&self, req: Request<BoxBody<Bytes, hyper::Error>>) -> ResponseFuture {
self.tcp_client.as_ref().unwrap().request(req)
}
}

#[cfg(feature = "rustls")]
Expand Down

0 comments on commit 73d5d47

Please sign in to comment.