Skip to content

Commit e7bbcfe

Browse files
committed
Add helper to get the sapi name
Useful to determine if the context is CLI / FPM etc
1 parent 42ef04a commit e7bbcfe

File tree

4 files changed

+181
-1
lines changed

4 files changed

+181
-1
lines changed

allowed_bindings.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,5 +248,6 @@ bind! {
248248
tsrm_get_ls_cache,
249249
executor_globals_offset,
250250
zend_atomic_bool_store,
251+
sapi_module,
251252
zend_interrupt_function
252253
}

docsrs_bindings.rs

Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,28 @@ extern "C" {
323323
extern "C" {
324324
pub fn __zend_malloc(len: usize) -> *mut ::std::os::raw::c_void;
325325
}
326+
#[repr(C)]
327+
#[derive(Debug, Copy, Clone)]
328+
pub struct _zend_llist_element {
329+
pub next: *mut _zend_llist_element,
330+
pub prev: *mut _zend_llist_element,
331+
pub data: [::std::os::raw::c_char; 1usize],
332+
}
333+
pub type zend_llist_element = _zend_llist_element;
334+
pub type llist_dtor_func_t =
335+
::std::option::Option<unsafe extern "C" fn(arg1: *mut ::std::os::raw::c_void)>;
336+
#[repr(C)]
337+
#[derive(Debug, Copy, Clone)]
338+
pub struct _zend_llist {
339+
pub head: *mut zend_llist_element,
340+
pub tail: *mut zend_llist_element,
341+
pub count: usize,
342+
pub size: usize,
343+
pub dtor: llist_dtor_func_t,
344+
pub persistent: ::std::os::raw::c_uchar,
345+
pub traverse_ptr: *mut zend_llist_element,
346+
}
347+
pub type zend_llist = _zend_llist;
326348
pub type zend_string_init_interned_func_t = ::std::option::Option<
327349
unsafe extern "C" fn(
328350
str_: *const ::std::os::raw::c_char,
@@ -473,6 +495,29 @@ pub struct _zend_class_arrayaccess_funcs {
473495
pub type zend_class_arrayaccess_funcs = _zend_class_arrayaccess_funcs;
474496
#[repr(C)]
475497
#[derive(Debug, Copy, Clone)]
498+
pub struct stat {
499+
pub st_dev: dev_t,
500+
pub st_mode: mode_t,
501+
pub st_nlink: nlink_t,
502+
pub st_ino: __darwin_ino64_t,
503+
pub st_uid: uid_t,
504+
pub st_gid: gid_t,
505+
pub st_rdev: dev_t,
506+
pub st_atimespec: timespec,
507+
pub st_mtimespec: timespec,
508+
pub st_ctimespec: timespec,
509+
pub st_birthtimespec: timespec,
510+
pub st_size: off_t,
511+
pub st_blocks: blkcnt_t,
512+
pub st_blksize: blksize_t,
513+
pub st_flags: __uint32_t,
514+
pub st_gen: __uint32_t,
515+
pub st_lspare: __int32_t,
516+
pub st_qspare: [__int64_t; 2usize],
517+
}
518+
pub type zend_stat_t = stat;
519+
#[repr(C)]
520+
#[derive(Debug, Copy, Clone)]
476521
pub struct _zend_serialize_data {
477522
_unused: [u8; 0],
478523
}
@@ -1514,3 +1559,127 @@ extern "C" {
15141559
extern "C" {
15151560
pub static mut zend_ce_stringable: *mut zend_class_entry;
15161561
}
1562+
#[repr(C)]
1563+
#[derive(Debug, Copy, Clone)]
1564+
pub struct sapi_header_struct {
1565+
pub header: *mut ::std::os::raw::c_char,
1566+
pub header_len: usize,
1567+
}
1568+
#[repr(C)]
1569+
#[derive(Debug, Copy, Clone)]
1570+
pub struct sapi_headers_struct {
1571+
pub headers: zend_llist,
1572+
pub http_response_code: ::std::os::raw::c_int,
1573+
pub send_default_content_type: ::std::os::raw::c_uchar,
1574+
pub mimetype: *mut ::std::os::raw::c_char,
1575+
pub http_status_line: *mut ::std::os::raw::c_char,
1576+
}
1577+
pub type sapi_module_struct = _sapi_module_struct;
1578+
extern "C" {
1579+
pub static mut sapi_module: sapi_module_struct;
1580+
}
1581+
pub const sapi_header_op_enum_SAPI_HEADER_REPLACE: sapi_header_op_enum = 0;
1582+
pub const sapi_header_op_enum_SAPI_HEADER_ADD: sapi_header_op_enum = 1;
1583+
pub const sapi_header_op_enum_SAPI_HEADER_DELETE: sapi_header_op_enum = 2;
1584+
pub const sapi_header_op_enum_SAPI_HEADER_DELETE_ALL: sapi_header_op_enum = 3;
1585+
pub const sapi_header_op_enum_SAPI_HEADER_SET_STATUS: sapi_header_op_enum = 4;
1586+
pub type sapi_header_op_enum = ::std::os::raw::c_uint;
1587+
#[repr(C)]
1588+
#[derive(Debug, Copy, Clone)]
1589+
pub struct _sapi_module_struct {
1590+
pub name: *mut ::std::os::raw::c_char,
1591+
pub pretty_name: *mut ::std::os::raw::c_char,
1592+
pub startup: ::std::option::Option<
1593+
unsafe extern "C" fn(sapi_module: *mut _sapi_module_struct) -> ::std::os::raw::c_int,
1594+
>,
1595+
pub shutdown: ::std::option::Option<
1596+
unsafe extern "C" fn(sapi_module: *mut _sapi_module_struct) -> ::std::os::raw::c_int,
1597+
>,
1598+
pub activate: ::std::option::Option<unsafe extern "C" fn() -> ::std::os::raw::c_int>,
1599+
pub deactivate: ::std::option::Option<unsafe extern "C" fn() -> ::std::os::raw::c_int>,
1600+
pub ub_write: ::std::option::Option<
1601+
unsafe extern "C" fn(str_: *const ::std::os::raw::c_char, str_length: usize) -> usize,
1602+
>,
1603+
pub flush:
1604+
::std::option::Option<unsafe extern "C" fn(server_context: *mut ::std::os::raw::c_void)>,
1605+
pub get_stat: ::std::option::Option<unsafe extern "C" fn() -> *mut zend_stat_t>,
1606+
pub getenv: ::std::option::Option<
1607+
unsafe extern "C" fn(
1608+
name: *const ::std::os::raw::c_char,
1609+
name_len: usize,
1610+
) -> *mut ::std::os::raw::c_char,
1611+
>,
1612+
pub sapi_error: ::std::option::Option<
1613+
unsafe extern "C" fn(
1614+
type_: ::std::os::raw::c_int,
1615+
error_msg: *const ::std::os::raw::c_char,
1616+
...
1617+
),
1618+
>,
1619+
pub header_handler: ::std::option::Option<
1620+
unsafe extern "C" fn(
1621+
sapi_header: *mut sapi_header_struct,
1622+
op: sapi_header_op_enum,
1623+
sapi_headers: *mut sapi_headers_struct,
1624+
) -> ::std::os::raw::c_int,
1625+
>,
1626+
pub send_headers: ::std::option::Option<
1627+
unsafe extern "C" fn(sapi_headers: *mut sapi_headers_struct) -> ::std::os::raw::c_int,
1628+
>,
1629+
pub send_header: ::std::option::Option<
1630+
unsafe extern "C" fn(
1631+
sapi_header: *mut sapi_header_struct,
1632+
server_context: *mut ::std::os::raw::c_void,
1633+
),
1634+
>,
1635+
pub read_post: ::std::option::Option<
1636+
unsafe extern "C" fn(buffer: *mut ::std::os::raw::c_char, count_bytes: usize) -> usize,
1637+
>,
1638+
pub read_cookies: ::std::option::Option<unsafe extern "C" fn() -> *mut ::std::os::raw::c_char>,
1639+
pub register_server_variables:
1640+
::std::option::Option<unsafe extern "C" fn(track_vars_array: *mut zval)>,
1641+
pub log_message: ::std::option::Option<
1642+
unsafe extern "C" fn(
1643+
message: *const ::std::os::raw::c_char,
1644+
syslog_type_int: ::std::os::raw::c_int,
1645+
),
1646+
>,
1647+
pub get_request_time:
1648+
::std::option::Option<unsafe extern "C" fn(request_time: *mut f64) -> zend_result>,
1649+
pub terminate_process: ::std::option::Option<unsafe extern "C" fn()>,
1650+
pub php_ini_path_override: *mut ::std::os::raw::c_char,
1651+
pub default_post_reader: ::std::option::Option<unsafe extern "C" fn()>,
1652+
pub treat_data: ::std::option::Option<
1653+
unsafe extern "C" fn(
1654+
arg: ::std::os::raw::c_int,
1655+
str_: *mut ::std::os::raw::c_char,
1656+
destArray: *mut zval,
1657+
),
1658+
>,
1659+
pub executable_location: *mut ::std::os::raw::c_char,
1660+
pub php_ini_ignore: ::std::os::raw::c_int,
1661+
pub php_ini_ignore_cwd: ::std::os::raw::c_int,
1662+
pub get_fd: ::std::option::Option<
1663+
unsafe extern "C" fn(fd: *mut ::std::os::raw::c_int) -> ::std::os::raw::c_int,
1664+
>,
1665+
pub force_http_10: ::std::option::Option<unsafe extern "C" fn() -> ::std::os::raw::c_int>,
1666+
pub get_target_uid:
1667+
::std::option::Option<unsafe extern "C" fn(arg1: *mut uid_t) -> ::std::os::raw::c_int>,
1668+
pub get_target_gid:
1669+
::std::option::Option<unsafe extern "C" fn(arg1: *mut gid_t) -> ::std::os::raw::c_int>,
1670+
pub input_filter: ::std::option::Option<
1671+
unsafe extern "C" fn(
1672+
arg: ::std::os::raw::c_int,
1673+
var: *const ::std::os::raw::c_char,
1674+
val: *mut *mut ::std::os::raw::c_char,
1675+
val_len: usize,
1676+
new_val_len: *mut usize,
1677+
) -> ::std::os::raw::c_uint,
1678+
>,
1679+
pub ini_defaults:
1680+
::std::option::Option<unsafe extern "C" fn(configuration_hash: *mut HashTable)>,
1681+
pub phpinfo_as_text: ::std::os::raw::c_int,
1682+
pub ini_entries: *mut ::std::os::raw::c_char,
1683+
pub additional_functions: *const zend_function_entry,
1684+
pub input_filter_init: ::std::option::Option<unsafe extern "C" fn() -> ::std::os::raw::c_uint>,
1685+
}

src/wrapper.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "zend_exceptions.h"
2121
#include "zend_inheritance.h"
2222
#include "zend_interfaces.h"
23+
#include "SAPI.h"
2324

2425
zend_string *ext_php_rs_zend_string_init(const char *str, size_t len, bool persistent);
2526
void ext_php_rs_zend_string_release(zend_string *zs);

src/zend/mod.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@ mod globals;
99
mod handlers;
1010
mod module;
1111

12-
use crate::{error::Result, ffi::php_printf};
12+
use crate::{
13+
error::Result,
14+
ffi::{php_printf, sapi_module},
15+
};
1316
use std::ffi::CString;
1417

1518
pub use _type::ZendType;
@@ -42,3 +45,9 @@ pub fn printf(message: &str) -> Result<()> {
4245
};
4346
Ok(())
4447
}
48+
49+
/// Get the name of the SAPI module.
50+
pub fn php_sapi_name() -> String {
51+
let c_str = unsafe { std::ffi::CStr::from_ptr(sapi_module.name) };
52+
c_str.to_str().expect("Unable to parse CStr").to_string()
53+
}

0 commit comments

Comments
 (0)