diff --git a/Cargo.toml b/Cargo.toml index 534b49c97c..8066a88dac 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -35,7 +35,6 @@ http-body-util = { version = "=0.1.0-rc.3", optional = true } httparse = { version = "1.8", optional = true } httpdate = { version = "1.0", optional = true } itoa = { version = "1", optional = true } -libc = { version = "0.2", optional = true } tracing = { version = "0.1", default-features = false, features = ["std"], optional = true } want = { version = "0.3", optional = true } @@ -82,7 +81,7 @@ client = ["dep:want"] server = ["dep:httpdate"] # C-API support (currently unstable (no semver)) -ffi = ["dep:libc", "dep:http-body-util"] +ffi = ["dep:http-body-util"] # Utilize tracing (currently unstable) tracing = ["dep:tracing"] diff --git a/src/ffi/body.rs b/src/ffi/body.rs index 91c5c7342f..6926689fd0 100644 --- a/src/ffi/body.rs +++ b/src/ffi/body.rs @@ -1,14 +1,14 @@ -use std::ffi::c_void; +use std::ffi::{c_int, c_void}; use std::mem::ManuallyDrop; use std::ptr; use std::task::{Context, Poll}; use http_body_util::BodyExt as _; -use libc::{c_int, size_t}; use super::task::{hyper_context, hyper_task, hyper_task_return_type, AsTaskType}; use super::{UserDataPointer, HYPER_ITER_CONTINUE}; use crate::body::{Bytes, Frame, Incoming as IncomingBody}; +use crate::ffi::c_size_t; /// A streaming HTTP body. pub struct hyper_body(pub(super) IncomingBody); @@ -214,7 +214,7 @@ ffi_fn! { /// `hyper_buf_free`. /// /// This returns `NULL` if allocating a new buffer fails. - fn hyper_buf_copy(buf: *const u8, len: size_t) -> *mut hyper_buf { + fn hyper_buf_copy(buf: *const u8, len: c_size_t) -> *mut hyper_buf { let slice = unsafe { std::slice::from_raw_parts(buf, len) }; @@ -237,7 +237,7 @@ ffi_fn! { ffi_fn! { /// Get the length of the bytes this buffer contains. - fn hyper_buf_len(buf: *const hyper_buf) -> size_t { + fn hyper_buf_len(buf: *const hyper_buf) -> c_size_t { unsafe { (*buf).0.len() } } } diff --git a/src/ffi/client.rs b/src/ffi/client.rs index 4de81d9b06..9ad27bcc4b 100644 --- a/src/ffi/client.rs +++ b/src/ffi/client.rs @@ -1,8 +1,7 @@ +use std::ffi::c_int; use std::ptr; use std::sync::Arc; -use libc::c_int; - use crate::client::conn; use crate::rt::Executor as _; diff --git a/src/ffi/error.rs b/src/ffi/error.rs index f545aa2737..cab70e2c13 100644 --- a/src/ffi/error.rs +++ b/src/ffi/error.rs @@ -1,4 +1,4 @@ -use libc::size_t; +use crate::ffi::c_size_t; /// A more detailed error object returned by some hyper functions. pub struct hyper_error(crate::Error); @@ -78,7 +78,7 @@ ffi_fn! { /// store. /// /// The return value is number of bytes that were written to `dst`. - fn hyper_error_print(err: *const hyper_error, dst: *mut u8, dst_len: size_t) -> size_t { + fn hyper_error_print(err: *const hyper_error, dst: *mut u8, dst_len: c_size_t) -> c_size_t { let dst = unsafe { std::slice::from_raw_parts_mut(dst, dst_len) }; diff --git a/src/ffi/http_types.rs b/src/ffi/http_types.rs index 565ab095ce..e95303f776 100644 --- a/src/ffi/http_types.rs +++ b/src/ffi/http_types.rs @@ -1,6 +1,6 @@ +use std::ffi::{c_int, c_void}; + use bytes::Bytes; -use libc::{c_int, size_t}; -use std::ffi::c_void; use super::body::hyper_body; use super::error::hyper_code; @@ -8,6 +8,7 @@ use super::task::{hyper_task_return_type, AsTaskType}; use super::{UserDataPointer, HYPER_ITER_CONTINUE}; use crate::body::Incoming as IncomingBody; use crate::ext::{HeaderCaseMap, OriginalHeaderOrder, ReasonPhrase}; +use crate::ffi::c_size_t; use crate::header::{HeaderName, HeaderValue}; use crate::{HeaderMap, Method, Request, Response, Uri}; @@ -57,7 +58,7 @@ ffi_fn! { ffi_fn! { /// Set the HTTP Method of the request. - fn hyper_request_set_method(req: *mut hyper_request, method: *const u8, method_len: size_t) -> hyper_code { + fn hyper_request_set_method(req: *mut hyper_request, method: *const u8, method_len: c_size_t) -> hyper_code { let bytes = unsafe { std::slice::from_raw_parts(method, method_len as usize) }; @@ -89,7 +90,7 @@ ffi_fn! { /// have been parsed/set are sent when the connection type is HTTP/2. /// /// To set each slot explicitly, use `hyper_request_set_uri_parts`. - fn hyper_request_set_uri(req: *mut hyper_request, uri: *const u8, uri_len: size_t) -> hyper_code { + fn hyper_request_set_uri(req: *mut hyper_request, uri: *const u8, uri_len: c_size_t) -> hyper_code { let bytes = unsafe { std::slice::from_raw_parts(uri, uri_len as usize) }; @@ -117,11 +118,11 @@ ffi_fn! { fn hyper_request_set_uri_parts( req: *mut hyper_request, scheme: *const u8, - scheme_len: size_t, + scheme_len: c_size_t, authority: *const u8, - authority_len: size_t, + authority_len: c_size_t, path_and_query: *const u8, - path_and_query_len: size_t + path_and_query_len: c_size_t ) -> hyper_code { let mut builder = Uri::builder(); if !scheme.is_null() { @@ -280,7 +281,7 @@ ffi_fn! { /// Get the length of the reason-phrase of this response. /// /// Use `hyper_response_reason_phrase()` to get the buffer pointer. - fn hyper_response_reason_phrase_len(resp: *const hyper_response) -> size_t { + fn hyper_response_reason_phrase_len(resp: *const hyper_response) -> c_size_t { non_null!(&*resp ?= 0).reason_phrase().len() } } @@ -371,7 +372,7 @@ unsafe impl AsTaskType for hyper_response { // ===== impl Headers ===== type hyper_headers_foreach_callback = - extern "C" fn(*mut c_void, *const u8, size_t, *const u8, size_t) -> c_int; + extern "C" fn(*mut c_void, *const u8, c_size_t, *const u8, c_size_t) -> c_int; impl hyper_headers { pub(super) fn get_or_default(ext: &mut http::Extensions) -> &mut hyper_headers { @@ -453,7 +454,7 @@ ffi_fn! { /// Sets the header with the provided name to the provided value. /// /// This overwrites any previous value set for the header. - fn hyper_headers_set(headers: *mut hyper_headers, name: *const u8, name_len: size_t, value: *const u8, value_len: size_t) -> hyper_code { + fn hyper_headers_set(headers: *mut hyper_headers, name: *const u8, name_len: c_size_t, value: *const u8, value_len: c_size_t) -> hyper_code { let headers = non_null!(&mut *headers ?= hyper_code::HYPERE_INVALID_ARG); match unsafe { raw_name_value(name, name_len, value, value_len) } { Ok((name, value, orig_name)) => { @@ -472,7 +473,7 @@ ffi_fn! { /// /// If there were already existing values for the name, this will append the /// new value to the internal list. - fn hyper_headers_add(headers: *mut hyper_headers, name: *const u8, name_len: size_t, value: *const u8, value_len: size_t) -> hyper_code { + fn hyper_headers_add(headers: *mut hyper_headers, name: *const u8, name_len: c_size_t, value: *const u8, value_len: c_size_t) -> hyper_code { let headers = non_null!(&mut *headers ?= hyper_code::HYPERE_INVALID_ARG); match unsafe { raw_name_value(name, name_len, value, value_len) } { @@ -499,9 +500,9 @@ impl Default for hyper_headers { unsafe fn raw_name_value( name: *const u8, - name_len: size_t, + name_len: c_size_t, value: *const u8, - value_len: size_t, + value_len: c_size_t, ) -> Result<(HeaderName, HeaderValue, Bytes), hyper_code> { let name = std::slice::from_raw_parts(name, name_len); let orig_name = Bytes::copy_from_slice(name); diff --git a/src/ffi/io.rs b/src/ffi/io.rs index c1ba87a02b..1f0e69ac23 100644 --- a/src/ffi/io.rs +++ b/src/ffi/io.rs @@ -2,22 +2,21 @@ use std::ffi::c_void; use std::pin::Pin; use std::task::{Context, Poll}; -use crate::rt::{Read, Write}; -use libc::size_t; - use super::task::hyper_context; +use crate::ffi::c_size_t; +use crate::rt::{Read, Write}; /// Sentinel value to return from a read or write callback that the operation /// is pending. -pub const HYPER_IO_PENDING: size_t = 0xFFFFFFFF; +pub const HYPER_IO_PENDING: c_size_t = 0xFFFFFFFF; /// Sentinel value to return from a read or write callback that the operation /// has errored. -pub const HYPER_IO_ERROR: size_t = 0xFFFFFFFE; +pub const HYPER_IO_ERROR: c_size_t = 0xFFFFFFFE; type hyper_io_read_callback = - extern "C" fn(*mut c_void, *mut hyper_context<'_>, *mut u8, size_t) -> size_t; + extern "C" fn(*mut c_void, *mut hyper_context<'_>, *mut u8, c_size_t) -> c_size_t; type hyper_io_write_callback = - extern "C" fn(*mut c_void, *mut hyper_context<'_>, *const u8, size_t) -> size_t; + extern "C" fn(*mut c_void, *mut hyper_context<'_>, *const u8, c_size_t) -> c_size_t; /// An IO object used to represent a socket or similar concept. pub struct hyper_io { @@ -108,8 +107,8 @@ extern "C" fn read_noop( _userdata: *mut c_void, _: *mut hyper_context<'_>, _buf: *mut u8, - _buf_len: size_t, -) -> size_t { + _buf_len: c_size_t, +) -> c_size_t { 0 } @@ -118,8 +117,8 @@ extern "C" fn write_noop( _userdata: *mut c_void, _: *mut hyper_context<'_>, _buf: *const u8, - _buf_len: size_t, -) -> size_t { + _buf_len: c_size_t, +) -> c_size_t { 0 } diff --git a/src/ffi/mod.rs b/src/ffi/mod.rs index 8dd8a5b9ab..1a3074d847 100644 --- a/src/ffi/mod.rs +++ b/src/ffi/mod.rs @@ -62,19 +62,19 @@ pub use self::io::*; pub use self::task::*; /// Return in iter functions to continue iterating. -pub const HYPER_ITER_CONTINUE: libc::c_int = 0; +pub const HYPER_ITER_CONTINUE: std::ffi::c_int = 0; /// Return in iter functions to stop iterating. #[allow(unused)] -pub const HYPER_ITER_BREAK: libc::c_int = 1; +pub const HYPER_ITER_BREAK: std::ffi::c_int = 1; /// An HTTP Version that is unspecified. -pub const HYPER_HTTP_VERSION_NONE: libc::c_int = 0; +pub const HYPER_HTTP_VERSION_NONE: std::ffi::c_int = 0; /// The HTTP/1.0 version. -pub const HYPER_HTTP_VERSION_1_0: libc::c_int = 10; +pub const HYPER_HTTP_VERSION_1_0: std::ffi::c_int = 10; /// The HTTP/1.1 version. -pub const HYPER_HTTP_VERSION_1_1: libc::c_int = 11; +pub const HYPER_HTTP_VERSION_1_1: std::ffi::c_int = 11; /// The HTTP/2 version. -pub const HYPER_HTTP_VERSION_2: libc::c_int = 20; +pub const HYPER_HTTP_VERSION_2: std::ffi::c_int = 20; struct UserDataPointer(*mut std::ffi::c_void); @@ -86,9 +86,13 @@ unsafe impl Sync for UserDataPointer {} /// cbindgen:ignore static VERSION_CSTR: &str = concat!(env!("CARGO_PKG_VERSION"), "\0"); +// `core::ffi::c_size_t` is a nightly-only experimental API. +// https://github.com/rust-lang/rust/issues/88345 +type c_size_t = usize; + ffi_fn! { /// Returns a static ASCII (null terminated) string of the hyper version. - fn hyper_version() -> *const libc::c_char { + fn hyper_version() -> *const std::ffi::c_char { VERSION_CSTR.as_ptr() as _ } ?= std::ptr::null() } diff --git a/src/ffi/task.rs b/src/ffi/task.rs index 78e92ba90c..f4059544a0 100644 --- a/src/ffi/task.rs +++ b/src/ffi/task.rs @@ -1,4 +1,4 @@ -use std::ffi::c_void; +use std::ffi::{c_int, c_void}; use std::future::Future; use std::pin::Pin; use std::ptr; @@ -9,7 +9,6 @@ use std::sync::{ use std::task::{Context, Poll}; use futures_util::stream::{FuturesUnordered, Stream}; -use libc::c_int; use super::error::hyper_code; use super::UserDataPointer;