Page 1 of 1

[HELP] Own webserver & PHP(CGI)

Posted: Mon Jun 09, 2003 9:50 am
by m.kappe
Hi,
first of all , excuse me for my bad english :oops:
i'm developing a webserver using libwebserver(http://libwebserver.sourceforge.net/) and i've found some problems to use php as CGI when i try to put in background my webserver.

I've used the example cgi.c:

Code: Select all

#include <stdio.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <dirent.h>
#include <unistd.h>
#include "web_server.h"
#include "debug.h"



extern struct web_client *current_web_client;
int PORT=80;

extern char **environ;
void cgi() {
	char *reqfile=ClientInfo->request+1; // Skip '/'
	char *tmp;
	FILE *instream;
	int stdo;
	int ret;
	int pid;
	int outp;
	if(!(pid=fork())) {
		instream=tmpfile();	
		// Setup envvars
		setenv("SCRIPT_NAME",ClientInfo->request,1);
		setenv("REQUEST_METHOD",ClientInfo->method,1);
		if(strlen(tmp=ClientInfo->Query(NULL))) {
			setenv("QUERY_STRING",tmp,1);
		};
		if(strlen(tmp=ClientInfo->Post(NULL))) {
			fwrite(tmp,strlen(tmp),1,instream);
		}
		setenv("CONTENT_TYPE",ClientInfo->Header("Content-type"),1);
		if(strlen(tmp=ClientInfo->Header("Cookie"))) {
			setenv("HTTP_COOKIE",tmp,1);
		};
		dup2(ClientInfo->outfd,1);
		dup2(fileno(instream),0);	
		fseek(instream,0,SEEK_SET);
		//execve(reqfile,NULL,environ);
		printf("Content-type: text/html\r\n\r\n");
		printf("<HTML><h1>PROVAMI</h1></HTML>");
		exit(0);
	};
	while(!(ret=waitpid(pid,&ret,0)))fprintf(stderr,"ret-%d\n",ret);;

};
void index_() {
	DIR *dir;
	struct dirent *dire;
	dir=opendir("cgi-bin");
	printf("Content-type: text/html\r\n\r\n");
	printf("<HTML>\n");
	printf("<BODY bgcolor='EFEFEF'>\n");
	printf("Browse /cgi-bin/*<BR>\n");
	while(dire=readdir(dir)) {
		if(dire->d_nameї0]!='.') {
			if((int)(strstr(dire->d_name,".cgi")+4)==(int)(dire->d_name+strlen(dire->d_name))) {
				printf("<a href='/cgi-bin/%s'>%s</a> -- ",dire->d_name,dire->d_name);
			};
		};
	};
	closedir(dir);
};

main() {
	struct web_server server;
        // i daemonize my webserver
        daemon(1,0);
	while(!web_server_init(&server,PORT,"cgi.log",0)) {
		PORT++;
	};
	printf("http://localhost:%d\n",PORT);
	web_server_addhandler(&server,"* /*.cgi",cgi,0);
	web_server_addhandler(&server,"* /",index_,0);
	while(1) {
		web_server_run(&server);
	};
};
I've created a cgi written in php:

Code: Select all

#!/usr/bin/php
<?php
phpinfo();
?>
When i try to get my cgi script the HTTP header was sent uncorrectly and my script doesn't work.
This problem happens only if i put in background(even with daemon() or with "&") and i close my terminal.

What can i do?

Thanks a lot.
M.

libwebserver

Posted: Wed May 12, 2004 12:04 pm
by stdio
maybe you should see the help html's or use the help server of libwebserver that say's to use that cgi.c at your own risc, as in, you should develop your cgi.c the cgi.c of lib as merely example is very very incomplete..!, but anyway i was think in develop the cgi part for lib, so if you wait for next version