Skip to content

Commit

Permalink
Add h3 feature flag to reduce the size of complied library
Browse files Browse the repository at this point in the history
  • Loading branch information
iyangsj committed Oct 29, 2024
1 parent 8af4622 commit e440c06
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 3 deletions.
8 changes: 8 additions & 0 deletions .github/workflows/tquic-features.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@ jobs:
run: cargo build --all --no-default-features -F ffi && cargo test --no-default-features -F ffi
- name: Build with feature(s) qlog
run: cargo build --all --no-default-features -F qlog && cargo test --no-default-features -F qlog
- name: Build with feature(s) h3
run: cargo build --all --no-default-features -F qlog && cargo test --no-default-features -F h3
- name: Build with feature(s) ffi,qlog
run: cargo build --all --no-default-features -F ffi,qlog && cargo test --no-default-features -F ffi,qlog
- name: Build with feature(s) ffi,h3
run: cargo build --all --no-default-features -F ffi,qlog && cargo test --no-default-features -F ffi,h3
- name: Build with feature(s) qlog,h3
run: cargo build --all --no-default-features -F ffi,qlog && cargo test --no-default-features -F qlog,h3
- name: Build with feature(s) ffi,qlog,h3
run: cargo build --all --no-default-features -F ffi,qlog && cargo test --no-default-features -F ffi,qlog,h3

