ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/repos/HostUpdate/HostUpdate.cpp
Revision: 22
Committed: 2003-11-10T23:58:38-08:00 (21 years, 7 months ago) by Douglas Thrift
File size: 4203 byte(s)
Log Message:
Stuff happens, nicely.

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     else
44     {
45 Douglas Thrift 15 display();
46 Douglas Thrift 12 }
47 Douglas Thrift 3 }
48    
49 Douglas Thrift 15 void HostUpdate::parse(const string& method)
50 Douglas Thrift 12 {
51 Douglas Thrift 15 string query;
52 Douglas Thrift 12
53 Douglas Thrift 15 if (method == "POST")
54     {
55     getline(cin, query);
56     }
57     else
58     {
59     query = sgetenv("QUERY_STRING");
60     }
61 Douglas Thrift 12
62 Douglas Thrift 15 if (query == "") return;
63 Douglas Thrift 12
64 Douglas Thrift 15 istringstream input(query);
65 Douglas Thrift 12
66 Douglas Thrift 15 do
67     {
68     string name, value;
69 Douglas Thrift 12
70 Douglas Thrift 15 getline(input, name, '=');
71     getline(input, value, '&');
72    
73     cgi.insert(pair<string, string>(name, value));
74     }
75     while (input.good());
76     }
77    
78 Douglas Thrift 22 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++)
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 Douglas Thrift 15 void HostUpdate::update(const string& agent)
120     {
121     cout << "Content-Type: text/plain\n\n";
122    
123     multimap<string, string>::iterator itor = cgi.find("host");
124 Douglas Thrift 17
125 Douglas Thrift 15 if (itor == cgi.end()) return;
126     if (itor->second == "") return;
127    
128     string host = itor->second, name = sgetenv("REMOTE_HOST"), address =
129     sgetenv("REMOTE_ADDR");
130    
131 Douglas Thrift 13 string::size_type begin = agent.find('(') + 1, end = agent.find(')',
132     begin);
133 Douglas Thrift 12 string platform = agent.substr(begin, end - begin);
134 Douglas Thrift 13
135     Host client(host, name, address, platform), saved(host);
136    
137 Douglas Thrift 22 if (client != ++saved) client--;
138 Douglas Thrift 15
139 Douglas Thrift 22 display(client);
140 Douglas Thrift 12 }
141    
142 Douglas Thrift 15 void HostUpdate::display()
143 Douglas Thrift 12 {
144 Douglas Thrift 14 cout << "Content-Type: text/plain\n\n";
145    
146 Douglas Thrift 22 {
147     bool request = false;
148    
149     for (multimap<string, string>::iterator itor = cgi.find("host"); itor !=
150     cgi.upper_bound("host"); itor++)
151     {
152     if (itor->second != "")
153     {
154     Host host(itor->second);
155    
156     display(++host);
157    
158     if (!request) request = true;
159     }
160     }
161    
162     if (request) return;
163     }
164    
165 Douglas Thrift 14 set<Host> hosts;
166 Douglas Thrift 17
167     #ifndef _WIN32
168 Douglas Thrift 15 DIR* dir = opendir("hosts");
169 Douglas Thrift 14 struct dirent* ent;
170    
171     while ((ent = readdir(dir)) != NULL)
172     {
173     string file = ent->d_name;
174    
175     if (file == "." || file == "..") continue;
176 Douglas Thrift 17
177 Douglas Thrift 14 Host host(file);
178    
179     hosts.insert(++host);
180     }
181 Douglas Thrift 15
182 Douglas Thrift 14 closedir(dir);
183 Douglas Thrift 17 #else
184     WIN32_FIND_DATA found;
185     HANDLE find = FindFirstFile("hosts\\*", &found);
186 Douglas Thrift 14
187 Douglas Thrift 17 do
188     {
189     string file = found.cFileName;
190    
191     if (file == "." || file == "..") continue;
192    
193     Host host(file);
194    
195     hosts.insert(++host);
196     }
197     while (FindNextFile(find, &found) != 0);
198    
199     FindClose(find);
200     #endif
201    
202 Douglas Thrift 14 for (set<Host>::iterator itor = hosts.begin(); itor != hosts.end(); itor++)
203     {
204 Douglas Thrift 20 display(*itor);
205     }
206     }
207 Douglas Thrift 14
208 Douglas Thrift 20 void HostUpdate::display(const Host& host)
209     {
210 Douglas Thrift 22 if (this->host) cout << "host=" << host.getHost() << '\n';
211     if (name) cout << "name=" << host.getName() << '\n';
212     if (address) cout << "address=" << host.getAddress() << '\n';
213     if (platform) cout << "platform=" << host.getPlatform() << '\n';
214 Douglas Thrift 19
215 Douglas Thrift 20 string name = string("hosts") + slash + host.getHost();
216     struct stat file;
217 Douglas Thrift 19
218 Douglas Thrift 22 if (since && stat(name.c_str(), &file) == 0)
219 Douglas Thrift 20 {
220     char since[20];
221 Douglas Thrift 19
222 Douglas Thrift 20 strftime(since, 20, "%m/%d/%Y %H:%M:%S", gmtime(&file.st_mtime));
223 Douglas Thrift 19
224 Douglas Thrift 20 cout << "since=" << since << " GMT\n";
225 Douglas Thrift 14 }
226 Douglas Thrift 22 else if (since)
227     {
228     char since[20];
229     time_t* now = new time_t;
230    
231     time(now);
232     strftime(since, 20, "%m/%d/%Y %H:%M:%S", gmtime(now));
233    
234     delete now;
235    
236     cout << "since=" << since << " GMT\n";
237     }
238 Douglas Thrift 12 }

Properties

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