Skip to content

Commit 2684931

Browse files
authored
Add an option to set the expect 100 timeout (#376)
For larger request bodies, `curl` will use an `Expect` header to first validate the request headers before sending the actual body. However, if the server does not reply within `CURLOPT_EXPECT_100_TIMEOUT_MS` then the server will send the request anyways. This behaviour can sometimes lead to higher total latency since in the best case, an additional server roundtrip is required and in the worst case, the request is delayed by `CURLOPT_EXPECT_100_TIMEOUT_MS`. The best-case scenario is where the request is invalid and the server replies with a `417 Expectation Failed` without having to wait for or process the request body at all. This commit adds the ability to define a custom timeout or override it by setting it to zero.
1 parent 15a48d2 commit 2684931

File tree

3 files changed

+29
-0
lines changed

3 files changed

+29
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
Cargo.lock
22
target/
3+
.idea/

curl-sys/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -574,6 +574,7 @@ pub const CURLOPT_SSL_OPTIONS: CURLoption = CURLOPTTYPE_LONG + 216;
574574
// pub const CURLOPT_DNS_LOCAL_IP4: CURLoption = CURLOPTTYPE_OBJECTPOINT + 222;
575575
// pub const CURLOPT_DNS_LOCAL_IP6: CURLoption = CURLOPTTYPE_OBJECTPOINT + 223;
576576
// pub const CURLOPT_LOGIN_OPTIONS: CURLoption = CURLOPTTYPE_OBJECTPOINT + 224;
577+
pub const CURLOPT_EXPECT_100_TIMEOUT_MS: CURLoption = CURLOPTTYPE_LONG + 227;
577578
pub const CURLOPT_UNIX_SOCKET_PATH: CURLoption = CURLOPTTYPE_OBJECTPOINT + 231;
578579
pub const CURLOPT_PIPEWAIT: CURLoption = CURLOPTTYPE_LONG + 237;
579580
pub const CURLOPT_CONNECT_TO: CURLoption = CURLOPTTYPE_OBJECTPOINT + 243;

src/easy/handler.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2290,6 +2290,33 @@ impl<H> Easy2<H> {
22902290
// =========================================================================
22912291
// getters
22922292

2293+
/// Set maximum time to wait for Expect 100 request before sending body.
2294+
///
2295+
/// `curl` has internal heuristics that trigger the use of a `Expect`
2296+
/// header for large enough request bodies where the client first sends the
2297+
/// request header along with an `Expect: 100-continue` header. The server
2298+
/// is supposed to validate the headers and respond with a `100` response
2299+
/// status code after which `curl` will send the actual request body.
2300+
///
2301+
/// However, if the server does not respond to the initial request
2302+
/// within `CURLOPT_EXPECT_100_TIMEOUT_MS` then `curl` will send the
2303+
/// request body anyways.
2304+
///
2305+
/// The best-case scenario is where the request is invalid and the server
2306+
/// replies with a `417 Expectation Failed` without having to wait for or process
2307+
/// the request body at all. However, this behaviour can also lead to higher
2308+
/// total latency since in the best case, an additional server roundtrip is required
2309+
/// and in the worst case, the request is delayed by `CURLOPT_EXPECT_100_TIMEOUT_MS`.
2310+
///
2311+
/// More info: https://curl.se/libcurl/c/CURLOPT_EXPECT_100_TIMEOUT_MS.html
2312+
///
2313+
/// By default this option is not set and corresponds to
2314+
/// `CURLOPT_EXPECT_100_TIMEOUT_MS`.
2315+
pub fn expect_100_timeout(&mut self, timeout: Duration) -> Result<(), Error> {
2316+
let ms = timeout.as_secs() * 1000 + (timeout.subsec_nanos() / 1_000_000) as u64;
2317+
self.setopt_long(curl_sys::CURLOPT_EXPECT_100_TIMEOUT_MS, ms as c_long)
2318+
}
2319+
22932320
/// Get info on unmet time conditional
22942321
///
22952322
/// Returns if the condition provided in the previous request didn't match

0 commit comments

Comments
 (0)