-
Notifications
You must be signed in to change notification settings - Fork 181
Expand file tree
/
Copy pathsapi.rs
More file actions
153 lines (143 loc) · 4.64 KB
/
Copy pathsapi.rs
File metadata and controls
153 lines (143 loc) · 4.64 KB
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
use crate::zend::sapi_request_info;
use log::warn;
use std::borrow::Cow;
use std::ffi::{CStr, OsStr};
use std::fmt::{Display, Formatter};
use std::os::unix::ffi::OsStrExt;
use std::path::Path;
// todo: unify with ../component/sapi
#[derive(Copy, Clone, Eq, PartialEq)]
#[repr(C)]
pub enum Sapi {
Unknown = 0,
Apache2Handler,
CgiFcgi,
Cli,
CliServer,
Embed,
FrankenPHP,
FpmFcgi,
Litespeed,
PhpDbg,
Tea,
}
impl Sapi {
pub fn from_name(name: &str) -> Sapi {
match name {
"apache2handler" => Sapi::Apache2Handler,
"cgi-fcgi" => Sapi::CgiFcgi,
"cli" => Sapi::Cli,
"cli-server" => Sapi::CliServer,
"embed" => Sapi::Embed,
"frankenphp" => Sapi::FrankenPHP,
"fpm-fcgi" => Sapi::FpmFcgi,
"litespeed" => Sapi::Litespeed,
"phpdbg" => Sapi::PhpDbg,
"tea" => Sapi::Tea,
_ => Sapi::Unknown,
}
}
pub fn request_script_name<'a>(
&self,
sapi_request_info: sapi_request_info,
) -> Option<Cow<'a, str>> {
match self {
/* Right now all we need is CLI support, but theoretically it can
* be obtained for web requests too if we care.
*/
Sapi::Cli => {
if sapi_request_info.argc > 0 && !sapi_request_info.argv.is_null() {
// Safety: It's not null; the VM should do the rest.
let cstr = unsafe { CStr::from_ptr(*sapi_request_info.argv) };
let bytes = cstr.to_bytes();
if !bytes.is_empty() {
let osstr = OsStr::from_bytes(bytes);
Path::new(osstr)
.file_name()
.map(|file| {
let bytes = file.as_bytes();
match std::str::from_utf8(bytes) {
Ok(str) => Cow::Borrowed(str),
Err(_) => {
let value = String::from_utf8_lossy(bytes);
warn!(
"sapi_globals.request_info.argv[0] contained non-utf8 data: {}",
value
);
value
}
}
})
} else {
None
}
} else {
None
}
}
_ => None,
}
}
}
impl AsRef<str> for Sapi {
fn as_ref(&self) -> &str {
match self {
Sapi::Unknown => "unknown",
Sapi::Apache2Handler => "apache2handler",
Sapi::CgiFcgi => "cgi-fcgi",
Sapi::Cli => "cli",
Sapi::CliServer => "cli-server",
Sapi::Embed => "embed",
Sapi::FrankenPHP => "frankenphp",
Sapi::FpmFcgi => "fpm-fcgi",
Sapi::Litespeed => "litespeed",
Sapi::PhpDbg => "phpdbg",
Sapi::Tea => "tea",
}
}
}
impl Display for Sapi {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
f.write_str(self.as_ref())
}
}
#[cfg(test)]
mod tests {
use crate::Sapi;
#[test]
fn test_is_recognized_sapi() {
let recognized_sapis = [
("apache2handler", Sapi::Apache2Handler),
("cgi-fcgi", Sapi::CgiFcgi),
("cli", Sapi::Cli),
("cli-server", Sapi::CliServer),
("embed", Sapi::Embed),
("frankenphp", Sapi::FrankenPHP),
("fpm-fcgi", Sapi::FpmFcgi),
("litespeed", Sapi::Litespeed),
("phpdbg", Sapi::PhpDbg),
("tea", Sapi::Tea),
];
assert!(recognized_sapis.iter().all(|(name, expected_sapi)| {
Sapi::from_name(name) == *expected_sapi && expected_sapi.to_string() == *name
}));
/* These used to be SAPIs, but have since been removed. I think
* that makes them good testing candidates for unknown SAPIs.
*/
let unrecognized_sapis = [
"aolserver",
"caudium",
"Continuity",
"isapi",
"nsapi",
"pi3web",
"roxen",
"webjames",
// Also, the empty string is not a SAPI.
"",
];
assert!(unrecognized_sapis
.into_iter()
.all(|name| Sapi::from_name(name) == Sapi::Unknown));
}
}