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 241 by Douglas Thrift, 2004-09-11T20:59:56-07:00

# Line 4 | Line 4
4   //
5   // $Id$
6  
7 #include <arpa/inet.h>
8
7   #include "Daemon.hpp"
8  
9   string Daemon::crlf("\r\n");
10  
11 < void Daemon::serve(int port, bool fork, Daemon* self)
11 > void Daemon::serve(bool fork, Daemon* self)
12   {
13          api::TcpSocket server;
14  
17        server.Create();
15          server.SetAddress(api::InternetAddress(api::InternetAddress::Any, port));
16  
17          if (fork)
18          {
19 <                switch (::fork())
19 >                switch (pid_t pid = ::fork())
20                  {
21                  case -1:
22                          cerr << program << ": fork()\n";
# Line 28 | Line 25 | void Daemon::serve(int port, bool fork,
25                  case 0:
26                          break;
27                  default:
28 +                        cout << pid << '\n';
29                          return;
30                  }
31          }
# Line 36 | Line 34 | void Daemon::serve(int port, bool fork,
34  
35          while (true)
36          {
37 <                api::TcpSocket* client(new api::TcpSocket());
40 <
41 <                server.Accept(*client);
42 <
37 >                Client* client (new Client(server));
38                  api::Thread thread(etl::BindAll(&Daemon::handle, self, client));
39          }
40   }
# Line 48 | Line 43 | Daemon::Status Daemon::request(istream&
43          ostream& log)
44   {
45          string line;
46 <        Matcher request("^([A-Z]+) .*?(\\?.+)? HTTP/(\\d+)\\.(\\d+)$");
46 >        Matcher request("^([A-Z]+) (.*?)(\\?.+)? HTTP/(\\d+)\\.(\\d+)$");
47  
48          getline(sin, line);
49  
# Line 59 | Line 54 | Daemon::Status Daemon::request(istream&
54  
55          if (line == request)
56          {
57 <                if (lexical_cast<unsigned>(request[3]) > 1) return version;
57 >                if (lexical_cast<unsigned>(request[4]) > 1) return version;
58  
59                  Matcher method("^GET|HEAD|POST$");
60  
61                  if (request[1] != method) return notImplemented;
62  
63                  env.set("REQUEST_METHOD", method);
64 +                env.set("REQUEST_URI", request[2] + request[3]);
65  
66 <                if (!request[2].empty()) env.set("QUERY_STRING", request[2].substr(1));
66 >                if (!request[3].empty()) env.set("QUERY_STRING", request[3].substr(1));
67  
68                  headers(sin, env);
69  
70 +                if (env.get("HTTP_HOST").empty()) return bad;
71                  if (method[0] == "POST") return message(sin, env, post);
72  
73                  return ok;
74          }
75  
76 <        return notFound;
76 >        return bad;
77   }
78  
79   void Daemon::response(ostream& sout, Status status)
80   {
81          sout << "HTTP/1.1 " << status << ' ' << reason(status) << crlf << "Date: "
82 <                << date() << crlf << "Server: Smersh/0.9" << crlf
82 >                << date() << crlf << "Server: " << server() << crlf
83                  << "Connection: close" << crlf;
84   }
85  
86 < streamsize Daemon::error(ostream& sout, Status status)
86 > string Daemon::reason(Status status)
87 > {
88 >        ostringstream sout;
89 >
90 >        switch (status)
91 >        {
92 >        case ok:
93 >                sout << "OK";
94 >                break;
95 >        case found:
96 >                sout << "Found";
97 >                break;
98 >        case seeOther:
99 >                sout << "See Other";
100 >                break;
101 >        case bad:
102 >                sout << "Bad Request";
103 >                break;
104 >        case notFound:
105 >                sout << "Not Found";
106 >                break;
107 >        case lengthRequired:
108 >                sout << "Length Required";
109 >                break;
110 >        case mediaType:
111 >                sout << "Unsupported Media Type";
112 >                break;
113 >        case serverError:
114 >                sout << "Internal Server Error";
115 >                break;
116 >        case notImplemented:
117 >                sout << "Not Implemented";
118 >                break;
119 >        case version:
120 >                sout << "HTTP Version not supported";
121 >        }
122 >
123 >        return sout.str();
124 > }
125 >
126 > string Daemon::server()
127 > {
128 >        utsname system;
129 >
130 >        uname(&system);
131 >
132 >        return string("Smersh/0.9 (") + system.sysname + ')';
133 > }
134 >
135 > string Daemon::server(const Environment& env)
136 > {
137 >        ostringstream server;
138 >        string port(env.get("SERVER_PORT"));
139 >
140 >        server << this->server() << " Server at " << env.get("SERVER_NAME") << " Po"
141 >                << "rt " << (port.empty() ? lexical_cast<string>(this->port) : port);
142 >
143 >        return server.str();
144 > }
145 >
146 > streamsize Daemon::error(ostream& sout, Status status, const Environment& env)
147   {
148          string reason(this->reason(status));
149          ostringstream error;
150  
151          error << "<html><head><title>" << status << ' ' << reason << "</title></hea"
152                  << "d><body><h1>" << reason << "</h1><p>Mistakes were made, deal with t"
153 <                << "hem.</p><hr /><address>Smersh/0.9</address></body></html>\n";
153 >                << "hem.</p><hr /><address>" << server(env) << "</address></body></html"
154 >                << ">\r\n";
155          sout << "Content-Length: " << error.str().length() << crlf
156                  << "Content-Type: text/html; charset=UTF-8\r\n\r\n" << error.str();
157  
# Line 115 | Line 173 | string Daemon::date(bool log)
173          return when;
174   }
175  
176 < string Daemon::ip(const api::TcpSocket& socket)
176 > int Daemon::handle(Client* client)
177   {
178 <        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);
128 <        iostream sio(&adapter);
178 >        ios::ToIoStream sio(&client->socket, &client->socket);
179          Environment env;
180          stringstream post;
181          ostringstream log;
182          Status code(request(sio, env, post, log));
183  
184 +        if (env.get("REQUEST_URI") == "/favicon.ico") code = notFound;
185 +
186          response(sio, code);
187  
188          bool head(env.get("REQUEST_METHOD") == "HEAD");
189          streamsize sent(0);
190  
191 <        if (code == ok && !head)
191 >        if (code == ok && env.get("REQUEST_URI") == "/robots.txt")
192          {
193 <                ostringstream output;
194 <                Smersh smersh(post, output, env);
143 <                string content(output.str().substr(40));
193 >                sio << "Content-Length: 28\r\nContent-Type: text/plain; charset=UTF-8\r"
194 >                        << "\n\r\n";
195  
196 <                sio << "Content-Length: " << content.length() << crlf
196 >                if (!head) { sio << "User-agent: *\r\nDisallow: /\r\n"; sent = 28; }
197 >        }
198 >        else if (code == ok && !head)
199 >        {
200 >                ostringstream content;
201 >                Smersh smersh(post, content, env);
202 >
203 >                sio << "Content-Length: " << content.str().length() << crlf
204                          << "Content-Type: text/html; charset=UTF-8\r\n\r\n";
205  
206 <                sio.write(content.data(), content.size());
206 >                sio.write(content.str().data(), content.str().size());
207  
208 <                sent = content.size();
208 >                sent = content.str().size();
209          }
210          else if (head) sio << "Content-Type: text/html; charset=UTF-8\r\n\r\n";
211 <        else sent = error(sio, code);
154 <
155 <        log << code << ' ' << (sent > 0 ? lexical_cast<string>(sent) : string("0"))
156 <                << " \"" << env.get("HTTP_REFERER") << "\" \""
157 <                << env.get("HTTP_USER_AGENT") << '"';
211 >        else sent = error(sio, code, env);
212  
213          ofstream fout(this->log.c_str(), ios_base::app);
214  
215 <        fout << ip(*client) << " - - " << date(true) << ' ' << log.str() << '\n';
215 >        fout << inet_ntoa(client->ip->sin_addr) << " - - " << date(true) << ' '
216 >                << log.str() << code << ' ' << lexical_cast<string>(sent) << " \""
217 >                << env.get("HTTP_REFERER") << "\" \"" << env.get("HTTP_USER_AGENT")
218 >                << "\"\n";
219 >        sio << flush;
220 >
221 >        client->socket.ShutdownWrite();
222  
223          delete client;
224 +
225 +        return 0;
226   }
227  
228   void Daemon::headers(istream& sin, Environment& env)
# Line 176 | Line 238 | void Daemon::headers(istream& sin, Envir
238                  istringstream input(line);
239                  string name, value;
240  
241 <                ::getline(input, name, ':');
241 >                std::getline(input, name, ':');
242                  getline(input, value);
243  
244                  for (char next(sin.peek()); next == ' ' || next == '\t'; next =
# Line 197 | Line 259 | void Daemon::headers(istream& sin, Envir
259                  {
260                          if (value == matcher) env.set("CONTENT_TYPE", matcher[1]);
261                  }
262 +                else if (name == "Host")
263 +                {
264 +                        Matcher matcher("^\\s*(.+?)(:[0-9]+)?\\s*$");
265 +
266 +                        if (value == matcher)
267 +                        {
268 +                                bool port(matcher.size() > 2);
269 +
270 +                                env.set("HTTP_HOST", matcher[1] + (port ? matcher[2] : ""));
271 +                                env.set("SERVER_NAME", matcher[1]);
272 +
273 +                                if (port) env.set("SERVER_PORT", matcher[2].substr(1));
274 +                        }
275 +                }
276                  else if (name == "Referer")
277                  {
278                          if (value == matcher) env.set("HTTP_REFERER", matcher[1]);
# Line 227 | Line 303 | Daemon::Status Daemon::message(istream&
303  
304          return ok;
305   }
230
231 string Daemon::reason(Status status)
232 {
233        ostringstream sout;
234
235        switch (status)
236        {
237        case ok:
238                sout << "OK";
239                break;
240        case found:
241                sout << "Found";
242                break;
243        case seeOther:
244                sout << "See Other";
245                break;
246        case bad:
247                sout << "Bad Request";
248                break;
249        case notFound:
250                sout << "Not Found";
251                break;
252        case lengthRequired:
253                sout << "Length Required";
254                break;
255        case mediaType:
256                sout << "Unsupported Media Type";
257                break;
258        case serverError:
259                sout << "Internal Server Error";
260                break;
261        case notImplemented:
262                sout << "Not Implemented";
263                break;
264        case version:
265                sout << "HTTP Version not supported";
266        }
267
268        return sout.str();
269 }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines