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 14 by Douglas Thrift, 2003-11-07T23:04:39-08:00 vs.
Revision 17 by Douglas Thrift, 2003-11-10T11:08:33-08:00

# Line 6 | Line 6
6  
7   #include "HostUpdate.hpp"
8   #include "Host.hpp"
9 + #include <windows.h>
10  
11   int main(int argc, char* argv[])
12   {
# Line 25 | Line 26 | HostUpdate::HostUpdate()
26  
27          delete [] hosts;
28  
29 <        chdir("hosts");
29 >        string agent = sgetenv("HTTP_USER_AGENT");
30 >        string method = sgetenv("REQUEST_METHOD");
31  
32 <        CgiEnvironment env = cgi.getEnvironment();
31 <
32 <        string agent = env.getUserAgent();
33 <        string method = env.getRequestMethod();
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(env, agent);
37 >                update(agent);
38          }
39          else
40          {
41 <                display(env);
41 >                display();
42          }
43   }
44  
# Line 48 | Line 47 | HostUpdate::~HostUpdate()
47          //
48   }
49  
50 < void HostUpdate::update(CgiEnvironment& env, const string& agent)
50 > void HostUpdate::parse(const string& method)
51   {
52 <        cout << "Content-Type: text/plain\n\n";
52 >        string query;
53 >
54 >        if (method == "POST")
55 >        {
56 >                getline(cin, query);
57 >        }
58 >        else
59 >        {
60 >                query = sgetenv("QUERY_STRING");
61 >        }
62 >
63 >        if (query == "") return;
64  
65 <        vector<FormEntry> entries;
65 >        istringstream input(query);
66 >
67 >        do
68 >        {
69 >                string name, value;
70 >
71 >                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 <        if (!cgi.getElement("host", entries)) return;
58 <        if (entries[0].isEmpty()) return;
83 >        multimap<string, string>::iterator itor = cgi.find("host");
84  
85 <        string host = entries[0].getStrippedValue(), name = env.getRemoteHost(),
86 <                address = env.getRemoteAddr();
85 >        if (itor == cgi.end()) return;
86 >        if (itor->second == "") return;
87  
88 <        if (name == address) name = "";
88 >        string host = itor->second, name = sgetenv("REMOTE_HOST"), address =
89 >           sgetenv("REMOTE_ADDR");
90  
91          string::size_type begin = agent.find('(') + 1, end = agent.find(')',
92                  begin);
93          string platform = agent.substr(begin, end - begin);
94  
69        cout << "host=" << host << '\n'
70                << "name=" << name << '\n'
71                << "address=" << address << '\n'
72                << "platform=" << platform << '\n';
73
95          Host client(host, name, address, platform), saved(host);
96  
97 +        cout << "host=" << client.getHost() << '\n'
98 +                << "name=" << client.getName() << '\n'
99 +                << "address=" << client.getAddress() << '\n'
100 +                << "platform=" << client.getPlatform() << '\n';
101 +
102          if (client != ++saved) client--;
103   }
104  
105 < void HostUpdate::display(CgiEnvironment& env)
105 > void HostUpdate::display()
106   {
107          cout << "Content-Type: text/plain\n\n";
108  
109          set<Host> hosts;
110 <        DIR* dir = opendir(".");
110 >
111 > #ifndef _WIN32
112 >        DIR* dir = opendir("hosts");
113          struct dirent* ent;
114  
115          while ((ent = readdir(dir)) != NULL)
116          {
117                  string file = ent->d_name;
118  
91                cerr << file << '\n';
92
119                  if (file == "." || file == "..") continue;
120 <                
120 >
121                  Host host(file);
122  
123                  hosts.insert(++host);
124          }
125 <        
125 >
126          closedir(dir);
127 + #else
128 +        WIN32_FIND_DATA found;
129 +        HANDLE find = FindFirstFile("hosts\\*", &found);
130 +
131 +        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          for (set<Host>::iterator itor = hosts.begin(); itor != hosts.end(); itor++)
147          {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines