Skip to content
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
9 changes: 8 additions & 1 deletion allowed_bindings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,14 @@ bind! {
zend_eval_string,
zend_file_handle,
zend_stream_init_filename,
zend_destroy_file_handle,
php_execute_script,
zend_register_module_ex,
_zend_bailout
_zend_bailout,
sapi_startup,
sapi_shutdown,
php_module_startup,
php_module_shutdown,
php_request_startup,
php_request_shutdown
}
249 changes: 138 additions & 111 deletions docsrs_bindings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -746,6 +746,9 @@ extern "C" {
filename: *const ::std::os::raw::c_char,
);
}
extern "C" {
pub fn zend_destroy_file_handle(file_handle: *mut zend_file_handle);
}
pub type zend_stat_t = stat;
#[repr(C)]
#[derive(Debug, Copy, Clone)]
Expand Down Expand Up @@ -2333,117 +2336,6 @@ extern "C" {
module_number: ::std::os::raw::c_int,
);
}
extern "C" {
pub fn php_info_print_table_header(num_cols: ::std::os::raw::c_int, ...);
}
extern "C" {
pub fn php_info_print_table_row(num_cols: ::std::os::raw::c_int, ...);
}
extern "C" {
pub fn php_info_print_table_start();
}
extern "C" {
pub fn php_info_print_table_end();
}
#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct hostent {
pub h_name: *mut ::std::os::raw::c_char,
pub h_aliases: *mut *mut ::std::os::raw::c_char,
pub h_addrtype: ::std::os::raw::c_int,
pub h_length: ::std::os::raw::c_int,
pub h_addr_list: *mut *mut ::std::os::raw::c_char,
}
#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct php_file_globals {
pub pclose_ret: ::std::os::raw::c_int,
pub def_chunk_size: usize,
pub auto_detect_line_endings: bool,
pub default_socket_timeout: zend_long,
pub user_agent: *mut ::std::os::raw::c_char,
pub from_address: *mut ::std::os::raw::c_char,
pub user_stream_current_filename: *const ::std::os::raw::c_char,
pub default_context: *mut php_stream_context,
pub stream_wrappers: *mut HashTable,
pub stream_filters: *mut HashTable,
pub wrapper_errors: *mut HashTable,
pub pclose_wait: ::std::os::raw::c_int,
pub tmp_host_info: hostent,
pub tmp_host_buf: *mut ::std::os::raw::c_char,
pub tmp_host_buf_len: usize,
}
extern "C" {
pub static mut file_globals: php_file_globals;
}
extern "C" {
pub static mut zend_ce_throwable: *mut zend_class_entry;
}
extern "C" {
pub static mut zend_ce_exception: *mut zend_class_entry;
}
extern "C" {
pub static mut zend_ce_error_exception: *mut zend_class_entry;
}
extern "C" {
pub static mut zend_ce_compile_error: *mut zend_class_entry;
}
extern "C" {
pub static mut zend_ce_parse_error: *mut zend_class_entry;
}
extern "C" {
pub static mut zend_ce_type_error: *mut zend_class_entry;
}
extern "C" {
pub static mut zend_ce_argument_count_error: *mut zend_class_entry;
}
extern "C" {
pub static mut zend_ce_value_error: *mut zend_class_entry;
}
extern "C" {
pub static mut zend_ce_arithmetic_error: *mut zend_class_entry;
}
extern "C" {
pub static mut zend_ce_division_by_zero_error: *mut zend_class_entry;
}
extern "C" {
pub static mut zend_ce_unhandled_match_error: *mut zend_class_entry;
}
extern "C" {
pub fn zend_throw_exception_ex(
exception_ce: *mut zend_class_entry,
code: zend_long,
format: *const ::std::os::raw::c_char,
...
) -> *mut zend_object;
}
extern "C" {
pub fn zend_throw_exception_object(exception: *mut zval);
}
extern "C" {
pub fn zend_do_implement_interface(ce: *mut zend_class_entry, iface: *mut zend_class_entry);
}
extern "C" {
pub static mut zend_ce_traversable: *mut zend_class_entry;
}
extern "C" {
pub static mut zend_ce_aggregate: *mut zend_class_entry;
}
extern "C" {
pub static mut zend_ce_iterator: *mut zend_class_entry;
}
extern "C" {
pub static mut zend_ce_arrayaccess: *mut zend_class_entry;
}
extern "C" {
pub static mut zend_ce_serializable: *mut zend_class_entry;
}
extern "C" {
pub static mut zend_ce_countable: *mut zend_class_entry;
}
extern "C" {
pub static mut zend_ce_stringable: *mut zend_class_entry;
}
#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct sapi_header_struct {
Expand Down Expand Up @@ -2514,6 +2406,12 @@ pub type sapi_globals_struct = _sapi_globals_struct;
extern "C" {
pub static mut sapi_globals: sapi_globals_struct;
}
extern "C" {
pub fn sapi_startup(sf: *mut sapi_module_struct);
}
extern "C" {
pub fn sapi_shutdown();
}
pub const sapi_header_op_enum_SAPI_HEADER_REPLACE: sapi_header_op_enum = 0;
pub const sapi_header_op_enum_SAPI_HEADER_ADD: sapi_header_op_enum = 1;
pub const sapi_header_op_enum_SAPI_HEADER_DELETE: sapi_header_op_enum = 2;
Expand Down Expand Up @@ -2632,3 +2530,132 @@ pub struct _sapi_post_entry {
),
>,
}
extern "C" {
pub fn php_request_startup() -> zend_result;
}
extern "C" {
pub fn php_request_shutdown(dummy: *mut ::std::os::raw::c_void);
}
extern "C" {
pub fn php_module_startup(
sf: *mut sapi_module_struct,
additional_module: *mut zend_module_entry,
) -> zend_result;
}
extern "C" {
pub fn php_module_shutdown();
}
extern "C" {
pub fn php_execute_script(primary_file: *mut zend_file_handle) -> bool;
}
extern "C" {
pub fn php_info_print_table_header(num_cols: ::std::os::raw::c_int, ...);
}
extern "C" {
pub fn php_info_print_table_row(num_cols: ::std::os::raw::c_int, ...);
}
extern "C" {
pub fn php_info_print_table_start();
}
extern "C" {
pub fn php_info_print_table_end();
}
#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct hostent {
pub h_name: *mut ::std::os::raw::c_char,
pub h_aliases: *mut *mut ::std::os::raw::c_char,
pub h_addrtype: ::std::os::raw::c_int,
pub h_length: ::std::os::raw::c_int,
pub h_addr_list: *mut *mut ::std::os::raw::c_char,
}
#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct php_file_globals {
pub pclose_ret: ::std::os::raw::c_int,
pub def_chunk_size: usize,
pub auto_detect_line_endings: bool,
pub default_socket_timeout: zend_long,
pub user_agent: *mut ::std::os::raw::c_char,
pub from_address: *mut ::std::os::raw::c_char,
pub user_stream_current_filename: *const ::std::os::raw::c_char,
pub default_context: *mut php_stream_context,
pub stream_wrappers: *mut HashTable,
pub stream_filters: *mut HashTable,
pub wrapper_errors: *mut HashTable,
pub pclose_wait: ::std::os::raw::c_int,
pub tmp_host_info: hostent,
pub tmp_host_buf: *mut ::std::os::raw::c_char,
pub tmp_host_buf_len: usize,
}
extern "C" {
pub static mut file_globals: php_file_globals;
}
extern "C" {
pub static mut zend_ce_throwable: *mut zend_class_entry;
}
extern "C" {
pub static mut zend_ce_exception: *mut zend_class_entry;
}
extern "C" {
pub static mut zend_ce_error_exception: *mut zend_class_entry;
}
extern "C" {
pub static mut zend_ce_compile_error: *mut zend_class_entry;
}
extern "C" {
pub static mut zend_ce_parse_error: *mut zend_class_entry;
}
extern "C" {
pub static mut zend_ce_type_error: *mut zend_class_entry;
}
extern "C" {
pub static mut zend_ce_argument_count_error: *mut zend_class_entry;
}
extern "C" {
pub static mut zend_ce_value_error: *mut zend_class_entry;
}
extern "C" {
pub static mut zend_ce_arithmetic_error: *mut zend_class_entry;
}
extern "C" {
pub static mut zend_ce_division_by_zero_error: *mut zend_class_entry;
}
extern "C" {
pub static mut zend_ce_unhandled_match_error: *mut zend_class_entry;
}
extern "C" {
pub fn zend_throw_exception_ex(
exception_ce: *mut zend_class_entry,
code: zend_long,
format: *const ::std::os::raw::c_char,
...
) -> *mut zend_object;
}
extern "C" {
pub fn zend_throw_exception_object(exception: *mut zval);
}
extern "C" {
pub fn zend_do_implement_interface(ce: *mut zend_class_entry, iface: *mut zend_class_entry);
}
extern "C" {
pub static mut zend_ce_traversable: *mut zend_class_entry;
}
extern "C" {
pub static mut zend_ce_aggregate: *mut zend_class_entry;
}
extern "C" {
pub static mut zend_ce_iterator: *mut zend_class_entry;
}
extern "C" {
pub static mut zend_ce_arrayaccess: *mut zend_class_entry;
}
extern "C" {
pub static mut zend_ce_serializable: *mut zend_class_entry;
}
extern "C" {
pub static mut zend_ce_countable: *mut zend_class_entry;
}
extern "C" {
pub static mut zend_ce_stringable: *mut zend_class_entry;
}
4 changes: 4 additions & 0 deletions src/builders/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@
mod class;
mod function;
mod module;
#[cfg(feature = "embed")]
mod sapi;

