Skip to content

Commit 108633e

Browse files
author
Shane Caraveo
committed
Windows compilation of fast cgi now working. See windows.txt for info.
1 parent 22aed39 commit 108633e

File tree

3 files changed

+192
-17
lines changed

3 files changed

+192
-17
lines changed

sapi/fastcgi/fastcgi.c

Lines changed: 49 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,9 @@
4949
#if HAVE_UNISTD_H
5050
#include <unistd.h>
5151
#endif
52+
#ifndef PHP_WIN32
5253
#include <sys/wait.h>
54+
#endif
5355
#include <sys/stat.h>
5456

5557
#if HAVE_SYS_TYPES_H
@@ -59,10 +61,16 @@
5961
#include <signal.h>
6062
#endif
6163

64+
#ifndef S_ISREG
65+
#define S_ISREG(mode) ((mode) & _S_IFREG)
66+
#endif
67+
6268
FCGX_Stream *in, *out, *err;
6369
FCGX_ParamArray envp;
6470
char *path_info = NULL;
71+
#ifndef PHP_WIN32
6572
struct sigaction act, old_term, old_quit, old_int;
73+
#endif
6674

6775
/* Our original environment from when the FastCGI first started */
6876
char **orig_env;
@@ -119,7 +127,7 @@ static void sapi_fastcgi_send_header(sapi_header_struct *sapi_header, void *serv
119127

120128
static int sapi_fastcgi_read_post(char *buffer, uint count_bytes TSRMLS_DC)
121129
{
122-
size_t read_bytes = 0, tmp;
130+
size_t read_bytes = 0;
123131
int c;
124132
char *pos = buffer;
125133

@@ -141,21 +149,13 @@ static char *sapi_fastcgi_read_cookies(TSRMLS_D)
141149

142150
static void sapi_fastcgi_register_variables(zval *track_vars_array TSRMLS_DC)
143151
{
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
153154
*/
155+
php_import_environment_variables(track_vars_array TSRMLS_CC);
154156

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);
159159
}
160160

161161

@@ -194,12 +194,31 @@ static sapi_module_struct fastcgi_sapi_module = {
194194
static void fastcgi_module_main(TSRMLS_D)
195195
{
196196
zend_file_handle file_handle;
197+
int c, retval = FAILURE;
197198

198199
file_handle.type = ZEND_HANDLE_FILENAME;
199200
file_handle.filename = SG(request_info).path_translated;
200201
file_handle.free_filename = 0;
201202
file_handle.opened_path = NULL;
202203

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+
203222
if (php_request_startup(TSRMLS_C) == SUCCESS) {
204223
php_execute_script(&file_handle TSRMLS_CC);
205224
php_request_shutdown(NULL);
@@ -215,16 +234,23 @@ static void init_request_info( TSRMLS_D )
215234
struct stat st;
216235
char *pi = getenv( "PATH_INFO" );
217236
char *pt = getenv( "PATH_TRANSLATED" );
237+
if (!pt) {
238+
pt = getenv("SCRIPT_FILENAME"); // apache mod_fastcgi sets this
239+
}
218240
path_info = strdup( pi );
219241

220242
SG(request_info).request_method = getenv("REQUEST_METHOD");
221243
SG(request_info).query_string = getenv("QUERY_STRING");
222244
SG(request_info).request_uri = path_info;
245+
if (!SG(request_info).request_uri) {
246+
SG(request_info).request_uri = getenv("SCRIPT_NAME");
247+
}
223248
SG(request_info).content_type = ( content_type ? content_type : "" );
224249
SG(request_info).content_length = (content_length?atoi(content_length):0);
225250
SG(sapi_headers).http_response_code = 200;
226251

227252
SG(request_info).path_translated = pt;
253+
if (!pt) return;
228254
/*
229255
* if the file doesn't exist, try to extract PATH_INFO out
230256
* of it by stat'ing back through the '/'
@@ -291,16 +317,17 @@ void fastcgi_php_shutdown(void)
291317
*/
292318
void fastcgi_cleanup(int signal)
293319
{
294-
int i;
295320

296321
#ifdef DEBUG_FASTCGI
297322
fprintf( stderr, "FastCGI shutdown, pid %d\n", getpid() );
298323
#endif
299324

325+
#ifndef PHP_WIN32
300326
sigaction( SIGTERM, &old_term, 0 );
301327

302328
/* Kill all the processes in our process group */
303329
kill( -pgroup, SIGTERM );
330+
#endif
304331

305332
/* We should exit at this point, but MacOSX doesn't seem to */
306333
exit( 0 );
@@ -310,15 +337,17 @@ void fastcgi_cleanup(int signal)
310337
int main(int argc, char *argv[])
311338
{
312339
int exit_status = SUCCESS;
340+
#ifndef PHP_WIN32
313341
int c, i, len;
314342
zend_file_handle file_handle;
315343
char *s;
344+
int status;
345+
#endif
316346
char *argv0=NULL;
317347
char *script_file=NULL;
318348
zend_llist global_vars;
319349
int max_requests = 500;
320350
int requests = 0;
321-
int status;
322351
int env_size, cgi_env_size;
323352

324353
#ifdef DEBUG_FASTCGI
@@ -355,7 +384,7 @@ int main(int argc, char *argv[])
355384
20000419 */
356385
#endif
357386
#endif
358-
387+
359388
sapi_startup(&fastcgi_sapi_module);
360389

361390
if (php_module_startup(&fastcgi_sapi_module)==FAILURE) {
@@ -372,6 +401,7 @@ int main(int argc, char *argv[])
372401
}
373402
}
374403

404+
#ifndef PHP_WIN32
375405
/* Pre-fork, if required */
376406
if( getenv( "PHP_FCGI_CHILDREN" )) {
377407
children = atoi( getenv( "PHP_FCGI_CHILDREN" ));
@@ -446,6 +476,8 @@ int main(int argc, char *argv[])
446476
}
447477
}
448478

479+
#endif /* WIN32 */
480+
449481
/* Main FastCGI loop */
450482
#ifdef DEBUG_FASTCGI
451483
fprintf( stderr, "Going into accept loop\n" );

sapi/fastcgi/fastcgi.dsp

Lines changed: 108 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

sapi/fastcgi/windows.txt

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
Windows support is currently experimental.
2+
3+
Tested under IIS with cgi-fcgi.exe and Apache with mod_fastcgi.
4+
5+
Compilation:
6+
Currently only supports Non-Thread safe compilation, however,
7+
that is realy all that is needed :)
8+
9+
Get the devkit from www.fastcgi.com, build it.
10+
Create the directories
11+
php4/sapi/fastcgi/fcgi
12+
php4/sapi/fastcgi/fcgi/include
13+
php4/sapi/fastcgi/fcgi/lib
14+
Place headers and libs in the include and lib directories.
15+
Load the php.dsw, and compile.
16+
17+
IIS configuration:
18+
19+
Under the application configuration in the IIS configuration program, use:
20+
.php C:\php-fcgi\cgi-fcgi.exe -connect php c:\php-fcgi\php-fcgi.exe
21+
22+
Apache Configuration:
23+
24+
LoadModule fastcgi_module modules/mod_fastcgi.dll
25+
<IfModule mod_fastcgi.c>
26+
AddHandler fastcgi-script fphp php
27+
</IfModule>
28+
29+
The scripts require the bang line as the first line
30+
of the script to work with mod_fastcgi,
31+
#!c:/php-fcgi/php-fcgi.exe
32+
33+
TODO:
34+
make 'thread-safe' compilation, though it isn't necessary.
35+

0 commit comments

Comments
 (0)