// Smersh // // Douglas Thrift // // $Id$ #include "Smersh.hpp" #include "Matcher.hpp" #include "Person.hpp" #include "Daemon.hpp" #include "Redirector.hpp" string program; bool debug(false); int main(int argc, char* argv[]) { program = argv[0]; int port(8080); bool daemon(false), redirector(false); string redirect; for (int index(1); index < argc; ++index) { string arg(argv[index]); Matcher matcher; if (arg == "-D") { if (!debug) debug = true; } else if (arg == "-daemon") { if (!daemon) daemon = true; } else if (arg == matcher("^-redirector=(.+)$")) { if (!redirector) redirector = true; redirect = matcher[1]; } else if (arg == matcher("^-port=([0-9]+)$")) { port = lexical_cast(matcher[1]); } } if (daemon) { Daemon daemon(port); } else if (redirector) { Redirector redirector(port, redirect); } else { Smersh smersh; } return 0; } Smersh::Smersh(istream& sin, ostream& sout) { parse(sin); smersh(sout); } void Smersh::parse(istream& sin) { string query(sgetenv("QUERY_STRING")); if (sgetenv("REQUEST_METHOD") == "POST") getline(sin, query); if (query == "") return; istringstream input(query); do { string name, value; getline(input, name, '='); getline(input, value, '&'); cgi.insert(pair(name, value)); } while (input.good()); } void Smersh::smersh(ostream& sout) { sout << "Content-Type: text/html\n\n"; vector people(1); for (multimap::iterator itor(cgi.lower_bound("sn")); itor != cgi.upper_bound("sn"); ++itor) { string sn(itor->second); for (string::size_type index(0); index < sn.length(); ++index) { while (!isalnum(sn[index]) && index < sn.length()) { if (sn[index] == '%' && index + 2 < sn.length()) { istringstream code(sn.substr(index + 1, 2)); unsigned short character; code.setf(ios_base::hex, ios_base::basefield); code >> character; sn.replace(index, 3, 1, character); if (isalnum(sn[index])) break; } sn.erase(index, 1); } } if (debug) cerr << "sn = " << sn << '\n'; Person person(sn); people.push_back(person); if (person.isMultiple()) { people.insert(people.end(), person.beginMultiple(), person.endMultiple()); person.clearMultiple(); } } if (people.size() > 1) { people.erase(people.begin()); sort(people.begin(), people.end()); people.erase(unique(people.begin(), people.end()), people.end()); } sout << "Hello, " << flush; output(sout, people); sout << "!

Hello, " << flush; output(sout, people); sout << "!

\n"; } void Smersh::output(ostream& sout, const vector& people) { for (vector::const_iterator person(people.begin()); person != people.end(); ++person) { sout << *person; if (person + 2 == people.end()) { sout << (people.size() > 2 ? "," : "") << " and/or "; } else if (person + 1 != people.end()) { sout << ", "; } sout << flush; } }