|
| 1 | +use crate::ffi::sapi_header_struct; |
| 2 | +use crate::{embed::SapiModule, error::Result}; |
| 3 | + |
| 4 | +use std::ffi::c_void; |
| 5 | +use std::{ffi::CString, ptr}; |
| 6 | + |
| 7 | +pub struct SapiBuilder { |
| 8 | + name: String, |
| 9 | + pretty_name: String, |
| 10 | + module: SapiModule, |
| 11 | +} |
| 12 | + |
| 13 | +impl SapiBuilder { |
| 14 | + pub fn new<T: Into<String>, U: Into<String>>(name: T, pretty_name: U) -> Self { |
| 15 | + Self { |
| 16 | + name: name.into(), |
| 17 | + pretty_name: pretty_name.into(), |
| 18 | + module: SapiModule { |
| 19 | + name: ptr::null_mut(), |
| 20 | + pretty_name: ptr::null_mut(), |
| 21 | + startup: None, |
| 22 | + shutdown: None, |
| 23 | + activate: None, |
| 24 | + deactivate: None, |
| 25 | + ub_write: None, |
| 26 | + flush: None, |
| 27 | + get_stat: None, |
| 28 | + getenv: None, |
| 29 | + sapi_error: None, |
| 30 | + header_handler: None, |
| 31 | + send_headers: None, |
| 32 | + send_header: None, |
| 33 | + read_post: None, |
| 34 | + read_cookies: None, |
| 35 | + register_server_variables: None, |
| 36 | + log_message: None, |
| 37 | + get_request_time: None, |
| 38 | + terminate_process: None, |
| 39 | + php_ini_path_override: ptr::null_mut(), |
| 40 | + default_post_reader: None, |
| 41 | + treat_data: None, |
| 42 | + executable_location: ptr::null_mut(), |
| 43 | + php_ini_ignore: 0, |
| 44 | + php_ini_ignore_cwd: 0, |
| 45 | + get_fd: None, |
| 46 | + force_http_10: None, |
| 47 | + get_target_uid: None, |
| 48 | + get_target_gid: None, |
| 49 | + input_filter: None, |
| 50 | + ini_defaults: None, |
| 51 | + phpinfo_as_text: 0, |
| 52 | + ini_entries: ptr::null_mut(), |
| 53 | + additional_functions: ptr::null(), |
| 54 | + input_filter_init: None, |
| 55 | + }, |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + /// Sets the send header function for this SAPI |
| 60 | + /// |
| 61 | + /// # Arguments |
| 62 | + /// |
| 63 | + /// * `func` - The function to be called on shutdown. |
| 64 | + pub fn send_header_function(mut self, func: SapiSendHeaderFunc) -> Self { |
| 65 | + self.module.send_header = Some(func); |
| 66 | + self |
| 67 | + } |
| 68 | + |
| 69 | + /// Builds the extension and returns a `SapiModule`. |
| 70 | + /// |
| 71 | + /// Returns a result containing the sapi module if successful. |
| 72 | + pub fn build(mut self) -> Result<SapiModule> { |
| 73 | + self.module.name = CString::new(self.name)?.into_raw(); |
| 74 | + self.module.pretty_name = CString::new(self.pretty_name)?.into_raw(); |
| 75 | + |
| 76 | + if self.module.send_header.is_none() { |
| 77 | + self.module.send_header = Some(dummy_send_header); |
| 78 | + } |
| 79 | + |
| 80 | + Ok(self.module) |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +/// A function to be called when the extension is starting up or shutting down. |
| 85 | +pub type SapiSendHeaderFunc = |
| 86 | + extern "C" fn(header: *mut sapi_header_struct, server_context: *mut c_void); |
| 87 | + |
| 88 | +extern "C" fn dummy_send_header(_header: *mut sapi_header_struct, _server_context: *mut c_void) {} |
0 commit comments