/* create by woundedsoul
* 本类是处理变量及其关联值
* 本人在暑假中没事干!偶尔看到一篇说用Delphi写CGI程序的文章
* 于是我想用C++为自已做一个动态网站吧!本来是想学ASP做的无
* 赖本人太懒(能用C++写何不用呢?呵呵!)
* 偶这个留言本是用文本文件存储访客留言的,不过我正打算把它转到Mysql 数据库里
*
*
*
*
*
*
*/
//---------------------------webtools.h-----------------------------//
#include<memory.h>
#include<stdlib.h>
class URL
{
public:
URL(char * string);
~URL();
char* operator [](const char * varname);
int Pos(const char *Data,char ch); //返回Ch在字串中第一次出现的位置
int Pos(const char *Data,const char*sub); //返回sub子串在字符串第一次出现的位置
int Pos(const char *Data,int begin,char ch); //返回Ch在字符串从begin后第一次出现位置
char * Sub(const char *Data,int begin,int end); //返回子串
char * ExtractFromData(const char * Data,const char * tag);
char * decodeURLString(const char* URLstr); //对客户端浏览器返回的数据流进行解码
char translateHex(char hex); //转换成16进制
private:
char *str; //解码前的URL
char *decode_str; //解码后的URL
};
//以下是类的实现部分
//--------------------------------------------------------------------------------------------------------------------------------------------
URL::URL(char* string)
{
int len=strlen(string);
str=new char[len+1];
strcpy(str,string);
str[len]='\0';
decode_str=decodeURLString(str);
}
//--------------------------------------------------------------------------------------------------------------------------------------------
URL::~URL()
{
delete str;
}
char * URL:: operator[](const char *varname)
{
return ExtractFromData(decode_str,varname);
}
//--------------------------------------------------------------------------------------------------------------------------------------------
int URL::Pos(const char *Data,char ch)
{
int length_Data=strlen(Data);
for(int i=0;i<length_Data;i++)
{
if(Data[i]==ch)
return i;
else
continue;
}
return -5000;
}
//--------------------------------------------------------------------------------------------------------------------------------------------
int URL::Pos(const char *Data,const char*sub)
{
int token=0;
int length_Data=strlen(Data);
int length_sub=strlen(sub);
for(int i=0;i<length_Data;i++)
{
token=0;
for(int j=0;j<length_sub;j++)
{
if(Data[i+j]==sub[j])
{
token++;
}
}
if(token==length_sub)
return i;
else
continue;
}
return -5000;
}
//--------------------------------------------------------------------------------------------------------------------------------------------
int URL::Pos(const char *Data,int begin,char ch)
{
int length_data=strlen(Data);
if(begin<0)
return -5000;
for(int i=begin;i<length_data;i++)
if(Data[i]==ch)
return i;
else
continue;
return -5000;
}
//--------------------------------------------------------------------------------------------------------------------------------------------
char * URL:: Sub(const char *Data,int begin,int end)
{
int length_Data=strlen(Data);
int count=end-begin;
if(count<0)
exit(1);
char *temp=new char[count+1];
temp[count]='\0';
if(temp!=NULL)
{
for(int i=0;i<count;i++)
{
temp[i]=Data[begin+i];
}
return temp;
}
else
return NULL;
}
//--------------------------------------------------------------------------------------------------------------------------------------------
char URL::translateHex(char hex)
{
if(hex >= 'A')
return (hex & 0xdf) - 'A' + 10;
else
return hex - '0';
}
//--------------------------------------------------------------------------------------------------------------------------------------------
//----------------------解码浏览器返回的URL数据-----------------------------------------------------------------------------------------------
char * URL::decodeURLString(const char* URLstr)
{
int len = strlen(URLstr);
char* result = new char[len + 1];
memset(result, len + 1, 0);
for(int i = 0, j = 0; i <= len; i++, j++) {
if(URLstr[i] == '+')
result[j] = ' ';
else if(URLstr[i] == '%') {
result[j] =
translateHex(URLstr[i + 1]) * 16 +
translateHex(URLstr[i + 2]);
i += 2;
} else
result[j] = URLstr[i];
}
return result;
}
//--------------------------------------------------------------------------------------------------------------------------------------------
//-----------------------从URL中提取变量的值--------------------------------------------------------------------------------------------------
char * URL::ExtractFromData(const char * Data,const char * tag)
{
int position=0;
int position1=0;
position=Pos(Data,tag);
if(position>=0)
{
position1=Pos(Data,position,'&');
if((position+1+strlen(tag))==position1)
return NULL; //如果为空则返回NULL
if(position1>0)
return Sub(Data,(position+1+strlen(tag)),position1);
else
return Sub(Data,(position+1+strlen(tag)),strlen(Data));
//return NULL;
}
else
{
return NULL;
}
}
//--------------------------------------------------------------------------------------------------------------------------------------------
GET 方法最简单了!
char *str=getenv("QUERY-STRING")就可以读到用GET文法从URL返回的数据了!
下面是POST方法!
char *len=getenv("CONTENT-LENGTH");
int length=atoi(len);
char *str=new char[length+1];
str[length]='\0';
cin.read(str,length);