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 |
> |
#include <windows.h> |
10 |
> |
|
11 |
> |
int main(int argc, char* argv[]) |
12 |
> |
{ |
13 |
> |
HostUpdate update; |
14 |
> |
|
15 |
> |
return 0; |
16 |
> |
} |
17 |
> |
|
18 |
> |
HostUpdate::HostUpdate() |
19 |
> |
{ |
20 |
> |
struct stat* hosts = new struct stat; |
21 |
> |
|
22 |
> |
if (stat("hosts", hosts) != 0) |
23 |
> |
{ |
24 |
> |
mkdir("hosts"); |
25 |
> |
} |
26 |
> |
|
27 |
> |
delete [] hosts; |
28 |
> |
|
29 |
> |
string agent = sgetenv("HTTP_USER_AGENT"); |
30 |
> |
string method = sgetenv("REQUEST_METHOD"); |
31 |
> |
|
32 |
> |
parse(method); |
33 |
> |
|
34 |
> |
if (agent.find("Host Update/") == 0 && method == "POST" && agent.find(" (") |
35 |
> |
!= string::npos && agent.find(") libwww-perl/") != string::npos) |
36 |
> |
{ |
37 |
> |
update(agent); |
38 |
> |
} |
39 |
> |
else |
40 |
> |
{ |
41 |
> |
display(); |
42 |
> |
} |
43 |
> |
} |
44 |
> |
|
45 |
> |
HostUpdate::~HostUpdate() |
46 |
> |
{ |
47 |
> |
// |
48 |
> |
} |
49 |
> |
|
50 |
> |
void HostUpdate::parse(const string& method) |
51 |
> |
{ |
52 |
> |
string query; |
53 |
> |
|
54 |
> |
if (method == "POST") |
55 |
> |
{ |
56 |
> |
getline(cin, query); |
57 |
> |
} |
58 |
> |
else |
59 |
> |
{ |
60 |
> |
query = sgetenv("QUERY_STRING"); |
61 |
> |
} |
62 |
> |
|
63 |
> |
if (query == "") return; |
64 |
> |
|
65 |
> |
istringstream input(query); |
66 |
> |
|
67 |
> |
do |
68 |
> |
{ |
69 |
> |
string name, value; |
70 |
> |
|
71 |
> |
getline(input, name, '='); |
72 |
> |
getline(input, value, '&'); |
73 |
> |
|
74 |
> |
cgi.insert(pair<string, string>(name, value)); |
75 |
> |
} |
76 |
> |
while (input.good()); |
77 |
> |
} |
78 |
> |
|
79 |
> |
void HostUpdate::update(const string& agent) |
80 |
> |
{ |
81 |
> |
cout << "Content-Type: text/plain\n\n"; |
82 |
> |
|
83 |
> |
multimap<string, string>::iterator itor = cgi.find("host"); |
84 |
> |
|
85 |
> |
if (itor == cgi.end()) return; |
86 |
> |
if (itor->second == "") return; |
87 |
> |
|
88 |
> |
string host = itor->second, name = sgetenv("REMOTE_HOST"), address = |
89 |
> |
sgetenv("REMOTE_ADDR"); |
90 |
> |
|
91 |
> |
string::size_type begin = agent.find('(') + 1, end = agent.find(')', |
92 |
> |
begin); |
93 |
> |
string platform = agent.substr(begin, end - begin); |
94 |
> |
|
95 |
> |
Host client(host, name, address, platform), saved(host); |
96 |
> |
|
97 |
> |
cout << "host=" << client.getHost() << '\n' |
98 |
> |
<< "name=" << client.getName() << '\n' |
99 |
> |
<< "address=" << client.getAddress() << '\n' |
100 |
> |
<< "platform=" << client.getPlatform() << '\n'; |
101 |
> |
|
102 |
> |
if (client != ++saved) client--; |
103 |
> |
} |
104 |
> |
|
105 |
> |
void HostUpdate::display() |
106 |
> |
{ |
107 |
> |
cout << "Content-Type: text/plain\n\n"; |
108 |
> |
|
109 |
> |
set<Host> hosts; |
110 |
> |
|
111 |
> |
#ifndef _WIN32 |
112 |
> |
DIR* dir = opendir("hosts"); |
113 |
> |
struct dirent* ent; |
114 |
> |
|
115 |
> |
while ((ent = readdir(dir)) != NULL) |
116 |
> |
{ |
117 |
> |
string file = ent->d_name; |
118 |
> |
|
119 |
> |
if (file == "." || file == "..") continue; |
120 |
> |
|
121 |
> |
Host host(file); |
122 |
> |
|
123 |
> |
hosts.insert(++host); |
124 |
> |
} |
125 |
> |
|
126 |
> |
closedir(dir); |
127 |
> |
#else |
128 |
> |
WIN32_FIND_DATA found; |
129 |
> |
HANDLE find = FindFirstFile("hosts\\*", &found); |
130 |
> |
|
131 |
> |
do |
132 |
> |
{ |
133 |
> |
string file = found.cFileName; |
134 |
> |
|
135 |
> |
if (file == "." || file == "..") continue; |
136 |
> |
|
137 |
> |
Host host(file); |
138 |
> |
|
139 |
> |
hosts.insert(++host); |
140 |
> |
} |
141 |
> |
while (FindNextFile(find, &found) != 0); |
142 |
> |
|
143 |
> |
FindClose(find); |
144 |
> |
#endif |
145 |
> |
|
146 |
> |
for (set<Host>::iterator itor = hosts.begin(); itor != hosts.end(); itor++) |
147 |
> |
{ |
148 |
> |
Host host = *itor; |
149 |
> |
|
150 |
> |
cout << "host=" << host.getHost() << '\n' |
151 |
> |
<< "name=" << host.getName() << '\n' |
152 |
> |
<< "address=" << host.getAddress() << '\n' |
153 |
> |
<< "platform=" << host.getPlatform() << '\n'; |
154 |
> |
|
155 |
> |
string name = string("hosts") + slash + host.getHost(); |
156 |
> |
struct stat file; |
157 |
> |
|
158 |
> |
if (stat(name.c_str(), &file) == 0) |
159 |
> |
{ |
160 |
> |
char since[20]; |
161 |
> |
|
162 |
> |
strftime(since, 20, "%m/%d/%Y %H:%M:%S", gmtime(&file.st_mtime)); |
163 |
> |
|
164 |
> |
cout << "since=" << since << " GMT\n"; |
165 |
> |
} |
166 |
> |
} |
167 |
> |
} |