1 |
// Smersh |
2 |
// |
3 |
// Douglas Thrift |
4 |
// |
5 |
// $Id$ |
6 |
|
7 |
#include "Smersh.hpp" |
8 |
#include "Matcher.hpp" |
9 |
#include "Person.hpp" |
10 |
#include "Daemon.hpp" |
11 |
#include "Redirector.hpp" |
12 |
|
13 |
string program; |
14 |
bool debug(false); |
15 |
|
16 |
int main(int argc, char* argv[]) |
17 |
{ |
18 |
program = argv[0]; |
19 |
|
20 |
int port(8080); |
21 |
bool fork(false), daemon(false), redirector(false); |
22 |
string redirect; |
23 |
|
24 |
for (int index(1); index < argc; ++index) |
25 |
{ |
26 |
string arg(argv[index]); |
27 |
Matcher matcher; |
28 |
|
29 |
if (arg == "-daemon") |
30 |
{ |
31 |
if (!daemon) daemon = true; |
32 |
} |
33 |
else if (arg == matcher("^-redirector=(.+)$")) |
34 |
{ |
35 |
if (!redirector) redirector = true; |
36 |
|
37 |
redirect = matcher[1]; |
38 |
} |
39 |
else if (arg == matcher("^-port=([0-9]+)$")) |
40 |
{ |
41 |
port = lexical_cast<int>(matcher[1]); |
42 |
} |
43 |
else if (arg == "-fork") |
44 |
{ |
45 |
if (!fork) fork = true; |
46 |
} |
47 |
else if (arg == "-D") |
48 |
{ |
49 |
if (!debug) debug = true; |
50 |
} |
51 |
else |
52 |
{ |
53 |
cout << "Usage: " << program << " [-daemon|-redirector=redirect] [" |
54 |
<< "-port=port] [-fork] [-D]\n"; |
55 |
|
56 |
return 1; |
57 |
} |
58 |
} |
59 |
|
60 |
if (daemon) |
61 |
{ |
62 |
Daemon daemon(port, fork); |
63 |
} |
64 |
else if (redirector) |
65 |
{ |
66 |
Redirector redirector(port, fork, redirect); |
67 |
} |
68 |
else |
69 |
{ |
70 |
Smersh smersh; |
71 |
} |
72 |
|
73 |
return 0; |
74 |
} |
75 |
|
76 |
Smersh::Smersh(istream& sin, ostream& sout) |
77 |
{ |
78 |
parse(sin); |
79 |
smersh(sout); |
80 |
} |
81 |
|
82 |
void Smersh::parse(istream& sin) |
83 |
{ |
84 |
stringstream query(sgetenv("QUERY_STRING")); |
85 |
|
86 |
if (sgetenv("REQUEST_METHOD") == "POST") |
87 |
{ |
88 |
streamsize length(lexical_cast<streamsize>(sgetenv("CONTENT_LENGTH"))); |
89 |
char* content = new char[length]; |
90 |
|
91 |
sin.read(content, length); |
92 |
query.write(content, length); |
93 |
|
94 |
delete [] content; |
95 |
} |
96 |
if (query.str() == "") return; |
97 |
|
98 |
do |
99 |
{ |
100 |
string name, value; |
101 |
|
102 |
getline(query, name, '='); |
103 |
getline(query, value, '&'); |
104 |
|
105 |
cgi.insert(pair<string, string>(name, value)); |
106 |
} |
107 |
while (query.good()); |
108 |
} |
109 |
|
110 |
void Smersh::smersh(ostream& sout) |
111 |
{ |
112 |
sout << "Content-Type: text/html\n\n"; |
113 |
|
114 |
vector<Person> people(1); |
115 |
|
116 |
for (multimap<string, string>::iterator itor(cgi.lower_bound("sn")); itor |
117 |
!= cgi.upper_bound("sn"); ++itor) |
118 |
{ |
119 |
string sn(itor->second); |
120 |
|
121 |
for (string::size_type index(0); index < sn.length(); ++index) |
122 |
{ |
123 |
while (!isalnum(sn[index]) && index < sn.length()) |
124 |
{ |
125 |
if (sn[index] == '%' && index + 2 < sn.length()) |
126 |
{ |
127 |
istringstream code(sn.substr(index + 1, 2)); |
128 |
unsigned short character; |
129 |
|
130 |
code.setf(ios_base::hex, ios_base::basefield); |
131 |
|
132 |
code >> character; |
133 |
|
134 |
sn.replace(index, 3, 1, character); |
135 |
|
136 |
if (isalnum(sn[index])) break; |
137 |
} |
138 |
|
139 |
sn.erase(index, 1); |
140 |
} |
141 |
} |
142 |
|
143 |
if (debug) cerr << "sn = " << sn << '\n'; |
144 |
|
145 |
Person person(sn); |
146 |
|
147 |
people.push_back(person); |
148 |
|
149 |
if (person.isMultiple()) |
150 |
{ |
151 |
people.insert(people.end(), person.beginMultiple(), |
152 |
person.endMultiple()); |
153 |
person.clearMultiple(); |
154 |
} |
155 |
} |
156 |
|
157 |
if (people.size() > 1) |
158 |
{ |
159 |
people.erase(people.begin()); |
160 |
|
161 |
sort(people.begin(), people.end()); |
162 |
|
163 |
people.erase(unique(people.begin(), people.end()), people.end()); |
164 |
} |
165 |
|
166 |
sout << "<html><head><title>Hello, " << flush; |
167 |
|
168 |
output(sout, people); |
169 |
|
170 |
sout << "!</title></head><body><p style=\"font-variant: small-caps\"><stro" |
171 |
<< "ng><font face=\"Comic Sans MS\" size=\"4\">Hello, " << flush; |
172 |
|
173 |
output(sout, people); |
174 |
|
175 |
sout << "!</font></strong></p></body></html>\n"; |
176 |
} |
177 |
|
178 |
void Smersh::output(ostream& sout, const vector<Person>& people) |
179 |
{ |
180 |
for (vector<Person>::const_iterator person(people.begin()); person != |
181 |
people.end(); ++person) |
182 |
{ |
183 |
sout << *person; |
184 |
|
185 |
if (person + 2 == people.end()) |
186 |
{ |
187 |
sout << (people.size() > 2 ? "," : "") << " and/or "; |
188 |
} |
189 |
else if (person + 1 != people.end()) |
190 |
{ |
191 |
sout << ", "; |
192 |
} |
193 |
|
194 |
sout << flush; |
195 |
} |
196 |
} |