ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/repos/Smersh/Smersh.cpp
(Generate patch)

Comparing Smersh/Smersh.cpp (file contents):
Revision 165 by Douglas Thrift, 2004-06-18T01:38:55-07:00 vs.
Revision 252 by Douglas Thrift, 2004-09-13T17:52:44-07:00

# Line 10 | Line 10
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);
24 <        bool daemon(false), redirector(false);
25 <        string redirect;
23 >        int port(54321);
24 >        bool fork(false), daemon(false), redirector(false);
25 >        string redirect, log("smersh.log");
26  
27          for (int index(1); index < argc; ++index)
28          {
29                  string arg(argv[index]);
30                  Matcher matcher;
31  
32 <                if (arg == "-D")
30 <                {
31 <                        if (!debug) debug = true;
32 <                }
33 <                else if (arg == "-daemon")
32 >                if (arg == "-daemon")
33                  {
34                          if (!daemon) daemon = true;
35                  }
# Line 44 | Line 43 | int main(int argc, char* argv[])
43                  {
44                          port = lexical_cast<int>(matcher[1]);
45                  }
46 +                else if (arg == "-fork")
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
61 +                                << " [-daemon|-redirector=redirect] [-port=port] [-fork] "
62 +                                << "[-log=log] [-D]\n";
63 +
64 +                        return 1;
65 +                }
66          }
67  
68          if (daemon)
69          {
70 <                Daemon daemon(port);
70 >                Daemon daemon(port, fork, log);
71          }
72          else if (redirector)
73          {
74 <                Redirector redirector(port, redirect);
74 >                Redirector redirector(port, fork, log, redirect);
75          }
76          else
77          {
# Line 62 | Line 81 | int main(int argc, char* argv[])
81          return 0;
82   }
83  
84 < Smersh::Smersh(ostream& sout)
84 > Smersh::Smersh(istream& sin, ostream& sout, const Environment& env)
85   {
86 <        string method = sgetenv("REQUEST_METHOD");
87 <
69 <        parse(method);
70 <
71 <        sout << "Content-Type: text/plain\n\n" << (cgi.find("sn") != cgi.end() ?
72 <                cgi.find("sn")->second : "Unknown") << '\n';
86 >        parse(sin, env);
87 >        smersh(sout, env);
88   }
89  
90 < void Smersh::parse(const string& method)
90 > Environment Smersh::env;
91 >
92 > void Smersh::parse(istream& sin, const Environment& env)
93   {
94 <        string query(sgetenv("QUERY_STRING"));
94 >        stringstream query(env.get("QUERY_STRING"));
95 >
96 >        if (env.get("REQUEST_METHOD") == "POST")
97 >        {
98 >                streamsize length(lexical_cast<streamsize>(env.get("CONTENT_LENGTH")));
99 >                char* content(new char[length]);
100  
101 <        if (method == "POST") getline(cin, query);
102 <        if (query == "") return;
101 >                sin.read(content, length);
102 >                query.write(content, length);
103  
104 <        istringstream input(query);
104 >                delete [] content;
105 >        }
106 >        if (query.str() == "") return;
107  
108          do
109          {
110                  string name, value;
111  
112 <                getline(input, name, '=');
113 <                getline(input, value, '&');
112 >                getline(query, name, '=');
113 >                getline(query, value, '&');
114  
115                  cgi.insert(pair<string, string>(name, value));
116          }
117 <        while (input.good());
117 >        while (query.good());
118 > }
119 >
120 > void Smersh::smersh(ostream& sout, const Environment& env)
121 > {
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 >
127 >        for (multimap<string, string>::iterator itor(cgi.lower_bound("sn")); itor
128 >                != cgi.upper_bound("sn"); ++itor)
129 >        {
130 >                string sn(itor->second);
131 >
132 >                for (string::size_type index(0); index < sn.length(); ++index)
133 >                {
134 >                        while (!isalnum(sn[index]) && index < sn.length())
135 >                        {
136 >                                if (sn[index] == '%' && index + 2 < sn.length())
137 >                                {
138 >                                        istringstream code(sn.substr(index + 1, 2));
139 >                                        unsigned short character;
140 >
141 >                                        code.setf(ios_base::hex, ios_base::basefield);
142 >
143 >                                        code >> character;
144 >
145 >                                        sn.replace(index, 3, 1, character);
146 >
147 >                                        if (isalnum(sn[index])) break;
148 >                                }
149 >
150 >                                sn.erase(index, 1);
151 >                        }
152 >                }
153 >
154 >                if (debug) cerr << "sn = " << sn << '\n';
155 >
156 >                Person person(sn);
157 >
158 >                people.push_back(person);
159 >
160 >                if (person.isMultiple())
161 >                {
162 >                        people.insert(people.end(), person.beginMultiple(),
163 >                                person.endMultiple());
164 >                        person.clearMultiple();
165 >                }
166 >        }
167 >
168 >        if (people.size() > 1)
169 >        {
170 >                people.erase(people.begin());
171 >
172 >                sort(people.begin(), people.end());
173 >
174 >                people.erase(unique(people.begin(), people.end()), people.end());
175 >        }
176 >
177 >        sout << "<html><head><title>Hello, " << flush;
178 >
179 >        output(sout, people);
180 >
181 >        sout << "!</title></head><body><p style=\"font-variant: small-caps\"><stro"
182 >                << "ng><font face=\"Comic Sans MS\" size=\"4\">Hello, " << flush;
183 >
184 >        output(sout, people);
185 >
186 >        sout << "!</font></strong></p></body></html>\r\n";
187 > }
188 >
189 > void Smersh::output(ostream& sout, const vector<Person>& people)
190 > {
191 >        for (vector<Person>::const_iterator person(people.begin()); person !=
192 >                people.end(); ++person)
193 >        {
194 >                sout << *person;
195 >
196 >                if (person + 2 == people.end())
197 >                {
198 >                        sout << (people.size() > 2 ? "," : "") << " and/or ";
199 >                }
200 >                else if (person + 1 != people.end())
201 >                {
202 >                        sout << ", ";
203 >                }
204 >
205 >                sout << flush;
206 >        }
207   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines