1 |
Douglas Thrift |
3 |
// Host Update |
2 |
|
|
// |
3 |
|
|
// Douglas Thrift |
4 |
|
|
// |
5 |
|
|
// $Id$ |
6 |
|
|
|
7 |
|
|
#include "HostUpdate.hpp" |
8 |
Douglas Thrift |
9 |
#include "Host.hpp" |
9 |
Douglas Thrift |
3 |
|
10 |
|
|
int main(int argc, char* argv[]) |
11 |
|
|
{ |
12 |
|
|
HostUpdate update; |
13 |
|
|
|
14 |
|
|
return 0; |
15 |
|
|
} |
16 |
|
|
|
17 |
|
|
HostUpdate::HostUpdate() |
18 |
|
|
{ |
19 |
Douglas Thrift |
12 |
struct stat* hosts = new struct stat; |
20 |
Douglas Thrift |
3 |
|
21 |
Douglas Thrift |
12 |
if (stat("hosts", hosts) != 0) |
22 |
|
|
{ |
23 |
|
|
mkdir("hosts"); |
24 |
|
|
} |
25 |
Douglas Thrift |
3 |
|
26 |
Douglas Thrift |
12 |
delete [] hosts; |
27 |
Douglas Thrift |
10 |
|
28 |
Douglas Thrift |
15 |
string agent = sgetenv("HTTP_USER_AGENT"); |
29 |
|
|
string method = sgetenv("REQUEST_METHOD"); |
30 |
Douglas Thrift |
10 |
|
31 |
Douglas Thrift |
15 |
parse(method); |
32 |
Douglas Thrift |
10 |
|
33 |
Douglas Thrift |
13 |
if (agent.find("Host Update/") == 0 && method == "POST" && agent.find(" (") |
34 |
Douglas Thrift |
12 |
!= string::npos && agent.find(") libwww-perl/") != string::npos) |
35 |
|
|
{ |
36 |
Douglas Thrift |
15 |
update(agent); |
37 |
Douglas Thrift |
12 |
} |
38 |
|
|
else |
39 |
|
|
{ |
40 |
Douglas Thrift |
15 |
display(); |
41 |
Douglas Thrift |
12 |
} |
42 |
Douglas Thrift |
3 |
} |
43 |
|
|
|
44 |
|
|
HostUpdate::~HostUpdate() |
45 |
|
|
{ |
46 |
Douglas Thrift |
10 |
// |
47 |
Douglas Thrift |
3 |
} |
48 |
Douglas Thrift |
12 |
|
49 |
Douglas Thrift |
15 |
void HostUpdate::parse(const string& method) |
50 |
Douglas Thrift |
12 |
{ |
51 |
Douglas Thrift |
15 |
string query; |
52 |
Douglas Thrift |
12 |
|
53 |
Douglas Thrift |
15 |
if (method == "POST") |
54 |
|
|
{ |
55 |
|
|
getline(cin, query); |
56 |
|
|
} |
57 |
|
|
else |
58 |
|
|
{ |
59 |
|
|
query = sgetenv("QUERY_STRING"); |
60 |
|
|
} |
61 |
Douglas Thrift |
12 |
|
62 |
Douglas Thrift |
15 |
if (query == "") return; |
63 |
Douglas Thrift |
12 |
|
64 |
Douglas Thrift |
15 |
istringstream input(query); |
65 |
Douglas Thrift |
12 |
|
66 |
Douglas Thrift |
15 |
do |
67 |
|
|
{ |
68 |
|
|
string name, value; |
69 |
Douglas Thrift |
12 |
|
70 |
Douglas Thrift |
15 |
getline(input, name, '='); |
71 |
|
|
getline(input, value, '&'); |
72 |
|
|
|
73 |
|
|
cgi.insert(pair<string, string>(name, value)); |
74 |
|
|
} |
75 |
|
|
while (input.good()); |
76 |
|
|
} |
77 |
|
|
|
78 |
|
|
void HostUpdate::update(const string& agent) |
79 |
|
|
{ |
80 |
|
|
cout << "Content-Type: text/plain\n\n"; |
81 |
|
|
|
82 |
|
|
multimap<string, string>::iterator itor = cgi.find("host"); |
83 |
|
|
|
84 |
|
|
if (itor == cgi.end()) return; |
85 |
|
|
if (itor->second == "") return; |
86 |
|
|
|
87 |
|
|
string host = itor->second, name = sgetenv("REMOTE_HOST"), address = |
88 |
|
|
sgetenv("REMOTE_ADDR"); |
89 |
|
|
|
90 |
Douglas Thrift |
13 |
string::size_type begin = agent.find('(') + 1, end = agent.find(')', |
91 |
|
|
begin); |
92 |
Douglas Thrift |
12 |
string platform = agent.substr(begin, end - begin); |
93 |
Douglas Thrift |
13 |
|
94 |
|
|
Host client(host, name, address, platform), saved(host); |
95 |
|
|
|
96 |
Douglas Thrift |
15 |
cout << "host=" << client.getHost() << '\n' |
97 |
|
|
<< "name=" << client.getName() << '\n' |
98 |
|
|
<< "address=" << client.getAddress() << '\n' |
99 |
|
|
<< "platform=" << client.getPlatform() << '\n'; |
100 |
|
|
|
101 |
Douglas Thrift |
13 |
if (client != ++saved) client--; |
102 |
Douglas Thrift |
12 |
} |
103 |
|
|
|
104 |
Douglas Thrift |
15 |
void HostUpdate::display() |
105 |
Douglas Thrift |
12 |
{ |
106 |
Douglas Thrift |
14 |
cout << "Content-Type: text/plain\n\n"; |
107 |
|
|
|
108 |
|
|
set<Host> hosts; |
109 |
Douglas Thrift |
15 |
DIR* dir = opendir("hosts"); |
110 |
Douglas Thrift |
14 |
struct dirent* ent; |
111 |
|
|
|
112 |
|
|
while ((ent = readdir(dir)) != NULL) |
113 |
|
|
{ |
114 |
|
|
string file = ent->d_name; |
115 |
|
|
|
116 |
|
|
if (file == "." || file == "..") continue; |
117 |
|
|
|
118 |
|
|
Host host(file); |
119 |
|
|
|
120 |
|
|
hosts.insert(++host); |
121 |
|
|
} |
122 |
Douglas Thrift |
15 |
|
123 |
Douglas Thrift |
14 |
closedir(dir); |
124 |
|
|
|
125 |
|
|
for (set<Host>::iterator itor = hosts.begin(); itor != hosts.end(); itor++) |
126 |
|
|
{ |
127 |
|
|
Host host = *itor; |
128 |
|
|
|
129 |
|
|
cout << "host=" << host.getHost() << '\n' |
130 |
|
|
<< "name=" << host.getName() << '\n' |
131 |
|
|
<< "address=" << host.getAddress() << '\n' |
132 |
|
|
<< "platform=" << host.getPlatform() << "\n\n"; |
133 |
|
|
} |
134 |
Douglas Thrift |
12 |
} |