-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathembed.rs
More file actions
379 lines (333 loc) · 10.8 KB
/
Copy pathembed.rs
File metadata and controls
379 lines (333 loc) · 10.8 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
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
use std::{
env::Args,
ops::DerefMut,
path::{Path, PathBuf},
sync::Arc,
};
use ext_php_rs::{
alloc::{efree, estrdup},
error::Error,
ffi::{php_execute_script, sapi_get_default_content_type},
zend::{try_catch, try_catch_first, ExecutorGlobals, SapiGlobals},
};
use http_handler::{Handler, Request, Response, ResponseBuilderExt};
use super::{
sapi::{ensure_sapi, Sapi},
scopes::{FileHandleScope, RequestScope},
strings::translate_path,
EmbedRequestError, EmbedStartError, RequestContext,
};
/// A simple trait for rewriting requests that works with our specific request type
pub trait RequestRewriter: Send + Sync {
/// Rewrite the given request and return the modified request
///
/// The docroot parameter is used by conditions like ExistenceCondition that need
/// to check for files on the filesystem.
fn rewrite_request(
&self,
request: Request,
docroot: &Path,
) -> Result<Request, http_rewriter::RewriteError>;
}
/// Blanket implementation: any type implementing http_rewriter::Rewriter
/// automatically implements RequestRewriter for our concrete Request type.
impl<T> RequestRewriter for T
where
T: http_rewriter::Rewriter,
{
fn rewrite_request(
&self,
request: Request,
docroot: &Path,
) -> Result<Request, http_rewriter::RewriteError> {
use http_handler::extensions::DocumentRoot;
use http_handler::RequestExt;
let mut request = request;
request.set_document_root(DocumentRoot {
path: docroot.to_path_buf(),
});
http_rewriter::Rewriter::rewrite(self, request)
}
}
/// Embed a PHP script into a Rust application to handle HTTP requests.
pub struct Embed {
docroot: PathBuf,
args: Vec<String>,
// NOTE: This needs to hold the SAPI to keep it alive
#[allow(dead_code)]
sapi: Arc<Sapi>,
rewriter: Option<Box<dyn RequestRewriter>>,
}
impl std::fmt::Debug for Embed {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Embed")
.field("docroot", &self.docroot)
.field("args", &self.args)
.field("sapi", &self.sapi)
.field("rewriter", &"Box<dyn RequestRewriter>")
.finish()
}
}
// An embed instance may be constructed on the main thread and then shared
// across multiple threads in a thread pool.
unsafe impl Send for Embed {}
unsafe impl Sync for Embed {}
impl Embed {
/// Creates a new `Embed` instance.
///
/// # Examples
///
/// ```
/// use std::env::current_dir;
/// use php::Embed;
///
/// let docroot = current_dir()
/// .expect("should have current_dir");
///
/// let embed = Embed::new(docroot, None);
/// ```
pub fn new<C>(
docroot: C,
rewriter: Option<Box<dyn RequestRewriter>>,
) -> Result<Self, EmbedStartError>
where
C: AsRef<Path>,
{
Embed::new_with_argv::<C, String>(docroot, rewriter, vec![])
}
/// Creates a new `Embed` instance with command-line arguments.
///
/// # Examples
///
/// ```
/// use std::env::{args, current_dir};
/// use php::Embed;
///
/// let docroot = current_dir()
/// .expect("should have current_dir");
///
/// let embed = Embed::new_with_args(docroot, None, args());
/// ```
pub fn new_with_args<C>(
docroot: C,
rewriter: Option<Box<dyn RequestRewriter>>,
args: Args,
) -> Result<Self, EmbedStartError>
where
C: AsRef<Path>,
{
Embed::new_with_argv(docroot, rewriter, args.collect())
}
/// Creates a new `Embed` instance with command-line arguments.
///
/// # Examples
///
/// ```
/// use std::env::current_dir;
/// use php::{Embed, Handler, Request, Response};
///
/// let docroot = current_dir()
/// .expect("should have current_dir");
///
/// let embed = Embed::new_with_argv(docroot, None, vec![
/// "foo"
/// ]);
/// ```
pub fn new_with_argv<C, S>(
docroot: C,
rewriter: Option<Box<dyn RequestRewriter>>,
argv: Vec<S>,
) -> Result<Self, EmbedStartError>
where
C: AsRef<Path>,
S: AsRef<str> + std::fmt::Debug,
{
let docroot_path = docroot.as_ref();
let docroot = docroot_path
.canonicalize()
.map_err(|_| EmbedStartError::DocRootNotFound(docroot_path.display().to_string()))?;
Ok(Embed {
docroot,
args: argv.iter().map(|v| v.as_ref().to_string()).collect(),
sapi: ensure_sapi()?,
rewriter,
})
}
/// Get the docroot used for this Embed instance
///
/// # Examples
///
/// ```rust
/// use std::env::current_dir;
/// use php::Embed;
///
/// let docroot = current_dir()
/// .expect("should have current_dir");
///
/// let embed = Embed::new(&docroot, None)
/// .expect("should have constructed Embed");
///
/// assert_eq!(embed.docroot(), docroot.as_path());
/// ```
pub fn docroot(&self) -> &Path {
self.docroot.as_path()
}
}
#[async_trait::async_trait]
impl Handler for Embed {
type Error = EmbedRequestError;
/// Handles an HTTP request.
///
/// # Examples
///
/// ```
/// use std::{env::temp_dir, fs::File, io::Write};
/// use php::{Embed, Handler, Request, Response, MockRoot};
///
/// let docroot = MockRoot::builder()
/// .file("index.php", "<?php echo \"Hello, World!\"; ?>")
/// .build()
/// .expect("should prepare docroot");
///
/// let handler = Embed::new(docroot.clone(), None)
/// .expect("should construct Embed");
///
/// let request = http_handler::request::Request::builder()
/// .method("GET")
/// .uri("http://example.com")
/// .body(bytes::BytesMut::new())
/// .expect("should build request");
///
/// # tokio_test::block_on(async {
/// let response = handler.handle(request)
/// .await
/// .expect("should handle request");
/// # });
///
/// //assert_eq!(response.status(), 200);
/// //assert_eq!(response.body(), "Hello, world!");
/// ```
async fn handle(&self, request: Request) -> Result<Response, Self::Error> {
let docroot = self.docroot.clone();
// Initialize the SAPI module
self.sapi.startup()?;
// Get REQUEST_URI _first_ as it needs the pre-rewrite state.
let url = request.uri();
let request_uri = url.path();
// Apply request rewriting rules
let mut request = request.clone();
if let Some(rewriter) = &self.rewriter {
request = rewriter
.rewrite_request(request, &self.docroot)
.map_err(|e| EmbedRequestError::RequestRewriteError(e.to_string()))?;
}
let translated_path = translate_path(&docroot, request.uri().path())?
.display()
.to_string();
// Convert REQUEST_URI and PATH_TRANSLATED to C strings
let request_uri = estrdup(request_uri);
let path_translated = estrdup(translated_path.clone());
// Extract request method, query string, and headers
let request_method = estrdup(request.method().as_str());
let query_string = estrdup(url.query().unwrap_or(""));
let headers = request.headers();
let content_type = headers
.get("Content-Type")
.and_then(|v| v.to_str().ok())
.map(estrdup)
.unwrap_or(std::ptr::null_mut());
let content_length = headers
.get("Content-Length")
.and_then(|v| v.to_str().ok())
.and_then(|s| s.parse::<i64>().ok())
.unwrap_or(0);
// Prepare argv and argc
let argc = self.args.len() as i32;
let mut argv_ptrs = vec![];
for arg in self.args.iter() {
argv_ptrs.push(estrdup(arg.to_owned()));
}
let script_name = translated_path.clone();
// Fixed RefUnwindSafe issue (FIXME.md #1) by setting up RequestContext before try_catch_first
// This avoids the need to rebuild the request inside the closure
RequestContext::for_request(request, docroot.clone());
let response = try_catch_first(move || {
// Set server context
{
let mut globals = SapiGlobals::get_mut();
globals.options |= ext_php_rs::ffi::SAPI_OPTION_NO_CHDIR as i32;
// Reset state
globals.request_info.proto_num = 110;
globals.request_info.argc = argc;
globals.request_info.argv = argv_ptrs.as_mut_ptr();
globals.request_info.headers_read = false;
globals.sapi_headers.http_response_code = 200;
// Set request info from request
globals.request_info.request_method = request_method;
globals.request_info.query_string = query_string;
globals.request_info.path_translated = path_translated;
globals.request_info.request_uri = request_uri;
// TODO: Add auth fields
globals.request_info.content_type = content_type;
globals.request_info.content_length = content_length;
}
let _request_scope = RequestScope::new()?;
// Run script in its own try/catch so bailout doesn't skip request shutdown.
{
let mut file_handle = FileHandleScope::new(script_name.clone());
try_catch(|| unsafe { php_execute_script(file_handle.deref_mut()) })
.map_err(|_| EmbedRequestError::Bailout)?;
}
if let Some(err) = ExecutorGlobals::take_exception() {
{
let mut globals = SapiGlobals::get_mut();
globals.sapi_headers.http_response_code = 500;
}
let ex = Error::Exception(err);
if let Some(ctx) = RequestContext::current() {
let builder = std::mem::replace(
ctx.response_builder_mut(),
http_handler::response::Builder::new(),
);
let builder = builder.exception(ex.to_string()).status(500);
*ctx.response_builder_mut() = builder;
}
return Err(EmbedRequestError::Exception(ex.to_string()));
};
let (mut mimetype, http_response_code) = {
let h = SapiGlobals::get().sapi_headers;
(h.mimetype, h.http_response_code)
};
if mimetype.is_null() {
mimetype = unsafe { sapi_get_default_content_type() };
}
let mime_ptr =
unsafe { mimetype.as_ref() }.ok_or(EmbedRequestError::FailedToDetermineContentType)?;
let mime = unsafe { std::ffi::CStr::from_ptr(mime_ptr) }
.to_str()
.map_err(|_| EmbedRequestError::FailedToDetermineContentType)?
.to_owned();
unsafe {
efree(mimetype.cast::<u8>());
}
// Set the final status and content-type header
if let Some(ctx) = RequestContext::current() {
let builder = std::mem::replace(
ctx.response_builder_mut(),
http_handler::response::Builder::new(),
);
let builder = builder
.status(http_response_code as u16)
.header("Content-Type", mime);
*ctx.response_builder_mut() = builder;
}
// Build the final response with accumulated data using the extension system
RequestContext::reclaim()
.ok_or(EmbedRequestError::ResponseBuildError)?
.build_response()
.map_err(|_| EmbedRequestError::ResponseBuildError)
})
.unwrap_or(Err(EmbedRequestError::Bailout))?;
Ok(response)
}
}