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.hpp" |
18 |
|
#include "Person.hpp" |
19 |
|
|
20 |
|
class Daemon |
21 |
|
{ |
22 |
+ |
protected: |
23 |
+ |
struct Client |
24 |
+ |
{ |
25 |
+ |
api::InternetAddress ip; |
26 |
+ |
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 void handle(); |
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 |
< |
Daemon(int port) : port(port) {} |
38 |
> |
static string crlf; |
39 |
> |
string log; |
40 |
> |
Daemon(int port, const string& log) : port(port), log(log) {} |
41 |
|
void serve(bool fork, Daemon* self); |
42 |
+ |
istream& getline(istream& sin, string& line) { std::getline(sin, line, |
43 |
+ |
'\r'); sin.get(); return sin; } |
44 |
+ |
Status request(istream& sin, Environment& env, ostream& post, ostream& log); |
45 |
+ |
void response(ostream& sout, Status status); |
46 |
+ |
string reason(Status status); |
47 |
+ |
string server(); |
48 |
+ |
string server(const Environment& env); |
49 |
+ |
streamsize error(ostream& sout, Status status, const Environment& env); |
50 |
+ |
string date(bool log = false); |
51 |
|
public: |
52 |
< |
Daemon(int port, bool fork) : port(port) { serve(fork, this); } |
52 |
> |
Daemon(int port, bool fork, const string& log) : port(port), log(log) { |
53 |
> |
Person::configure(); serve(fork, this); } |
54 |
|
virtual ~Daemon() {} |
55 |
|
}; |
56 |
|
|