马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
×
0.CGI步伐紧张干什么?
- CGI数据处理处罚紧张由外部CGI步伐负责,HttpServer只负责将拿到的参数交给CGI步伐
- 即:CGI步伐既必要数据处理处罚又必要使命处理处罚,末了将结果转达回HttpServer
- 本质上就是给一个某个功能的软件,套上了一层壳
- 分析HttpServer转达的参数
- 实现某个功能
- 将结果转达回HttpServer
1.数据处理处罚
- 数据处理处罚,紧张体现在分析参数
- 此处举个简单的例子
- bool GetQuery(std::string& out)
- {
- std::string method = getenv("METHOD");
- bool ret = false;
- if (method == "GET")
- {
- out = getenv("ARG");
- ret = true;
- }
- else if (method == "POST")
- {
- // CGI如何得知需要从标准输入读取多少字节呢?
- int content_length = atoi(getenv("CLENGTH"));
- char ch = 'K';
- while (content_length--)
- {
- read(0, &ch, 1);
- out.push_back(ch);
- }
- ret = true;
- }
- else
- {
- // Do Nothing
- }
- return ret;
- }
- void CutString(const std::string& in, std::string& out1, std::string& out2, const std::string sep)
- {
- auto pos = in.find(sep);
- if(pos != std::string::npos)
- {
- out1 = in.substr(0, pos);
- out2 = in.substr(pos + sep.size());
- }
- }
复制代码 2.使命处理处罚
- 将分析出来的参数,用于使命处理处罚,处理处罚什么使命,就必要看详细场景,写详细代码了
- 此处以一个浅易盘算器为例
- int main()
- {
- // 此时子进程标准输出已经重定向,想看打印只能从标准错误输出 #27 // TODO
- std::string queryStr;
- GetQuery(queryStr);
- // Test Code:x=100&y=200
- std::string arg1, arg2;
- CutString(queryStr, arg1, arg2, "&");
- std::string key1, value1, key2, value2;
- CutString(arg1, key1, value1, "=");
- CutString(arg2, key2, value2, "=");
- // 1 -> 数据给父进程
- std::cout << key1 << ":" << value1 << endl;
- std::cout << key2 << ":" << value2 << endl;
- // 2 -> DEBUG,输出到命令行
- std::cerr << "CGI: " << key1 << ":" << value1 << endl;
- std::cerr << "CGI: " << key2 << ":" << value2 << endl;
- int x = atoi(value1.c_str());
- int y = atoi(value2.c_str());
- // 可能想进行某种计算(搜索、登陆等),想进行某种存储(注册)
- std::cout << "<html>";
- std::cout << "<head><meta charset="utf-8"></head>";
- std::cout << "<body>";
- std::cout << "<h3> " << value1 << " + " << value2 << " = " << x + y << "</h3>";
- std::cout << "<h3> " << value1 << " - " << value2 << " = " << x - y << "</h3>";
- std::cout << "<h3> " << value1 << " * " << value2 << " = " << x * y << "</h3>";
- std::cout << "<h3> " << value1 << " / " << value2 << " = " << x / y << "</h3>";
- std::cout << "</body>";
- std::cout << "</html>";
- return 0;
- }
复制代码 |