5 |
|
// $Id$ |
6 |
|
|
7 |
|
#include "Smersh.hpp" |
8 |
< |
#include "Matcher.hpp" |
8 |
> |
#include "Matcher/Matcher.hpp" |
9 |
|
#include "Person.hpp" |
10 |
|
#include "Daemon.hpp" |
11 |
|
#include "Redirector.hpp" |
13 |
|
#include <menes-api/exename.hpp> |
14 |
|
#include <menes-app/application.hpp> |
15 |
|
|
16 |
– |
string program; |
17 |
– |
bool debug(false); |
18 |
– |
|
16 |
|
struct SmershCommand : public app::Application |
17 |
|
{ |
18 |
|
virtual int Run(const app::ArgumentList& args) |
19 |
|
{ |
20 |
< |
program = api::GetExecutablePath().GetName(); |
20 |
> |
Smersh::program = api::GetExecutablePath().GetName(); |
21 |
|
|
22 |
|
int port(54321); |
23 |
|
bool daemon(false), redirector(false); |
24 |
< |
string redirect, log("smersh.log"); |
24 |
> |
ext::String redirect, log("smersh.log"); |
25 |
|
|
26 |
< |
for (unsigned index(0); index < args.GetSize(); ++index) |
26 |
> |
_foreach (app::ArgumentList, arg, args) |
27 |
|
{ |
31 |
– |
string arg(args[index]); |
28 |
|
Matcher matcher; |
29 |
|
|
30 |
< |
if (arg == "-daemon") |
30 |
> |
if (*arg == "-daemon") |
31 |
|
{ |
32 |
|
if (!daemon) daemon = true; |
33 |
|
} |
34 |
< |
else if (arg == matcher("^-redirector=(.+)$")) |
34 |
> |
else if (*arg == matcher("^-redirector=(.+)$")) |
35 |
|
{ |
36 |
|
if (!redirector) redirector = true; |
37 |
|
|
38 |
|
redirect = matcher[1]; |
39 |
|
} |
40 |
< |
else if (arg == matcher("^-port=([0-9]+)$")) |
40 |
> |
else if (*arg == matcher("^-port=([0-9]+)$")) |
41 |
|
{ |
42 |
|
port = lexical_cast<int>(matcher[1]); |
43 |
|
} |
44 |
< |
else if (arg == matcher("^-log=(.+)$")) |
44 |
> |
else if (*arg == matcher("^-log=(.+)$")) |
45 |
|
{ |
46 |
|
log = matcher[1]; |
47 |
|
} |
48 |
< |
else if (arg == "-D") |
48 |
> |
else if (*arg == "-D") |
49 |
|
{ |
50 |
< |
if (!debug) debug = true; |
50 |
> |
if (!Smersh::debug) Smersh::debug = true; |
51 |
|
} |
52 |
|
else |
53 |
|
{ |
54 |
< |
cout << "Usage: " << program |
59 |
< |
<< " [-daemon|-redirector=redirect] [-port=port] [-log=log]" |
60 |
< |
<< " [-D]\n"; |
54 |
> |
api::Cerr << "Usage: " << Smersh::program << " [-daemon|-redirector=redirect] [-port=port] [-log=log] [-D]\n"; |
55 |
|
|
56 |
|
return 1; |
57 |
|
} |
74 |
|
} |
75 |
|
} smersh; |
76 |
|
|
77 |
< |
Smersh::Smersh(istream& sin, ostream& sout, const Environment& env) |
77 |
> |
Smersh::Smersh(std::istream& sin, std::ostream& sout, const Environment& env) |
78 |
|
{ |
79 |
|
parse(sin, env); |
80 |
|
smersh(sout, env); |
81 |
|
} |
82 |
|
|
83 |
+ |
ext::String Smersh::program; |
84 |
+ |
bool Smersh::debug(false); |
85 |
+ |
|
86 |
|
Environment Smersh::env; |
87 |
|
|
88 |
|
void Smersh::parse(istream& sin, const Environment& env) |
89 |
|
{ |
90 |
< |
stringstream query(env.get("QUERY_STRING")); |
90 |
> |
std::stringstream query(env.get("QUERY_STRING")); |
91 |
|
|
92 |
|
if (env.get("REQUEST_METHOD") == "POST") |
93 |
|
{ |
94 |
< |
streamsize length(lexical_cast<streamsize>(env.get("CONTENT_LENGTH"))); |
94 |
> |
std::streamsize length(lexical_cast<std::streamsize>(env.get("CONTENT_LENGTH"))); |
95 |
|
char* content(new char[length]); |
96 |
|
|
97 |
|
sin.read(content, length); |
99 |
|
|
100 |
|
delete [] content; |
101 |
|
} |
102 |
+ |
|
103 |
|
if (query.str() == "") return; |
104 |
|
|
105 |
|
do |
106 |
|
{ |
107 |
< |
string name, value; |
107 |
> |
std::string name, value; |
108 |
|
|
109 |
< |
getline(query, name, '='); |
110 |
< |
getline(query, value, '&'); |
109 |
> |
std::getline(query, name, '='); |
110 |
> |
std::getline(query, value, '&'); |
111 |
|
|
112 |
< |
cgi.insert(pair<string, string>(name, value)); |
112 |
> |
cgi.insert(std::pair<std::string, std::string>(name, value)); |
113 |
|
} |
114 |
|
while (query.good()); |
115 |
|
} |
116 |
|
|
117 |
< |
void Smersh::smersh(ostream& sout, const Environment& env) |
117 |
> |
void Smersh::smersh(std::ostream& sout, const Environment& env) |
118 |
|
{ |
119 |
< |
if (&env == &Smersh::env) sout << "Content-Type: text/html; charset=UTF-8\r" |
122 |
< |
<< "\n\r\n"; |
119 |
> |
if (&env == &Smersh::env) sout << "Content-Type: text/html; charset=UTF-8\r\n\r\n"; |
120 |
|
|
121 |
< |
vector<Person> people(1); |
121 |
> |
ext::RedBlackSet<Person> people; |
122 |
|
|
123 |
< |
for (multimap<string, string>::iterator itor(cgi.lower_bound("sn")); itor |
124 |
< |
!= cgi.upper_bound("sn"); ++itor) |
123 |
> |
typedef std::multimap<std::string, std::string> MultiMap; |
124 |
> |
|
125 |
> |
_for (MultiMap::iterator, itor, cgi.lower_bound("sn"), cgi.upper_bound("sn")) |
126 |
|
{ |
127 |
< |
string sn(itor->second); |
127 |
> |
std::string sn(itor->second); |
128 |
|
|
129 |
< |
for (string::size_type index(0); index < sn.length(); ++index) |
129 |
> |
for (std::string::size_type index(0); index < sn.length(); ++index) |
130 |
|
{ |
131 |
|
while (!isalnum(sn[index]) && index < sn.length()) |
132 |
|
{ |
133 |
|
if (sn[index] == '%' && index + 2 < sn.length()) |
134 |
|
{ |
135 |
< |
istringstream code(sn.substr(index + 1, 2)); |
135 |
> |
std::istringstream code(sn.substr(index + 1, 2)); |
136 |
|
unsigned short character; |
137 |
|
|
138 |
< |
code.setf(ios_base::hex, ios_base::basefield); |
138 |
> |
code.setf(std::ios_base::hex, std::ios_base::basefield); |
139 |
|
|
140 |
|
code >> character; |
141 |
|
|
152 |
|
|
153 |
|
Person person(sn); |
154 |
|
|
155 |
< |
people.push_back(person); |
155 |
> |
people.Insert(person); |
156 |
|
|
157 |
|
if (person.isMultiple()) |
158 |
|
{ |
159 |
< |
people.insert(people.end(), person.beginMultiple(), |
160 |
< |
person.endMultiple()); |
159 |
> |
_foreach (ext::Vector<Person>, single, person.getMultiple()) people.Insert(*single); |
160 |
> |
|
161 |
|
person.clearMultiple(); |
162 |
|
} |
163 |
|
} |
164 |
|
|
165 |
< |
if (people.size() > 1) |
168 |
< |
{ |
169 |
< |
people.erase(people.begin()); |
170 |
< |
|
171 |
< |
sort(people.begin(), people.end()); |
172 |
< |
|
173 |
< |
people.erase(unique(people.begin(), people.end()), people.end()); |
174 |
< |
} |
165 |
> |
if (people.IsEmpty()) people.Insert(Person()); |
166 |
|
|
167 |
< |
sout << "<html><head><title>Hello, " << flush; |
167 |
> |
sout << "<html><head><title>Hello, " << std::flush; |
168 |
|
|
169 |
|
output(sout, people); |
170 |
|
|
171 |
< |
sout << "!</title></head><body><p style=\"font-variant: small-caps\"><stro" |
181 |
< |
<< "ng><font face=\"Comic Sans MS\" size=\"4\">Hello, " << flush; |
171 |
> |
sout << "!</title></head><body><p style=\"font-variant: small-caps\"><strong><font face=\"Comic Sans MS\" size=\"4\">Hello, " << std::flush; |
172 |
|
|
173 |
|
output(sout, people); |
174 |
|
|
175 |
|
sout << "!</font></strong></p></body></html>\r\n"; |
176 |
|
} |
177 |
|
|
178 |
< |
void Smersh::output(ostream& sout, const vector<Person>& people) |
178 |
> |
void Smersh::output(std::ostream& sout, const ext::RedBlackSet<Person>& people) |
179 |
|
{ |
180 |
< |
for (vector<Person>::const_iterator person(people.begin()); person != |
191 |
< |
people.end(); ++person) |
180 |
> |
_foreach (ext::RedBlackSet<Person>, person, people) |
181 |
|
{ |
182 |
|
sout << *person; |
183 |
|
|
184 |
< |
if (person + 2 == people.end()) |
184 |
> |
if (person + 2 == people.End()) |
185 |
|
{ |
186 |
< |
sout << (people.size() > 2 ? "," : "") << " and/or "; |
186 |
> |
sout << (people.GetSize() > 2 ? "," : "") << " and/or "; |
187 |
|
} |
188 |
< |
else if (person + 1 != people.end()) |
188 |
> |
else if (person + 1 != people.End()) |
189 |
|
{ |
190 |
|
sout << ", "; |
191 |
|
} |
192 |
|
|
193 |
< |
sout << flush; |
193 |
> |
sout << std::flush; |
194 |
|
} |
195 |
|
} |