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