Skip to content

Commit 1ade4eb

Browse files
authored
fix(globals): split sapi header value containing : correctly
When SapiHeader::value() is used on a header with a value containing a : character, it cuts off after that character. This change ensures the full header value can be retrieved. Refs: #441
1 parent 96555c4 commit 1ade4eb

File tree

1 file changed

+34
-1
lines changed

1 file changed

+34
-1
lines changed

src/zend/globals.rs

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -532,7 +532,7 @@ impl<'a> SapiHeader {
532532
/// Returns the header value.
533533
#[must_use]
534534
pub fn value(&'a self) -> Option<&'a str> {
535-
self.as_str().split(':').nth(1).map(str::trim)
535+
self.as_str().split_once(':').map(|(_, value)| value.trim())
536536
}
537537
}
538538

@@ -835,3 +835,36 @@ impl<T> DerefMut for GlobalWriteGuard<T> {
835835
self.globals
836836
}
837837
}
838+
839+
#[cfg(feature = "embed")]
840+
#[cfg(test)]
841+
mod embed_tests {
842+
use crate::embed::Embed;
843+
use std::os::raw::c_char;
844+
845+
use super::SapiHeader;
846+
847+
#[test]
848+
fn test_sapi_header() {
849+
Embed::run(|| {
850+
let headers = [
851+
("Content-Type: text/html", "Content-Type", "text/html"),
852+
("X: Custom:Header", "X", "Custom:Header"),
853+
];
854+
855+
for (header_text, name, value) in headers {
856+
let header = SapiHeader {
857+
header: header_text.as_bytes().as_ptr() as *mut c_char,
858+
header_len: header_text.len(),
859+
};
860+
assert_eq!(header.name(), name, "Header name mismatch");
861+
assert_eq!(header.value(), Some(value), "Header value mismatch");
862+
assert_eq!(
863+
header.as_str(),
864+
format!("{name}: {value}"),
865+
"Header string mismatch"
866+
);
867+
}
868+
});
869+
}
870+
}

0 commit comments

Comments
 (0)