Skip to content

Commit 29731f7

Browse files
authored
Revert "Fix SAPI shutdown (#19)" (#22)
1 parent 814bd2a commit 29731f7

File tree

4 files changed

+87
-145
lines changed

4 files changed

+87
-145
lines changed

README.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ console.log(response.body.toString())
5858
### `new Php(config)`
5959

6060
* `config` {Object} Configuration object
61-
* `argv` {String[]} Process arguments. **Default:** []
6261
* `docroot` {String} Document root for PHP. **Default:** process.cwd()
6362
* Returns: {Php}
6463

@@ -68,7 +67,6 @@ Construct a new PHP instance to which to dispatch requests.
6867
import { Php } from '@platformatic/php-node'
6968

7069
const php = new Php({
71-
argv: process.argv,
7270
docroot: process.cwd()
7371
})
7472
````

__test__/handler.spec.mjs

Lines changed: 8 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,12 @@ test('Support input/output streams', async (t) => {
1515
t.teardown(() => mockroot.clean())
1616

1717
const php = new Php({
18+
argv: process.argv,
1819
docroot: mockroot.path
1920
})
2021

2122
const req = new Request({
22-
method: 'POST',
23+
method: 'GET',
2324
url: 'http://example.com/index.php',
2425
body: Buffer.from('Hello, from Node.js!')
2526
})
@@ -38,10 +39,12 @@ test('Capture logs', async (t) => {
3839
t.teardown(() => mockroot.clean())
3940

4041
const php = new Php({
42+
argv: process.argv,
4143
docroot: mockroot.path
4244
})
4345

4446
const req = new Request({
47+
method: 'GET',
4548
url: 'http://example.com/index.php'
4649
})
4750

@@ -59,10 +62,12 @@ test('Capture exceptions', async (t) => {
5962
t.teardown(() => mockroot.clean())
6063

6164
const php = new Php({
65+
argv: process.argv,
6266
docroot: mockroot.path
6367
})
6468

6569
const req = new Request({
70+
method: 'GET',
6671
url: 'http://example.com/index.php'
6772
})
6873

@@ -84,10 +89,12 @@ test('Support request and response headers', async (t) => {
8489
t.teardown(() => mockroot.clean())
8590

8691
const php = new Php({
92+
argv: process.argv,
8793
docroot: mockroot.path
8894
})
8995

9096
const req = new Request({
97+
method: 'GET',
9198
url: 'http://example.com/index.php',
9299
headers: {
93100
'X-Test': ['Hello, from Node.js!']
@@ -99,33 +106,3 @@ test('Support request and response headers', async (t) => {
99106
t.is(res.body.toString(), 'Hello, from Node.js!')
100107
t.is(res.headers.get('X-Test'), 'Hello, from PHP!')
101108
})
102-
103-
test('Has expected args', async (t) => {
104-
const mockroot = await MockRoot.from({
105-
'index.php': `<?php
106-
echo "[";
107-
$first = true;
108-
foreach ($argv as $value) {
109-
if ($first) { $first = false; }
110-
else { echo ","; }
111-
echo "\\"$value\\"";
112-
}
113-
echo "]";
114-
?>`
115-
})
116-
t.teardown(() => mockroot.clean())
117-
118-
const php = new Php({
119-
argv: process.argv,
120-
docroot: mockroot.path
121-
})
122-
123-
const req = new Request({
124-
url: 'http://example.com/index.php'
125-
})
126-
127-
const res = await php.handleRequest(req)
128-
t.is(res.status, 200)
129-
130-
t.is(res.body.toString('utf8'), JSON.stringify(process.argv))
131-
})

crates/php/src/embed.rs

Lines changed: 11 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
use std::{
22
env::Args,
3-
ffi::{c_char, CString},
3+
ffi::c_char,
44
path::{Path, PathBuf},
5-
sync::Arc,
65
};
76

87
use ext_php_rs::{
@@ -26,14 +25,6 @@ use crate::{
2625
#[derive(Debug)]
2726
pub struct Embed {
2827
docroot: PathBuf,
29-
30-
// TODO: Do something with this...
31-
#[allow(dead_code)]
32-
args: Vec<String>,
33-
34-
// NOTE: This needs to hold the SAPI to keep it alive
35-
#[allow(dead_code)]
36-
sapi: Arc<Sapi>,
3728
}
3829

3930
// An embed instance may be constructed on the main thread and then shared
@@ -99,16 +90,14 @@ impl Embed {
9990
C: AsRef<Path>,
10091
S: AsRef<str> + std::fmt::Debug,
10192
{
93+
ensure_sapi(argv)?;
94+
10295
let docroot = docroot
10396
.as_ref()
10497
.canonicalize()
10598
.map_err(|_| EmbedException::DocRootNotFound(docroot.as_ref().display().to_string()))?;
10699

107-
Ok(Embed {
108-
docroot,
109-
args: argv.iter().map(|v| v.as_ref().to_string()).collect(),
110-
sapi: ensure_sapi()?,
111-
})
100+
Ok(Embed { docroot })
112101
}
113102

114103
/// Get the docroot used for this Embed instance
@@ -163,8 +152,12 @@ impl Handler for Embed {
163152
/// //assert_eq!(response.body(), "Hello, world!");
164153
/// ```
165154
fn handle(&self, request: Request) -> Result<Response, Self::Error> {
155+
unsafe {
156+
ext_php_rs::embed::ext_php_rs_sapi_per_thread_init();
157+
}
158+
166159
// Initialize the SAPI module
167-
self.sapi.startup()?;
160+
Sapi::startup()?;
168161

169162
let url = request.url();
170163

@@ -189,14 +182,6 @@ impl Handler for Embed {
189182
.unwrap_or(0);
190183
let cookie_data = nullable_cstr(headers.get("Cookie"))?;
191184

192-
let argc = self.args.len() as i32;
193-
let mut argv_ptrs = vec![];
194-
for arg in self.args.iter() {
195-
let string = CString::new(arg.as_bytes())
196-
.map_err(|_| EmbedException::CStringEncodeFailed(arg.to_owned()))?;
197-
argv_ptrs.push(string.into_raw());
198-
}
199-
200185
// Prepare memory stream of the code
201186
let mut file_handle = unsafe {
202187
let mut file_handle = zend_file_handle {
@@ -229,8 +214,8 @@ impl Handler for Embed {
229214

230215
// Reset state
231216
globals.request_info.proto_num = 110;
232-
globals.request_info.argc = argc;
233-
globals.request_info.argv = argv_ptrs.as_mut_ptr();
217+
globals.request_info.argc = 0;
218+
globals.request_info.argv = std::ptr::null_mut();
234219
globals.request_info.headers_read = false;
235220
globals.sapi_headers.http_response_code = 200;
236221

0 commit comments

Comments
 (0)