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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
|
/*
* Copyright 2017 Chris Xiong
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#ifndef CGILIB_HPP
#define CGILIB_HPP
#include <cstdlib>
#include <map>
#include <string>
#include <vector>
const char* base64_table="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
std::string base64_encode(std::string s)
{
std::string r="";
for(unsigned i=0;i<s.length();i+=3)
{
int b=(s[i]&0xFC)>>2;
r+=base64_table[b];
b=(s[i]&0x03)<<4;
if(i+1<s.length())
{
b|=(s[i+1]&0xF0)>>4;
r+=base64_table[b];
b=(s[i+1]&0x0F)<<2;
if(i+2<s.length())
{
b|=(s[i+2]&0xC0)>>6;
r+=base64_table[b];
b=s[i+2]&0x3F;
r+=base64_table[b];
}else r+=base64_table[b],r+='=';
}else r+=base64_table[b],r+="==";
}
return r;
}
void split(std::string s,char c,std::vector<std::string>& v)
{
v.clear();
for(size_t anch=0;;)
{
std::string sec;
if(s.find(c,anch)==std::string::npos)
sec=s.substr(anch);
else sec=s.substr(anch,s.find(c,anch)-anch);
v.push_back(sec);
if(s.find(c,anch)==std::string::npos)break;
anch=s.find(c,anch)+1;
}
}
std::string trim(std::string s)
{
int l=0;for(;isblank(s[l]);++l)
s=s.substr(l);
while(isblank(s.back()))s.pop_back();
return s;
}
class QueryStrParser
{
private:
std::map<std::string,std::string> q;
void parse(std::string es)
{
for(size_t anch=0;;)
{
std::string sec;
if(es.find('&',anch)==std::string::npos)
sec=es.substr(anch);
else sec=es.substr(anch,es.find('&',anch)-anch);
if(sec.find('=')==std::string::npos)
q[sec.substr(0)]="";
else
q[sec.substr(0,sec.find('='))]=sec.substr(sec.find('=')+1);
if(es.find('&',anch)==std::string::npos)break;
anch=es.find('&',anch)+1;
}
}
public:
QueryStrParser()
{
char* e=getenv("QUERY_STRING");
if(!e)return;
parse(std::string(e));
}
QueryStrParser(std::string s)
{
parse(s);
}
bool exist(std::string s)
{
return q.find(s)!=q.end();
}
std::string value(std::string s)
{
if(!exist(s))return "";
return q.find(s)->second;
}
};
class RequestCookies
{
private:
std::map<std::string,std::string> m;
void parse(std::string cookie)
{
std::vector<std::string> v;
split(cookie,';',v);
}
public:
RequestCookies()
{
char* e=getenv("HTTP_COOKIE");
if(!e)return;
parse(std::string(e));
}
};
class DOMAttrib
{
private:
std::map<std::string,std::string> m;
public:
DOMAttrib(std::string s="")
{
for(size_t anch=0;;)
{
std::string sec;
if(s.find('"',anch)==std::string::npos)
sec=s.substr(anch);
else sec=s.substr(anch,s.find('"',s.find('"',anch)+1)-anch+1);
if(sec.find('=')==std::string::npos)
m[sec.substr(0)]="";
else
m[sec.substr(0,sec.find('='))]=sec.substr(sec.find('=')+2),
m[sec.substr(0,sec.find('='))].pop_back();
if(s.find(' ',anch+sec.length())==std::string::npos)break;
anch=s.find(' ',anch+sec.length())+1;
}
}
void setAttrib(std::string a,std::string v){m[a]=v;}
void eraseAttrib(std::string a){if(existAttrib(a))m.erase(m.find(a));}
bool existAttrib(std::string a){return m.find(a)!=m.end();}
std::string getAttrib(std::string a){if(!existAttrib(a))return "";return m[a];}
std::string to_string()
{
std::string r;bool cf=false;
for(auto i=m.begin();i!=m.end();++i)
{
if(cf)r+=' ';else cf=true;
r+=i->first+"=\""+i->second+"\"";
}
return r;
}
};
static const char* twoxx[]={
"OK",
"Created",
"Accepted",
"Non-Authoritative Information",
"No Content",
"Reset Content",
"Partial Content",
"Multi-Status",
"Already Reported"
};
static const char* threexx[]={
"Multiple Choices",
"Moved Permanently",
"Found",
"See Other",
"Not Modified",
"Use Proxy",
"???",
"Temporary Redirect"
};
static const char* fourxx[]={
"Bad Request",
"Unauthorized",
"Payment Required",
"Forbidden",
"Not Found",
"Method Not Allowed",
"Not Acceptable",
"Proxy Authentication Required",
"Request Timeout",
"Conflict",
"Gone",
"Length Required",
"Precondition Failed",
"Request Entity Too Large",
"Request-URI Too Long",
"Unsupported Media Type",
"Requested Range Not Satisfiable",
"Expectation Failed"
};
static const char* fivexx[]={
"Internal Server Error",
"Not Implemented",
"Bad Gateway",
"Service Unavailable",
"Gateway Timeout",
"HTTP Version Not Supported"
};
class HTTPHeader
{
private:
int status;
std::vector<std::string> hdr;
public:
HTTPHeader(){status=200;}
void setStatusCode(int c){status=c;}
int statusCode(){return status;}
void appendHeader(std::string s){hdr.push_back(s);}
void print()
{
if(status>=200&&status<=208)
printf("Status: %d %s\r\n",status,twoxx[status-200]);
if(status>=300&&status<=307)
printf("Status: %d %s\r\n",status,threexx[status-300]);
if(status>=400&&status<=417)
printf("Status: %d %s\r\n",status,fourxx[status-400]);
if(status>=500&&status<=505)
printf("Status: %d %s\r\n",status,fivexx[status-500]);
for(unsigned i=0;i<hdr.size();++i)
printf("%s\r\n",hdr[i].c_str());
printf("\r\n");
}
};
#endif
|