10 |
|
#include "Daemon.hpp" |
11 |
|
#include "Redirector.hpp" |
12 |
|
|
13 |
+ |
#include <menes-api/exename.hpp> |
14 |
+ |
#include <menes-app/application.hpp> |
15 |
+ |
|
16 |
|
string program; |
17 |
|
bool debug(false); |
18 |
|
|
19 |
< |
int main(int argc, char* argv[]) |
19 |
> |
struct SmershCommand : public app::Application |
20 |
|
{ |
21 |
< |
program = argv[0]; |
19 |
< |
|
20 |
< |
int port(8080); |
21 |
< |
bool fork(false), daemon(false), redirector(false); |
22 |
< |
string redirect, log("smersh.log"); |
23 |
< |
|
24 |
< |
for (int index(1); index < argc; ++index) |
21 |
> |
virtual int Run(const app::ArgumentList& args) |
22 |
|
{ |
23 |
< |
string arg(argv[index]); |
27 |
< |
Matcher matcher; |
23 |
> |
program = api::GetExecutableName(); |
24 |
|
|
25 |
< |
if (arg == "-daemon") |
26 |
< |
{ |
27 |
< |
if (!daemon) daemon = true; |
32 |
< |
} |
33 |
< |
else if (arg == matcher("^-redirector=(.+)$")) |
34 |
< |
{ |
35 |
< |
if (!redirector) redirector = true; |
25 |
> |
int port(54321); |
26 |
> |
bool fork(false), daemon(false), redirector(false); |
27 |
> |
string redirect, log("smersh.log"); |
28 |
|
|
29 |
< |
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") |
29 |
> |
for (size_t index(0); index < args.GetSize(); ++index) |
30 |
|
{ |
31 |
< |
if (!fork) fork = true; |
31 |
> |
string arg(args[index]); |
32 |
> |
Matcher matcher; |
33 |
> |
|
34 |
> |
if (arg == "-daemon") |
35 |
> |
{ |
36 |
> |
if (!daemon) daemon = true; |
37 |
> |
} |
38 |
> |
else if (arg == matcher("^-redirector=(.+)$")) |
39 |
> |
{ |
40 |
> |
if (!redirector) redirector = true; |
41 |
> |
|
42 |
> |
redirect = matcher[1]; |
43 |
> |
} |
44 |
> |
else if (arg == matcher("^-port=([0-9]+)$")) |
45 |
> |
{ |
46 |
> |
port = lexical_cast<int>(matcher[1]); |
47 |
> |
} |
48 |
> |
else if (arg == "-fork") |
49 |
> |
{ |
50 |
> |
if (!fork) fork = true; |
51 |
> |
} |
52 |
> |
else if (arg == matcher("^-log=(.+)$")) |
53 |
> |
{ |
54 |
> |
log = matcher[1]; |
55 |
> |
} |
56 |
> |
else if (arg == "-D") |
57 |
> |
{ |
58 |
> |
if (!debug) debug = true; |
59 |
> |
} |
60 |
> |
else |
61 |
> |
{ |
62 |
> |
cout << "Usage: " << program |
63 |
> |
<< " [-daemon|-redirector=redirect] [-port=port] [-fork] " |
64 |
> |
<< "[-log=log] [-D]\n"; |
65 |
> |
|
66 |
> |
return 1; |
67 |
> |
} |
68 |
|
} |
69 |
< |
else if (arg == matcher("^-log=(.+)$")) |
69 |
> |
|
70 |
> |
if (daemon) |
71 |
|
{ |
72 |
< |
log = matcher[1]; |
72 |
> |
Daemon daemon(port, fork, log); |
73 |
|
} |
74 |
< |
else if (arg == "-D") |
74 |
> |
else if (redirector) |
75 |
|
{ |
76 |
< |
if (!debug) debug = true; |
76 |
> |
Redirector redirector(port, fork, log, redirect); |
77 |
|
} |
78 |
|
else |
79 |
|
{ |
80 |
< |
cout << "Usage: " << program << " [-daemon|-redirector=redirect] [" |
58 |
< |
<< "-port=port] [-fork] [-log=log] [-D]\n"; |
59 |
< |
|
60 |
< |
return 1; |
80 |
> |
Smersh smersh; |
81 |
|
} |
62 |
– |
} |
82 |
|
|
83 |
< |
if (daemon) |
65 |
< |
{ |
66 |
< |
Daemon daemon(port, fork, log); |
67 |
< |
} |
68 |
< |
else if (redirector) |
69 |
< |
{ |
70 |
< |
Redirector redirector(port, fork, log, redirect); |
83 |
> |
return 0; |
84 |
|
} |
85 |
< |
else |
73 |
< |
{ |
74 |
< |
Smersh smersh; |
75 |
< |
} |
76 |
< |
|
77 |
< |
return 0; |
78 |
< |
} |
85 |
> |
} smersh; |
86 |
|
|
87 |
|
Smersh::Smersh(istream& sin, ostream& sout, const Environment& env) |
88 |
|
{ |
89 |
|
parse(sin, env); |
90 |
< |
smersh(sout); |
90 |
> |
smersh(sout, env); |
91 |
|
} |
92 |
|
|
93 |
|
Environment Smersh::env; |
120 |
|
while (query.good()); |
121 |
|
} |
122 |
|
|
123 |
< |
void Smersh::smersh(ostream& sout) |
123 |
> |
void Smersh::smersh(ostream& sout, const Environment& env) |
124 |
|
{ |
125 |
< |
sout << "Content-Type: text/html; charset=UTF-8\n\n"; |
125 |
> |
if (&env == &Smersh::env) sout << "Content-Type: text/html; charset=UTF-8\r" |
126 |
> |
<< "\n\r\n"; |
127 |
|
|
128 |
|
vector<Person> people(1); |
129 |
|
|
186 |
|
|
187 |
|
output(sout, people); |
188 |
|
|
189 |
< |
sout << "!</font></strong></p></body></html>\n"; |
189 |
> |
sout << "!</font></strong></p></body></html>\r\n"; |
190 |
|
} |
191 |
|
|
192 |
|
void Smersh::output(ostream& sout, const vector<Person>& people) |