eggfetch-ffi 0.2.0

Stable C ABI bindings for the eggfetch-core HTTP engine
Documentation
//! Response FFI functions.

use crate::handle::ResponseHandle;

/// Free a response handle.
///
/// # Safety
///
/// `handle` must have been returned by an eggfetch FFI function and not freed yet.
/// Passing a null pointer is a no-op.
#[no_mangle]
pub unsafe extern "C" fn eggfetch_response_free(handle: *mut ResponseHandle) {
    crate::ffi_guard!((), {
        if !handle.is_null() {
            drop(Box::from_raw(handle));
        }
    });
}

/// Get the HTTP status code of the response.
///
/// Returns 0 if handle is null.
///
/// # Safety
///
/// `handle` must be a valid, non-freed response handle.
#[no_mangle]
pub unsafe extern "C" fn eggfetch_response_status(handle: *const ResponseHandle) -> u16 {
    crate::ffi_guard!(0, { handle.as_ref().map_or(0, |h| h.status) })
}

/// Get the response URL as a newly allocated C string.
///
/// Returns null if handle is null or the URL contains an interior null
/// byte. Caller must free with [`crate::handle::eggfetch_string_free`].
///
/// # Safety
///
/// `handle` must be a valid, non-freed response handle.
#[no_mangle]
pub unsafe extern "C" fn eggfetch_response_url(
    handle: *const ResponseHandle,
) -> *mut std::os::raw::c_char {
    crate::ffi_guard!(std::ptr::null_mut(), {
        let Some(handle) = handle.as_ref() else {
            return std::ptr::null_mut();
        };
        crate::handle::FfiString::from_string(handle.url.clone())
            .map_or_else(std::ptr::null_mut, crate::handle::FfiString::into_raw)
    })
}

/// Get the number of response headers.
///
/// Returns 0 if handle is null.
///
/// # Safety
///
/// `handle` must be a valid, non-freed response handle.
#[no_mangle]
pub unsafe extern "C" fn eggfetch_response_header_count(handle: *const ResponseHandle) -> usize {
    crate::ffi_guard!(0, { handle.as_ref().map_or(0, |h| h.headers.len()) })
}

/// Get a response header by index.
///
/// On success, sets `*name_out` and `*value_out` to newly allocated C strings.
/// Caller must free both with [`crate::handle::eggfetch_string_free`].
///
/// Returns 0 on success, -1 on error (invalid index, null handle, or a
/// name/value containing an interior null byte).
///
/// # Safety
///
/// - `handle` must be a valid, non-freed response handle.
/// - `name_out` and `value_out` must be valid pointers to `*mut c_char`.
#[no_mangle]
pub unsafe extern "C" fn eggfetch_response_header(
    handle: *const ResponseHandle,
    index: usize,
    name_out: *mut *mut std::os::raw::c_char,
    value_out: *mut *mut std::os::raw::c_char,
) -> i32 {
    crate::ffi_guard!(-1, {
        let Some(handle) = handle.as_ref() else {
            return -1;
        };
        if name_out.is_null() || value_out.is_null() {
            return -1;
        }
        let Some((name, value)) = handle.headers.get(index) else {
            return -1;
        };
        let Some(name_str) = crate::handle::FfiString::from_string(name.clone()) else {
            return -1;
        };
        let Some(value_str) = crate::handle::FfiString::from_string(value.clone()) else {
            return -1;
        };
        *name_out = name_str.into_raw();
        *value_out = value_str.into_raw();
        0
    })
}

/// Get the response body as a newly allocated byte buffer.
///
/// On success, sets `*data_out` to a newly allocated buffer and `*len_out` to its length.
/// Caller must free the buffer with [`eggfetch_body_free`].
///
/// Returns 0 on success, -1 on error.
///
/// # Safety
///
/// - `handle` must be a valid, non-freed response handle.
/// - `data_out` and `len_out` must be valid pointers.
#[no_mangle]
pub unsafe extern "C" fn eggfetch_response_body(
    handle: *const ResponseHandle,
    data_out: *mut *mut u8,
    len_out: *mut usize,
) -> i32 {
    crate::ffi_guard!(-1, {
        let Some(handle) = handle.as_ref() else {
            return -1;
        };
        if data_out.is_null() || len_out.is_null() {
            return -1;
        }
        let len = handle.body.len();
        if len == 0 {
            *data_out = std::ptr::null_mut();
            *len_out = 0;
            return 0;
        }
        let Ok(layout) = std::alloc::Layout::array::<u8>(len) else {
            return -1;
        };
        let buf = std::alloc::alloc(layout);
        if buf.is_null() {
            return -1;
        }
        std::ptr::copy_nonoverlapping(handle.body.as_ptr(), buf, len);
        *data_out = buf;
        *len_out = len;
        0
    })
}

/// Free a body buffer allocated by [`eggfetch_response_body`].
///
/// # Safety
///
/// - `data` must have been returned by [`eggfetch_response_body`].
/// - `len` must be the length returned with that buffer.
/// - Passing a null pointer is a no-op.
#[no_mangle]
pub unsafe extern "C" fn eggfetch_body_free(data: *mut u8, len: usize) {
    crate::ffi_guard!((), {
        if !data.is_null() && len > 0 {
            // A buffer this API allocated always had a computable layout,
            // so failure here implies caller corruption; leak rather than
            // abort the host process.
            if let Ok(layout) = std::alloc::Layout::array::<u8>(len) {
                std::alloc::dealloc(data, layout);
            }
        }
    });
}