pub use class::ClassBuilder;
pub use function::FunctionBuilder;
pub use module::ModuleBuilder;
#[cfg(feature = "embed")]
pub use sapi::SapiBuilder;
96 changes: 96 additions & 0 deletions src/builders/sapi.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
use crate::ffi::sapi_header_struct;
use crate::{embed::SapiModule, error::Result};

use std::ffi::{c_char, c_void};
use std::{ffi::CString, ptr};

pub struct SapiBuilder {
name: String,
pretty_name: String,
module: SapiModule,
}

impl SapiBuilder {
pub fn new<T: Into<String>, U: Into<String>>(name: T, pretty_name: U) -> Self {
Self {
name: name.into(),
pretty_name: pretty_name.into(),
module: SapiModule {
name: ptr::null_mut(),
pretty_name: ptr::null_mut(),
startup: None,
shutdown: None,
activate: None,
deactivate: None,
ub_write: None,
flush: None,
get_stat: None,
getenv: None,
sapi_error: None,
header_handler: None,
send_headers: None,
send_header: None,
read_post: None,
read_cookies: None,
register_server_variables: None,
log_message: None,
get_request_time: None,
terminate_process: None,
php_ini_path_override: ptr::null_mut(),
default_post_reader: None,
treat_data: None,
executable_location: ptr::null_mut(),
php_ini_ignore: 0,
php_ini_ignore_cwd: 0,
get_fd: None,
force_http_10: None,
get_target_uid: None,
get_target_gid: None,
input_filter: None,
ini_defaults: None,
phpinfo_as_text: 0,
ini_entries: ptr::null_mut(),
additional_functions: ptr::null(),
input_filter_init: None,
},
}
}

pub fn ub_write_function(mut self, func: SapiUbWriteFunc) -> Self {
self.module.ub_write = Some(func);
self
}

/// Sets the send header function for this SAPI
///
/// # Arguments
///
/// * `func` - The function to be called on shutdown.
pub fn send_header_function(mut self, func: SapiSendHeaderFunc) -> Self {
self.module.send_header = Some(func);
self
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a lot of thing can be defined but ATM this is the only required function for PHP to not segfault (that's why we set a dummy handler if nothing is set latter)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's enough for me, let's make it evolve when users will have more usage


/// Builds the extension and returns a `SapiModule`.
///
/// Returns a result containing the sapi module if successful.
pub fn build(mut self) -> Result<SapiModule> {
self.module.name = CString::new(self.name)?.into_raw();
self.module.pretty_name = CString::new(self.pretty_name)?.into_raw();

if self.module.send_header.is_none() {
self.module.send_header = Some(dummy_send_header);
}

Ok(self.module)
}
}

/// A function to be called when PHP send a header
pub type SapiSendHeaderFunc =
extern "C" fn(header: *mut sapi_header_struct, server_context: *mut c_void);

/// A function to be called when PHP write to the output buffer
pub type SapiUbWriteFunc = extern "C" fn(str: *const c_char, str_length: usize) -> usize;

extern "C" fn dummy_send_header(_header: *mut sapi_header_struct, _server_context: *mut c_void) {}
Loading