一、fcgi程序编写
#include <fcgi_stdio.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char* getcgidata(FILE* fp, char* requestmethod)
{
char* input = NULL;
int len;
int size = 1024;
int i = 0;
if (!strcmp(requestmethod, "GET"))
{
return NULL;
}
else if (!strcmp(requestmethod, "POST"))
{
len = atoi(getenv("CONTENT_LENGTH"));
input = (char*)malloc(sizeof(char)*(size + 1));
if (len == 0)
{
input[0] = '\0';
return input;
}
while(1)
{
input[i] = (char)fgetc(fp);
if (i == size)
{
input[i+1] = '\0';
return input;
}
--len;
if (feof(fp) || (!(len)))
{
i++;
input[i] = '\0';
return input;
}
i++;
}
}
return NULL;
}
int main(){
int count = 0;
char *request_method = NULL;
char *fcgidata = NULL;
while(FCGI_Accept() >= 0){
request_method = getenv("REQUEST_METHOD");
fcgidata = getcgidata(stdin,request_method);
if(NULL != fcgidata)
{
printf("Content-type: text/html\r\n"
"\r\n"
"<title>FastCGI Hello! (C, fcgi_stdio library)</title>"
"<h1>FastCGI Hello! (C, fcgi_stdio library)</h1>"
"Request number %d running on host <i>%s</i><br>"
"<i>request_method:%s</i><br>"
"<i>content_length:%s</i><br>"
"<i>query_string:%s</i><br>"
"<i>usr_agent:%s</i><br>"
"<i>script_name:%s</i><br>"
"<i>fcgidata:%s</i><br>",
++count, getenv("SERVER_NAME"),getenv("REQUEST_METHOD"),\
getenv("CONTENT_LENGTH"),getenv("QUERY_STRING"),\
getenv("HTTP_USER_AGENT"),getenv("SCRIPT_NAME"),fcgidata);
if(NULL != fcgidata)
free(fcgidata);
}
else
{
printf("Content-type: text/html\r\n"
"\r\n"
"<title>FastCGI Hello! (C, fcgi_stdio library)</title>"
"<h1>FastCGI Hello! (C, fcgi_stdio library)</h1>"
"Request number %d running on host <i>%s</i><br>"
"<i>request_method:%s</i><br>"
"<i>content_length:%s</i><br>"
"<i>query_string fcgidata:%s</i><br>"
"<i>usr_agent:%s</i><br>"
"<i>script_name:%s</i><br>",
++count, getenv("SERVER_NAME"),getenv("REQUEST_METHOD"),\
getenv("CONTENT_LENGTH"),getenv("QUERY_STRING"),\
getenv("HTTP_USER_AGENT"),getenv("SCRIPT_NAME"));
}
}
}
编译方法:gcc -o caoft.fcgi caoft.c -lfcgi
二、ubuntu环境下的下载路径
https://download.youkuaiyun.com/upload/success
三、测试方法
http://192.168.115.128/cgi-bin/net_monitor/
http://192.168.115.128/cgi-bin/lan_setting/?name=1
http://192.168.115.128/cgi-bin/dev_mgr/
http://192.168.115.128/cgi-bin/update/?name=1
http://192.168.115.128/cgi-bin/sys/
http://192.168.115.128/cgi-bin/login/
http://192.168.115.128/cgi-bin/wireless/
http://192.168.115.128/cgi-bin/wan/