ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/repos/HostUpdate/HostUpdate.cpp
(Generate patch)

Comparing HostUpdate/HostUpdate.cpp (file contents):
Revision 17 by Douglas Thrift, 2003-11-10T11:08:33-08:00 vs.
Revision 91 by Douglas Thrift, 2004-03-01T19:41:20-08:00

# Line 6 | Line 6
6  
7   #include "HostUpdate.hpp"
8   #include "Host.hpp"
9 +
10 + #ifdef _WIN32
11   #include <windows.h>
12 + #endif
13  
14   int main(int argc, char* argv[])
15   {
# Line 30 | Line 33 | HostUpdate::HostUpdate()
33          string method = sgetenv("REQUEST_METHOD");
34  
35          parse(method);
36 +        mode();
37  
38          if (agent.find("Host Update/") == 0 && method == "POST" && agent.find(" (")
39                  != string::npos && agent.find(") libwww-perl/") != string::npos)
40          {
41                  update(agent);
42          }
43 +        else if (agent.find("Host Update Sharp/") == 0 && method == "POST" &&
44 +                agent.find(" (") != string::npos && agent.find(')') != string::npos)
45 +        {
46 +                update(agent);
47 +        }
48          else
49          {
50                  display();
51          }
52   }
53  
45 HostUpdate::~HostUpdate()
46 {
47        //
48 }
49
54   void HostUpdate::parse(const string& method)
55   {
56          string query;
# Line 76 | Line 80 | void HostUpdate::parse(const string& met
80          while (input.good());
81   }
82  
83 + void HostUpdate::mode()
84 + {
85 +        host = false, name = false, address = false, platform = false, since =
86 +                false;
87 +
88 +        for (multimap<string, string>::iterator itor = cgi.find("mode"); itor !=
89 +                cgi.upper_bound("mode") && itor != cgi.end(); itor++)
90 +        {
91 +                string mode = itor->second;
92 +
93 +                for (string::iterator itor = mode.begin(); itor != mode.end(); itor++)
94 +                {
95 +                        char mode = *itor;
96 +
97 +                        switch (mode)
98 +                        {
99 +                        case 'h':
100 +                                if (!host) host = true;
101 +                                break;
102 +                        case 'n':
103 +                                if (!name) name = true;
104 +                                break;
105 +                        case 'a':
106 +                                if (!address) address = true;
107 +                                break;
108 +                        case 'p':
109 +                                if (!platform) platform = true;
110 +                                break;
111 +                        case 's':
112 +                                if (!since) since = true;
113 +                                break;
114 +                        default:
115 +                                break;
116 +                        }
117 +                }
118 +        }
119 +
120 +        if (!host && !name && !address && !platform && !since) host = true, name =
121 +                true, address = true, platform = true, since = true;
122 + }
123 +
124   void HostUpdate::update(const string& agent)
125   {
126          cout << "Content-Type: text/plain\n\n";
# Line 88 | Line 133 | void HostUpdate::update(const string& ag
133          string host = itor->second, name = sgetenv("REMOTE_HOST"), address =
134             sgetenv("REMOTE_ADDR");
135  
136 <        string::size_type begin = agent.find('(') + 1, end = agent.find(')',
137 <                begin);
136 >        string::size_type begin = agent.find('(') + 1, end = agent.rfind(')');
137 >
138 >        if (begin >= end) return;
139 >
140          string platform = agent.substr(begin, end - begin);
141  
142          Host client(host, name, address, platform), saved(host);
143  
97        cout << "host=" << client.getHost() << '\n'
98                << "name=" << client.getName() << '\n'
99                << "address=" << client.getAddress() << '\n'
100                << "platform=" << client.getPlatform() << '\n';
101
144          if (client != ++saved) client--;
145 +
146 +        display(client);
147   }
148  
149   void HostUpdate::display()
150   {
151          cout << "Content-Type: text/plain\n\n";
152  
153 +        {
154 +                bool request = false;
155 +
156 +                for (multimap<string, string>::iterator itor = cgi.find("host"); itor !=
157 +                        cgi.upper_bound("host") && itor != cgi.end(); itor++)
158 +                {
159 +                        if (itor->second != "")
160 +                        {
161 +                                Host host(itor->second);
162 +
163 +                                display(++host);
164 +
165 +                                if (!request) request = true;
166 +                        }
167 +                }
168 +
169 +                if (request) return;
170 +        }
171 +
172          set<Host> hosts;
173  
174   #ifndef _WIN32
# Line 145 | Line 208 | void HostUpdate::display()
208  
209          for (set<Host>::iterator itor = hosts.begin(); itor != hosts.end(); itor++)
210          {
211 <                Host host = *itor;
211 >                display(*itor);
212 >        }
213 > }
214 >
215 > void HostUpdate::display(const Host& host)
216 > {
217 >        if (this->host) cout << "host=" << host.getHost() << '\n';
218 >        if (name) cout << "name=" << host.getName() << '\n';
219 >        if (address) cout << "address=" << host.getAddress() << '\n';
220 >        if (platform) cout << "platform=" << host.getPlatform() << '\n';
221 >
222 >        string name = string("hosts") + slash + host.getHost();
223 >        struct stat file;
224 >
225 >        if (since && stat(name.c_str(), &file) == 0)
226 >        {
227 >                char since[20];
228 >
229 >                strftime(since, 20, "%m/%d/%Y %H:%M:%S", gmtime(&file.st_mtime));
230 >
231 >                cout << "since=" << since << " GMT\n";
232 >        }
233 >        else if (since)
234 >        {
235 >                char since[20];
236 >                time_t* now = new time_t;
237 >
238 >                time(now);
239 >                strftime(since, 20, "%m/%d/%Y %H:%M:%S", gmtime(now));
240 >
241 >                delete now;
242  
243 <                cout << "host=" << host.getHost() << '\n'
151 <                        << "name=" << host.getName() << '\n'
152 <                        << "address=" << host.getAddress() << '\n'
153 <                        << "platform=" << host.getPlatform() << "\n\n";
243 >                cout << "since=" << since << " GMT\n";
244          }
245   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines