6 |
|
|
7 |
|
#include "HostUpdate.hpp" |
8 |
|
#include "Host.hpp" |
9 |
+ |
|
10 |
+ |
#ifdef _WIN32 |
11 |
|
#include <windows.h> |
12 |
+ |
#endif |
13 |
|
|
14 |
|
int main(int argc, char* argv[]) |
15 |
|
{ |
33 |
|
string method = sgetenv("REQUEST_METHOD"); |
34 |
|
|
35 |
|
parse(method); |
36 |
+ |
mode(); |
37 |
|
|
38 |
|
if (agent.find("Host Update/") == 0 && method == "POST" && agent.find(" (") |
39 |
|
!= string::npos && agent.find(") libwww-perl/") != string::npos) |
46 |
|
} |
47 |
|
} |
48 |
|
|
45 |
– |
HostUpdate::~HostUpdate() |
46 |
– |
{ |
47 |
– |
// |
48 |
– |
} |
49 |
– |
|
49 |
|
void HostUpdate::parse(const string& method) |
50 |
|
{ |
51 |
|
string query; |
75 |
|
while (input.good()); |
76 |
|
} |
77 |
|
|
78 |
+ |
void HostUpdate::mode() |
79 |
+ |
{ |
80 |
+ |
host = false, name = false, address = false, platform = false, since = |
81 |
+ |
false; |
82 |
+ |
|
83 |
+ |
for (multimap<string, string>::iterator itor = cgi.find("mode"); itor != |
84 |
+ |
cgi.upper_bound("mode") && itor != cgi.end(); itor++) |
85 |
+ |
{ |
86 |
+ |
string mode = itor->second; |
87 |
+ |
|
88 |
+ |
for (string::iterator itor = mode.begin(); itor != mode.end(); itor++) |
89 |
+ |
{ |
90 |
+ |
char mode = *itor; |
91 |
+ |
|
92 |
+ |
switch (mode) |
93 |
+ |
{ |
94 |
+ |
case 'h': |
95 |
+ |
if (!host) host = true; |
96 |
+ |
break; |
97 |
+ |
case 'n': |
98 |
+ |
if (!name) name = true; |
99 |
+ |
break; |
100 |
+ |
case 'a': |
101 |
+ |
if (!address) address = true; |
102 |
+ |
break; |
103 |
+ |
case 'p': |
104 |
+ |
if (!platform) platform = true; |
105 |
+ |
break; |
106 |
+ |
case 's': |
107 |
+ |
if (!since) since = true; |
108 |
+ |
break; |
109 |
+ |
default: |
110 |
+ |
break; |
111 |
+ |
} |
112 |
+ |
} |
113 |
+ |
} |
114 |
+ |
|
115 |
+ |
if (!host && !name && !address && !platform && !since) host = true, name = |
116 |
+ |
true, address = true, platform = true, since = true; |
117 |
+ |
} |
118 |
+ |
|
119 |
|
void HostUpdate::update(const string& agent) |
120 |
|
{ |
121 |
|
cout << "Content-Type: text/plain\n\n"; |
128 |
|
string host = itor->second, name = sgetenv("REMOTE_HOST"), address = |
129 |
|
sgetenv("REMOTE_ADDR"); |
130 |
|
|
131 |
< |
string::size_type begin = agent.find('(') + 1, end = agent.find(')', |
132 |
< |
begin); |
131 |
> |
string::size_type begin = agent.find('(') + 1, end = agent.rfind(')'); |
132 |
> |
|
133 |
> |
if (begin >= end) return; |
134 |
> |
|
135 |
|
string platform = agent.substr(begin, end - begin); |
136 |
|
|
137 |
|
Host client(host, name, address, platform), saved(host); |
138 |
|
|
97 |
– |
cout << "host=" << client.getHost() << '\n' |
98 |
– |
<< "name=" << client.getName() << '\n' |
99 |
– |
<< "address=" << client.getAddress() << '\n' |
100 |
– |
<< "platform=" << client.getPlatform() << '\n'; |
101 |
– |
|
139 |
|
if (client != ++saved) client--; |
140 |
+ |
|
141 |
+ |
display(client); |
142 |
|
} |
143 |
|
|
144 |
|
void HostUpdate::display() |
145 |
|
{ |
146 |
|
cout << "Content-Type: text/plain\n\n"; |
147 |
|
|
148 |
+ |
{ |
149 |
+ |
bool request = false; |
150 |
+ |
|
151 |
+ |
for (multimap<string, string>::iterator itor = cgi.find("host"); itor != |
152 |
+ |
cgi.upper_bound("host") && itor != cgi.end(); itor++) |
153 |
+ |
{ |
154 |
+ |
if (itor->second != "") |
155 |
+ |
{ |
156 |
+ |
Host host(itor->second); |
157 |
+ |
|
158 |
+ |
display(++host); |
159 |
+ |
|
160 |
+ |
if (!request) request = true; |
161 |
+ |
} |
162 |
+ |
} |
163 |
+ |
|
164 |
+ |
if (request) return; |
165 |
+ |
} |
166 |
+ |
|
167 |
|
set<Host> hosts; |
168 |
|
|
169 |
|
#ifndef _WIN32 |
203 |
|
|
204 |
|
for (set<Host>::iterator itor = hosts.begin(); itor != hosts.end(); itor++) |
205 |
|
{ |
206 |
< |
Host host = *itor; |
206 |
> |
display(*itor); |
207 |
> |
} |
208 |
> |
} |
209 |
> |
|
210 |
> |
void HostUpdate::display(const Host& host) |
211 |
> |
{ |
212 |
> |
if (this->host) cout << "host=" << host.getHost() << '\n'; |
213 |
> |
if (name) cout << "name=" << host.getName() << '\n'; |
214 |
> |
if (address) cout << "address=" << host.getAddress() << '\n'; |
215 |
> |
if (platform) cout << "platform=" << host.getPlatform() << '\n'; |
216 |
> |
|
217 |
> |
string name = string("hosts") + slash + host.getHost(); |
218 |
> |
struct stat file; |
219 |
> |
|
220 |
> |
if (since && stat(name.c_str(), &file) == 0) |
221 |
> |
{ |
222 |
> |
char since[20]; |
223 |
|
|
224 |
< |
cout << "host=" << host.getHost() << '\n' |
151 |
< |
<< "name=" << host.getName() << '\n' |
152 |
< |
<< "address=" << host.getAddress() << '\n' |
153 |
< |
<< "platform=" << host.getPlatform() << '\n'; |
224 |
> |
strftime(since, 20, "%m/%d/%Y %H:%M:%S", gmtime(&file.st_mtime)); |
225 |
|
|
226 |
< |
string name = string("hosts") + slash + host.getHost(); |
227 |
< |
struct stat file; |
226 |
> |
cout << "since=" << since << " GMT\n"; |
227 |
> |
} |
228 |
> |
else if (since) |
229 |
> |
{ |
230 |
> |
char since[20]; |
231 |
> |
time_t* now = new time_t; |
232 |
|
|
233 |
< |
if (stat(name.c_str(), &file) == 0) |
234 |
< |
{ |
160 |
< |
char since[20]; |
233 |
> |
time(now); |
234 |
> |
strftime(since, 20, "%m/%d/%Y %H:%M:%S", gmtime(now)); |
235 |
|
|
236 |
< |
strftime(since, 20, "%m/%d/%Y %H:%M:%S", gmtime(&file.st_mtime)); |
236 |
> |
delete now; |
237 |
|
|
238 |
< |
cout << "since=" << since << " GMT\n"; |
165 |
< |
} |
238 |
> |
cout << "since=" << since << " GMT\n"; |
239 |
|
} |
240 |
|
} |