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 166 by Douglas Thrift, 2004-06-18T22:24:07-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(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 <        string query(sgetenv("QUERY_STRING"));
94 >        stringstream query(env.get("QUERY_STRING"));
95  
96 <        if (sgetenv("REQUEST_METHOD") == "POST") getline(sin, query);
97 <        if (query == "") return;
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 >                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)
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;
125 >        vector<Person> people(1);
126  
127          for (multimap<string, string>::iterator itor(cgi.lower_bound("sn")); itor
128                  != cgi.upper_bound("sn"); ++itor)
# Line 104 | Line 133 | void Smersh::smersh(ostream& sout)
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                          }
109
110                        if (index < sn.length()) sn[index] = tolower(sn[index]);
152                  }
153  
154 +                if (debug) cerr << "sn = " << sn << '\n';
155 +
156                  Person person(sn);
157  
158                  people.push_back(person);
# Line 122 | Line 165 | void Smersh::smersh(ostream& sout)
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);
# Line 131 | Line 183 | void Smersh::smersh(ostream& sout)
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)
# Line 143 | Line 195 | void Smersh::output(ostream& sout, const
195  
196                  if (person + 2 == people.end())
197                  {
198 <                        sout << (people.size() > 2 ? "," : "") << " and ";
198 >                        sout << (people.size() > 2 ? "," : "") << " and/or ";
199                  }
200                  else if (person + 1 != people.end())
201                  {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines