6 |
|
|
7 |
|
#include "HostUpdate.hpp" |
8 |
|
#include "Host.hpp" |
9 |
+ |
#include <windows.h> |
10 |
|
|
11 |
|
int main(int argc, char* argv[]) |
12 |
|
{ |
26 |
|
|
27 |
|
delete [] hosts; |
28 |
|
|
29 |
< |
chdir("hosts"); |
29 |
> |
string agent = sgetenv("HTTP_USER_AGENT"); |
30 |
> |
string method = sgetenv("REQUEST_METHOD"); |
31 |
|
|
32 |
< |
CgiEnvironment env = cgi.getEnvironment(); |
32 |
> |
parse(method); |
33 |
|
|
34 |
< |
string agent = env.getUserAgent(); |
33 |
< |
string method = env.getRequestMethod(); |
34 |
< |
|
35 |
< |
if (agent.find("Host Update/") == 0 && method == "GET" && agent.find(" (") |
34 |
> |
if (agent.find("Host Update/") == 0 && method == "POST" && agent.find(" (") |
35 |
|
!= string::npos && agent.find(") libwww-perl/") != string::npos) |
36 |
|
{ |
37 |
< |
update(env, agent); |
37 |
> |
update(agent); |
38 |
|
} |
39 |
|
else |
40 |
|
{ |
41 |
< |
display(env); |
41 |
> |
display(); |
42 |
|
} |
43 |
|
} |
44 |
|
|
47 |
|
// |
48 |
|
} |
49 |
|
|
50 |
< |
void HostUpdate::update(CgiEnvironment& env, const string& agent) |
50 |
> |
void HostUpdate::parse(const string& method) |
51 |
|
{ |
52 |
< |
cout << "Content-Type: text/plain\n\n"; |
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 |
< |
vector<FormEntry> entries; |
71 |
> |
getline(input, name, '='); |
72 |
> |
getline(input, value, '&'); |
73 |
|
|
74 |
< |
if (!cgi.getElement("host", entries)) return; |
75 |
< |
if (entries[0].isEmpty()) return; |
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 |
< |
string host = entries[0].getStrippedValue(); |
86 |
< |
string name = env.getRemoteHost(); |
62 |
< |
string address = env.getRemoteAddr(); |
85 |
> |
if (itor == cgi.end()) return; |
86 |
> |
if (itor->second == "") return; |
87 |
|
|
88 |
< |
if (name == address) name = ""; |
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(begin, ')'); |
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(CgiEnvironment& env) |
105 |
> |
void HostUpdate::display() |
106 |
|
{ |
107 |
< |
// |
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 |
|
} |