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

Comparing Smersh/Daemon.cpp (file contents):
Revision 177 by Douglas Thrift, 2004-06-26T01:26:41-07:00 vs.
Revision 179 by Douglas Thrift, 2004-07-02T21:16:12-07:00

# Line 5 | Line 5
5   // $Id$
6  
7   #include <arpa/inet.h>
8 + #include <sys/utsname.h>
9  
10   #include "Daemon.hpp"
11  
# Line 19 | Line 20 | void Daemon::serve(int port, bool fork,
20  
21          if (fork)
22          {
23 <                switch (::fork())
23 >                switch (pid_t pid = ::fork())
24                  {
25                  case -1:
26                          cerr << program << ": fork()\n";
# Line 28 | Line 29 | void Daemon::serve(int port, bool fork,
29                  case 0:
30                          break;
31                  default:
32 +                        cout << pid << '\n';
33                          return;
34                  }
35          }
# Line 36 | Line 38 | void Daemon::serve(int port, bool fork,
38  
39          while (true)
40          {
41 <                api::TcpSocket* client(new api::TcpSocket());
41 >                Client* client (new Client);
42  
43 <                server.Accept(*client);
43 >                server.Accept(client->socket, &client->ip);
44  
45                  api::Thread thread(etl::BindAll(&Daemon::handle, self, client));
46          }
# Line 82 | Line 84 | Daemon::Status Daemon::request(istream&
84   void Daemon::response(ostream& sout, Status status)
85   {
86          sout << "HTTP/1.1 " << status << ' ' << reason(status) << crlf << "Date: "
87 <                << date() << crlf << "Server: Smersh/0.9" << crlf
87 >                << date() << crlf << "Server: " << server() << crlf
88                  << "Connection: close" << crlf;
89   }
90  
91 + string Daemon::server()
92 + {
93 +        utsname system;
94 +
95 +        uname(&system);
96 +
97 +        return string("Smersh/0.9 (") + system.sysname + ')';
98 + }
99 +
100   streamsize Daemon::error(ostream& sout, Status status)
101   {
102          string reason(this->reason(status));
# Line 93 | Line 104 | streamsize Daemon::error(ostream& sout,
104  
105          error << "<html><head><title>" << status << ' ' << reason << "</title></hea"
106                  << "d><body><h1>" << reason << "</h1><p>Mistakes were made, deal with t"
107 <                << "hem.</p><hr /><address>Smersh/0.9</address></body></html>\n";
107 >                << "hem.</p><hr /><address>" << server() + "</address></body></html>\n";
108          sout << "Content-Length: " << error.str().length() << crlf
109                  << "Content-Type: text/html; charset=UTF-8\r\n\r\n" << error.str();
110  
# Line 115 | Line 126 | string Daemon::date(bool log)
126          return when;
127   }
128  
129 < string Daemon::ip(const api::TcpSocket& socket)
129 > int Daemon::handle(Client* client)
130   {
131 <        api::InternetAddress address(socket.GetAddress());
121 <
122 <        return inet_ntoa(address->sin_addr);
123 < }
124 <
125 < int Daemon::handle(api::TcpSocket* client)
126 < {
127 <        ios::InputOutputStreamBufAdapter adapter(*client);
131 >        ios::InputOutputStreamBufAdapter adapter(client->socket);
132          iostream sio(&adapter);
133          Environment env;
134          stringstream post;
# Line 158 | Line 162 | int Daemon::handle(api::TcpSocket* clien
162  
163          ofstream fout(this->log.c_str(), ios_base::app);
164  
165 <        fout << ip(*client) << " - - " << date(true) << ' ' << log.str() << '\n';
165 >        fout << inet_ntoa(client->ip->sin_addr) << " - - " << date(true) << ' '
166 >                << log.str() << '\n';
167 >        sio << flush;
168 >
169 >        client->socket.ShutdownWrite();
170  
171          delete client;
172   }
# Line 176 | Line 184 | void Daemon::headers(istream& sin, Envir
184                  istringstream input(line);
185                  string name, value;
186  
187 <                ::getline(input, name, ':');
187 >                std::getline(input, name, ':');
188                  getline(input, value);
189  
190                  for (char next(sin.peek()); next == ' ' || next == '\t'; next =

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines