5 |
|
// $Id$ |
6 |
|
|
7 |
|
#include <arpa/inet.h> |
8 |
+ |
#include <sys/utsname.h> |
9 |
|
|
10 |
|
#include "Daemon.hpp" |
11 |
|
|
20 |
|
|
21 |
|
if (fork) |
22 |
|
{ |
23 |
< |
switch (::fork()) |
23 |
> |
switch (pid_t pid = ::fork()) |
24 |
|
{ |
25 |
|
case -1: |
26 |
|
cerr << program << ": fork()\n"; |
29 |
|
case 0: |
30 |
|
break; |
31 |
|
default: |
32 |
+ |
cout << pid << '\n'; |
33 |
|
return; |
34 |
|
} |
35 |
|
} |
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 |
|
} |
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)); |
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 |
|
|
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; |
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 |
|
} |
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 = |