ripht-php-sapi 0.1.0-rc.9

Ripht PHP SAPI - A PHP SAPI written in Rust to expose safe and convenient APIs to encourage additional Rust tooling development for PHP
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
//! SAPI callback implementations for the Ripht PHP SAPI.
//!
//! # Safety
//!
//! All callbacks share these invariants:
//!
//! - **Threading**: NTS build only. One request executes at a time.
//! - **Context lifetime**: `sapi_globals.server_context` is valid only during
//!   request execution (between `php_request_startup` and `php_request_shutdown`).
//! - **Panic safety**: Callbacks wrap Rust code in `catch_unwind` to prevent
//!   unwinding across FFI.
//! - **Pointer validity**: PHP-provided pointers are valid for the callback duration.

#![allow(clippy::missing_safety_doc)]

use std::ffi::{CStr, CString};
use std::os::raw::{c_char, c_double, c_int, c_uint, c_void};

#[cfg(feature = "tracing")]
use tracing::{debug, error, info, trace, warn};

use super::ffi;
use super::server_context::ServerContext;
use super::RESOLVED_CONFIG;
use crate::execution::{ExecutionMessage, ResponseHeader};

const HTTP_STATUS_MIN: i32 = 100;
const HTTP_STATUS_MAX: i32 = 599;
const HTTP_STATUS_FALLBACK: u16 = 500;

/// Returns the [`ServerContext`] pointer if valid. See module docs for safety.
#[inline]
pub(crate) unsafe fn get_context() -> Option<*mut ServerContext> {
    let ptr = ffi::sapi_globals.server_context as *mut ServerContext;
    if ptr.is_null() {
        return None;
    }

    // Alignment check catches corruption
    let align = std::mem::align_of::<ServerContext>();
    if !(ptr as usize).is_multiple_of(align) {
        return None;
    }

    Some(ptr)
}

#[no_mangle]
pub unsafe extern "C" fn ripht_sapi_startup(
    _module: *mut ffi::sapi_module_struct,
) -> c_int {
    ffi::SUCCESS
}

#[no_mangle]
pub unsafe extern "C" fn ripht_sapi_shutdown(
    _module: *mut ffi::sapi_module_struct,
) -> c_int {
    ffi::SUCCESS
}

#[no_mangle]
pub extern "C" fn ripht_sapi_activate() -> c_int {
    ffi::SUCCESS
}

#[no_mangle]
pub extern "C" fn ripht_sapi_deactivate() -> c_int {
    ffi::SUCCESS
}

/// Unbuffered write callback.
#[no_mangle]
pub unsafe extern "C" fn ripht_sapi_ub_write(
    str: *const c_char,
    str_length: usize,
) -> usize {
    if str.is_null() || str_length == 0 {
        return 0;
    }

    if ffi::sapi_globals.headers_sent == 0 {
        ffi::sapi_send_headers();
    }

    let Some(ctx_ptr) = get_context() else {
        return 0;
    };

    let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
        let bytes = std::slice::from_raw_parts(str as *const u8, str_length);

        #[cfg(feature = "tracing")]
        trace!(bytes_written = str_length, "Output captured");

        (*ctx_ptr).write_output(bytes)
    }));

    result.unwrap_or(0)
}

/// Flush output callback.
#[no_mangle]
pub unsafe extern "C" fn ripht_sapi_flush(_server_context: *mut c_void) {
    #[cfg(feature = "tracing")]
    trace!("Flush called");

    if ffi::sapi_globals.headers_sent == 0 {
        ffi::sapi_send_headers();
    }

    let Some(ctx_ptr) = get_context() else {
        return;
    };

    let _ = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
        (*ctx_ptr).flush();
    }));
}

/// Send all response headers callback.
#[no_mangle]
pub unsafe extern "C" fn ripht_sapi_send_headers(
    sapi_headers: *mut ffi::sapi_headers_struct,
) -> c_int {
    if sapi_headers.is_null() {
        return ffi::SAPI_HEADER_SEND_FAILED;
    }

    let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
        let Some(ctx_ptr) = get_context() else {
            return ffi::SAPI_HEADER_SEND_FAILED;
        };

        let status = (*sapi_headers).http_response_code;
        let status_code: u16 =
            if !(HTTP_STATUS_MIN..=HTTP_STATUS_MAX).contains(&status) {
                HTTP_STATUS_FALLBACK
            } else {
                status as u16
            };

        (*ctx_ptr).set_status(status_code);
        (*ctx_ptr)
            .response_headers
            .clear();

        // Iterate PHP's header list
        let mut elem = (*sapi_headers).headers.head;
        while !elem.is_null() {
            let header_ptr =
                (*elem).data.as_ptr() as *mut ffi::sapi_header_struct;
            ripht_sapi_send_header(header_ptr, std::ptr::null_mut());
            elem = (*elem).next;
        }

        ffi::SAPI_HEADER_SENT_SUCCESSFULLY
    }));

    result.unwrap_or(ffi::SAPI_HEADER_SEND_FAILED)
}

/// Send single response header callback.
#[no_mangle]
pub unsafe extern "C" fn ripht_sapi_send_header(
    sapi_header: *mut ffi::sapi_header_struct,
    _server_context: *mut c_void,
) {
    if sapi_header.is_null() {
        return;
    }

    let _ = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
        let Some(ctx_ptr) = get_context() else {
            return;
        };

        let header_ptr = (*sapi_header).header;
        let header_len = (*sapi_header).header_len;

        if header_ptr.is_null() || header_len == 0 {
            return;
        }

        let bytes =
            std::slice::from_raw_parts(header_ptr as *const u8, header_len);

        if let Some(h) = ResponseHeader::parse(bytes) {
            (*ctx_ptr).add_header(h);
        }
    }));
}

/// Read POST body callback. May be called multiple times.
#[no_mangle]
pub unsafe extern "C" fn ripht_sapi_read_post(
    buffer: *mut c_char,
    count_bytes: usize,
) -> usize {
    if buffer.is_null() || count_bytes == 0 {
        return 0;
    }

    let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
        let Some(ctx_ptr) = get_context() else {
            return 0;
        };

        let slice =
            std::slice::from_raw_parts_mut(buffer as *mut u8, count_bytes);
        (*ctx_ptr).read_post(slice)
    }));

    result.unwrap_or(0)
}

/// Read cookies callback. Returns pointer to Cookie header value.
#[no_mangle]
pub unsafe extern "C" fn ripht_sapi_read_cookies() -> *mut c_char {
    let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
        let Some(ctx_ptr) = get_context() else {
            return std::ptr::null_mut();
        };
        (*ctx_ptr).cookie_data_ptr()
    }));

    result.unwrap_or(std::ptr::null_mut())
}

/// Register $_SERVER variables callback.
#[no_mangle]
pub unsafe extern "C" fn ripht_sapi_register_server_variables(
    track_vars_array: *mut ffi::zval,
) {
    if track_vars_array.is_null() {
        return;
    }

    let _ = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
        let sw = RESOLVED_CONFIG
            .get()
            .map(|c| c.server_software)
            .unwrap_or("Ripht/unknown");
        register_var_static(track_vars_array, c"SERVER_SOFTWARE", sw);

        let Some(ctx_ptr) = get_context() else {
            return;
        };

        let ctx = &*ctx_ptr;
        for (name, value) in ctx.server_vars() {
            ffi::php_register_variable_safe(
                name.as_ptr(),
                value.as_ptr(),
                value.as_bytes().len(),
                track_vars_array,
            );
        }
    }));
}

#[inline]
unsafe fn register_var_static(array: *mut ffi::zval, name: &CStr, value: &str) {
    if let Ok(value_cstr) = CString::new(value) {
        ffi::php_register_variable_safe(
            name.as_ptr(),
            value_cstr.as_ptr(),
            value_cstr.as_bytes().len(),
            array,
        );
    }
}

#[no_mangle]
pub extern "C" fn ripht_sapi_default_post_reader() {}

/// Parse GET/POST data callback. Delegates to PHP's default.
#[no_mangle]
pub unsafe extern "C" fn ripht_sapi_treat_data(
    arg: c_int,
    str: *mut c_char,
    dest_array: *mut ffi::zval,
) {
    let _ = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
        ffi::php_default_treat_data(arg, str, dest_array);
    }));
}

#[no_mangle]
pub unsafe extern "C" fn ripht_sapi_input_filter(
    _arg: c_int,
    _var: *const c_char,
    _val: *mut *mut c_char,
    _val_len: usize,
    _new_val_len: *mut usize,
) -> c_uint {
    ffi::php_default_input_filter(_arg, _var, _val, _val_len, _new_val_len)
}

