Skip to content

Commit ce0b11d

Browse files
committed
cleaner support for protocols
1 parent 060d01a commit ce0b11d

File tree

2 files changed

+25
-34
lines changed

2 files changed

+25
-34
lines changed

libdd-profiling/src/exporter/file_exporter.rs

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,28 @@
1414
//! This module exists as a workaround and will hopefully be replaced once reqwest adds
1515
//! native support for file output: <https://github.com/seanmonstar/reqwest/issues/2883>
1616
17-
#[cfg(any(unix, windows))]
1817
use std::path::PathBuf;
1918

2019
/// HTTP 200 OK response with no body
21-
#[cfg(any(unix, windows))]
2220
const HTTP_200_RESPONSE: &[u8] = b"HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n";
2321

22+
/// Spawns a dump server and configures the ClientBuilder with the appropriate transport
23+
pub(crate) fn spawn_dump_server_with_builder(
24+
builder: reqwest::ClientBuilder,
25+
output_path: PathBuf,
26+
) -> anyhow::Result<reqwest::ClientBuilder> {
27+
let server_path = spawn_dump_server(output_path)?;
28+
29+
#[cfg(unix)]
30+
{
31+
Ok(builder.unix_socket(server_path))
32+
}
33+
#[cfg(windows)]
34+
{
35+
Ok(builder.windows_named_pipe(server_path.to_string_lossy().to_string()))
36+
}
37+
}
38+
2439
/// Async server loop for Unix sockets
2540
#[cfg(unix)]
2641
async fn run_dump_server_unix(
@@ -147,15 +162,13 @@ pub(crate) fn spawn_dump_server(output_path: PathBuf) -> anyhow::Result<PathBuf>
147162
}
148163

149164
/// Helper function to find a subsequence in a byte slice
150-
#[cfg(any(unix, windows))]
151165
fn find_subsequence(haystack: &[u8], needle: &[u8]) -> Option<usize> {
152166
haystack
153167
.windows(needle.len())
154168
.position(|window| window == needle)
155169
}
156170

157171
/// Parse Content-Length from HTTP headers
158-
#[cfg(any(unix, windows))]
159172
fn parse_content_length(headers_data: &[u8]) -> Option<usize> {
160173
if let Ok(headers_str) = std::str::from_utf8(headers_data) {
161174
for line in headers_str.lines() {
@@ -170,7 +183,6 @@ fn parse_content_length(headers_data: &[u8]) -> Option<usize> {
170183
}
171184

172185
/// Check if we have received a complete HTTP request
173-
#[cfg(any(unix, windows))]
174186
fn is_request_complete(
175187
request_data: &[u8],
176188
headers_end_pos: Option<usize>,
@@ -186,7 +198,6 @@ fn is_request_complete(
186198
}
187199

188200
/// Read complete HTTP request from an async stream
189-
#[cfg(any(unix, windows))]
190201
async fn read_http_request_async<R: tokio::io::AsyncReadExt + Unpin>(stream: &mut R) -> Vec<u8> {
191202
let mut request_data = Vec::new();
192203
let mut buffer = [0u8; 8192];
@@ -223,7 +234,6 @@ async fn read_http_request_async<R: tokio::io::AsyncReadExt + Unpin>(stream: &mu
223234
}
224235

225236
/// Write request data to file if non-empty (async version)
226-
#[cfg(any(unix, windows))]
227237
async fn write_request_to_file_async(output_path: &PathBuf, request_data: &[u8]) {
228238
if !request_data.is_empty() {
229239
if let Err(e) = tokio::fs::write(output_path, request_data).await {
@@ -236,7 +246,6 @@ async fn write_request_to_file_async(output_path: &PathBuf, request_data: &[u8])
236246
}
237247

238248
/// Handle a connection: read HTTP request, write to file, send response
239-
#[cfg(any(unix, windows))]
240249
async fn handle_connection_async<S>(mut stream: S, output_path: PathBuf)
241250
where
242251
S: tokio::io::AsyncReadExt + tokio::io::AsyncWriteExt + Unpin,

libdd-profiling/src/exporter/profile_exporter.rs

Lines changed: 8 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
//! The dumped files contain the complete HTTP request including headers and body,
2525
//! which can be useful for debugging or replaying requests.
2626
27-
use super::file_exporter::spawn_dump_server;
27+
use super::file_exporter::spawn_dump_server_with_builder;
2828
use anyhow::Context;
2929
use libdd_common::tag::Tag;
3030
use libdd_common::{azure_app_services, tag, Endpoint};
@@ -64,29 +64,19 @@ impl ProfileExporter {
6464
mut tags: Vec<Tag>,
6565
endpoint: Endpoint,
6666
) -> anyhow::Result<Self> {
67-
#[cfg(not(any(unix, windows)))]
68-
compile_error!("ProfileExporter requires either Unix or Windows platform");
69-
7067
let mut builder = reqwest::Client::builder()
7168
.use_rustls_tls()
7269
.timeout(std::time::Duration::from_millis(endpoint.timeout_ms));
7370

7471
let request_url = match endpoint.url.scheme_str() {
75-
// File dump endpoint (debugging)
76-
#[cfg(unix)]
77-
Some("file") => {
78-
let output_path = libdd_common::decode_uri_path_in_authority(&endpoint.url)
79-
.context("Failed to decode file path from URI")?;
80-
let socket_path = spawn_dump_server(output_path)?;
81-
builder = builder.unix_socket(socket_path);
82-
"http://localhost/v1/input".to_string()
83-
}
84-
#[cfg(windows)]
72+
// HTTP/HTTPS endpoints
73+
Some("http") | Some("https") => endpoint.url.to_string(),
74+
75+
// File dump endpoint (debugging) - uses platform-specific local transport
8576
Some("file") => {
8677
let output_path = libdd_common::decode_uri_path_in_authority(&endpoint.url)
8778
.context("Failed to decode file path from URI")?;
88-
let pipe_path = spawn_dump_server(output_path)?;
89-
builder = builder.windows_named_pipe(pipe_path.to_string_lossy().to_string());
79+
builder = spawn_dump_server_with_builder(builder, output_path)?;
9080
"http://localhost/v1/input".to_string()
9181
}
9282

@@ -98,10 +88,6 @@ impl ProfileExporter {
9888
builder = builder.unix_socket(socket_path);
9989
format!("http://localhost{}", endpoint.url.path())
10090
}
101-
#[cfg(windows)]
102-
Some("unix") => {
103-
anyhow::bail!("unix:// endpoints are only supported on Unix platforms")
104-
}
10591

10692
// Windows named pipes
10793
#[cfg(windows)]
@@ -111,13 +97,9 @@ impl ProfileExporter {
11197
builder = builder.windows_named_pipe(pipe_path.to_string_lossy().to_string());
11298
format!("http://localhost{}", endpoint.url.path())
11399
}
114-
#[cfg(unix)]
115-
Some("windows") => {
116-
anyhow::bail!("windows:// endpoints are only supported on Windows platforms")
117-
}
118100

119-
// HTTP/HTTPS endpoints
120-
_ => endpoint.url.to_string(),
101+
// Unsupported schemes
102+
scheme => anyhow::bail!("Unsupported endpoint scheme: {:?}", scheme),
121103
};
122104

123105
// Pre-build all static headers

0 commit comments

Comments
 (0)