Skip to content

Commit dfd5cba

Browse files
committed
add request_read_response_version function to return "HTTP/1.1" etc.
Enhances HTTP response handling by adding version information. Updated the process_request function to include HTTP version in responses and added a new function to retrieve the HTTP version as a string. Modified related types and FFI interface accordingly.
1 parent 6c2b0d2 commit dfd5cba

File tree

4 files changed

+63
-4
lines changed

4 files changed

+63
-4
lines changed

src/async_support.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@ pub async fn process_request(
1515
) {
1616
match request_future.await {
1717
Ok(response) => {
18-
// Get the status code and headers before consuming the response
18+
// Get the status code, version, and headers before consuming the response
1919
let status = response.status();
20+
let version = response.version();
2021
let headers = response.headers().clone();
2122

2223
// Check if we're streaming to a file
@@ -52,6 +53,7 @@ pub async fn process_request(
5253
progress.status = RequestStatus::Error;
5354
progress.final_response = Some(Response {
5455
status,
56+
version,
5557
headers,
5658
body: Err(format!("File write error: {e}")),
5759
});
@@ -69,6 +71,7 @@ pub async fn process_request(
6971
progress.status = RequestStatus::Error;
7072
progress.final_response = Some(Response {
7173
status,
74+
version,
7275
headers,
7376
body: Err(format!("Network error: {e}")),
7477
});
@@ -82,6 +85,7 @@ pub async fn process_request(
8285
progress.status = RequestStatus::Completed;
8386
progress.final_response = Some(Response {
8487
status,
88+
version,
8589
headers,
8690
body: Ok(Vec::new()), // Empty body since it was streamed to file
8791
});
@@ -92,6 +96,7 @@ pub async fn process_request(
9296
progress.status = RequestStatus::Error;
9397
progress.final_response = Some(Response {
9498
status,
99+
version,
95100
headers,
96101
body: Err(format!("File open error: {e}")),
97102
});
@@ -111,6 +116,7 @@ pub async fn process_request(
111116
progress.total_bytes = Some(bytes_len);
112117
progress.final_response = Some(Response {
113118
status,
119+
version,
114120
headers,
115121
body: Ok(bytes_vec),
116122
});
@@ -121,6 +127,7 @@ pub async fn process_request(
121127
progress.status = RequestStatus::Error;
122128
progress.final_response = Some(Response {
123129
status,
130+
version,
124131
headers,
125132
body: Err(format!("Body read error: {e}")),
126133
});
@@ -134,6 +141,7 @@ pub async fn process_request(
134141
progress.status = RequestStatus::Error;
135142
progress.final_response = Some(Response {
136143
status: reqwest::StatusCode::BAD_REQUEST, // Default status code for errors
144+
version: reqwest::Version::HTTP_11, // Default to HTTP/1.1 for errors
137145
headers: reqwest::header::HeaderMap::new(),
138146
body: Err(format!("Request error: {e}")),
139147
});

src/ffi/request.rs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,3 +246,53 @@ pub extern "C" fn request_has_transport_error(request_id: RequestId) -> bool {
246246

247247
false // Return false if request not found or no response yet
248248
}
249+
250+
/// Get the HTTP version as a string directly from a request ID
251+
/// Returns a C string like "HTTP/1.1", "HTTP/2", etc. Caller must free the memory.
252+
#[unsafe(no_mangle)]
253+
pub extern "C" fn request_read_response_version(
254+
request_id: RequestId,
255+
num_bytes: *mut u32,
256+
) -> *mut c_char {
257+
if num_bytes.is_null() {
258+
return ptr::null_mut();
259+
}
260+
261+
// Get the response info
262+
let tracker = REQUEST_TRACKER.lock().unwrap();
263+
264+
if let Some(progress_info) = tracker.get(&request_id) {
265+
let progress = progress_info.read().unwrap();
266+
if let Some(ref response) = progress.final_response {
267+
let version_str = match response.version {
268+
reqwest::Version::HTTP_09 => "HTTP/0.9",
269+
reqwest::Version::HTTP_10 => "HTTP/1.0",
270+
reqwest::Version::HTTP_11 => "HTTP/1.1",
271+
reqwest::Version::HTTP_2 => "HTTP/2",
272+
reqwest::Version::HTTP_3 => "HTTP/3",
273+
_ => "HTTP/Unknown",
274+
};
275+
276+
// Calculate size including null terminator
277+
let str_len = version_str.len();
278+
unsafe { *num_bytes = str_len as u32 };
279+
280+
// Allocate memory for the string + null terminator
281+
let c_str_ptr = unsafe { libc::malloc(str_len + 1) as *mut c_char };
282+
if c_str_ptr.is_null() {
283+
return ptr::null_mut();
284+
}
285+
286+
// Copy the string and add null terminator
287+
unsafe {
288+
std::ptr::copy_nonoverlapping(version_str.as_ptr(), c_str_ptr as *mut u8, str_len);
289+
*(c_str_ptr.add(str_len)) = 0;
290+
}
291+
292+
return c_str_ptr;
293+
}
294+
}
295+
296+
unsafe { *num_bytes = 0 };
297+
ptr::null_mut() // Return null if request not found or no response yet
298+
}

src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ pub use ffi::multipart::{
3030
pub use ffi::request::{
3131
request_cancel, request_destroy, request_has_transport_error, request_is_complete,
3232
request_read_progress, request_read_received_bytes, request_read_response_body,
33-
request_read_response_headers, request_read_response_status, request_read_total_bytes,
34-
request_read_transport_error,
33+
request_read_response_headers, request_read_response_status, request_read_response_version,
34+
request_read_total_bytes, request_read_transport_error,
3535
};
3636
pub use ffi::request_builder::{
3737
request_builder_basic_auth, request_builder_bearer_auth, request_builder_body,

src/types.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use reqwest::{Client, RequestBuilder, StatusCode, header::HeaderMap};
1+
use reqwest::{Client, RequestBuilder, StatusCode, Version, header::HeaderMap};
22
use std::sync::{Arc, RwLock};
33

44
/// Type aliases for IDs
@@ -68,6 +68,7 @@ pub struct RequestProgress {
6868
/// Response struct that can be passed back to LabVIEW
6969
pub struct Response {
7070
pub status: StatusCode,
71+
pub version: Version,
7172
pub headers: HeaderMap,
7273
pub body: Result<Vec<u8>, String>,
7374
}

0 commit comments

Comments
 (0)