ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/repos/HostUpdate/HostUpdate.cpp
Revision: 14
Committed: 2003-11-07T23:04:39-08:00 (21 years, 7 months ago) by Douglas Thrift
File size: 2071 byte(s)
Log Message:
Well the world is still going to hell, along with Linux, FreeBSD seems ok
though.

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 3
10     int main(int argc, char* argv[])
11     {
12     HostUpdate update;
13    
14     return 0;
15     }
16    
17     HostUpdate::HostUpdate()
18     {
19 Douglas Thrift 12 struct stat* hosts = new struct stat;
20 Douglas Thrift 3
21 Douglas Thrift 12 if (stat("hosts", hosts) != 0)
22     {
23     mkdir("hosts");
24     }
25 Douglas Thrift 3
26 Douglas Thrift 12 delete [] hosts;
27 Douglas Thrift 10
28 Douglas Thrift 12 chdir("hosts");
29 Douglas Thrift 10
30 Douglas Thrift 12 CgiEnvironment env = cgi.getEnvironment();
31 Douglas Thrift 10
32 Douglas Thrift 12 string agent = env.getUserAgent();
33     string method = env.getRequestMethod();
34    
35 Douglas Thrift 13 if (agent.find("Host Update/") == 0 && method == "POST" && agent.find(" (")
36 Douglas Thrift 12 != string::npos && agent.find(") libwww-perl/") != string::npos)
37     {
38     update(env, agent);
39     }
40     else
41     {
42     display(env);
43     }
44 Douglas Thrift 3 }
45    
46     HostUpdate::~HostUpdate()
47     {
48 Douglas Thrift 10 //
49 Douglas Thrift 3 }
50 Douglas Thrift 12
51     void HostUpdate::update(CgiEnvironment& env, const string& agent)
52     {
53     cout << "Content-Type: text/plain\n\n";
54    
55     vector<FormEntry> entries;
56    
57     if (!cgi.getElement("host", entries)) return;
58     if (entries[0].isEmpty()) return;
59    
60 Douglas Thrift 13 string host = entries[0].getStrippedValue(), name = env.getRemoteHost(),
61     address = env.getRemoteAddr();
62 Douglas Thrift 12
63     if (name == address) name = "";
64    
65 Douglas Thrift 13 string::size_type begin = agent.find('(') + 1, end = agent.find(')',
66     begin);
67 Douglas Thrift 12 string platform = agent.substr(begin, end - begin);
68 Douglas Thrift 13
69     cout << "host=" << host << '\n'
70     << "name=" << name << '\n'
71     << "address=" << address << '\n'
72     << "platform=" << platform << '\n';
73    
74     Host client(host, name, address, platform), saved(host);
75    
76     if (client != ++saved) client--;
77 Douglas Thrift 12 }
78    
79     void HostUpdate::display(CgiEnvironment& env)
80     {
81 Douglas Thrift 14 cout << "Content-Type: text/plain\n\n";
82    
83     set<Host> hosts;
84     DIR* dir = opendir(".");
85     struct dirent* ent;
86    
87     while ((ent = readdir(dir)) != NULL)
88     {
89     string file = ent->d_name;
90    
91     cerr << file << '\n';
92    
93     if (file == "." || file == "..") continue;
94    
95     Host host(file);
96    
97     hosts.insert(++host);
98     }
99    
100     closedir(dir);
101    
102     for (set<Host>::iterator itor = hosts.begin(); itor != hosts.end(); itor++)
103     {
104     Host host = *itor;
105    
106     cout << "host=" << host.getHost() << '\n'
107     << "name=" << host.getName() << '\n'
108     << "address=" << host.getAddress() << '\n'
109     << "platform=" << host.getPlatform() << "\n\n";
110     }
111 Douglas Thrift 12 }

Properties

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