41
41
// cout << "deciper: " << str << endl;
45
46
string::size_type b = 0, e;
46
47
while ((e = str.find_first_of("%+", b)) != string::npos)
48
49
out.append(str, b, e - b);
49
50
if (str[e] == '+') out.append(" ");
50
else if (str.length() > e+2)
51
else if (str.length() > e+2 &&
52
(high = fromHex(str[e+1])) != -1 &&
53
(low = fromHex(str[e+2])) != -1)
52
int ch = fromHex(str[e+1]) << 4 | fromHex(str[e+2]);
55
int ch = high << 4 | low;
60
{ // keep the broken escape char
60
out.append(str, b, str.length() - b);
67
out.append(str, b, string::npos);
92
99
out.insert(splitParam(str.substr(b, e - b)));
93
100
b = str.find_first_not_of('&', e+1);
95
out.insert(splitParam(str.substr(b, str.length() - b)));
101
if (b == string::npos) break;
103
if (b != string::npos)
104
out.insert(splitParam(str.substr(b, str.length() - b)));
109
map<string, string> getCookies()
111
map<string, string> out;
113
const char* x = getenv("HTTP_COOKIE");
117
// cout << "parse: " << str << endl;
119
string::size_type b = str.find_first_not_of("; ", 0), e;
120
if (b == string::npos) return out;
122
while ((e = str.find_first_of("; ", b)) != string::npos)
124
out.insert(splitParam(str.substr(b, e - b)));
125
b = str.find_first_not_of("; ", e+1);
126
if (b == string::npos) break;
128
if (b != string::npos)
129
out.insert(splitParam(str.substr(b, str.length() - b)));
134
string uriEncode(const string& str)
137
static const char tbl[17] = "0123456789ABCDEF";
139
string::size_type b = 0, e;
140
while ((e = str.find_first_not_of(
141
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789~:/.,@-!",
144
out.append(str, b, e - b);
147
out += tbl[(str[e] >> 4) & 0xF];
148
out += tbl[(str[e] >> 0) & 0xF];
153
out.append(str, b, str.length() - b);
100
158
int redirectUrl(const string& url)
102
cout << "Status: 303 Moved Permanently\r\n"
103
<< "Location: " << url << "\r\n"
104
<< "Content-type: text/html\r\n\r\n"
105
<< "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">\r\n"
106
<< "<html><head>\r\n"
107
<< "<title>301 Moved Permanently</title>\r\n"
108
<< "</head><body>\r\n"
109
<< "<h1>Moved Permanently</h1>\r\n"
110
<< "The document has moved <a href=\"" << url << "\">here</a>.\r\n"
112
<< "</body></html>\r\n";
160
string proto(url, 0, 11);
161
for (string::size_type i = 0; i < proto.length(); ++i)
162
if (proto[i] >= 'A' && proto[i] <= 'Z')
163
proto[i] += 'a' - 'A';
165
if (proto == "javascript:")
167
cout << "Status: 200 OK\r\n"
168
<< "Content-type: text/plain\r\n\r\n"
169
<< "Javascript go awaaayyyyyaaayyy! GO AWAY!\n";
173
cout << "Status: 303 Moved Permanently\r\n"
174
<< "Location: " << uriEncode(url) << "\r\n"
175
<< "Content-type: text/html\r\n\r\n"
176
<< "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">\r\n"
177
<< "<html><head>\r\n"
178
<< "<title>301 Moved Permanently</title>\r\n"
179
<< "</head><body>\r\n"
180
<< "<h1>Moved Permanently</h1>\r\n"
181
<< "The document has moved <a href=\"" << uriEncode(url) << "\">here</a>.\r\n"
183
<< "</body></html>\r\n";