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[]) |
20 |
|
{ |
21 |
< |
program = argv[0]; |
21 |
> |
program = api::GetExecutableName(); |
22 |
|
|
23 |
< |
int port(8080); |
23 |
> |
int port(54321); |
24 |
|
bool fork(false), daemon(false), redirector(false); |
25 |
< |
string redirect; |
25 |
> |
string redirect, log("smersh.log"); |
26 |
|
|
27 |
|
for (int index(1); index < argc; ++index) |
28 |
|
{ |
47 |
|
{ |
48 |
|
if (!fork) fork = true; |
49 |
|
} |
50 |
+ |
else if (arg == matcher("^-log=(.+)$")) |
51 |
+ |
{ |
52 |
+ |
log = matcher[1]; |
53 |
+ |
} |
54 |
|
else if (arg == "-D") |
55 |
|
{ |
56 |
|
if (!debug) debug = true; |
57 |
|
} |
58 |
|
else |
59 |
|
{ |
60 |
< |
cout << "Usage: " << program << " [-daemon|-redirector=redirect] [" |
61 |
< |
<< "-port=port] [-fork] [-D]\n"; |
60 |
> |
cout << "Usage: " << program |
61 |
> |
<< " [-daemon|-redirector=redirect] [-port=port] [-fork] " |
62 |
> |
<< "[-log=log] [-D]\n"; |
63 |
|
|
64 |
|
return 1; |
65 |
|
} |
67 |
|
|
68 |
|
if (daemon) |
69 |
|
{ |
70 |
< |
Daemon daemon(port, fork); |
70 |
> |
Daemon daemon(port, fork, log); |
71 |
|
} |
72 |
|
else if (redirector) |
73 |
|
{ |
74 |
< |
Redirector redirector(port, fork, redirect); |
74 |
> |
Redirector redirector(port, fork, log, redirect); |
75 |
|
} |
76 |
|
else |
77 |
|
{ |
81 |
|
return 0; |
82 |
|
} |
83 |
|
|
84 |
< |
Smersh::Smersh(istream& sin, ostream& sout) |
84 |
> |
Smersh::Smersh(istream& sin, ostream& sout, const Environment& env) |
85 |
|
{ |
86 |
< |
parse(sin); |
87 |
< |
smersh(sout); |
86 |
> |
parse(sin, env); |
87 |
> |
smersh(sout, env); |
88 |
|
} |
89 |
|
|
90 |
< |
void Smersh::parse(istream& sin) |
90 |
> |
Environment Smersh::env; |
91 |
> |
|
92 |
> |
void Smersh::parse(istream& sin, const Environment& env) |
93 |
|
{ |
94 |
< |
stringstream query(sgetenv("QUERY_STRING")); |
94 |
> |
stringstream query(env.get("QUERY_STRING")); |
95 |
|
|
96 |
< |
if (sgetenv("REQUEST_METHOD") == "POST") |
96 |
> |
if (env.get("REQUEST_METHOD") == "POST") |
97 |
|
{ |
98 |
< |
streamsize length(lexical_cast<streamsize>(sgetenv("CONTENT_LENGTH"))); |
99 |
< |
char* content = new char[length]; |
98 |
> |
streamsize length(lexical_cast<streamsize>(env.get("CONTENT_LENGTH"))); |
99 |
> |
char* content(new char[length]); |
100 |
|
|
101 |
|
sin.read(content, length); |
102 |
|
query.write(content, length); |
117 |
|
while (query.good()); |
118 |
|
} |
119 |
|
|
120 |
< |
void Smersh::smersh(ostream& sout) |
120 |
> |
void Smersh::smersh(ostream& sout, const Environment& env) |
121 |
|
{ |
122 |
< |
sout << "Content-Type: text/html\n\n"; |
122 |
> |
if (&env == &Smersh::env) sout << "Content-Type: text/html; charset=UTF-8\r" |
123 |
> |
<< "\n\r\n"; |
124 |
|
|
125 |
|
vector<Person> people(1); |
126 |
|
|
183 |
|
|
184 |
|
output(sout, people); |
185 |
|
|
186 |
< |
sout << "!</font></strong></p></body></html>\n"; |
186 |
> |
sout << "!</font></strong></p></body></html>\r\n"; |
187 |
|
} |
188 |
|
|
189 |
|
void Smersh::output(ostream& sout, const vector<Person>& people) |