Skip to content

armgit / moli

Public Imported from GitHub
0

Best headless browser for AI agents. Lite, Fast, High-Compatibility. Built in Rust

refactor(fetch): use curl-rust pre-request callbacks

ldm0012bd1098372
diff --git a/Cargo.lock b/Cargo.lock
index f3d43b327..e24d69a60 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -924,7 +924,7 @@ dependencies = [
[[package]]
name = "curl"
version = "0.4.49"
-source = "git+https://github.com/lexmount/curl-rust?rev=a0ea59f5ca1b1e4bc8627a28c29463ac96aeb758#a0ea59f5ca1b1e4bc8627a28c29463ac96aeb758"
+source = "git+https://github.com/lexmount/curl-rust?rev=dc8c170e4370772c0085c08a67dda0fa86efca70#dc8c170e4370772c0085c08a67dda0fa86efca70"
dependencies = [
"curl-sys",
"libc",
@@ -938,7 +938,7 @@ dependencies = [
[[package]]
name = "curl-sys"
version = "0.4.87+curl-8.19.0"
-source = "git+https://github.com/lexmount/curl-rust?rev=a0ea59f5ca1b1e4bc8627a28c29463ac96aeb758#a0ea59f5ca1b1e4bc8627a28c29463ac96aeb758"
+source = "git+https://github.com/lexmount/curl-rust?rev=dc8c170e4370772c0085c08a67dda0fa86efca70#dc8c170e4370772c0085c08a67dda0fa86efca70"
dependencies = [
"brotlic-sys",
"cc",
diff --git a/Cargo.toml b/Cargo.toml
index abdb35ba2..d870d4d9d 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -104,8 +104,8 @@ fontique = { git = "https://github.com/lexmount/parley", rev = "a18cfc3ba6638d44
subsetter = { git = "https://github.com/lexmount/subsetter", rev = "8d3815245835af309559c3b4a872d6a0a54913a9" }
url = { git = "https://github.com/lexmount/rust-url", rev = "ca9fcad2b99f20c30c28a618ad78410f6fd31161" }
xml5ever = { path = "vendor/xml5ever-0.39.0" }
-curl = { git = "https://github.com/lexmount/curl-rust", rev = "a0ea59f5ca1b1e4bc8627a28c29463ac96aeb758" }
-curl-sys = { git = "https://github.com/lexmount/curl-rust", rev = "a0ea59f5ca1b1e4bc8627a28c29463ac96aeb758" }
+curl = { git = "https://github.com/lexmount/curl-rust", rev = "dc8c170e4370772c0085c08a67dda0fa86efca70" }
+curl-sys = { git = "https://github.com/lexmount/curl-rust", rev = "dc8c170e4370772c0085c08a67dda0fa86efca70" }
cookie = { git = "https://github.com/ldm0/cookie-rs", branch = "priority" }
v8 = { path = "vendor/v8-152.2.0" }
deno_v8 = { path = "vendor/deno_v8-0.3.0" }
diff --git a/moli-fetch/src/cancellation.rs b/moli-fetch/src/cancellation.rs
index cb975c29b..bd454db69 100644
--- a/moli-fetch/src/cancellation.rs
+++ b/moli-fetch/src/cancellation.rs
@@ -112,7 +112,7 @@ impl FetchResponseTiming {
self.body_bytes_received
}
/// The transport's pre-request boundary, after connection/TLS setup and
- /// before sending the request. Absent for responses served from cache.
+ /// before sending the request. Pure cache hits do not produce this boundary.
pub fn request_started_at(self) -> Option<Instant> {
self.request_started_at
}
diff --git a/moli-fetch/src/runtime.rs b/moli-fetch/src/runtime.rs
index 223b7ad6b..86456b33b 100644
--- a/moli-fetch/src/runtime.rs
+++ b/moli-fetch/src/runtime.rs
@@ -61,8 +61,6 @@ use crate::{
proxy_connect::{ProxyConnectResponse, ProxyConnectResponseRecorder},
};
-mod request_timing;
-
const DEFAULT_RUNTIME_TRANSFERS: usize = 256;
const RUNTIME_POLL_INTERVAL: Duration = Duration::from_millis(50);
// curl-sys does not currently expose CURLINFO_HTTP_VERSION. This value is
@@ -2842,6 +2840,19 @@ impl FetchTransferHandler {
}
impl Handler for FetchTransferHandler {
+ fn prereq(
+ &mut self,
+ _primary_ip: &str,
+ _local_ip: &str,
+ _primary_port: u16,
+ _local_port: u16,
+ ) -> bool {
+ if let Some(timing) = self.response_progress() {
+ timing.mark_request_started();
+ }
+ true
+ }
+
fn write(&mut self, data: &[u8]) -> std::result::Result<usize, WriteError> {
if !data.is_empty() {
self.mark_response_body_received();
@@ -2914,7 +2925,8 @@ fn configure_network_observation(
}
easy.get_mut()
.begin_transfer(recorder, capture_proxy_connect_response);
- request_timing::configure(easy)?;
+ easy.prereq(true)
+ .context("failed to configure curl pre-request callback")?;
easy.verbose(verbose)
.context("failed to configure curl network observation")
}
diff --git a/moli-fetch/src/runtime/request_timing.rs b/moli-fetch/src/runtime/request_timing.rs
deleted file mode 100644
index 358d17110..000000000
--- a/moli-fetch/src/runtime/request_timing.rs
+++ /dev/null
@@ -1,54 +0,0 @@
-use std::{
- ffi::{c_char, c_int, c_void},
- panic::{AssertUnwindSafe, catch_unwind},
-};
-
-use super::*;
-
-// curl-sys does not expose these options yet. Both values are part of curl's
-// public curl.h ABI, available since libcurl 7.80.0.
-const CURLOPT_PREREQFUNCTION: curl_sys::CURLoption = curl_sys::CURLOPTTYPE_FUNCTIONPOINT + 312;
-const CURLOPT_PREREQDATA: curl_sys::CURLoption = curl_sys::CURLOPTTYPE_OBJECTPOINT + 313;
-
-pub(super) fn configure(easy: &mut Easy2<FetchTransferHandler>) -> Result<()> {
- let callback: extern "C" fn(*mut c_void, *mut c_char, *mut c_char, c_int, c_int) -> c_int =
- request_started;
- let handler = std::ptr::from_mut(easy.get_mut()).cast::<c_void>();
- // SAFETY: curl-rust keeps the handler in Easy2's boxed Inner allocation.
- // Moving Easy2 does not move the handler, and curl_easy_cleanup runs before
- // that allocation is freed. libcurl calls this synchronously on the same
- // thread as the other Handler callbacks, with no overlapping handler access.
- // Every attempt reconfigures these options after Easy2::reset; its collector
- // is bound to that attempt's ResponseProgress before the easy is submitted.
- let code = unsafe { curl_sys::curl_easy_setopt(easy.raw(), CURLOPT_PREREQDATA, handler) };
- if code != curl_sys::CURLE_OK {
- return Err(curl::Error::new(code)).context("failed to configure curl pre-request data");
- }
- // SAFETY: callback has the curl_prereq_callback signature from curl.h.
- let code = unsafe { curl_sys::curl_easy_setopt(easy.raw(), CURLOPT_PREREQFUNCTION, callback) };
- if code != curl_sys::CURLE_OK {
- return Err(curl::Error::new(code))
- .context("failed to configure curl pre-request callback");
- }
- Ok(())
-}
-
-extern "C" fn request_started(
- handler: *mut c_void,
- _remote_ip: *mut c_char,
- _local_ip: *mut c_char,
- _remote_port: c_int,
- _local_port: c_int,
-) -> c_int {
- // Do not unwind across the C ABI. Abort this transfer if the callback fails.
- catch_unwind(AssertUnwindSafe(|| {
- // SAFETY: configure installs a live FetchTransferHandler as PREREQDATA;
- // libcurl invokes callbacks serially while its Easy2 remains alive.
- let handler = unsafe { &*handler.cast::<FetchTransferHandler>() };
- if let Some(timing) = handler.response_progress() {
- timing.mark_request_started();
- }
- 0 // CURL_PREREQFUNC_OK
- }))
- .unwrap_or(1) // CURL_PREREQFUNC_ABORT
-}
diff --git a/moli-fetch/src/runtime/request_timing_tests.rs b/moli-fetch/src/runtime/request_timing_tests.rs
index e17def320..6532fde70 100644
--- a/moli-fetch/src/runtime/request_timing_tests.rs
+++ b/moli-fetch/src/runtime/request_timing_tests.rs
@@ -49,8 +49,7 @@ fn read_head(stream: &mut std::net::TcpStream) -> std::io::Result<Vec<u8>> {
}
#[test]
-fn prerequest_timing_survives_reuse_reset_moves_and_progress_reselection_without_verbose()
--> Result<()> {
+fn prerequest_timing_survives_moves_and_reuse_without_verbose() -> Result<()> {
let listener = std::net::TcpListener::bind("127.0.0.1:0")?;
let request = Request::get(&format!("http://{}/timing", listener.local_addr()?))?;
let server = thread::spawn(move || -> std::io::Result<()> {