1 |
// Host Status |
2 |
// |
3 |
// Douglas Thrift |
4 |
// |
5 |
// $Id$ |
6 |
|
7 |
#include "HostStatus.hpp" |
8 |
#include "Host.hpp" |
9 |
|
10 |
int main(int argc, char* argv[]) |
11 |
{ |
12 |
HostStatus status; |
13 |
|
14 |
return 0; |
15 |
} |
16 |
|
17 |
HostStatus::HostStatus() |
18 |
{ |
19 |
#ifndef __CYGWIN__ |
20 |
sputenv("TZ=:America/Los_Angeles"); |
21 |
#else |
22 |
sputenv("TZ= PST8PDT"); |
23 |
#endif |
24 |
tzset(); |
25 |
|
26 |
string method = sgetenv("REQUEST_METHOD"); |
27 |
|
28 |
parse(method); |
29 |
|
30 |
multimap<string, string>::iterator itor = cgi.find("format"); |
31 |
|
32 |
if (itor != cgi.end()) |
33 |
{ |
34 |
string format = itor->second; |
35 |
|
36 |
page = format != "t" ? true : false; |
37 |
} |
38 |
|
39 |
display(method); |
40 |
} |
41 |
|
42 |
void HostStatus::parse(const string& method) |
43 |
{ |
44 |
string query; |
45 |
|
46 |
if (method == "POST") |
47 |
{ |
48 |
getline(cin, query); |
49 |
} |
50 |
else |
51 |
{ |
52 |
query = sgetenv("QUERY_STRING"); |
53 |
} |
54 |
|
55 |
if (query == "") return; |
56 |
|
57 |
istringstream input(query); |
58 |
|
59 |
do |
60 |
{ |
61 |
string name, value; |
62 |
|
63 |
getline(input, name, '='); |
64 |
getline(input, value, '&'); |
65 |
|
66 |
cgi.insert(pair<string, string>(name, value)); |
67 |
} |
68 |
while (input.good()); |
69 |
} |
70 |
|
71 |
void HostStatus::display(const string& method) |
72 |
{ |
73 |
cout << "Content-Type: text/html\n\n"; |
74 |
|
75 |
if (method == "POST") sunsetenv("HTTP_USER_AGENT"); |
76 |
|
77 |
pstream hostupdate("./hostupdate.cgi"); |
78 |
|
79 |
if (method == "POST") |
80 |
{ |
81 |
for (multimap<string, string>::iterator itor = cgi.begin(); itor != |
82 |
cgi.end(); itor++) |
83 |
{ |
84 |
hostupdate << itor->first << '=' << itor->second << '&'; |
85 |
} |
86 |
|
87 |
hostupdate << '\n' << flush; |
88 |
} |
89 |
|
90 |
hostupdate.ignore(26); |
91 |
|
92 |
list<Host> hosts; |
93 |
bool done = false; |
94 |
|
95 |
do |
96 |
{ |
97 |
Host host; |
98 |
|
99 |
hostupdate >> host; |
100 |
|
101 |
hosts.push_back(host); |
102 |
|
103 |
switch (hostupdate.peek()) |
104 |
{ |
105 |
case 'h': |
106 |
case 'n': |
107 |
case 'a': |
108 |
case 'p': |
109 |
case 's': |
110 |
break; |
111 |
default: |
112 |
done = true; |
113 |
break; |
114 |
} |
115 |
} |
116 |
while (!done && hostupdate.good()); |
117 |
|
118 |
if (page) header(); |
119 |
|
120 |
for (list<Host>::iterator itor = hosts.begin(); itor != hosts.end(); itor++) |
121 |
{ |
122 |
Host host = *itor; |
123 |
|
124 |
cout << host << flush; |
125 |
} |
126 |
|
127 |
if (page) footer(); |
128 |
} |
129 |
|
130 |
void HostStatus::header() |
131 |
{ |
132 |
cout << "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\"\n" |
133 |
<< "\t\"http://www.w3.org/TR/REC-html40/strict.dtd\">\n" |
134 |
<< "<html>\n" |
135 |
<< "<head>\n" |
136 |
<< "<link rel=\"StyleSheet\" href=\"../stylesheets/regular.css\" type=" |
137 |
<< "\"text/css\">\n" |
138 |
<< "<title>Host Status</title>\n" |
139 |
<< "</head>\n" |
140 |
<< "<body>\n" |
141 |
<< "<h1 id=\"title\" class=\"center\">Host Status</h1>\n" |
142 |
<< "<table class=\"center\">\n"; |
143 |
} |
144 |
|
145 |
void HostStatus::footer() |
146 |
{ |
147 |
cout << "</table>\n" |
148 |
/* << "<pre>" << flush; |
149 |
|
150 |
ipstream env("env"); |
151 |
|
152 |
do |
153 |
{ |
154 |
string line; |
155 |
|
156 |
getline(env, line); |
157 |
|
158 |
cout << line << '\n'; |
159 |
} |
160 |
while (env.good()); |
161 |
|
162 |
cout << "</pre>\n"*/ |
163 |
<< "</html>\n"; |
164 |
} |