ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/repos/HostUpdate/HostUpdate.cpp
Revision: 21
Committed: 2003-11-10T20:16:12-08:00 (21 years, 7 months ago) by Douglas Thrift
File size: 2968 byte(s)
Log Message:
Oops, evil Windows!

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

Properties

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