Skip to content

Add support for curl_easy_upkeep #378

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ static-ssl = ["curl-sys/static-ssl"]
force-system-lib-on-osx = ['curl-sys/force-system-lib-on-osx']
protocol-ftp = ["curl-sys/protocol-ftp"]
zlib-ng-compat = ["curl-sys/zlib-ng-compat", "static-curl"]
upkeep_7_62_0 = ["curl-sys/upkeep_7_62_0"]

[[test]]
name = "atexit"
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ with various Cargo features:
- `static-curl`: Use a bundled libcurl version and statically link to it. Disabled by default.
- `static-ssl`: Use a bundled OpenSSL version and statically link to it. Only applies on platforms that use OpenSSL. Disabled by default.
- `spnego`: Enable SPNEGO support. Disabled by default.
- `upkeep_7_62_0`: Enable curl_easy_upkeep() support, introduced in curl 7.62.0. Disabled by default.

## Version Support

Expand Down
1 change: 1 addition & 0 deletions curl-sys/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,4 @@ spnego = []
force-system-lib-on-osx = []
protocol-ftp = []
zlib-ng-compat = ["libz-sys/zlib-ng", "static-curl"]
upkeep_7_62_0 = []
3 changes: 3 additions & 0 deletions curl-sys/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1030,6 +1030,9 @@ extern "C" {
n: *mut size_t,
) -> CURLcode;

#[cfg(feature = "upkeep_7_62_0")]
pub fn curl_easy_upkeep(curl: *mut CURL) -> CURLcode;

pub fn curl_multi_init() -> *mut CURLM;
pub fn curl_multi_add_handle(multi_handle: *mut CURLM, curl_handle: *mut CURL) -> CURLMcode;
pub fn curl_multi_remove_handle(multi_handle: *mut CURLM, curl_handle: *mut CURL) -> CURLMcode;
Expand Down
12 changes: 12 additions & 0 deletions src/easy/handle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1256,6 +1256,12 @@ impl Easy {
}
}

/// Same as [`Easy2::upkeep`](struct.Easy2.html#method.upkeep)
#[cfg(feature = "upkeep_7_62_0")]
pub fn upkeep(&self) -> Result<(), Error> {
self.inner.upkeep()
}

/// Same as [`Easy2::unpause_read`](struct.Easy2.html#method.unpause_read)
pub fn unpause_read(&self) -> Result<(), Error> {
self.inner.unpause_read()
Expand Down Expand Up @@ -1504,6 +1510,12 @@ impl<'easy, 'data> Transfer<'easy, 'data> {
self.easy.do_perform()
}

/// Same as `Easy::upkeep`
#[cfg(feature = "upkeep_7_62_0")]
pub fn upkeep(&self) -> Result<(), Error> {
self.easy.upkeep()
}

/// Same as `Easy::unpause_read`.
pub fn unpause_read(&self) -> Result<(), Error> {
self.easy.unpause_read()
Expand Down
15 changes: 15 additions & 0 deletions src/easy/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2737,6 +2737,21 @@ impl<H> Easy2<H> {
ret
}

/// Some protocols have "connection upkeep" mechanisms. These mechanisms
/// usually send some traffic on existing connections in order to keep them
/// alive; this can prevent connections from being closed due to overzealous
/// firewalls, for example.
///
/// Currently the only protocol with a connection upkeep mechanism is
/// HTTP/2: when the connection upkeep interval is exceeded and upkeep() is
/// called, an HTTP/2 PING frame is sent on the connection.
#[cfg(feature = "upkeep_7_62_0")]
pub fn upkeep(&self) -> Result<(), Error> {
let ret = unsafe { self.cvt(curl_sys::curl_easy_upkeep(self.inner.handle)) };
panic::propagate();
return ret;
}

/// Unpause reading on a connection.
///
/// Using this function, you can explicitly unpause a connection that was
Expand Down
21 changes: 21 additions & 0 deletions tests/easy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -828,3 +828,24 @@ fn check_unix_socket() {
t!(h.post_fields_copy(b"data\n"));
t!(h.perform());
}

#[cfg(feature = "upkeep_7_62_0")]
#[test]
fn test_upkeep() {
let s = Server::new();
s.receive(
"\
GET / HTTP/1.1\r\n\
Host: 127.0.0.1:$PORT\r\n\
Accept: */*\r\n\
\r\n",
);
s.send("HTTP/1.1 200 OK\r\n\r\n");

let mut handle = handle();
t!(handle.url(&s.url("/")));
t!(handle.perform());

// Ensure that upkeep can be called on the handle without problem.
t!(handle.upkeep());
}