ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/repos/HostUpdate/HostUpdate.cpp
Revision: 20
Committed: 2003-11-10T20:11:13-08:00 (21 years, 7 months ago) by Douglas Thrift
File size: 2946 byte(s)
Log Message:
Fixit.

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 17 #include <windows.h>
10 Douglas Thrift 3
11     int main(int argc, char* argv[])
12     {
13     HostUpdate update;
14    
15     return 0;
16     }
17    
18     HostUpdate::HostUpdate()
19     {
20 Douglas Thrift 12 struct stat* hosts = new struct stat;
21 Douglas Thrift 3
22 Douglas Thrift 12 if (stat("hosts", hosts) != 0)
23     {
24     mkdir("hosts");
25     }
26 Douglas Thrift 3
27 Douglas Thrift 12 delete [] hosts;
28 Douglas Thrift 10
29 Douglas Thrift 15 string agent = sgetenv("HTTP_USER_AGENT");
30     string method = sgetenv("REQUEST_METHOD");
31 Douglas Thrift 10
32 Douglas Thrift 15 parse(method);
33 Douglas Thrift 10
34 Douglas Thrift 13 if (agent.find("Host Update/") == 0 && method == "POST" && agent.find(" (")
35 Douglas Thrift 12 != string::npos && agent.find(") libwww-perl/") != string::npos)
36     {
37 Douglas Thrift 15 update(agent);
38 Douglas Thrift 12 }
39     else
40     {
41 Douglas Thrift 15 display();
42 Douglas Thrift 12 }
43 Douglas Thrift 3 }
44    
45 Douglas Thrift 15 void HostUpdate::parse(const string& method)
46 Douglas Thrift 12 {
47 Douglas Thrift 15 string query;
48 Douglas Thrift 12
49 Douglas Thrift 15 if (method == "POST")
50     {
51     getline(cin, query);
52     }
53     else
54     {
55     query = sgetenv("QUERY_STRING");
56     }
57 Douglas Thrift 12
58 Douglas Thrift 15 if (query == "") return;
59 Douglas Thrift 12
60 Douglas Thrift 15 istringstream input(query);
61 Douglas Thrift 12
62 Douglas Thrift 15 do
63     {
64     string name, value;
65 Douglas Thrift 12
66 Douglas Thrift 15 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(const string& agent)
75     {
76     cout << "Content-Type: text/plain\n\n";
77    
78     multimap<string, string>::iterator itor = cgi.find("host");
79 Douglas Thrift 17
80 Douglas Thrift 15 if (itor == cgi.end()) return;
81     if (itor->second == "") return;
82    
83     string host = itor->second, name = sgetenv("REMOTE_HOST"), address =
84     sgetenv("REMOTE_ADDR");
85    
86 Douglas Thrift 13 string::size_type begin = agent.find('(') + 1, end = agent.find(')',
87     begin);
88 Douglas Thrift 12 string platform = agent.substr(begin, end - begin);
89 Douglas Thrift 13
90     Host client(host, name, address, platform), saved(host);
91    
92 Douglas Thrift 15 cout << "host=" << client.getHost() << '\n'
93     << "name=" << client.getName() << '\n'
94     << "address=" << client.getAddress() << '\n'
95     << "platform=" << client.getPlatform() << '\n';
96    
97 Douglas Thrift 13 if (client != ++saved) client--;
98 Douglas Thrift 12 }
99    
100 Douglas Thrift 15 void HostUpdate::display()
101 Douglas Thrift 12 {
102 Douglas Thrift 14 cout << "Content-Type: text/plain\n\n";
103    
104     set<Host> hosts;
105 Douglas Thrift 17
106     #ifndef _WIN32
107 Douglas Thrift 15 DIR* dir = opendir("hosts");
108 Douglas Thrift 14 struct dirent* ent;
109    
110     while ((ent = readdir(dir)) != NULL)
111     {
112     string file = ent->d_name;
113    
114     if (file == "." || file == "..") continue;
115 Douglas Thrift 17
116 Douglas Thrift 14 Host host(file);
117    
118     hosts.insert(++host);
119     }
120 Douglas Thrift 15
121 Douglas Thrift 14 closedir(dir);
122 Douglas Thrift 17 #else
123     WIN32_FIND_DATA found;
124     HANDLE find = FindFirstFile("hosts\\*", &found);
125 Douglas Thrift 14
126 Douglas Thrift 17 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 Douglas Thrift 14 for (set<Host>::iterator itor = hosts.begin(); itor != hosts.end(); itor++)
142     {
143 Douglas Thrift 20 display(*itor);
144     }
145     }
146 Douglas Thrift 14
147 Douglas Thrift 20 void HostUpdate::display(const Host& host)
148     {
149     cout << "host=" << host.getHost() << '\n'
150     << "name=" << host.getName() << '\n'
151     << "address=" << host.getAddress() << '\n'
152     << "platform=" << host.getPlatform() << '\n';
153 Douglas Thrift 19
154 Douglas Thrift 20 string name = string("hosts") + slash + host.getHost();
155     struct stat file;
156 Douglas Thrift 19
157 Douglas Thrift 20 if (stat(name.c_str(), &file) == 0)
158     {
159     char since[20];
160 Douglas Thrift 19
161 Douglas Thrift 20 strftime(since, 20, "%m/%d/%Y %H:%M:%S", gmtime(&file.st_mtime));
162 Douglas Thrift 19
163 Douglas Thrift 20 cout << "since=" << since << " GMT\n";
164 Douglas Thrift 14 }
165 Douglas Thrift 12 }

Properties

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