当前位置:网站首页>Web programming (II) CGI related
Web programming (II) CGI related
2022-07-25 12:07:00 【Boring ah le】

1、CGI environment variable

stay /usr/local/apache/cgi-bin/ Under the new CGI Script env.cpp:
#include <iostream>
#include <stdlib.h>
using namespace std;
const string ENV[24] =
{
"COMSPEC", "DOCUMENT_ROOT", "GATEWAY_INTERFACE",
"HTTP_ACCEPT", "HTTP_ACCEPT_ENCODING",
"HTTP_ACCEPT_LANGUAGE", "HTTP_CONNECTION",
"HTTP_HOST", "HTTP_USER_AGENT", "PATH",
"QUERY_STRING", "REMOTE_ADDR", "REMOTE_PORT",
"REQUEST_METHOD", "REQUEST_URI", "SCRIPT_FILENAME",
"SCRIPT_NAME", "SERVER_ADDR", "SERVER_ADMIN",
"SERVER_NAME","SERVER_PORT","SERVER_PROTOCOL",
"SERVER_SIGNATURE","SERVER_SOFTWARE"};
int main()
{
cout<<"Content-type:text/html\r\n\r\n";
cout<<"<html>\n";
cout<<"<head>\n";
cout<<"<title>CGI Envrionment Variables</title>\n";
cout<<"</head>\n";
cout<<"<body>\n";
cout<<"<table border=\"0\"cellspacing=\"2\">";
for(int i=0;i<24;i++)
{
cout<<"<tr><td>"<<ENV[i]<<"</td><td>";
char *value = getenv(ENV[i].c_str());
if(value!= 0)
{
cout << value;
}
else
{
cout<<"Environment variable does not exist.";
}
cout<<"</td></tr>\n";
}
cout<<"</table><\n";
cout<<"</body>\n";
cout<<"</html>\n";
return 0;
}
g++ -o env env.cpp
Local execution cannot get environment variables , Only put it in cgi-bin Next , It can only be executed in the browser .
Turn on http service , Enter the following in the browser
http://192.168.122.1/cgi-bin/env

2、GET How to get parameters
stay GET Methods ,CGI The program cannot get data directly from the standard input of the server , Because the server puts it
Received from standard input Data is encoded into environment variables QUERY_STRING ( or PATH_INFO ). use GET When the method is used , Just append these data to URL At the end of , Such as
http://192.168.122.1/cgi-bin/get?a=l&b=2&c=3 ,
Now QUERY_STRING The value of is a=l&b=2&c=3
Write test script
#include<iostream>
#include<stdlib.h>
#include<vector>
#include<string>
using namespace std;
/* Functions that separate strings sData It's the source string , sDelim The separator is , The return value is a separated list of strings */
vector<string> StringSplit(const string& sData, const string& sDelim)
{
vector<string>vItems;
vItems.clear();
string::size_type bpos = 0;
string::size_type epos = 0;
string::size_type nlen = sDelim.size();
while ((epos=sData.find(sDelim, epos)) != string::npos)
{
vItems.push_back(sData.substr(bpos, epos-bpos));
epos += nlen;
bpos = epos;
}
vItems.push_back(sData.substr(bpos, sData.size()-bpos));
return vItems;
}
int main()
{
cout << "Content-type:text/html\r\n\r\n";
cout << "<html>\n";
cout << "<head>\n";
cout << "<title>Hello World - First CGI Program</title>\n";
cout << "</head>\n";
cout << "<body>\n";
cout<<"get parameter:<br/>";
char *value = getenv("QUERY_STRING");
if(value!= 0)
{
/*"a=1&b=2&c=3"*/
/* So here is getting GET The code of the parameter , First, according to “&” To separate , And then based on “=” Symbol for secondary separation , You can get the content of the parameter */
vector<string>paras=StringSplit((const string)value,"&");
vector<string>::iterator iter=paras.begin();
for(;iter!=paras.end();iter++)
{
vector<string>singlepara=StringSplit(*iter,"=");
cout<<singlepara[0]<<" "<<singlepara[1]<<"<br/>";
}
cout << "</body>\n";
cout << "</html>\n";
return 0;
}
}
After compiling, put apache/cgi-bin/ Under the table of contents
Type in the browser :
http://192.168.122.1/cgi-bin/get?a=1&b=2&c=3&d=4

3、POST How to get parameters
stay POST Methods , CGI The program can get data directly from the standard input of the server , But first from
CONTENT_LENGTH From this environment variable POST Length of parameter , Then read the content of the corresponding length .
Write test script post.html
<html>
<head>
<title>CGI Post</title>
</head>
<body>
<tr><td>
<div style="font-weight:bold; font-size:15px">Method: POST</div>
<div>lease input two number:<div>
<form method="post" action="./cgi-bin/post">
<input type="txt" size="3" name="m">*
<input type="txt" size="3" name="n">=
<input type="submit" value="result">
</form>
</td></tr>
</table>
</body>
</html>
Write a test program post.cpp
#include<iostream>
#include<stdlib.h>
#include<stdio.h>
using namespace std;
int main()
{
cout << "Content-type:text/html\r\n\r\n";
cout << "<html>\n";
cout << "<head>\n";
cout << "<title>Testing Post</title>\n";
cout << "</head>\n";
cout << "<body>\n";
char *lenstr =getenv("CONTENT_LENGTH");
if(lenstr==NULL)
{
cout<<"Error, CONTENT_LENGTH should be entered!"<<"<br/>";
}
else
{
int len=atoi(lenstr);
char poststr[20];
fgets(poststr,len+1,stdin);
cout<<"poststr:"<<poststr<<"<br/>";
char m[10],n[10];
//m=3&n=4
if(sscanf(poststr,"m=%[^&]&n=%s",m,n)!=2)//????
{
cout<<"Error: Parameters are not right!<br/>";
}
else
{
cout<<"m * n = "<<atoi(m)*atoi(n)<<"<br/>";
}
}
cout << "</body>\n";
cout << "</html>\n";
return 0;
}
g++ -o post post.cpp
Copy post To /usr/local/apache/cgi-bin/ Next
Copy post.html To /usr/local/apache/htdocs/ Next
Browser input :
http://192.168.122.1/post.html
input data , Click on result


4、 Get access to the user's IP
Sometimes when positioning problems , Need to know the user's IP Address , Get users IP Address has two environment variables :
HTTP_VIA and REMOTE_ADDR. When users use proxy server to access , HTTP_QVIA The value of the environment variable is not empty , User IP That is, the proxy server's IP 了 . When the user does not use a proxy server to access , be IP The address is in REMOTE_ADDR In the environment variable
#include<iostream>
#include<stdlib.h>
#include<cstring>
#include<stdio.h>
using namespace std;
// Get users IP
std::string GetClientIP()
{
const char * p = getenv("REMOTE_ADDR");
if(p)
{
return p;
}
return "0.0.0.0";
}
int main()
{
cout << "Content-type:text/html\r\n\r\n";
cout << "<html>\n";
cout << "<head>\n";
cout << "<title>Testing Post</title>\n";
cout << "</head>\n";
cout << "<body>\n";
char *lenstr =getenv("CONTENT_LENGTH");
if(lenstr==NULL)
{
cout<<"Error, CONTENT_LENGTH should be entered!"<<"<br/>";
}
else
{
int len=atoi(lenstr);
char poststr[20];
fgets(poststr,len+1,stdin);
cout<<"poststr:"<<poststr<<"<br/>";
char m[10],n[10];
//m=3&n=4
if(sscanf(poststr,"m=%[^&]&n=%s",m,n)!=2)//????
{
cout<<"Error: Parameters are not right!<br/>";
}
else
{
cout<<"m * n = "<<atoi(m)*atoi(n)<<"<br/>";
}
}
cout<<"usr ip="<< GetClientIP() <<"<br/>";
cout << "</body>\n";
cout << "</html>\n";
return 0;
}
modify post.cpp after , Run again

边栏推荐
- NLP的基本概念1
- Introduction to pl/sql, very detailed notes
- 利用wireshark对TCP抓包分析
- brpc源码解析(七)—— worker基于ParkingLot的bthread调度
- OSPF comprehensive experiment
- R语言使用lm函数构建多元回归模型(Multiple Linear Regression)、使用step函数构建前向逐步回归模型筛选预测变量的最佳子集、scope参数指定候选预测变量
- Data transmission under the same LAN based on tcp/ip
- brpc源码解析(四)—— Bthread机制
- 【RS采样】A Gain-Tuning Dynamic Negative Sampler for Recommendation (WWW 2022)
- 【AI4Code】《Unified Pre-training for Program Understanding and Generation》 NAACL 2021
猜你喜欢

【微服务~Sentinel】Sentinel降级、限流、熔断

What is the global event bus?
![[GCN multimodal RS] pre training representations of multi modal multi query e-commerce search KDD 2022](/img/9c/0434d40fa540078309249d415b3659.png)
[GCN multimodal RS] pre training representations of multi modal multi query e-commerce search KDD 2022

Figure neural network for recommending system problems (imp-gcn, lr-gcn)
![[imx6ull notes] - a preliminary exploration of the underlying driver of the kernel](/img/0f/a0139be99c61fde08e73a5be6d6b4c.png)
[imx6ull notes] - a preliminary exploration of the underlying driver of the kernel

利用wireshark对TCP抓包分析

马斯克的“灵魂永生”:一半炒作,一半忽悠

Brpc source code analysis (V) -- detailed explanation of basic resource pool

Risks in software testing phase

【AI4Code】《InferCode: Self-Supervised Learning of Code Representations by Predicting Subtrees》ICSE‘21
随机推荐
LeetCode 50. Pow(x,n)
[GCN multimodal RS] pre training representations of multi modal multi query e-commerce search KDD 2022
How to solve the problem of the error reported by the Flink SQL client when connecting to MySQL?
R语言ggplot2可视化:可视化散点图并为散点图中的部分数据点添加文本标签、使用ggrepel包的geom_text_repel函数避免数据点之间的标签互相重叠(为数据点标签添加线段、指定线段的角度
知识图谱用于推荐系统问题(MVIN,KERL,CKAN,KRED,GAEAT)
Learning to Pre-train Graph Neural Networks(图预训练与微调差异)
【RS采样】A Gain-Tuning Dynamic Negative Sampler for Recommendation (WWW 2022)
Transformer变体(Routing Transformer,Linformer,Big Bird)
pycharm连接远程服务器ssh -u 报错:No such file or directory
【云驻共创】AI在数学界有哪些作用?未来对数学界会有哪些颠覆性影响?
Application and innovation of low code technology in logistics management
图神经网络用于推荐系统问题(IMP-GCN,LR-GCN)
【GCN-RS】Towards Representation Alignment and Uniformity in Collaborative Filtering (KDD‘22)
【AI4Code】《Pythia: AI-assisted Code Completion System》(KDD 2019)
Word中的空白页,怎么也删不掉?如何操作?
Classification parameter stack of JS common built-in object data types
brpc源码解析(四)—— Bthread机制
[untitled]
GPT plus money (OpenAI CLIP,DALL-E)
PHP curl post x-www-form-urlencoded