/// Log message callback (errors, warnings, notices).
#[no_mangle]
pub unsafe extern "C" fn ripht_sapi_log_message(
    message: *const c_char,
    syslog_type: c_int,
) {
    if message.is_null() {
        return;
    }

    let msg = CStr::from_ptr(message).to_string_lossy();

    let should_log_to_stderr = get_context()
        .map(|ctx_ptr| (*ctx_ptr).log_to_stderr)
        .unwrap_or(false);

    if should_log_to_stderr {
        eprintln!("{}", msg);
    }

    let _ = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
        #[cfg(feature = "tracing")]
        match syslog_type {
            0..=3 => error!(message = %msg, "PHP error"),
            4 => warn!(message = %msg, "PHP warning"),
            5 => info!(message = %msg, "PHP notice"),
            6 => info!(message = %msg, "PHP info"),
            _ => debug!(message = %msg, "PHP debug"),
        }

        if let Some(ctx_ptr) = get_context() {
            (*ctx_ptr).add_message(ExecutionMessage::from_syslog(
                syslog_type,
                msg.to_string(),
            ));
        }
    }));
}

/// Get request time callback.
#[no_mangle]
pub unsafe extern "C" fn ripht_sapi_get_request_time(
    request_time: *mut c_double,
) -> c_int {
    if request_time.is_null() {
        return ffi::FAILURE;
    }

    let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
        use std::time::{SystemTime, UNIX_EPOCH};

        let now = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .map(|d| d.as_secs_f64())
            .unwrap_or(0.0);

        *request_time = now;
        ffi::SUCCESS
    }));

    result.unwrap_or(ffi::FAILURE)
}

/// PHP's asking for an environment variable
#[no_mangle]
pub unsafe extern "C" fn ripht_sapi_getenv(
    name: *const c_char,
    name_len: usize,
) -> *mut c_char {
    if name.is_null() || name_len == 0 {
        return std::ptr::null_mut();
    }

    let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
        let name_bytes =
            std::slice::from_raw_parts(name as *const u8, name_len);

        if let Some(ctx_ptr) = get_context() {
            if let Some(ptr) = (*ctx_ptr).get_env(name_bytes) {
                return ptr as *mut c_char;
            }
        }

        std::ptr::null_mut()
    }));

    result.unwrap_or(std::ptr::null_mut())
}

#[cfg(test)]
mod tests {
    use crate::{php_script_path, RiphtSapi, WebRequest};

    use super::*;

    unsafe fn get_context_for_test() -> Option<*mut ServerContext> {
        let ptr = super::ffi::sapi_globals.server_context as *mut ServerContext;

        if ptr.is_null() {
            return None;
        }
        let align = std::mem::align_of::<ServerContext>();
        if !(ptr as usize).is_multiple_of(align) {
            return None;
        }
        Some(ptr)
    }

    #[test]
    fn test_ub_write_null_buffer() {
        unsafe {
            let result = ripht_sapi_ub_write(std::ptr::null(), 10);
            assert_eq!(result, 0, "ub_write should return 0 for null buffer");
        }
    }

    #[test]
    fn test_ub_write_zero_length() {
        unsafe {
            let data = b"test";
            let result = ripht_sapi_ub_write(data.as_ptr() as *const c_char, 0);
            assert_eq!(result, 0, "ub_write should return 0 for zero length");
        }
    }

    #[test]
    fn test_log_message_all_levels() {
        unsafe {
            for level in 0..=7 {
                let msg = CString::new(format!("Test message level {}", level))
                    .unwrap();

                ripht_sapi_log_message(msg.as_ptr(), level);
            }
        }
    }

    #[test]
    fn test_get_request_time_null_pointer() {
        unsafe {
            let result = ripht_sapi_get_request_time(std::ptr::null_mut());
            assert_eq!(
                result,
                super::ffi::FAILURE,
                "get_request_time should return FAILURE for null pointer"
            );
        }
    }

    #[test]
    fn test_get_request_time_valid_pointer() {
        unsafe {
            let mut request_time: f64 = 0.0;
            let result = ripht_sapi_get_request_time(&mut request_time);

            assert_eq!(
                result,
                super::ffi::SUCCESS,
                "get_request_time should return SUCCESS for valid pointer"
            );

            assert!(
                request_time > 0.0,
                "request_time should be set to a positive value"
            );
        }
    }

    #[test]
    fn test_get_context_null_handling() {
        unsafe {
            super::ffi::sapi_globals.server_context = std::ptr::null_mut();

            let result = get_context_for_test();

            assert!(
                result.is_none(),
                "get_context should return None for null pointer"
            );
        }
    }

    #[test]
    fn test_get_context_alignment_check() {
        unsafe {
            let ctx = Box::new(ServerContext::new());
            let ctx_ptr = Box::into_raw(ctx);

            super::ffi::sapi_globals.server_context =
                ctx_ptr as *mut std::ffi::c_void;

            let result = get_context_for_test();

            assert!(
                result.is_some(),
                "get_context should return Some for valid aligned pointer"
            );

            let _ = Box::from_raw(ctx_ptr);
            super::ffi::sapi_globals.server_context = std::ptr::null_mut();
        }
    }

    #[test]
    fn test_read_post_bounds() {
        let php = RiphtSapi::instance();
        let script_path = php_script_path("hello.php");

        let post_data = b"test data".to_vec();

        let exec = WebRequest::post()
            .with_content_type("application/x-www-form-urlencoded")
            .with_body(post_data)
            .build(&script_path)
            .expect("failed to build POST WebRequest");

        let _result = php
            .execute(exec)
            .expect("POST request execution failed");

        unsafe {
            let mut buffer = vec![0u8; 100];
            let result = ripht_sapi_read_post(
                buffer.as_mut_ptr() as *mut c_char,
                buffer.len(),
            );
            assert!(
                result <= buffer.len(),
                "read_post should not exceed buffer size"
            );
        }
    }

    #[test]
    fn test_send_headers_null_pointer() {
        unsafe {
            let result = ripht_sapi_send_headers(std::ptr::null_mut());
            assert_eq!(
                result,
                super::ffi::SAPI_HEADER_SEND_FAILED,
                "send_headers should return SAPI_HEADER_SEND_FAILED for null pointer"
            );
        }
    }

    #[test]
    fn test_send_headers_invalid_status() {
        let php = RiphtSapi::instance();

        let script_path = php_script_path("hello.php");

        let exec = WebRequest::get()
            .build(&script_path)
            .expect("failed to build WebRequest");

        let _result = php
            .execute(exec)
            .expect("hello.php request execution failed");

        unsafe {
            let ctx = Box::new(ServerContext::new());
            let ctx_ptr = Box::into_raw(ctx);
            super::ffi::sapi_globals.server_context =
                ctx_ptr as *mut std::ffi::c_void;

            let mut headers = super::ffi::sapi_headers_struct {
                http_response_code: -1,
                ..Default::default()
            };
            let result = ripht_sapi_send_headers(&mut headers);
            assert_eq!(
                result,
                super::ffi::SAPI_HEADER_SENT_SUCCESSFULLY,
                "send_headers should handle invalid status codes gracefully"
            );

            headers.http_response_code = 99999;
            let result2 = ripht_sapi_send_headers(&mut headers);
            assert_eq!(
                result2,
                super::ffi::SAPI_HEADER_SENT_SUCCESSFULLY,
                "send_headers should handle out-of-range status codes gracefully"
            );

            let _ = Box::from_raw(ctx_ptr);
            super::ffi::sapi_globals.server_context = std::ptr::null_mut();
        }
    }

    #[test]
    fn test_header_parsing_whitespace() {
        let php = RiphtSapi::instance();
        let script_path = php_script_path("hello.php");

        let exec = WebRequest::get()
            .build(&script_path)
            .expect("failed to build WebRequest");
        let _result = php
            .execute(exec)
            .expect("hello.php request execution failed");

        unsafe {
            let mut header = super::ffi::sapi_header_struct::default();
            let header_str = b"Content-Type:   application/json\r\n";
            header.header = header_str.as_ptr() as *mut c_char;
            header.header_len = header_str.len();

            ripht_sapi_send_header(&mut header, std::ptr::null_mut());

            let header_str2 = b"X-Custom-Header:  value with spaces  \r\n";
            header.header = header_str2.as_ptr() as *mut c_char;
            header.header_len = header_str2.len();

            ripht_sapi_send_header(&mut header, std::ptr::null_mut());
        }
    }

