// Smersh // // Douglas Thrift // // $Id$ #ifndef _Daemon_hpp_ #define _Daemon_hpp_ #include #include #include #include #include #include #include "Matcher/Matcher.hpp" #include "Person.hpp" class Daemon { protected: struct Client { api::InternetAddress ip; ext::StackReference socket; Client(api::TcpSocket& server) : socket(server, &ip) {} }; enum Status { ok = 200, found = 302, seeOther, bad = 400, notFound = 404, lengthRequired = 411, mediaType = 415, serverError = 500, notImplemented, version = 505 }; private: int port; virtual int handle(Client* client); void headers(istream& sin, Environment& env); Status message(istream& sin, Environment& env, ostream& post); protected: static string crlf; string log; void serve(Daemon* self); istream& getline(istream& sin, string& line) { std::getline(sin, line, '\r'); sin.get(); return sin; } Status request(istream& sin, Environment& env, ostream& post, ostream& log); void response(ostream& sout, Status status); string reason(Status status); string server(); string server(const Environment& env); streamsize error(ostream& sout, Status status, const Environment& env); string date(bool log = false); public: Daemon(int port, const string& log, bool run = true) : port(port), log(log) { if (run) { Person::configure(); serve(this); } } virtual ~Daemon() {} }; #endif // _Daemon_hpp_