2013年3月23日星期六

ZSCGI——– 一个简单的C++ CGI封装

ZSCGI——– 一个简单的C++ CGI封装

本文是继上一篇文章之后的一点深入吧~~ 学了一些关于C/C++ CGI的相关知识,为了方便使用,所以做了这个封装,由于我也是刚刚开始玩CGI,所以写的过程中难免会有所疏漏,所以希望看到这篇文章的朋友,有什么好 的建议,都可以在这里留言,我会一一回复大家的,谢谢!
下面是CGI中用到的环境变量的一个定义,目前只加进去了一部分,如果要扩展,可以有EnvName的最后一项INVALID_ENV之前添加新项的枚举 定义,然后再在EnvNameStr的0之前,添加相应的字符串定义即可,这里需要注意的是,枚举值的定义和字符串的定义一定要对应起来:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#ifndef __EVN_DEF_H__
#define __EVN_DEF_H__
 
enum EnvName
{
 HTTP_HOST,
 HTTP_CONNECTION,
 HTTP_USER_AGENT,
 HTTP_REFERER,
 CONTENT_LENGTH,
 HTTP_CACHE_CONTROL,
 HTTP_ORIGIN,
 CONTENT_TYPE,
 HTTP_ACCEPT,
 HTTP_ACCEPT_ENCODING,
 HTTP_ACCEPT_LANGUAGE,
 HTTP_ACCEPT_CHARSET,
 SERVER_SIGNATURE,
 SERVER_SOFTWARE,
 SERVER_NAME,
 SERVER_ADDR,
 SERVER_PORT,
 REMOTE_ADDR,
 DOCUMENT_ROOT,
 SERVER_ADMIN,
 SCRIPT_FILENAME,
 REMOTE_PORT,
 GATEWAY_INTERFACE,
 SERVER_PROTOCOL,
 REQUEST_METHOD,
 QUERY_STRING,
 REQUEST_URI,
 SCRIPT_NAME,
 INVALID_ENV
};
 
static const char * EnvNameStr[] =
{
 "HTTP_HOST",
 "HTTP_CONNECTION",
 "HTTP_USER_AGENT",
 "HTTP_REFERER",
 "CONTENT_LENGTH",
 "HTTP_CACHE_CONTROL",
 "HTTP_ORIGIN",
 "CONTENT_TYPE",
 "HTTP_ACCEPT",
 "HTTP_ACCEPT_ENCODING",
 "HTTP_ACCEPT_LANGUAGE",
 "HTTP_ACCEPT_CHARSET",
 "SERVER_SIGNATURE",
 "SERVER_SOFTWARE",
 "SERVER_NAME",
 "SERVER_ADDR",
 "SERVER_PORT",
 "REMOTE_ADDR",
 "DOCUMENT_ROOT",
 "SERVER_ADMIN",
 "SCRIPT_FILENAME",
 "REMOTE_PORT",
 "GATEWAY_INTERFACE",
 "SERVER_PROTOCOL",
 "REQUEST_METHOD",
 "QUERY_STRING",
 "REQUEST_URI",
 "SCRIPT_NAME",
 0
};
 
#endif __EVN_DEF_H__
下面即是ZSCGI类,它里面封装了一些比较常用的C/C++ CGI中常用的操作,目前该类的功能还是比较基础的,后续我会在学习的过程中不断的扩展该类的:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#ifndef __ZS_CGI_H__
#define __ZS_CGI_H__
 
#include "EnvDef.h"
#include <string>
#include <map>
 
/**
 * @brief this is a utility class for c++ CGI
 */
class ZSCGI
{
public:
 /**
  * @brief get the value of specified environment variable
  * @param eEnvName, the enum definition of the environment variable
  * @return  the point to the value
  */
 static char * GetEnv(EnvName eEnvName);
 /**
  * @brief encode a url
  * @param strSrc, the souce url
  * @param strDest, string to store the encoded url
  */
 static void EncodeUrl(const std::string & strSrc, std::string & strDest);
 /**
  * @brief decode a url
  * @param strSrc, the source url
  * @param strDest, string to store the decoded url
  */
 static void DecodeUrl(const std::string & strSrc, std::string & strDest);
 /**
  * @brief extract variable from a url
  * @param pUrl, the url to be extracted
  * @param mapRes, the result key-value pair set
  */
 static void ExtractVar(const char * pUrl, 
    std::map<std ::string, const char *> & mapRes);
 /**
  * @brief write data to the webpages
  * @param pBuffer, the data buffer
  * @param nLen, the length of data
  * @return the num of bytes written
  */
 static int WriteData(const char * pBuffer, int nLen);
 /**
  * @brief read data from the webserver
  * @param pBuffer, the buffer to store the data
  * @param nLen, the length of the buffer
  * @return the num of bytes read
  */
 static int ReadData(char * pBuffer, int nLen);
 /**
  * @brief print the mine header
  * @param pHeader, the header
  */
 static void MineHeader(const char * pHeader);
private:
 /**
  * @brief convert a char to hexidecimal number
  * @param bCh, the char to be converted
  * @param szBuf, the buffer to store the result
  */
 static void CharToHex(unsigned char bCh, char * szBuf);
 /**
  * @brief convert hexdecimal number to char
  * @param hHigh, the high 4 bits
  * @param bLow, the low 4 bits
  * @return the converted char
  */
 static unsigned char HexToChar(char bHigh, char bLow);
};
 
#endif //__ZS_CGI_H__
</std></map></string>
关于每个API的用法,上面的代码中,已经有比较详细的说明了,在这里,我就不做过多的介绍了。关于该类的实现,我在这里就不贴了,如果有朋友要用该类,那么下面是该类的实现的lib文件:libzscgi.lib,你可以在自己的项目中加入上面的头文件,然后再依赖libzscgi.lib库就可以了,如果有朋友对该类的实现感兴趣的话,可以给我发邮件wuzesheng86@gmail.com
欢迎大家提意见!

没有评论:

发表评论