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> |
22 |
|
protected: |
23 |
|
struct Client { api::TcpSocket socket; api::InternetAddress ip; Client() : |
24 |
|
ip(api::InternetAddress::Any) {} }; |
23 |
– |
private: |
24 |
– |
virtual int handle(Client* client); |
25 |
– |
void headers(istream& sin, Environment& env); |
26 |
– |
protected: |
25 |
|
enum Status { ok = 200, found = 302, seeOther, bad = 400, notFound = 404, |
26 |
|
lengthRequired = 411, mediaType = 415, serverError = 500, |
27 |
|
notImplemented, version = 505 }; |
28 |
|
private: |
29 |
+ |
int port; |
30 |
+ |
virtual int handle(Client* client); |
31 |
+ |
void headers(istream& sin, Environment& env); |
32 |
|
Status message(istream& sin, Environment& env, ostream& post); |
32 |
– |
string reason(Status status); |
33 |
|
protected: |
34 |
|
static string crlf; |
35 |
|
string log; |
36 |
< |
Daemon(const string& log) : log(log) {} |
37 |
< |
void serve(int port, bool fork, Daemon* self); |
36 |
> |
Daemon(int port, const string& log) : port(port), log(log) {} |
37 |
> |
void serve(bool fork, Daemon* self); |
38 |
|
istream& getline(istream& sin, string& line) { std::getline(sin, line, |
39 |
|
'\r'); sin.get(); return sin; } |
40 |
|
Status request(istream& sin, Environment& env, ostream& post, ostream& log); |
41 |
|
void response(ostream& sout, Status status); |
42 |
+ |
string reason(Status status); |
43 |
|
string server(); |
44 |
< |
streamsize error(ostream& sout, Status status); |
44 |
> |
string server(const Environment& env); |
45 |
> |
streamsize error(ostream& sout, Status status, const Environment& env); |
46 |
|
string date(bool log = false); |
47 |
|
public: |
48 |
< |
Daemon(int port, bool fork, const string& log) : log(log) { |
49 |
< |
Person::configure(); serve(port, fork, this); } |
48 |
> |
Daemon(int port, bool fork, const string& log) : port(port), log(log) { |
49 |
> |
Person::configure(); serve(fork, this); } |
50 |
|
virtual ~Daemon() {} |
51 |
|
}; |
52 |
|
|