diff options
author | Pixel <Pixel> | 2001-11-14 22:25:39 +0000 |
---|---|---|
committer | Pixel <Pixel> | 2001-11-14 22:25:39 +0000 |
commit | 8694409f2a5531e9f3263e8a42248a3ca91ac14f (patch) | |
tree | b1bba533d7ee9f66b8f470851ff534beac97a7d6 /lib/HttpServ.cc | |
parent | aa92df3c58daefb994da555fb45f2e3ee55f38d5 (diff) |
More advances....
Diffstat (limited to 'lib/HttpServ.cc')
-rw-r--r-- | lib/HttpServ.cc | 102 |
1 files changed, 73 insertions, 29 deletions
diff --git a/lib/HttpServ.cc b/lib/HttpServ.cc index 3bec28f..7464e5b 100644 --- a/lib/HttpServ.cc +++ b/lib/HttpServ.cc @@ -3,38 +3,54 @@ #include "HttpServ.h" #include "Buffer.h" #include "ReadJob.h" +#include "Task.h" #include "config.h" String endhl = "\r\n", endnl = "\n"; -HttpServ::HttpServ(int port, const String & nname) throw (GeneralException) : name(nname), localport(port) { - bool r = true; - - r = Listener.SetLocal("", port); - if (r) { - r = Listener.Listen(); - } - - if (!r) { - throw GeneralException("Initialisation of the Mini HTTP-Server failed."); - } - - cerr << "Mini HTTP-Server '" << name << "' ready and listening for port " << port << endl; -} - -void HttpServ::MainLoop(Action * p) { - while (1) { - ProcessRequest(p, Listener.Accept()); - } -} +class ProcessRequest : public Task { + public: + ProcessRequest(Action *, const Socket &); + virtual ~ProcessRequest() {} + virtual String GetName(); + protected: + virtual int Do(); + private: + String GetMime(const String &); + bool ParseUri(String &, String &, Handle *); + void ParseVars(Handle *, int); + void ShowError(Handle *); + void SendHeads(Handle *, const String &); + void SendRedirect(Handle *); -void HttpServ::ProcessRequest(Action * p, Socket s) { String file, domain, t; Buffer b; - Task * c = new ReadJob(&s, &b); + Task * c; Action * f; int len; - + Action * p; + Socket s; + + String name; + Variables * Vars; + bool bad; + +}; + +ProcessRequest::ProcessRequest(Action * ap, const Socket & as) : p(ap), s(as) { } + +String ProcessRequest::GetName() { + return "Processing HTTP request"; +} + +int ProcessRequest::Do() { + c = new ReadJob(&s, &b); + +} + +void ProcessRequest::ProcessRequest(Action * p, Socket s) { + c = new ReadJob(&s, &b); + s.SetNonBlock(); c->Run(); @@ -126,7 +142,7 @@ void HttpServ::ProcessRequest(Action * p, Socket s) { cerr << "----\n"; } -void HttpServ::ParseVars(Handle * s, int len) { +void ProcessRequest::ParseVars(Handle * s, int len) { String t, v; char conv[3], l; int hconv, nbvars; @@ -186,7 +202,7 @@ void HttpServ::ParseVars(Handle * s, int len) { * c'est à dire la méthode demandée par le client. */ -bool HttpServ::ParseUri(String & file, String & domain, Handle * s) { +bool ProcessRequest::ParseUri(String & file, String & domain, Handle * s) { String t, Uri; bool post = false; const char * p = NULL; @@ -245,7 +261,7 @@ bool HttpServ::ParseUri(String & file, String & domain, Handle * s) { /* * Ceci sert à rediriger le navigateur vers l'url de démarrage. */ -void HttpServ::SendRedirect(Handle * s) { +void ProcessRequest::SendRedirect(Handle * s) { *s << "HTTP/1.1 301 Moved Permanently" << endhl << "Server: " << name << endhl << "Location: http://127.0.0.1:" << localport << "/bin/start" << endhl << @@ -262,7 +278,7 @@ void HttpServ::SendRedirect(Handle * s) { * Nous envoyons les entetes de réponse HTTP. */ -void HttpServ::SendHeads(Handle * s, const String & mime) { +void ProcessRequest::SendHeads(Handle * s, const String & mime) { *s << "HTTP/1.1 200 OK" << endhl << "Server: " << name << endhl << "Cache-Control: no-cache" << endhl << @@ -274,7 +290,7 @@ void HttpServ::SendHeads(Handle * s, const String & mime) { * Affichage d'une erreur 404. */ -void HttpServ::ShowError(Handle * s) { +void ProcessRequest::ShowError(Handle * s) { *s << "HTTP/1.1 404 Not Found" << endhl << "Server: " << name << endhl << "Cache-Control: no-cache" << endhl << @@ -291,7 +307,7 @@ void HttpServ::ShowError(Handle * s) { * Par défaut, nous mettons "text/plain". */ -String HttpServ::GetMime(const String & f) { +String ProcessRequest::GetMime(const String & f) { String ext; size_t ppos; @@ -309,3 +325,31 @@ String HttpServ::GetMime(const String & f) { return "text/plain"; } + +HttpServ::HttpServ(int port, const String & nname) throw (GeneralException) : name(nname), localport(port) { + bool r = true; + + r = Listener.SetLocal("", port); + if (r) { + r = Listener.Listen(); + } + + if (!r) { + throw GeneralException("Initialisation of the Mini HTTP-Server failed."); + } + + r.SetNonBlock(); + + cerr << "Mini HTTP-Server '" << name << "' ready and listening for port " << port << endl; +} + +int HttpServ::Do(Action * p) { + try { + Socket s = Listener.Accept(); + new ProcessRequest(p, Listener.Accept()); + } + catch (GeneralException) { + return TASK_ON_HOLD; + } +} + |