CGI原理解析系列之三----CGI如何完整擷取WEB伺服器資料,cgi----cgi
//類比了伺服器端 httpd 進程啟動 cgi的過程,
//完整展現了 get,post 方法支援
//gcc -g httpd_all.c -o httpd_all.ums;
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/wait.h>
#include <stdlib.h>
#define CGI_NAME "get_post.ums"
#define REQUEST_METHOD "REQUEST_METHOD=POST"
#define REQUEST_PARAMETER "myname=huangzhihui"
int main(int argc, char *argv[])
{
int parent[2],child[2];
if (pipe(parent) < 0)
{
printf("create pipe fail.\n");
}
if (pipe(child) < 0)
{
close(parent[0]);
close(parent[1]);
printf("create pipe fail.\n");
}
pid_t pid;
if ((pid = fork()) < 0)
{
printf("fork fail.\n");
}
else if (pid > 0)
{
/* parent */
//關閉多餘的控制代碼,構造單一功能讀寫通道
close(parent[0]);
close(child[1]);
//類比向 CGI 傳送資料
ssize_t length = strlen(REQUEST_PARAMETER);
if (write(parent[1], REQUEST_PARAMETER, length) != length)
{
printf("write error to pipe\n");
}
close(parent[1]);
//等待CGI子進程完全把資料讀取後寫入,
//實際情況應該是使用select 或者 epoll 監聽
//usleep(1000);
//類比接收 CGI 應答的資料
char buff[256] = { 0 };
length = read(child[0], buff, sizeof(buff));
if (length <= 0)
{
printf("read error from pipe\n");
}
else
{
printf("pid %d read data=%u\n%s",getpid(),length, buff);
}
close(child[0]);
if (waitpid(pid, NULL, 0) < 0)
{
printf("waitpid error\n");
}
exit(0);
}
else
{
/* child */
//關閉多餘的控制代碼,構造單一功能讀寫通道
close(parent[1]);
close(child[0]);
//重新導向管道的輸入端到標準輸入
if (parent[0] != STDIN_FILENO)
{
if (dup2(parent[0], STDIN_FILENO) != STDIN_FILENO)
{
printf("dup2 error to stdin");
}
close(parent[0]);
}
//重新導向管道的輸出端到標準輸出
if (child[1] != STDOUT_FILENO)
{
if (dup2(child[1], STDOUT_FILENO) != STDOUT_FILENO)
{
printf("dup2 error to stdout");
}
close(child[1]);
}
//覆蓋進程空間,設定CGI環境變數
char content_length[128] = { 0 };
sprintf(content_length, "CONTENT_LENGTH=%u", strlen(REQUEST_PARAMETER));
char *exec_argv[3] = { REQUEST_METHOD, content_length };
if (execve(CGI_NAME,argv,exec_argv) < 0)
{
printf("execl error for %s", CGI_NAME);
}
exit(0);
}
exit(0);
}