@@ -24,12 +24,12 @@ function find_file(url, php_dir, callback) {
2424 }
2525 if ( file . includes ( __dirname ) ) {
2626 fs . exists ( file , function ( exists ) {
27- console . log ( "Path join " , file , exists )
27+ console . log ( "File path exists: " , file , exists )
2828 callback ( exists && file ) ;
2929 } ) ;
3030 } else {
3131 fs . exists ( path . join ( __dirname , file ) , function ( exists ) {
32- console . log ( "No path join " , file , exists )
32+ console . log ( "No file path exists: " , file , exists )
3333 callback ( exists && file ) ;
3434 } ) ;
3535 }
@@ -41,15 +41,24 @@ function find_file(url, php_dir, callback) {
4141 } ) ;
4242}
4343
44- function run_php ( req , response , next , url , file , php_cgi_path ) {
44+ function strOptions ( options ) {
45+ let optionsArray = Object . entries ( options ) ;
46+ let newArray = " " ;
47+ for ( let i = 0 ; i < optionsArray . length ; i ++ ) {
48+ newArray = newArray + ( optionsArray [ i ] [ 0 ] + " " + optionsArray [ i ] [ 1 ] + " " ) ;
49+ }
50+ return newArray ;
51+ }
52+
53+ function run_php ( req , response , next , url , file , exe_config ) {
4554 var pathinfo = '' ;
4655 var i = req . url . indexOf ( '.php' ) ;
4756 if ( i > 0 ) {
4857 pathinfo = url . pathname . substring ( i + 4 )
4958 } else {
5059 pathinfo = url . pathname
5160 } ;
52-
61+
5362 // console.log("run_php pathinfo", pathinfo)
5463 // console.log("run_php req", req)
5564
@@ -161,25 +170,30 @@ function run_php(req, response, next, url, file, php_cgi_path) {
161170
162171 if ( / .* ?\. p h p $ / . test ( path . join ( __dirname , file ) ) ) {
163172 var res = '' , err = '' ;
164-
165173 var php ;
166- if ( ! ! php_cgi_path && php_cgi_path !== '' ) {
167- php = child . spawn ( php_cgi_path + "/php-cgi" , [ ] , {
174+
175+ if ( ! ! exe_config . cgi_path && exe_config . cgi_path !== '' ) {
176+ console . log ( ( exe_config . cgi_path . lastIndexOf ( "/" ) == exe_config . cgi_path . length - 1 ? exe_config . cgi_path : exe_config . cgi_path + "/" ) + "php-cgi" + strOptions ( exe_config . options ) ) ;
177+
178+ php = child . spawn (
179+ ( exe_config . cgi_path . lastIndexOf ( "/" ) == exe_config . cgi_path . length - 1 ?
180+ exe_config . cgi_path : exe_config . cgi_path + "/" ) + "php-cgi" ,
181+ [ strOptions ( exe_config . options ) ] , {
168182 env : env
169183 } ) ;
170184 } else {
171185 if ( ! PHP_CGI ) {
172186 throw new Error ( '"php-cgi" cannot be found' ) ;
173187 }
174- php = child . spawn ( PHP_CGI , [ ] , {
188+ php = child . spawn ( PHP_CGI , [ strOptions ( exe_config ) ] , {
175189 env : env
176190 } ) ;
177191 }
178192
179193 // php.stdin.resume();
180194 // console.log(req.rawBody);
181195 // (new Stream(req.rawBody)).pipe(php.stdin);
182-
196+
183197 php . stdin . on ( 'error' , function ( ) {
184198 console . error ( "Error from server" )
185199 } ) ;
@@ -201,7 +215,7 @@ function run_php(req, response, next, url, file, php_cgi_path) {
201215 console . error ( "error" , err ) ;
202216 } ) ;
203217 php . on ( 'exit' , function ( ) {
204-
218+
205219 // extract headers
206220 php . stdin . end ( ) ;
207221
@@ -225,9 +239,9 @@ function run_php(req, response, next, url, file, php_cgi_path) {
225239 } else {
226240 html = res ;
227241 }
228-
242+
229243 // console.log('STATUS: '+response.statusCode);
230-
244+
231245 response . status ( response . statusCode ) . send ( html ) ;
232246 response . end ( ) ;
233247 } ) ;
@@ -238,15 +252,15 @@ function run_php(req, response, next, url, file, php_cgi_path) {
238252
239253exports . cgi = function ( php_root , exe_config ) {
240254 return function ( req , res , next ) {
241-
255+
242256 // stop stream until child-process is opened
243257 req . pause ( ) ;
244258 var url = URL . parse ( req . url ) ;
245259
246260 file = find_file ( url , php_root , function ( file ) {
247261 if ( file ) {
248- // console.log("find_file call", php_cgi_path, file);
249- run_php ( req , res , next , url , file , exe_config . cgi_path ) ;
262+ // console.log("find_file call", exe_config. php_cgi_path, file);
263+ run_php ( req , res , next , url , file , exe_config ) ;
250264 } else {
251265 next ( ) ;
252266 }
0 commit comments