Skip to content

Commit d5022a1

Browse files
authored
Merge pull request #576 from Qard/add-sapi-send-headers
2 parents 3f6595d + e933246 commit d5022a1

File tree

1 file changed

+34
-1
lines changed

1 file changed

+34
-1
lines changed

src/builders/sapi.rs

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::embed::ext_php_rs_php_error;
22
use crate::ffi::{
33
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,
55
};
66
use crate::types::Zval;
77
use crate::{embed::SapiModule, error::Result};
@@ -192,6 +192,18 @@ impl SapiBuilder {
192192
self
193193
}
194194

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+
195207
/// Sets the read post function for this SAPI
196208
///
197209
/// # Parameters
@@ -388,6 +400,9 @@ pub type SapiDeactivateFunc = extern "C" fn() -> c_int;
388400
pub type SapiSendHeaderFunc =
389401
extern "C" fn(header: *mut sapi_header_struct, server_context: *mut c_void);
390402

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+
391406
/// A function to be called when PHP write to the output buffer
392407
pub type SapiUbWriteFunc = extern "C" fn(str: *const c_char, str_length: usize) -> usize;
393408

@@ -454,6 +469,9 @@ mod test {
454469
// extern "C" fn test_sapi_error(_type: c_int, _error_msg: *const c_char, _args: ...) {}
455470
extern "C" fn test_send_header(_header: *mut sapi_header_struct, _server_context: *mut c_void) {
456471
}
472+
extern "C" fn test_send_headers(_sapi_headers: *mut sapi_headers_struct) -> c_int {
473+
0
474+
}
457475
extern "C" fn test_read_post(_buffer: *mut c_char, _length: usize) -> usize {
458476
0
459477
}
@@ -609,6 +627,21 @@ mod test {
609627
);
610628
}
611629

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+
612645
#[test]
613646
fn test_read_post_function() {
614647
let sapi = SapiBuilder::new("test", "Test")

0 commit comments

Comments
 (0)