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 3 by Douglas Thrift, 2003-11-05T18:37:16-08:00 vs.
Revision 20 by Douglas Thrift, 2003-11-10T20:11:13-08:00

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines