// Smersh // // Douglas Thrift // // $Id$ #include "Smersh.hpp" #include "Matcher/Matcher.hpp" #include "Person.hpp" #include "Daemon.hpp" #include "Redirector.hpp" #include #include struct SmershCommand : public app::Application { virtual int Run(const app::ArgumentList& args) { Smersh::program = api::GetExecutablePath().GetName(); int port(54321); bool daemon(false), redirector(false); ext::String redirect, log("smersh.log"); _foreach (app::ArgumentList, arg, args) { Matcher matcher; 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]); } else if (*arg == matcher("^-log=(.+)$")) { log = matcher[1]; } else if (*arg == "-D") { if (!Smersh::debug) Smersh::debug = true; } else { api::Cerr << "Usage: " << Smersh::program << " [-daemon|-redirector=redirect] [-port=port] [-log=log] [-D]\n"; return 1; } } if (daemon) { Daemon daemon(port, log); } else if (redirector) { Redirector redirector(port, log, redirect); } else { Smersh smersh; } return 0; } } smersh; Smersh::Smersh(std::istream& sin, std::ostream& sout, const Environment& env) { parse(sin, env); smersh(sout, env); } ext::String Smersh::program; bool Smersh::debug(false); Environment Smersh::env; void Smersh::parse(istream& sin, const Environment& env) { std::stringstream query(env.get("QUERY_STRING")); if (env.get("REQUEST_METHOD") == "POST") { std::streamsize length(lexical_cast(env.get("CONTENT_LENGTH"))); char* content(new char[length]); sin.read(content, length); query.write(content, length); delete [] content; } if (query.str() == "") return; do { std::string name, value; std::getline(query, name, '='); std::getline(query, value, '&'); cgi.insert(std::pair(name, value)); } while (query.good()); } void Smersh::smersh(std::ostream& sout, const Environment& env) { if (&env == &Smersh::env) sout << "Content-Type: text/html; charset=UTF-8\r\n\r\n"; ext::RedBlackSet people; typedef std::multimap MultiMap; _for (MultiMap::iterator, itor, cgi.lower_bound("sn"), cgi.upper_bound("sn")) { std::string sn(itor->second); for (std::string::size_type index(0); index < sn.length(); ++index) { while (!isalnum(sn[index]) && index < sn.length()) { if (sn[index] == '%' && index + 2 < sn.length()) { std::istringstream code(sn.substr(index + 1, 2)); unsigned short character; code.setf(std::ios_base::hex, std::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.Insert(person); if (person.isMultiple()) { _foreach (ext::Vector, single, person.getMultiple()) people.Insert(*single); person.clearMultiple(); } } if (people.IsEmpty()) people.Insert(Person()); sout << "Hello, " << std::flush; output(sout, people); sout << "!

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

\r\n"; } void Smersh::output(std::ostream& sout, const ext::RedBlackSet& people) { _foreach (ext::RedBlackSet, person, people) { sout << *person; if (person + 2 == people.End()) { sout << (people.GetSize() > 2 ? "," : "") << " and/or "; } else if (person + 1 != people.End()) { sout << ", "; } sout << std::flush; } }