ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/repos/HostUpdate/HostUpdate.cpp
Revision: 17
Committed: 2003-11-10T11:08:33-08:00 (21 years, 7 months ago) by Douglas Thrift
File size: 2693 byte(s)
Log Message:
It works on Windows again.

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

Properties

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