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 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 == "-D") |
30 |
{ |
31 |
if (!debug) debug = true; |
32 |
} |
33 |
else if (arg == "-daemon") |
34 |
{ |
35 |
if (!daemon) daemon = true; |
36 |
} |
37 |
else if (arg == matcher("^-redirector=(.+)$")) |
38 |
{ |
39 |
if (!redirector) redirector = true; |
40 |
|
41 |
redirect = matcher[1]; |
42 |
} |
43 |
else if (arg == matcher("^-port=([0-9]+)$")) |
44 |
{ |
45 |
port = lexical_cast<int>(matcher[1]); |
46 |
} |
47 |
} |
48 |
|
49 |
if (daemon) |
50 |
{ |
51 |
Daemon daemon(port); |
52 |
} |
53 |
else if (redirector) |
54 |
{ |
55 |
Redirector redirector(port, redirect); |
56 |
} |
57 |
else |
58 |
{ |
59 |
Smersh smersh; |
60 |
} |
61 |
|
62 |
return 0; |
63 |
} |
64 |
|
65 |
Smersh::Smersh(ostream& sout) |
66 |
{ |
67 |
string method = sgetenv("REQUEST_METHOD"); |
68 |
|
69 |
parse(method); |
70 |
|
71 |
sout << "Content-Type: text/plain\n\n" << (cgi.find("sn") != cgi.end() ? |
72 |
cgi.find("sn")->second : "Unknown") << '\n'; |
73 |
} |
74 |
|
75 |
void Smersh::parse(const string& method) |
76 |
{ |
77 |
string query(sgetenv("QUERY_STRING")); |
78 |
|
79 |
if (method == "POST") getline(cin, query); |
80 |
if (query == "") return; |
81 |
|
82 |
istringstream input(query); |
83 |
|
84 |
do |
85 |
{ |
86 |
string name, value; |
87 |
|
88 |
getline(input, name, '='); |
89 |
getline(input, value, '&'); |
90 |
|
91 |
cgi.insert(pair<string, string>(name, value)); |
92 |
} |
93 |
while (input.good()); |
94 |
} |