1 |
< |
// Host Update |
2 |
< |
// |
3 |
< |
// Douglas Thrift |
4 |
< |
// |
5 |
< |
// $Id$ |
6 |
< |
|
7 |
< |
#include "HostUpdate.hpp" |
8 |
< |
|
9 |
< |
int main(int argc, char* argv[]) |
10 |
< |
{ |
11 |
< |
HostUpdate update; |
12 |
< |
|
13 |
< |
return 0; |
14 |
< |
} |
15 |
< |
|
16 |
< |
HostUpdate::HostUpdate() |
17 |
< |
{ |
18 |
< |
CgiEnvironment env = cgi.getEnvironment(); |
19 |
< |
|
20 |
< |
cout << "Location: " << (env.usingHTTPS() ? "https" : "http") << "://" |
21 |
< |
<< env.getServerName() << ":" << env.getServerPort() << "/\n\n"; |
22 |
< |
|
23 |
< |
form_iterator itor = cgi["host"]; |
24 |
< |
FormEntry host = *itor; |
25 |
< |
} |
26 |
< |
|
27 |
< |
HostUpdate::~HostUpdate() |
28 |
< |
{ |
29 |
< |
} |
1 |
> |
// Host Update |
2 |
> |
// |
3 |
> |
// Douglas Thrift |
4 |
> |
// |
5 |
> |
// $Id$ |
6 |
> |
|
7 |
> |
#include "HostUpdate.hpp" |
8 |
> |
#include "Host.hpp" |
9 |
> |
|
10 |
> |
int main(int argc, char* argv[]) |
11 |
> |
{ |
12 |
> |
HostUpdate update; |
13 |
> |
|
14 |
> |
return 0; |
15 |
> |
} |
16 |
> |
|
17 |
> |
HostUpdate::HostUpdate() |
18 |
> |
{ |
19 |
> |
struct stat* hosts = new struct stat; |
20 |
> |
|
21 |
> |
if (stat("hosts", hosts) != 0) |
22 |
> |
{ |
23 |
> |
mkdir("hosts"); |
24 |
> |
} |
25 |
> |
|
26 |
> |
delete [] hosts; |
27 |
> |
|
28 |
> |
string agent = sgetenv("HTTP_USER_AGENT"); |
29 |
> |
string method = sgetenv("REQUEST_METHOD"); |
30 |
> |
|
31 |
> |
parse(method); |
32 |
> |
|
33 |
> |
if (agent.find("Host Update/") == 0 && method == "POST" && agent.find(" (") |
34 |
> |
!= string::npos && agent.find(") libwww-perl/") != string::npos) |
35 |
> |
{ |
36 |
> |
update(agent); |
37 |
> |
} |
38 |
> |
else |
39 |
> |
{ |
40 |
> |
display(); |
41 |
> |
} |
42 |
> |
} |
43 |
> |
|
44 |
> |
HostUpdate::~HostUpdate() |
45 |
> |
{ |
46 |
> |
// |
47 |
> |
} |
48 |
> |
|
49 |
> |
void HostUpdate::parse(const string& method) |
50 |
> |
{ |
51 |
> |
string query; |
52 |
> |
|
53 |
> |
if (method == "POST") |
54 |
> |
{ |
55 |
> |
getline(cin, query); |
56 |
> |
} |
57 |
> |
else |
58 |
> |
{ |
59 |
> |
query = sgetenv("QUERY_STRING"); |
60 |
> |
} |
61 |
> |
|
62 |
> |
if (query == "") return; |
63 |
> |
|
64 |
> |
istringstream input(query); |
65 |
> |
|
66 |
> |
do |
67 |
> |
{ |
68 |
> |
string name, value; |
69 |
> |
|
70 |
> |
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 |
> |
string::size_type begin = agent.find('(') + 1, end = agent.find(')', |
91 |
> |
begin); |
92 |
> |
string platform = agent.substr(begin, end - begin); |
93 |
> |
|
94 |
> |
Host client(host, name, address, platform), saved(host); |
95 |
> |
|
96 |
> |
cout << "host=" << client.getHost() << '\n' |
97 |
> |
<< "name=" << client.getName() << '\n' |
98 |
> |
<< "address=" << client.getAddress() << '\n' |
99 |
> |
<< "platform=" << client.getPlatform() << '\n'; |
100 |
> |
|
101 |
> |
if (client != ++saved) client--; |
102 |
> |
} |
103 |
> |
|
104 |
> |
void HostUpdate::display() |
105 |
> |
{ |
106 |
> |
cout << "Content-Type: text/plain\n\n"; |
107 |
> |
|
108 |
> |
set<Host> hosts; |
109 |
> |
DIR* dir = opendir("hosts"); |
110 |
> |
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 |
> |
|
123 |
> |
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 |
> |
} |