CGI原理解析之二------WEB服务和CGI交互数据
来源:互联网 发布:sql获取当前系统时间 编辑:程序博客网 时间:2024/05/29 04:36
#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 fd[2];
if (pipe(fd) < 0)
{
printf("create pipe fail.\n");
}
pid_t pid;
if ((pid = fork()) < 0)
{
printf("fork fail.\n");
}
else if (pid > 0)
{
/* parent */
//模拟向 CGI 传送数据
ssize_t length = strlen(REQUEST_PARAMETER);
if (write(fd[1], REQUEST_PARAMETER, length) != length)
{
printf("write error to pipe\n");
}
close(fd[1]);
//等待CGI子进程完全把数据读取后写入,
//实际情况应该是使用select 或者 epoll 监听
usleep(1000);
//模拟接收 CGI 应答的数据
char buff[256] = { 0 };
length = read(fd[0], buff, sizeof(buff));
if (length <= 0)
{
printf("read error from pipe\n");
}
else
{
printf("pid %d read data=%u,%s\n",getpid(),length, buff);
}
close(fd[0]);
if (waitpid(pid, NULL, 0) < 0)
{
printf("waitpid error\n");
}
exit(0);
}
else
{
/* child */
//重定向管道的输入端到标准输入
if (fd[0] != STDIN_FILENO)
{
if (dup2(fd[0], STDIN_FILENO) != STDIN_FILENO)
{
printf("dup2 error to stdin");
}
close(fd[0]);
}
//重定向管道的输出端到标准输出
if (fd[1] != STDOUT_FILENO)
{
if (dup2(fd[1], STDOUT_FILENO) != STDOUT_FILENO)
{
printf("dup2 error to stdout");
}
close(fd[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);
}
0 0
- CGI原理解析之二------WEB服务和CGI交互数据
- CGI原理解析系列之三----CGI如何完整获取WEB服务器数据
- CGI原理解析系列之一----CGI如何获取WEB服务器数据
- cgi交互
- 动态web技术(二) --- CGI
- python web编程--CGI模块简单交互。。。
- CGI原理
- CGI原理
- CGI原理
- CGI的作用和原理
- 解析CGI
- cgi 解析
- web框架之cgi.FieldStorage()与数据提交
- 在IPCam上搭建WEB服务并通过CGI交互的相关问题
- cgi脚本进行数据的处理和反馈(二)
- Apache服务器技术之CGI环境变量和POST原理
- WEB服务CGI接口漏洞分析(转)
- 使用Bash脚本实现Web服务CGI
- 【Linux开发】Linux下的多线程编程
- 多线程4
- 数论uva575-Skew Binary
- 进程与线程机理
- 求二进制数中1的个数
- CGI原理解析之二------WEB服务和CGI交互数据
- linux下C语言多线程编程
- The Swift Programming Language6
- 枚举类型的单例模式(java)
- poj 食物链
- 要开始博客了!
- 2014大一下学期总结
- 关于memset()
- Linux进程间通信——使用信号