49
49
#if HAVE_UNISTD_H
50
50
#include <unistd.h>
51
51
#endif
52
+ #ifndef PHP_WIN32
52
53
#include <sys/wait.h>
54
+ #endif
53
55
#include <sys/stat.h>
54
56
55
57
#if HAVE_SYS_TYPES_H
59
61
#include <signal.h>
60
62
#endif
61
63
64
+ #ifndef S_ISREG
65
+ #define S_ISREG (mode ) ((mode) & _S_IFREG)
66
+ #endif
67
+
62
68
FCGX_Stream * in , * out , * err ;
63
69
FCGX_ParamArray envp ;
64
70
char * path_info = NULL ;
71
+ #ifndef PHP_WIN32
65
72
struct sigaction act , old_term , old_quit , old_int ;
73
+ #endif
66
74
67
75
/* Our original environment from when the FastCGI first started */
68
76
char * * orig_env ;
@@ -119,7 +127,7 @@ static void sapi_fastcgi_send_header(sapi_header_struct *sapi_header, void *serv
119
127
120
128
static int sapi_fastcgi_read_post (char * buffer , uint count_bytes TSRMLS_DC )
121
129
{
122
- size_t read_bytes = 0 , tmp ;
130
+ size_t read_bytes = 0 ;
123
131
int c ;
124
132
char * pos = buffer ;
125
133
@@ -141,21 +149,13 @@ static char *sapi_fastcgi_read_cookies(TSRMLS_D)
141
149
142
150
static void sapi_fastcgi_register_variables (zval * track_vars_array TSRMLS_DC )
143
151
{
144
- char * self = getenv ("REQUEST_URI" );
145
- char * ptr = strchr ( self , '?' );
146
-
147
- /*
148
- * note that the environment will already have been set up
149
- * via fastcgi_module_main(), below.
150
- *
151
- * fastcgi_module_main() -> php_request_startup() ->
152
- * php_hash_environment() -> php_import_environment_variables()
152
+ /* In CGI mode, we consider the environment to be a part of the server
153
+ * variables
153
154
*/
155
+ php_import_environment_variables (track_vars_array TSRMLS_CC );
154
156
155
- /* strip query string off this */
156
- if ( ptr ) * ptr = 0 ;
157
- php_register_variable ( "PHP_SELF" , getenv ("REQUEST_URI" ), track_vars_array TSRMLS_CC );
158
- if ( ptr ) * ptr = '?' ;
157
+ /* Build the special-case PHP_SELF variable for the CGI version */
158
+ php_register_variable ("PHP_SELF" , (SG (request_info ).request_uri ? SG (request_info ).request_uri :"" ), track_vars_array TSRMLS_CC );
159
159
}
160
160
161
161
@@ -194,12 +194,31 @@ static sapi_module_struct fastcgi_sapi_module = {
194
194
static void fastcgi_module_main (TSRMLS_D )
195
195
{
196
196
zend_file_handle file_handle ;
197
+ int c , retval = FAILURE ;
197
198
198
199
file_handle .type = ZEND_HANDLE_FILENAME ;
199
200
file_handle .filename = SG (request_info ).path_translated ;
200
201
file_handle .free_filename = 0 ;
201
202
file_handle .opened_path = NULL ;
202
203
204
+ /* eat the bang line */
205
+ if (SG (request_info ).path_translated ) {
206
+ retval = php_fopen_primary_script (& file_handle TSRMLS_CC );
207
+ }
208
+
209
+ if (retval == SUCCESS ) {
210
+ /* #!php support */
211
+ c = fgetc (file_handle .handle .fp );
212
+ if (c == '#' ) {
213
+ while (c != 10 && c != 13 ) {
214
+ c = fgetc (file_handle .handle .fp ); /* skip to end of line */
215
+ }
216
+ CG (zend_lineno )++ ;
217
+ } else {
218
+ rewind (file_handle .handle .fp );
219
+ }
220
+ }
221
+
203
222
if (php_request_startup (TSRMLS_C ) == SUCCESS ) {
204
223
php_execute_script (& file_handle TSRMLS_CC );
205
224
php_request_shutdown (NULL );
@@ -215,16 +234,23 @@ static void init_request_info( TSRMLS_D )
215
234
struct stat st ;
216
235
char * pi = getenv ( "PATH_INFO" );
217
236
char * pt = getenv ( "PATH_TRANSLATED" );
237
+ if (!pt ) {
238
+ pt = getenv ("SCRIPT_FILENAME" ); // apache mod_fastcgi sets this
239
+ }
218
240
path_info = strdup ( pi );
219
241
220
242
SG (request_info ).request_method = getenv ("REQUEST_METHOD" );
221
243
SG (request_info ).query_string = getenv ("QUERY_STRING" );
222
244
SG (request_info ).request_uri = path_info ;
245
+ if (!SG (request_info ).request_uri ) {
246
+ SG (request_info ).request_uri = getenv ("SCRIPT_NAME" );
247
+ }
223
248
SG (request_info ).content_type = ( content_type ? content_type : "" );
224
249
SG (request_info ).content_length = (content_length ?atoi (content_length ):0 );
225
250
SG (sapi_headers ).http_response_code = 200 ;
226
251
227
252
SG (request_info ).path_translated = pt ;
253
+ if (!pt ) return ;
228
254
/*
229
255
* if the file doesn't exist, try to extract PATH_INFO out
230
256
* of it by stat'ing back through the '/'
@@ -291,16 +317,17 @@ void fastcgi_php_shutdown(void)
291
317
*/
292
318
void fastcgi_cleanup (int signal )
293
319
{
294
- int i ;
295
320
296
321
#ifdef DEBUG_FASTCGI
297
322
fprintf ( stderr , "FastCGI shutdown, pid %d\n" , getpid () );
298
323
#endif
299
324
325
+ #ifndef PHP_WIN32
300
326
sigaction ( SIGTERM , & old_term , 0 );
301
327
302
328
/* Kill all the processes in our process group */
303
329
kill ( - pgroup , SIGTERM );
330
+ #endif
304
331
305
332
/* We should exit at this point, but MacOSX doesn't seem to */
306
333
exit ( 0 );
@@ -310,15 +337,17 @@ void fastcgi_cleanup(int signal)
310
337
int main (int argc , char * argv [])
311
338
{
312
339
int exit_status = SUCCESS ;
340
+ #ifndef PHP_WIN32
313
341
int c , i , len ;
314
342
zend_file_handle file_handle ;
315
343
char * s ;
344
+ int status ;
345
+ #endif
316
346
char * argv0 = NULL ;
317
347
char * script_file = NULL ;
318
348
zend_llist global_vars ;
319
349
int max_requests = 500 ;
320
350
int requests = 0 ;
321
- int status ;
322
351
int env_size , cgi_env_size ;
323
352
324
353
#ifdef DEBUG_FASTCGI
@@ -355,7 +384,7 @@ int main(int argc, char *argv[])
355
384
20000419 */
356
385
#endif
357
386
#endif
358
-
387
+
359
388
sapi_startup (& fastcgi_sapi_module );
360
389
361
390
if (php_module_startup (& fastcgi_sapi_module )== FAILURE ) {
@@ -372,6 +401,7 @@ int main(int argc, char *argv[])
372
401
}
373
402
}
374
403
404
+ #ifndef PHP_WIN32
375
405
/* Pre-fork, if required */
376
406
if ( getenv ( "PHP_FCGI_CHILDREN" )) {
377
407
children = atoi ( getenv ( "PHP_FCGI_CHILDREN" ));
@@ -446,6 +476,8 @@ int main(int argc, char *argv[])
446
476
}
447
477
}
448
478
479
+ #endif /* WIN32 */
480
+
449
481
/* Main FastCGI loop */
450
482
#ifdef DEBUG_FASTCGI
451
483
fprintf ( stderr , "Going into accept loop\n" );
0 commit comments