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