ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/repos/HostUpdate/HostUpdate.cpp
Revision: 91
Committed: 2004-03-01T19:41:20-08:00 (21 years, 3 months ago) by Douglas Thrift
File size: 4431 byte(s)
Log Message:
Ooh, now it works with HostUpdateSharp!

File Contents

# User Rev Content
1 Douglas Thrift 3 // Host Update
2     //
3     // Douglas Thrift
4     //
5     // $Id$
6    
7     #include "HostUpdate.hpp"
8 Douglas Thrift 9 #include "Host.hpp"
9 Douglas Thrift 21
10     #ifdef _WIN32
11 Douglas Thrift 17 #include <windows.h>
12 Douglas Thrift 21 #endif
13 Douglas Thrift 3
14     int main(int argc, char* argv[])
15     {
16     HostUpdate update;
17    
18     return 0;
19     }
20    
21     HostUpdate::HostUpdate()
22     {
23 Douglas Thrift 12 struct stat* hosts = new struct stat;
24 Douglas Thrift 3
25 Douglas Thrift 12 if (stat("hosts", hosts) != 0)
26     {
27     mkdir("hosts");
28     }
29 Douglas Thrift 3
30 Douglas Thrift 12 delete [] hosts;
31 Douglas Thrift 10
32 Douglas Thrift 15 string agent = sgetenv("HTTP_USER_AGENT");
33     string method = sgetenv("REQUEST_METHOD");
34 Douglas Thrift 10
35 Douglas Thrift 15 parse(method);
36 Douglas Thrift 22 mode();
37 Douglas Thrift 10
38 Douglas Thrift 13 if (agent.find("Host Update/") == 0 && method == "POST" && agent.find(" (")
39 Douglas Thrift 12 != string::npos && agent.find(") libwww-perl/") != string::npos)
40     {
41 Douglas Thrift 15 update(agent);
42 Douglas Thrift 12 }
43 Douglas Thrift 91 else if (agent.find("Host Update Sharp/") == 0 && method == "POST" &&
44     agent.find(" (") != string::npos && agent.find(')') != string::npos)
45     {
46     update(agent);
47     }
48 Douglas Thrift 12 else
49     {
50 Douglas Thrift 15 display();
51 Douglas Thrift 12 }
52 Douglas Thrift 3 }
53    
54 Douglas Thrift 15 void HostUpdate::parse(const string& method)
55 Douglas Thrift 12 {
56 Douglas Thrift 15 string query;
57 Douglas Thrift 12
58 Douglas Thrift 15 if (method == "POST")
59     {
60     getline(cin, query);
61     }
62     else
63     {
64     query = sgetenv("QUERY_STRING");
65     }
66 Douglas Thrift 12
67 Douglas Thrift 15 if (query == "") return;
68 Douglas Thrift 12
69 Douglas Thrift 15 istringstream input(query);
70 Douglas Thrift 12
71 Douglas Thrift 15 do
72     {
73     string name, value;
74 Douglas Thrift 12
75 Douglas Thrift 15 getline(input, name, '=');
76     getline(input, value, '&');
77    
78     cgi.insert(pair<string, string>(name, value));
79     }
80     while (input.good());
81     }
82    
83 Douglas Thrift 22 void HostUpdate::mode()
84     {
85     host = false, name = false, address = false, platform = false, since =
86     false;
87    
88     for (multimap<string, string>::iterator itor = cgi.find("mode"); itor !=
89 Douglas Thrift 24 cgi.upper_bound("mode") && itor != cgi.end(); itor++)
90 Douglas Thrift 22 {
91     string mode = itor->second;
92    
93     for (string::iterator itor = mode.begin(); itor != mode.end(); itor++)
94     {
95     char mode = *itor;
96    
97     switch (mode)
98     {
99     case 'h':
100     if (!host) host = true;
101     break;
102     case 'n':
103     if (!name) name = true;
104     break;
105     case 'a':
106     if (!address) address = true;
107     break;
108     case 'p':
109     if (!platform) platform = true;
110     break;
111     case 's':
112     if (!since) since = true;
113     break;
114     default:
115     break;
116     }
117     }
118     }
119    
120     if (!host && !name && !address && !platform && !since) host = true, name =
121     true, address = true, platform = true, since = true;
122     }
123    
124 Douglas Thrift 15 void HostUpdate::update(const string& agent)
125     {
126     cout << "Content-Type: text/plain\n\n";
127    
128     multimap<string, string>::iterator itor = cgi.find("host");
129 Douglas Thrift 17
130 Douglas Thrift 15 if (itor == cgi.end()) return;
131     if (itor->second == "") return;
132    
133     string host = itor->second, name = sgetenv("REMOTE_HOST"), address =
134     sgetenv("REMOTE_ADDR");
135    
136 Douglas Thrift 23 string::size_type begin = agent.find('(') + 1, end = agent.rfind(')');
137    
138     if (begin >= end) return;
139    
140 Douglas Thrift 12 string platform = agent.substr(begin, end - begin);
141 Douglas Thrift 13
142     Host client(host, name, address, platform), saved(host);
143    
144 Douglas Thrift 22 if (client != ++saved) client--;
145 Douglas Thrift 15
146 Douglas Thrift 22 display(client);
147 Douglas Thrift 12 }
148    
149 Douglas Thrift 15 void HostUpdate::display()
150 Douglas Thrift 12 {
151 Douglas Thrift 14 cout << "Content-Type: text/plain\n\n";
152    
153 Douglas Thrift 22 {
154     bool request = false;
155    
156     for (multimap<string, string>::iterator itor = cgi.find("host"); itor !=
157 Douglas Thrift 24 cgi.upper_bound("host") && itor != cgi.end(); itor++)
158 Douglas Thrift 22 {
159     if (itor->second != "")
160     {
161     Host host(itor->second);
162    
163     display(++host);
164    
165     if (!request) request = true;
166     }
167     }
168    
169     if (request) return;
170     }
171    
172 Douglas Thrift 14 set<Host> hosts;
173 Douglas Thrift 17
174     #ifndef _WIN32
175 Douglas Thrift 15 DIR* dir = opendir("hosts");
176 Douglas Thrift 14 struct dirent* ent;
177    
178     while ((ent = readdir(dir)) != NULL)
179     {
180     string file = ent->d_name;
181    
182     if (file == "." || file == "..") continue;
183 Douglas Thrift 17
184 Douglas Thrift 14 Host host(file);
185    
186     hosts.insert(++host);
187     }
188 Douglas Thrift 15
189 Douglas Thrift 14 closedir(dir);
190 Douglas Thrift 17 #else
191     WIN32_FIND_DATA found;
192     HANDLE find = FindFirstFile("hosts\\*", &found);
193 Douglas Thrift 14
194 Douglas Thrift 17 do
195     {
196     string file = found.cFileName;
197    
198     if (file == "." || file == "..") continue;
199    
200     Host host(file);
201    
202     hosts.insert(++host);
203     }
204     while (FindNextFile(find, &found) != 0);
205    
206     FindClose(find);
207     #endif
208    
209 Douglas Thrift 14 for (set<Host>::iterator itor = hosts.begin(); itor != hosts.end(); itor++)
210     {
211 Douglas Thrift 20 display(*itor);
212     }
213     }
214 Douglas Thrift 14
215 Douglas Thrift 20 void HostUpdate::display(const Host& host)
216     {
217 Douglas Thrift 22 if (this->host) cout << "host=" << host.getHost() << '\n';
218     if (name) cout << "name=" << host.getName() << '\n';
219     if (address) cout << "address=" << host.getAddress() << '\n';
220     if (platform) cout << "platform=" << host.getPlatform() << '\n';
221 Douglas Thrift 19
222 Douglas Thrift 20 string name = string("hosts") + slash + host.getHost();
223     struct stat file;
224 Douglas Thrift 19
225 Douglas Thrift 22 if (since && stat(name.c_str(), &file) == 0)
226 Douglas Thrift 20 {
227     char since[20];
228 Douglas Thrift 19
229 Douglas Thrift 20 strftime(since, 20, "%m/%d/%Y %H:%M:%S", gmtime(&file.st_mtime));
230 Douglas Thrift 19
231 Douglas Thrift 20 cout << "since=" << since << " GMT\n";
232 Douglas Thrift 14 }
233 Douglas Thrift 22 else if (since)
234     {
235     char since[20];
236     time_t* now = new time_t;
237    
238     time(now);
239     strftime(since, 20, "%m/%d/%Y %H:%M:%S", gmtime(now));
240    
241     delete now;
242    
243     cout << "since=" << since << " GMT\n";
244     }
245 Douglas Thrift 12 }

Properties

Name Value
svn:eol-style native
svn:keywords Id