25 |
|
|
26 |
|
delete [] hosts; |
27 |
|
|
28 |
< |
chdir("hosts"); |
28 |
> |
string agent = sgetenv("HTTP_USER_AGENT"); |
29 |
> |
string method = sgetenv("REQUEST_METHOD"); |
30 |
|
|
31 |
< |
CgiEnvironment env = cgi.getEnvironment(); |
31 |
> |
parse(method); |
32 |
|
|
33 |
< |
string agent = env.getUserAgent(); |
33 |
< |
string method = env.getRequestMethod(); |
34 |
< |
|
35 |
< |
if (agent.find("Host Update/") == 0 && method == "GET" && agent.find(" (") |
33 |
> |
if (agent.find("Host Update/") == 0 && method == "POST" && agent.find(" (") |
34 |
|
!= string::npos && agent.find(") libwww-perl/") != string::npos) |
35 |
|
{ |
36 |
< |
update(env, agent); |
36 |
> |
update(agent); |
37 |
|
} |
38 |
|
else |
39 |
|
{ |
40 |
< |
display(env); |
40 |
> |
display(); |
41 |
|
} |
42 |
|
} |
43 |
|
|
46 |
|
// |
47 |
|
} |
48 |
|
|
49 |
< |
void HostUpdate::update(CgiEnvironment& env, const string& agent) |
49 |
> |
void HostUpdate::parse(const string& method) |
50 |
|
{ |
51 |
< |
cout << "Content-Type: text/plain\n\n"; |
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 |
< |
vector<FormEntry> entries; |
64 |
> |
istringstream input(query); |
65 |
|
|
66 |
< |
if (!cgi.getElement("host", entries)) return; |
67 |
< |
if (entries[0].isEmpty()) return; |
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 |
< |
string host = entries[0].getStrippedValue(); |
83 |
< |
string name = env.getRemoteHost(); |
84 |
< |
string address = env.getRemoteAddr(); |
82 |
> |
multimap<string, string>::iterator itor = cgi.find("host"); |
83 |
> |
|
84 |
> |
if (itor == cgi.end()) return; |
85 |
> |
if (itor->second == "") return; |
86 |
|
|
87 |
< |
if (name == address) name = ""; |
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(begin, ')'); |
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(CgiEnvironment& env) |
104 |
> |
void HostUpdate::display() |
105 |
|
{ |
106 |
< |
// |
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 |
|
} |