1 |
Douglas Thrift |
164 |
// Smersh |
2 |
|
|
// |
3 |
|
|
// Douglas Thrift |
4 |
|
|
// |
5 |
|
|
// $Id$ |
6 |
|
|
|
7 |
|
|
#ifndef _Daemon_hpp_ |
8 |
|
|
#define _Daemon_hpp_ |
9 |
|
|
|
10 |
Douglas Thrift |
180 |
#include <arpa/inet.h> |
11 |
|
|
#include <sys/utsname.h> |
12 |
Douglas Thrift |
172 |
#include <menes-api/socket.hpp> |
13 |
Douglas Thrift |
173 |
#include <menes-api/threads.hpp> |
14 |
Douglas Thrift |
174 |
#include <menes-etl/fnbind.hpp> |
15 |
|
|
#include <menes-ios/stdadapters.hpp> |
16 |
Douglas Thrift |
164 |
|
17 |
Douglas Thrift |
179 |
#include "Matcher.hpp" |
18 |
Douglas Thrift |
172 |
#include "Person.hpp" |
19 |
|
|
|
20 |
Douglas Thrift |
164 |
class Daemon |
21 |
|
|
{ |
22 |
Douglas Thrift |
179 |
protected: |
23 |
|
|
struct Client { api::TcpSocket socket; api::InternetAddress ip; Client() : |
24 |
|
|
ip(api::InternetAddress::Any) {} }; |
25 |
Douglas Thrift |
175 |
enum Status { ok = 200, found = 302, seeOther, bad = 400, notFound = 404, |
26 |
Douglas Thrift |
176 |
lengthRequired = 411, mediaType = 415, serverError = 500, |
27 |
|
|
notImplemented, version = 505 }; |
28 |
|
|
private: |
29 |
Douglas Thrift |
181 |
int port; |
30 |
Douglas Thrift |
180 |
virtual int handle(Client* client); |
31 |
|
|
void headers(istream& sin, Environment& env); |
32 |
Douglas Thrift |
176 |
Status message(istream& sin, Environment& env, ostream& post); |
33 |
|
|
protected: |
34 |
Douglas Thrift |
175 |
static string crlf; |
35 |
Douglas Thrift |
177 |
string log; |
36 |
Douglas Thrift |
181 |
Daemon(int port, const string& log) : port(port), log(log) {} |
37 |
|
|
void serve(bool fork, Daemon* self); |
38 |
Douglas Thrift |
178 |
istream& getline(istream& sin, string& line) { std::getline(sin, line, |
39 |
|
|
'\r'); sin.get(); return sin; } |
40 |
Douglas Thrift |
175 |
Status request(istream& sin, Environment& env, ostream& post, ostream& log); |
41 |
Douglas Thrift |
176 |
void response(ostream& sout, Status status); |
42 |
Douglas Thrift |
180 |
string reason(Status status); |
43 |
Douglas Thrift |
178 |
string server(); |
44 |
Douglas Thrift |
181 |
string server(const Environment& env); |
45 |
|
|
streamsize error(ostream& sout, Status status, const Environment& env); |
46 |
Douglas Thrift |
176 |
string date(bool log = false); |
47 |
Douglas Thrift |
164 |
public: |
48 |
Douglas Thrift |
181 |
Daemon(int port, bool fork, const string& log) : port(port), log(log) { |
49 |
|
|
Person::configure(); serve(fork, this); } |
50 |
Douglas Thrift |
172 |
virtual ~Daemon() {} |
51 |
Douglas Thrift |
164 |
}; |
52 |
|
|
|
53 |
|
|
#endif // _Daemon_hpp_ |