1use parking_lot::{ArcRwLockReadGuard, ArcRwLockWriteGuard, RawRwLock, RwLock};
4use std::collections::HashMap;
5use std::ffi::CStr;
6use std::ops::{Deref, DerefMut};
7use std::slice;
8use std::str;
9use std::sync::{Arc, LazyLock};
10
11use crate::boxed::ZBox;
12use crate::error::{Error, Result};
13use crate::exception::{PhpException, PhpResult};
14#[cfg(php82)]
15use crate::ffi::zend_atomic_bool_store;
16use crate::ffi::{
17 _sapi_module_struct, _zend_compiler_globals, _zend_executor_globals, TRACK_VARS_COOKIE,
18 TRACK_VARS_ENV, TRACK_VARS_FILES, TRACK_VARS_GET, TRACK_VARS_POST, TRACK_VARS_SERVER,
19 ext_php_rs_compiler_globals, ext_php_rs_executor_globals, ext_php_rs_file_globals,
20 ext_php_rs_process_globals, ext_php_rs_sapi_globals, ext_php_rs_sapi_module, php_core_globals,
21 php_file_globals, sapi_globals_struct, sapi_header_struct, sapi_headers_struct,
22 sapi_request_info, zend_ini_entry, zend_is_auto_global,
23};
24#[cfg(not(php81))]
25use crate::ffi::{_zend_hash_find_known_hash, _zend_string};
26#[cfg(php81)]
27use crate::ffi::{
28 _zend_known_string_id_ZEND_STR_AUTOGLOBAL_REQUEST, zend_hash_find_known_hash,
29 zend_known_strings,
30};
31
32use crate::types::{ZendHashTable, ZendObject, ZendStr, Zval};
33
34use super::linked_list::ZendLinkedListIterator;
35
36fn class_name_or_fallback(obj: &ZendObject) -> String {
39 obj.get_class_name().unwrap_or_else(|_| "Exception".into())
40}
41
42pub type ExecutorGlobals = _zend_executor_globals;
44
45impl ExecutorGlobals {
46 #[expect(
58 clippy::expect_used,
59 reason = "the engine declares these globals statically, so the pointer is valid for the life of the process"
60 )]
61 pub fn get() -> GlobalReadGuard<Self> {
62 let globals = unsafe { ext_php_rs_executor_globals().as_ref() }
65 .expect("Static executor globals were invalid");
66
67 cfg_if::cfg_if! {
68 if #[cfg(php_zts)] {
69 let guard = lock::GLOBALS_LOCK.with(RwLock::read_arc);
70 } else {
71 let guard = lock::GLOBALS_LOCK.read_arc();
72 }
73 }
74
75 GlobalReadGuard { globals, guard }
76 }
77
78 #[expect(
90 clippy::expect_used,
91 reason = "the engine declares these globals statically, so the pointer is valid for the life of the process"
92 )]
93 pub fn get_mut() -> GlobalWriteGuard<Self> {
94 let globals = unsafe { ext_php_rs_executor_globals().as_mut() }
97 .expect("Static executor globals were invalid");
98
99 cfg_if::cfg_if! {
100 if #[cfg(php_zts)] {
101 let guard = lock::GLOBALS_LOCK.with(RwLock::write_arc);
102 } else {
103 let guard = lock::GLOBALS_LOCK.write_arc();
104 }
105 }
106
107 GlobalWriteGuard { globals, guard }
108 }
109
110 #[must_use]
112 pub fn class_table(&self) -> Option<&ZendHashTable> {
113 unsafe { self.class_table.as_ref() }
114 }
115
116 #[must_use]
118 pub fn function_table(&self) -> Option<&ZendHashTable> {
119 unsafe { self.function_table.as_ref() }
120 }
121
122 #[must_use]
124 pub fn function_table_mut(&mut self) -> Option<&mut ZendHashTable> {
125 unsafe { self.function_table.as_mut() }
126 }
127
128 #[must_use]
136 pub fn ini_values(&self) -> HashMap<String, Option<String>> {
137 let Some(hash_table) = (unsafe { self.ini_directives.as_ref() }) else {
140 return HashMap::new();
141 };
142 let mut ini_hash_map: HashMap<String, Option<String>> = HashMap::new();
143 for (key, entry) in hash_table {
144 let value = unsafe {
148 let Some(entry) = entry.ptr::<zend_ini_entry>().and_then(|e| e.as_ref()) else {
149 continue;
150 };
151 entry
152 .value
153 .as_ref()
154 .map(|value| String::from_utf8_lossy(value.as_bytes()).into_owned())
155 };
156 ini_hash_map.insert(key.to_string(), value);
157 }
158 ini_hash_map
159 }
160
161 #[must_use]
163 pub fn constants(&self) -> Option<&ZendHashTable> {
164 unsafe { self.zend_constants.as_ref() }
165 }
166
167 #[must_use]
174 pub fn take_exception() -> Option<ZBox<ZendObject>> {
175 {
176 if Self::get().exception.is_null() {
178 return None;
179 }
180 }
181
182 let mut globals = Self::get_mut();
183
184 let mut exception_ptr = std::ptr::null_mut();
185 std::mem::swap(&mut exception_ptr, &mut globals.exception);
186
187 Some(unsafe { ZBox::from_raw(exception_ptr.as_mut()?) })
189 }
190
191 #[must_use]
193 pub fn has_exception() -> bool {
194 !Self::get().exception.is_null()
195 }
196
197 #[must_use]
203 pub fn exception(&self) -> Option<&ZendObject> {
204 unsafe { self.exception.as_ref() }
207 }
208
209 #[must_use]
215 pub fn pending_exception_class() -> Option<String> {
216 Some(class_name_or_fallback(Self::get().exception()?))
217 }
218
219 pub fn throw_if_exception() -> PhpResult<()> {
231 let Some(mut obj) = Self::take_exception() else {
232 return Ok(());
233 };
234
235 let class = class_name_or_fallback(&obj);
236 let mut zv = Zval::new();
237 zv.set_object(&mut obj);
238 Err(PhpException::from_message(class).with_object(zv))
239 }
240
241 pub fn request_interrupt(&mut self) {
245 cfg_if::cfg_if! {
246 if #[cfg(php82)] {
247 unsafe {
248 zend_atomic_bool_store(&raw mut self.vm_interrupt, true);
249 }
250 } else {
251 self.vm_interrupt = true;
252 }
253 }
254 }
255
256 pub fn cancel_interrupt(&mut self) {
258 cfg_if::cfg_if! {
259 if #[cfg(php82)] {
260 unsafe {
261 zend_atomic_bool_store(&raw mut self.vm_interrupt, false);
262 }
263 } else {
264 self.vm_interrupt = true;
265 }
266 }
267 }
268}
269
270pub type CompilerGlobals = _zend_compiler_globals;
271
272impl CompilerGlobals {
273 #[expect(
285 clippy::expect_used,
286 reason = "the engine declares these globals statically, so the pointer is valid for the life of the process"
287 )]
288 pub fn get() -> GlobalReadGuard<Self> {
289 let globals = unsafe { ext_php_rs_compiler_globals().as_ref() }
292 .expect("Static compiler globals were invalid");
293
294 cfg_if::cfg_if! {
295 if #[cfg(php_zts)] {
296 let guard = lock::GLOBALS_LOCK.with(RwLock::read_arc);
297 } else {
298 let guard = lock::GLOBALS_LOCK.read_arc();
299 }
300 }
301
302 GlobalReadGuard { globals, guard }
303 }
304
305 #[expect(
317 clippy::expect_used,
318 reason = "the engine declares these globals statically, so the pointer is valid for the life of the process"
319 )]
320 pub fn get_mut() -> GlobalWriteGuard<Self> {
321 let globals = unsafe { ext_php_rs_compiler_globals().as_mut() }
324 .expect("Static compiler globals were invalid");
325
326 cfg_if::cfg_if! {
327 if #[cfg(php_zts)] {
328 let guard = lock::GLOBALS_LOCK.with(RwLock::write_arc);
329 } else {
330 let guard = lock::GLOBALS_LOCK.write_arc();
331 }
332 }
333
334 GlobalWriteGuard { globals, guard }
335 }
336}
337
338pub type SapiModule = _sapi_module_struct;
340
341impl SapiModule {
342 #[expect(
354 clippy::expect_used,
355 reason = "the engine declares these globals statically, so the pointer is valid for the life of the process"
356 )]
357 pub fn get() -> GlobalReadGuard<Self> {
358 let globals = unsafe { ext_php_rs_sapi_module().as_ref() }
361 .expect("Static executor globals were invalid");
362 let guard = SAPI_MODULE_LOCK.read_arc();
363 GlobalReadGuard { globals, guard }
364 }
365
366 #[expect(
378 clippy::expect_used,
379 reason = "the engine declares these globals statically, so the pointer is valid for the life of the process"
380 )]
381 pub fn get_mut() -> GlobalWriteGuard<Self> {
382 let globals = unsafe { ext_php_rs_sapi_module().as_mut() }
385 .expect("Static executor globals were invalid");
386 let guard = SAPI_MODULE_LOCK.write_arc();
387 GlobalWriteGuard { globals, guard }
388 }
389}
390
391pub type ProcessGlobals = php_core_globals;
393
394impl ProcessGlobals {
395 pub fn get() -> GlobalReadGuard<Self> {
403 let globals = unsafe { &*ext_php_rs_process_globals() };
406
407 cfg_if::cfg_if! {
408 if #[cfg(php_zts)] {
409 let guard = lock::PROCESS_GLOBALS_LOCK.with(RwLock::read_arc);
410 } else {
411 let guard = lock::PROCESS_GLOBALS_LOCK.read_arc();
412 }
413 }
414
415 GlobalReadGuard { globals, guard }
416 }
417
418 pub fn get_mut() -> GlobalWriteGuard<Self> {
426 let globals = unsafe { &mut *ext_php_rs_process_globals() };
429
430 cfg_if::cfg_if! {
431 if #[cfg(php_zts)] {
432 let guard = lock::PROCESS_GLOBALS_LOCK.with(RwLock::write_arc);
433 } else {
434 let guard = lock::PROCESS_GLOBALS_LOCK.write_arc();
435 }
436 }
437
438 GlobalWriteGuard { globals, guard }
439 }
440
441 #[must_use]
443 pub fn http_server_vars(&self) -> Option<&ZendHashTable> {
444 if !self.http_globals[TRACK_VARS_SERVER as usize].is_array() {
447 let mut name = ZendStr::new("_SERVER", false);
448 unsafe { zend_is_auto_global(name.as_mut_ptr()) };
453 }
454 if self.http_globals[TRACK_VARS_SERVER as usize].is_array() {
455 self.http_globals[TRACK_VARS_SERVER as usize].array()
456 } else {
457 None
458 }
459 }
460
461 #[must_use]
465 pub fn http_post_vars(&self) -> Option<&ZendHashTable> {
466 self.http_globals[TRACK_VARS_POST as usize].array()
467 }
468
469 #[must_use]
473 pub fn http_get_vars(&self) -> Option<&ZendHashTable> {
474 self.http_globals[TRACK_VARS_GET as usize].array()
475 }
476
477 #[must_use]
481 pub fn http_cookie_vars(&self) -> Option<&ZendHashTable> {
482 self.http_globals[TRACK_VARS_COOKIE as usize].array()
483 }
484
485 pub fn http_request_vars(&self) -> Result<Option<&ZendHashTable>> {
495 cfg_if::cfg_if! {
496 if #[cfg(php81)] {
497 let key = unsafe {
498 *zend_known_strings.add(_zend_known_string_id_ZEND_STR_AUTOGLOBAL_REQUEST as usize)
499 };
500 } else {
501 let mut owned_key = _zend_string::new("_REQUEST", false);
505 let key = owned_key.as_mut_ptr();
506 }
507 };
508
509 if !unsafe { zend_is_auto_global(key) } {
512 return Err(Error::AutoGlobalLoadFailed("_REQUEST"));
513 }
514
515 let symbol_table = &ExecutorGlobals::get().symbol_table;
516 cfg_if::cfg_if! {
517 if #[cfg(php81)] {
518 let request = unsafe { zend_hash_find_known_hash(symbol_table, key) };
519 } else {
520 let request = unsafe { _zend_hash_find_known_hash(symbol_table, key) };
521 }
522 };
523
524 let Some(request) = (unsafe { request.as_ref() }) else {
525 return Ok(None);
526 };
527
528 request
529 .array()
530 .map(Some)
531 .ok_or_else(|| Error::ZvalConversion(request.get_type()))
532 }
533
534 #[must_use]
538 pub fn http_env_vars(&self) -> Option<&ZendHashTable> {
539 self.http_globals[TRACK_VARS_ENV as usize].array()
540 }
541
542 #[must_use]
546 pub fn http_files_vars(&self) -> Option<&ZendHashTable> {
547 self.http_globals[TRACK_VARS_FILES as usize].array()
548 }
549}
550
551pub type SapiGlobals = sapi_globals_struct;
553
554impl SapiGlobals {
555 #[must_use]
563 pub fn get() -> GlobalReadGuard<Self> {
564 let globals = unsafe { &*ext_php_rs_sapi_globals() };
567
568 cfg_if::cfg_if! {
569 if #[cfg(php_zts)] {
570 let guard = lock::SAPI_GLOBALS_LOCK.with(RwLock::read_arc);
571 } else {
572 let guard = lock::SAPI_GLOBALS_LOCK.read_arc();
573 }
574 }
575
576 GlobalReadGuard { globals, guard }
577 }
578
579 pub fn get_mut() -> GlobalWriteGuard<Self> {
587 let globals = unsafe { &mut *ext_php_rs_sapi_globals() };
590
591 cfg_if::cfg_if! {
592 if #[cfg(php_zts)] {
593 let guard = lock::SAPI_GLOBALS_LOCK.with(RwLock::write_arc);
594 } else {
595 let guard = lock::SAPI_GLOBALS_LOCK.write_arc();
596 }
597 }
598
599 GlobalWriteGuard { globals, guard }
600 }
601
602 #[must_use]
604 pub fn request_info(&self) -> &SapiRequestInfo {
605 &self.request_info
606 }
607
608 #[must_use]
610 pub fn sapi_headers(&self) -> &SapiHeaders {
611 &self.sapi_headers
612 }
613}
614
615pub type SapiHeaders = sapi_headers_struct;
617
618impl<'a> SapiHeaders {
619 pub fn headers(&'a mut self) -> ZendLinkedListIterator<'a, SapiHeader> {
621 self.headers.iter()
622 }
623}
624
625pub type SapiHeader = sapi_header_struct;
627
628impl<'a> SapiHeader {
629 #[must_use]
633 pub fn as_str(&'a self) -> Option<&'a str> {
634 if self.header.is_null() || self.header_len == 0 {
635 return None;
636 }
637 unsafe {
638 let slice = slice::from_raw_parts(self.header as *const u8, self.header_len);
639 str::from_utf8(slice).ok()
640 }
641 }
642
643 #[must_use]
647 pub fn name(&'a self) -> Option<&'a str> {
648 Some(self.as_str()?.split(':').next().unwrap_or("").trim())
649 }
650
651 #[must_use]
656 pub fn value(&'a self) -> Option<&'a str> {
657 self.as_str()?
658 .split_once(':')
659 .map(|(_, value)| value.trim())
660 }
661}
662
663pub type SapiRequestInfo = sapi_request_info;
664
665impl SapiRequestInfo {
666 #[must_use]
668 pub fn request_method(&self) -> Option<&str> {
669 if self.request_method.is_null() {
670 return None;
671 }
672 unsafe { CStr::from_ptr(self.request_method).to_str().ok() }
673 }
674
675 #[must_use]
677 pub fn query_string(&self) -> Option<&str> {
678 if self.query_string.is_null() {
679 return None;
680 }
681 unsafe { CStr::from_ptr(self.query_string).to_str().ok() }
682 }
683
684 #[must_use]
686 pub fn cookie_data(&self) -> Option<&str> {
687 if self.cookie_data.is_null() {
688 return None;
689 }
690 unsafe { CStr::from_ptr(self.cookie_data).to_str().ok() }
691 }
692
693 #[must_use]
695 pub fn content_length(&self) -> i64 {
696 self.content_length
697 }
698
699 #[must_use]
701 pub fn path_translated(&self) -> Option<&str> {
702 if self.path_translated.is_null() {
703 return None;
704 }
705 unsafe { CStr::from_ptr(self.path_translated).to_str().ok() }
706 }
707
708 #[must_use]
710 pub fn request_uri(&self) -> Option<&str> {
711 if self.request_uri.is_null() {
712 return None;
713 }
714 unsafe { CStr::from_ptr(self.request_uri).to_str().ok() }
715 }
716
717 #[must_use]
721 pub fn content_type(&self) -> Option<&str> {
722 if self.content_type.is_null() {
723 return None;
724 }
725 unsafe { CStr::from_ptr(self.content_type).to_str().ok() }
726 }
727
728 #[must_use]
730 pub fn headers_only(&self) -> bool {
731 self.headers_only
732 }
733
734 #[must_use]
736 pub fn no_headers(&self) -> bool {
737 self.no_headers
738 }
739
740 #[must_use]
742 pub fn headers_read(&self) -> bool {
743 self.headers_read
744 }
745
746 #[must_use]
750 pub fn auth_user(&self) -> Option<&str> {
751 if self.auth_user.is_null() {
752 return None;
753 }
754 unsafe { CStr::from_ptr(self.auth_user).to_str().ok() }
755 }
756
757 #[must_use]
759 pub fn auth_password(&self) -> Option<&str> {
760 if self.auth_password.is_null() {
761 return None;
762 }
763 unsafe { CStr::from_ptr(self.auth_password).to_str().ok() }
764 }
765
766 #[must_use]
768 pub fn auth_digest(&self) -> Option<&str> {
769 if self.auth_digest.is_null() {
770 return None;
771 }
772 unsafe { CStr::from_ptr(self.auth_digest).to_str().ok() }
773 }
774
775 #[must_use]
777 pub fn argv0(&self) -> Option<&str> {
778 if self.argv0.is_null() {
779 return None;
780 }
781 unsafe { CStr::from_ptr(self.argv0).to_str().ok() }
782 }
783
784 #[must_use]
786 pub fn current_user(&self) -> Option<&str> {
787 if self.current_user.is_null() {
788 return None;
789 }
790 unsafe { CStr::from_ptr(self.current_user).to_str().ok() }
791 }
792
793 #[must_use]
795 pub fn current_user_length(&self) -> i32 {
796 self.current_user_length
797 }
798
799 #[must_use]
801 pub fn argvc(&self) -> i32 {
802 self.argc
803 }
804
805 #[must_use]
807 pub fn argv(&self) -> Option<&str> {
808 if self.argv.is_null() {
809 return None;
810 }
811 unsafe { CStr::from_ptr(*self.argv).to_str().ok() }
812 }
813
814 #[must_use]
816 pub fn proto_num(&self) -> i32 {
817 self.proto_num
818 }
819}
820
821pub type FileGlobals = php_file_globals;
823
824impl FileGlobals {
825 #[expect(
837 clippy::expect_used,
838 reason = "the engine declares these globals statically, so the pointer is valid for the life of the process"
839 )]
840 pub fn get() -> GlobalReadGuard<Self> {
841 let globals = unsafe { ext_php_rs_file_globals().as_ref() }
844 .expect("Static file globals were invalid");
845
846 cfg_if::cfg_if! {
847 if #[cfg(php_zts)] {
848 let guard = lock::FILE_GLOBALS_LOCK.with(RwLock::read_arc);
849 } else {
850 let guard = lock::FILE_GLOBALS_LOCK.read_arc();
851 }
852 }
853
854 GlobalReadGuard { globals, guard }
855 }
856
857 #[must_use]
865 pub fn get_mut() -> GlobalWriteGuard<Self> {
866 let globals = unsafe { &mut *ext_php_rs_file_globals() };
869
870 cfg_if::cfg_if! {
871 if #[cfg(php_zts)] {
872 let guard = lock::FILE_GLOBALS_LOCK.with(RwLock::write_arc);
873 } else {
874 let guard = lock::FILE_GLOBALS_LOCK.write_arc();
875 }
876 }
877
878 GlobalWriteGuard { globals, guard }
879 }
880
881 #[must_use]
883 pub fn stream_wrappers(&self) -> Option<&'static ZendHashTable> {
884 unsafe { self.stream_wrappers.as_ref() }
885 }
886}
887
888#[cfg(not(php_zts))]
893pub(crate) mod lock {
894 use parking_lot::RwLock;
895 use std::sync::{Arc, LazyLock};
896
897 pub(crate) static GLOBALS_LOCK: LazyLock<Arc<RwLock<()>>> =
898 LazyLock::new(|| Arc::new(RwLock::new(())));
899 pub(crate) static PROCESS_GLOBALS_LOCK: LazyLock<Arc<RwLock<()>>> =
900 LazyLock::new(|| Arc::new(RwLock::new(())));
901 pub(crate) static SAPI_GLOBALS_LOCK: LazyLock<Arc<RwLock<()>>> =
902 LazyLock::new(|| Arc::new(RwLock::new(())));
903 pub(crate) static FILE_GLOBALS_LOCK: LazyLock<Arc<RwLock<()>>> =
904 LazyLock::new(|| Arc::new(RwLock::new(())));
905}
906
907#[cfg(php_zts)]
912pub(crate) mod lock {
913 use parking_lot::{RwLock, const_rwlock};
914 use std::sync::Arc;
915
916 thread_local! {
917 pub(crate) static GLOBALS_LOCK: Arc<RwLock<()>> = Arc::new(const_rwlock(()));
918 pub(crate) static PROCESS_GLOBALS_LOCK: Arc<RwLock<()>> = Arc::new( const_rwlock(()) );
919 pub(crate) static SAPI_GLOBALS_LOCK: Arc<RwLock<()>> = Arc::new( const_rwlock(()) );
920 pub(crate) static FILE_GLOBALS_LOCK: Arc<RwLock<()>> = Arc::new( const_rwlock(()) );
921 }
922}
923
924static SAPI_MODULE_LOCK: LazyLock<Arc<RwLock<()>>> = LazyLock::new(|| Arc::new(RwLock::new(())));
929
930pub struct GlobalReadGuard<T: 'static> {
933 globals: &'static T,
934 #[allow(dead_code)]
935 guard: ArcRwLockReadGuard<RawRwLock, ()>,
936}
937
938impl<T> Deref for GlobalReadGuard<T> {
939 type Target = T;
940
941 fn deref(&self) -> &Self::Target {
942 self.globals
943 }
944}
945
946pub struct GlobalWriteGuard<T: 'static> {
949 globals: &'static mut T,
950 #[allow(dead_code)]
951 guard: ArcRwLockWriteGuard<RawRwLock, ()>,
952}
953
954impl<T> Deref for GlobalWriteGuard<T> {
955 type Target = T;
956
957 fn deref(&self) -> &Self::Target {
958 self.globals
959 }
960}
961
962impl<T> DerefMut for GlobalWriteGuard<T> {
963 fn deref_mut(&mut self) -> &mut Self::Target {
964 self.globals
965 }
966}
967
968#[cfg(test)]
969mod tests {
970 use super::*;
971 use std::os::raw::c_char;
972
973 #[test]
974 fn sapi_header_parses_name_and_value() {
975 let headers = [
976 ("Content-Type: text/html", "Content-Type", "text/html"),
977 ("X: Custom:Header", "X", "Custom:Header"),
978 ];
979
980 for (header_text, name, value) in headers {
981 let header = SapiHeader {
982 header: header_text.as_bytes().as_ptr() as *mut c_char,
983 header_len: header_text.len(),
984 };
985 assert_eq!(header.name(), Some(name), "Header name mismatch");
986 assert_eq!(header.value(), Some(value), "Header value mismatch");
987 assert_eq!(
988 header.as_str(),
989 Some(format!("{name}: {value}").as_str()),
990 "Header string mismatch"
991 );
992 }
993 }
994
995 #[test]
996 fn sapi_header_with_invalid_utf8_returns_none() {
997 let bytes: &[u8] = b"X: \xFF\xFE";
998 let header = SapiHeader {
999 header: bytes.as_ptr() as *mut c_char,
1000 header_len: bytes.len(),
1001 };
1002 assert_eq!(header.as_str(), None);
1003 assert_eq!(header.name(), None);
1004 assert_eq!(header.value(), None);
1005 }
1006
1007 #[test]
1008 fn sapi_header_with_null_pointer_returns_none() {
1009 let header = SapiHeader {
1010 header: std::ptr::null_mut(),
1011 header_len: 0,
1012 };
1013 assert_eq!(header.as_str(), None);
1014 assert_eq!(header.name(), None);
1015 assert_eq!(header.value(), None);
1016 }
1017
1018 #[test]
1019 fn sapi_header_with_zero_length_returns_none() {
1020 let header = SapiHeader {
1021 header: c"".as_ptr().cast_mut(),
1022 header_len: 0,
1023 };
1024 assert_eq!(header.as_str(), None);
1025 assert_eq!(header.name(), None);
1026 assert_eq!(header.value(), None);
1027 }
1028}
1029
1030#[cfg(feature = "embed")]
1031#[cfg(test)]
1032mod embed_tests {
1033 use super::*;
1034 use crate::embed::Embed;
1035
1036 #[test]
1037 fn test_executor_globals() {
1038 Embed::run(|| {
1039 let state = ExecutorGlobals::get().active;
1040 ExecutorGlobals::get_mut().active = !state;
1041 let changed = ExecutorGlobals::get().active;
1042 ExecutorGlobals::get_mut().active = state;
1043 assert_eq!(changed, !state);
1044 });
1045 }
1046
1047 #[test]
1048 fn test_compiler_globals() {
1049 Embed::run(|| {
1050 let state = CompilerGlobals::get().in_compilation;
1051 CompilerGlobals::get_mut().in_compilation = !state;
1052 let changed = CompilerGlobals::get().in_compilation;
1053 CompilerGlobals::get_mut().in_compilation = state;
1054 assert_eq!(changed, !state);
1055 });
1056 }
1057
1058 #[test]
1059 fn test_file_globals_reflect_ini_set() {
1060 let timeout = Embed::run(|| {
1061 Embed::eval("ini_set('default_socket_timeout', '123');")
1062 .expect("failed to set the ini value");
1063 FileGlobals::get().default_socket_timeout
1064 });
1065 assert_eq!(timeout, 123);
1066 }
1067
1068 #[test]
1069 fn test_ini_values_decodes_invalid_utf8_lossily() {
1070 let value: Option<String> = Embed::run(|| {
1072 Embed::eval("ini_set('error_append_string', \"\\xFF\\xFE\");")
1073 .expect("failed to set the ini value");
1074 ExecutorGlobals::get()
1075 .ini_values()
1076 .remove("error_append_string")
1077 .expect("the directive should be present")
1078 });
1079 assert_eq!(value.as_deref(), Some("\u{FFFD}\u{FFFD}"));
1080 }
1081}