    #[test]
    fn test_header_parsing_no_colon() {
        let php = RiphtSapi::instance();
        let script_path = php_script_path("hello.php");

        let exec = WebRequest::get()
            .build(&script_path)
            .expect("failed to build WebRequest");

        let _result = php
            .execute(exec)
            .expect("hello.php request execution failed");

        unsafe {
            let ctx = Box::new(ServerContext::new());
            let ctx_ptr = Box::into_raw(ctx);
            super::ffi::sapi_globals.server_context =
                ctx_ptr as *mut std::ffi::c_void;

            let mut header = super::ffi::sapi_header_struct::default();
            let header_str = b"InvalidHeaderNoColon\r\n";
            header.header = header_str.as_ptr() as *mut c_char;
            header.header_len = header_str.len();

            ripht_sapi_send_header(&mut header, std::ptr::null_mut());

            assert_eq!(
                (*ctx_ptr)
                    .response_headers
                    .len(),
                0,
                "Header with no colon should not be added"
            );

            let _ = Box::from_raw(ctx_ptr);
            super::ffi::sapi_globals.server_context = std::ptr::null_mut();
        }
    }

    #[test]
    fn test_header_parsing_colon_at_start() {
        let php = RiphtSapi::instance();
        let script_path = php_script_path("hello.php");

        let exec = WebRequest::get()
            .build(&script_path)
            .expect("failed to build WebRequest");

        let _result = php
            .execute(exec)
            .expect("hello.php request execution failed");

        unsafe {
            let ctx = Box::new(ServerContext::new());
            let ctx_ptr = Box::into_raw(ctx);
            super::ffi::sapi_globals.server_context =
                ctx_ptr as *mut std::ffi::c_void;

            let mut header = super::ffi::sapi_header_struct::default();
            let header_str = b": value\r\n";
            header.header = header_str.as_ptr() as *mut c_char;
            header.header_len = header_str.len();

            ripht_sapi_send_header(&mut header, std::ptr::null_mut());

            assert_eq!(
                (*ctx_ptr)
                    .response_headers
                    .len(),
                0,
                "Header with colon at start (empty name) should not be added"
            );

            let _ = Box::from_raw(ctx_ptr);
            super::ffi::sapi_globals.server_context = std::ptr::null_mut();
        }
    }

    #[test]
    fn test_header_parsing_empty_value() {
        let php = RiphtSapi::instance();
        let script_path = php_script_path("hello.php");

        let exec = WebRequest::get()
            .build(&script_path)
            .expect("failed to build WebRequest");

        let _result = php
            .execute(exec)
            .expect("hello.php request execution failed");

        unsafe {
            let ctx = Box::new(ServerContext::new());
            let ctx_ptr = Box::into_raw(ctx);
            super::ffi::sapi_globals.server_context =
                ctx_ptr as *mut std::ffi::c_void;

            let mut header = super::ffi::sapi_header_struct::default();
            let header_str = b"X-Empty-Value:\r\n";
            header.header = header_str.as_ptr() as *mut c_char;
            header.header_len = header_str.len();

            ripht_sapi_send_header(&mut header, std::ptr::null_mut());

            let headers_len = (*ctx_ptr)
                .response_headers
                .len();

            assert_eq!(
                headers_len, 1,
                "Header with empty value should be added"
            );

            let headers = &(*ctx_ptr).response_headers;
            assert_eq!(headers[0].name(), "X-Empty-Value");
            assert_eq!(headers[0].value(), "");

            let _ = Box::from_raw(ctx_ptr);
            super::ffi::sapi_globals.server_context = std::ptr::null_mut();
        }
    }

    #[test]
    fn test_header_parsing_colon_at_end() {
        let php = RiphtSapi::instance();
        let script_path = php_script_path("hello.php");

        let exec = WebRequest::get()
            .build(&script_path)
            .expect("failed to build WebRequest");

        let _result = php
            .execute(exec)
            .expect("hello.php request execution failed");

        unsafe {
            let ctx = Box::new(ServerContext::new());
            let ctx_ptr = Box::into_raw(ctx);
            super::ffi::sapi_globals.server_context =
                ctx_ptr as *mut std::ffi::c_void;

            let mut header = super::ffi::sapi_header_struct::default();
            let header_str = b"X-Trailing-Colon:\r\n";
            header.header = header_str.as_ptr() as *mut c_char;
            header.header_len = header_str.len();

            ripht_sapi_send_header(&mut header, std::ptr::null_mut());

            assert_eq!(
                (*ctx_ptr)
                    .response_headers
                    .len(),
                1,
                "Header with colon at end should be added with empty value"
            );

            let _ = Box::from_raw(ctx_ptr);
            super::ffi::sapi_globals.server_context = std::ptr::null_mut();
        }
    }
}