--- HostStatus/HostStatus.cpp 2003/11/14 19:38:42 27 +++ HostStatus/HostStatus.cpp 2003/11/17 19:05:37 44 @@ -5,6 +5,7 @@ // $Id$ #include "HostStatus.hpp" +#include "Host.hpp" int main(int argc, char* argv[]) { @@ -15,5 +16,263 @@ int main(int argc, char* argv[]) HostStatus::HostStatus() { - // +#ifndef __CYGWIN__ + sputenv("TZ=:America/Los_Angeles"); +#else + sputenv("TZ= PST8PDT"); +#endif + tzset(); + + string method = sgetenv("REQUEST_METHOD"); + + parse(method); + mode(); + + display(method); +} + +void HostStatus::parse(const string& method) +{ + string query; + + if (method == "POST") + { + getline(cin, query); + } + else + { + query = sgetenv("QUERY_STRING"); + } + + 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 HostStatus::mode() +{ + multimap::iterator itor = cgi.find("format"); + + if (itor != cgi.end()) + { + string format = itor->second; + + page = format != "t" ? true : false; + } + + if (!page) return; + + host = false, name = false, address = false, platform = false, since = + false; + + for (multimap::iterator itor = cgi.find("mode"); itor != + cgi.upper_bound("mode") && itor != cgi.end(); itor++) + { + string mode = itor->second; + + for (string::iterator itor = mode.begin(); itor != mode.end(); itor++) + { + char mode = *itor; + + switch (mode) + { + case 'h': + if (!host) host = true; + break; + case 'n': + if (!name) name = true; + break; + case 'a': + if (!address) address = true; + break; + case 'p': + if (!platform) platform = true; + break; + case 's': + if (!since) since = true; + break; + default: + break; + } + } + } + + if (!host && !name && !address && !platform && !since) host = true, name = + true, address = true, platform = true, since = true; +} + +void HostStatus::display(const string& method) +{ + cout << "Content-Type: text/html\n\n"; + + if (method == "POST") sunsetenv("HTTP_USER_AGENT"); + + pstream hostupdate("./hostupdate.cgi"); + + if (method == "POST") + { + for (multimap::iterator itor = cgi.begin(); itor != + cgi.end(); itor++) + { + hostupdate << itor->first << '=' << itor->second << '&'; + } + + hostupdate << '\n' << flush; + } + + hostupdate.ignore(26); + + list hosts; + bool done = false; + + do + { + Host host; + + hostupdate >> host; + + hosts.push_back(host); + + switch (hostupdate.peek()) + { + case 'h': + case 'n': + case 'a': + case 'p': + case 's': + break; + default: + done = true; + break; + } + } + while (!done && hostupdate.good()); + + if (page) header(method); + + for (list::iterator itor = hosts.begin(); itor != hosts.end(); + itor++) + { + Host host = *itor; + + cout << host << flush; + } + + if (page) footer(); +} + +void HostStatus::header(const string& method) +{ + cout << "\n" + << "\n" + << "\n" + << "\n" + << "Host Status\n" + << "\n" + << "\n" + << "

Host Status

\n" + << "
\n" + << "\n" + << "\n" + << "\n" + << "\n" + << "\n" + << "\n" + << "
\n" + << "\n" + << "\n" + << " Host\n" + << " Name\n" + << " Address\n" + << " Platform\n" + << " Since\n" + << "
\n" + << "
\n" + << "\n" + << "\n"; + + if (host) cout << "\n"; + if (name) cout << "\n"; + if (address) cout << "\n"; + if (platform) cout << "\n"; + if (since) cout << "\n"; + + cout << "\n"; +} + +void HostStatus::footer() +{ + cout << "
HostNameAddressPlatformSince
\n" + << "\n"; }