CGI 是Web 服务器运行时外部程序的规范,按CGI 编写的程序可以扩展服务器功能。
CGI(Common Gateway Interface) 是WWW技术中最重要的技术之一,有着不可替代的重要地位。CGI是外部应用程序(CGI程序)与WEB服务器之间的接口标准,是在CGI程序和Web服务器之间传递信息的过程。
CGI规范允许Web服务器执行外部程序,并将它们的输出发送给Web浏览器,CGI将Web的一组简单的静态超媒体文档变成一个完整的新的交互式媒体。
CGI的处理步骤:
- 通过Internet把用户请求送到web服务器。
- web服务器接收用户请求并交给CGI程序处理。
- CGI程序把处理结果传送给web服务器。
- web服务器把结果送回到用户。
示例代码如下:
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <assert.h>
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <fcntl.h>
#include <stdlib.h>
#include <sys/epoll.h>
#include <signal.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include "processpool.h"
// 用于处理客户cgi请求的类,他可以作为processpool类的模板参数
class cgi_conn {
public:
cgi_conn(){
};
~cgi_conn(){
};
// 初始化客户连接,清空读缓冲区
void init(int epollfd, int sockfd, const sockaddr_in& client_addr) {
m_epollfd = epollfd

本文详细介绍了CGI(Common Gateway Interface)的工作原理及其在Web服务器中的作用,阐述了CGI如何扩展服务器功能,允许执行外部程序并将结果返回给浏览器。文中提供了一个简单的CGI程序示例,展示了如何处理用户请求,创建子进程执行CGI程序,并通过进程池管理。此程序适用于接收用户输入并判断是否存在对应的CGI程序进行执行。
最低0.47元/天 解锁文章
1289

被折叠的 条评论
为什么被折叠?