9 changes: 6 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,20 @@ include = [

[package.metadata.docs.rs]
no-default-features = true
features = ["qlog"]
features = ["qlog", "h3"]

[features]
default = ["qlog"]
default = ["qlog", "h3"]

# build the FFI API
ffi = []

# enable support for the qlog
qlog = ["dep:serde", "dep:serde_json", "dep:serde_derive", "dep:serde_with"]

# enable support for h3
h3 = ["dep:sfv"]

[dependencies]
bytes = "1"
rustc-hash = "1.1"
Expand All @@ -58,7 +61,7 @@ serde_derive = { version = "1.0", optional=true }
serde_with = { version="3.0.0", optional=true }
hex = "0.4"
priority-queue = "1.3.2"
sfv = { version = "0.9" }
sfv = { version = "0.9", optional=true }

[target."cfg(windows)".dependencies]
winapi = { version = "0.3", features = ["wincrypt", "ws2def", "ws2ipdef", "ws2tcpip"] }
Expand Down
38 changes: 38 additions & 0 deletions src/ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,17 @@ pub struct iovec {
use crate::codec::Decoder;
use crate::connection::ConnectionStats;
use crate::error::Error;
#[cfg(feature = "h3")]
use crate::h3::connection::Http3Connection;
#[cfg(feature = "h3")]
use crate::h3::connection::Http3Priority;
#[cfg(feature = "h3")]
use crate::h3::Http3Config;
#[cfg(feature = "h3")]
use crate::h3::Http3Event;
#[cfg(feature = "h3")]
use crate::h3::Http3Headers;
#[cfg(feature = "h3")]
use crate::h3::NameValue;
#[cfg(feature = "qlog")]
use crate::qlog::events;
Expand Down Expand Up @@ -2064,6 +2070,7 @@ pub extern "C" fn quic_packet_header_info(
}

/// Create default config for HTTP3.
#[cfg(feature = "h3")]
#[no_mangle]
pub extern "C" fn http3_config_new() -> *mut Http3Config {
match Http3Config::new() {
Expand All @@ -2073,6 +2080,7 @@ pub extern "C" fn http3_config_new() -> *mut Http3Config {
}

/// Destroy the HTTP3 config.
#[cfg(feature = "h3")]
#[no_mangle]
pub extern "C" fn http3_config_free(config: *mut Http3Config) {
unsafe {
Expand All @@ -2082,20 +2090,23 @@ pub extern "C" fn http3_config_free(config: *mut Http3Config) {

/// Set the `SETTINGS_MAX_FIELD_SECTION_SIZE` setting.
/// By default no limit is enforced.
#[cfg(feature = "h3")]
#[no_mangle]
pub extern "C" fn http3_config_set_max_field_section_size(config: &mut Http3Config, v: u64) {
config.set_max_field_section_size(v);
}

/// Set the `SETTINGS_QPACK_MAX_TABLE_CAPACITY` setting.
/// The default value is `0`.
#[cfg(feature = "h3")]
#[no_mangle]
pub extern "C" fn http3_config_set_qpack_max_table_capacity(config: &mut Http3Config, v: u64) {
config.set_qpack_max_table_capacity(v);
}

/// Set the `SETTINGS_QPACK_BLOCKED_STREAMS` setting.
/// The default value is `0`.
#[cfg(feature = "h3")]
#[no_mangle]
pub extern "C" fn http3_config_set_qpack_blocked_streams(config: &mut Http3Config, v: u64) {
config.set_qpack_blocked_streams(v);
Expand All @@ -2104,6 +2115,7 @@ pub extern "C" fn http3_config_set_qpack_blocked_streams(config: &mut Http3Confi
/// Create an HTTP/3 connection using the given QUIC connection. It also
/// initiate the HTTP/3 handshake by opening all control streams and sending
/// the local settings.
#[cfg(feature = "h3")]
#[no_mangle]
pub extern "C" fn http3_conn_new(
quic_conn: &mut Connection,
Expand All @@ -2116,6 +2128,7 @@ pub extern "C" fn http3_conn_new(
}

/// Destroy the HTTP/3 connection.
#[cfg(feature = "h3")]
#[no_mangle]
pub extern "C" fn http3_conn_free(conn: *mut Http3Connection) {
unsafe {
Expand All @@ -2124,6 +2137,7 @@ pub extern "C" fn http3_conn_free(conn: *mut Http3Connection) {
}

/// Send goaway with the given id.
#[cfg(feature = "h3")]
#[no_mangle]
pub extern "C" fn http3_send_goaway(
conn: &mut Http3Connection,
Expand All @@ -2137,6 +2151,7 @@ pub extern "C" fn http3_send_goaway(
}

/// Set HTTP/3 connection events handler.
#[cfg(feature = "h3")]
#[no_mangle]
pub extern "C" fn http3_conn_set_events_handler(
conn: &mut Http3Connection,
Expand All @@ -2148,6 +2163,7 @@ pub extern "C" fn http3_conn_set_events_handler(
}

/// Process HTTP/3 settings.
#[cfg(feature = "h3")]
#[no_mangle]
pub extern "C" fn http3_for_each_setting(
conn: &Http3Connection,
Expand All @@ -2171,6 +2187,7 @@ pub extern "C" fn http3_for_each_setting(
}

/// Process internal events of all streams of the specified HTTP/3 connection.
#[cfg(feature = "h3")]
#[no_mangle]
pub extern "C" fn http3_conn_process_streams(
conn: &mut Http3Connection,
Expand All @@ -2183,6 +2200,7 @@ pub extern "C" fn http3_conn_process_streams(
}

/// Process HTTP/3 headers.
#[cfg(feature = "h3")]
#[no_mangle]
pub extern "C" fn http3_for_each_header(
headers: &Http3Headers,
Expand Down Expand Up @@ -2212,13 +2230,15 @@ pub extern "C" fn http3_for_each_header(
}

/// Return true if all the data has been read from the stream.
#[cfg(feature = "h3")]
#[no_mangle]
pub extern "C" fn http3_stream_read_finished(conn: &mut Connection, stream_id: u64) -> bool {
conn.stream_finished(stream_id)
}

/// Create a new HTTP/3 request stream.
/// On success the stream ID is returned.
#[cfg(feature = "h3")]
#[no_mangle]
pub extern "C" fn http3_stream_new(conn: &mut Http3Connection, quic_conn: &mut Connection) -> i64 {
match conn.stream_new_with_priority(quic_conn, &Http3Priority::default()) {
Expand All @@ -2229,6 +2249,7 @@ pub extern "C" fn http3_stream_new(conn: &mut Http3Connection, quic_conn: &mut C

/// Create a new HTTP/3 request stream with the given priority.
/// On success the stream ID is returned.
#[cfg(feature = "h3")]
#[no_mangle]
pub extern "C" fn http3_stream_new_with_priority(
conn: &mut Http3Connection,
Expand All @@ -2242,6 +2263,7 @@ pub extern "C" fn http3_stream_new_with_priority(
}

/// Close the given HTTP/3 stream.
#[cfg(feature = "h3")]
#[no_mangle]
pub extern "C" fn http3_stream_close(
conn: &mut Http3Connection,
Expand All @@ -2255,6 +2277,7 @@ pub extern "C" fn http3_stream_close(
}

/// Set priority for an HTTP/3 stream.
#[cfg(feature = "h3")]
#[no_mangle]
pub extern "C" fn http3_stream_set_priority(
conn: &mut Http3Connection,
Expand All @@ -2268,6 +2291,7 @@ pub extern "C" fn http3_stream_set_priority(
}
}

#[cfg(feature = "h3")]
#[repr(C)]
pub struct Header {
name: *mut u8,
Expand All @@ -2277,6 +2301,7 @@ pub struct Header {
}

/// Send HTTP/3 request or response headers on the given stream.
#[cfg(feature = "h3")]
#[no_mangle]
pub extern "C" fn http3_send_headers(
conn: &mut Http3Connection,
Expand All @@ -2295,6 +2320,7 @@ pub extern "C" fn http3_send_headers(
}

/// Send HTTP/3 request or response body on the given stream.
#[cfg(feature = "h3")]
#[no_mangle]
pub extern "C" fn http3_send_body(
conn: &mut Http3Connection,
Expand All @@ -2316,6 +2342,7 @@ pub extern "C" fn http3_send_body(
}

/// Read request/response body from the given stream.
#[cfg(feature = "h3")]
#[no_mangle]
pub extern "C" fn http3_recv_body(
conn: &mut Http3Connection,
Expand All @@ -2336,6 +2363,7 @@ pub extern "C" fn http3_recv_body(
}

/// Parse HTTP/3 priority data.
#[cfg(feature = "h3")]
#[no_mangle]
pub extern "C" fn http3_parse_extensible_priority(
priority: *const u8,
Expand All @@ -2356,6 +2384,7 @@ pub extern "C" fn http3_parse_extensible_priority(

/// Send a PRIORITY_UPDATE frame on the control stream with specified
/// request stream ID and priority.
#[cfg(feature = "h3")]
#[no_mangle]
pub extern "C" fn http3_send_priority_update_for_request(
conn: &mut Http3Connection,
Expand All @@ -2370,6 +2399,7 @@ pub extern "C" fn http3_send_priority_update_for_request(
}

/// Take the last PRIORITY_UPDATE for the given stream.
#[cfg(feature = "h3")]
#[no_mangle]
pub extern "C" fn http3_take_priority_update(
conn: &mut Http3Connection,
Expand All @@ -2395,6 +2425,7 @@ pub extern "C" fn http3_take_priority_update(
}

/// Convert HTTP/3 header.
#[cfg(feature = "h3")]
fn headers_from_ptr<'a>(ptr: *const Header, len: size_t) -> Vec<h3::HeaderRef<'a>> {
let headers = unsafe { slice::from_raw_parts(ptr, len) };

Expand Down Expand Up @@ -2433,6 +2464,7 @@ pub extern "C" fn quic_set_logger(
}
}

#[cfg(feature = "h3")]
#[repr(C)]
pub struct Http3Methods {
/// Called when the stream got headers.
Expand All @@ -2455,18 +2487,24 @@ pub struct Http3Methods {
pub on_conn_goaway: Option<fn(ctx: *mut c_void, stream_id: u64)>,
}

#[cfg(feature = "h3")]
#[repr(transparent)]
pub struct Http3Context(*mut c_void);

#[cfg(feature = "h3")]
#[repr(C)]
pub struct Http3Handler {
pub methods: *const Http3Methods,
pub context: Http3Context,
}

#[cfg(feature = "h3")]
unsafe impl Send for Http3Handler {}

#[cfg(feature = "h3")]
unsafe impl Sync for Http3Handler {}

#[cfg(feature = "h3")]
impl crate::h3::Http3Handler for Http3Handler {
fn on_stream_headers(&self, stream_id: u64, ev: &mut Http3Event) {
unsafe {
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1236,6 +1236,7 @@ mod multipath_scheduler;
#[path = "tls/tls.rs"]
mod tls;

#[cfg(feature = "h3")]
#[path = "h3/h3.rs"]
pub mod h3;

Expand Down

0 comments on commit e440c06

Please sign in to comment.