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