|
1 | 1 | use crate::embed::ext_php_rs_php_error; |
2 | 2 | use crate::ffi::{ |
3 | 3 | gid_t, php_default_input_filter, php_default_post_reader, php_default_treat_data, |
4 | | - sapi_header_struct, uid_t, |
| 4 | + sapi_header_struct, sapi_headers_struct, uid_t, |
5 | 5 | }; |
6 | 6 | use crate::types::Zval; |
7 | 7 | use crate::{embed::SapiModule, error::Result}; |
@@ -192,6 +192,18 @@ impl SapiBuilder { |
192 | 192 | self |
193 | 193 | } |
194 | 194 |
|
| 195 | + /// Sets the send headers function for this SAPI |
| 196 | + /// |
| 197 | + /// This function is called once when all headers are finalized and ready to send. |
| 198 | + /// |
| 199 | + /// # Arguments |
| 200 | + /// |
| 201 | + /// * `func` - The function to be called when headers are ready. |
| 202 | + pub fn send_headers_function(mut self, func: SapiSendHeadersFunc) -> Self { |
| 203 | + self.module.send_headers = Some(func); |
| 204 | + self |
| 205 | + } |
| 206 | + |
195 | 207 | /// Sets the read post function for this SAPI |
196 | 208 | /// |
197 | 209 | /// # Parameters |
@@ -388,6 +400,9 @@ pub type SapiDeactivateFunc = extern "C" fn() -> c_int; |
388 | 400 | pub type SapiSendHeaderFunc = |
389 | 401 | extern "C" fn(header: *mut sapi_header_struct, server_context: *mut c_void); |
390 | 402 |
|
| 403 | +/// A function to be called when PHP finalizes all headers |
| 404 | +pub type SapiSendHeadersFunc = extern "C" fn(sapi_headers: *mut sapi_headers_struct) -> c_int; |
| 405 | + |
391 | 406 | /// A function to be called when PHP write to the output buffer |
392 | 407 | pub type SapiUbWriteFunc = extern "C" fn(str: *const c_char, str_length: usize) -> usize; |
393 | 408 |
|
@@ -454,6 +469,9 @@ mod test { |
454 | 469 | // extern "C" fn test_sapi_error(_type: c_int, _error_msg: *const c_char, _args: ...) {} |
455 | 470 | extern "C" fn test_send_header(_header: *mut sapi_header_struct, _server_context: *mut c_void) { |
456 | 471 | } |
| 472 | + extern "C" fn test_send_headers(_sapi_headers: *mut sapi_headers_struct) -> c_int { |
| 473 | + 0 |
| 474 | + } |
457 | 475 | extern "C" fn test_read_post(_buffer: *mut c_char, _length: usize) -> usize { |
458 | 476 | 0 |
459 | 477 | } |
@@ -609,6 +627,21 @@ mod test { |
609 | 627 | ); |
610 | 628 | } |
611 | 629 |
|
| 630 | + #[test] |
| 631 | + fn test_send_headers_function() { |
| 632 | + let sapi = SapiBuilder::new("test", "Test") |
| 633 | + .send_headers_function(test_send_headers) |
| 634 | + .build() |
| 635 | + .expect("should build sapi module"); |
| 636 | + |
| 637 | + assert!(sapi.send_headers.is_some()); |
| 638 | + assert_eq!( |
| 639 | + sapi.send_headers |
| 640 | + .expect("should have send_headers function") as usize, |
| 641 | + test_send_headers as usize |
| 642 | + ); |
| 643 | + } |
| 644 | + |
612 | 645 | #[test] |
613 | 646 | fn test_read_post_function() { |
614 | 647 | let sapi = SapiBuilder::new("test", "Test") |
|
0 commit comments