diff options
Diffstat (limited to 'lib')
-rw-r--r-- | lib/CopyJob.cc | 4 | ||||
-rw-r--r-- | lib/HttpServ.cc | 102 | ||||
-rw-r--r-- | lib/ReadJob.cc | 2 | ||||
-rw-r--r-- | lib/Socket.cc | 4 | ||||
-rw-r--r-- | lib/Task.cc | 2 |
5 files changed, 80 insertions, 34 deletions
diff --git a/lib/CopyJob.cc b/lib/CopyJob.cc index e46e3da..fb8c6e2 100644 --- a/lib/CopyJob.cc +++ b/lib/CopyJob.cc @@ -1,7 +1,7 @@ #include "CopyJob.h" #include "General.h" -CopyJob::CopyJob(Handle * as, Handle * ad, ssize_t asiz) : s(as), d(ad), siz(asiz), cursiz(0), current(0) { } +CopyJob::CopyJob(Handle * as, Handle * ad, ssize_t asiz) : s(as), d(ad), siz(asiz), cursiz(0) { } CopyJob::~CopyJob() { } @@ -30,6 +30,8 @@ int CopyJob::Do() throw (GeneralException) { } cursiz += r; } + + return TASK_DONE; } String CopyJob::GetName() { 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; + } +} + diff --git a/lib/ReadJob.cc b/lib/ReadJob.cc index 2c244ad..6640d82 100644 --- a/lib/ReadJob.cc +++ b/lib/ReadJob.cc @@ -1,7 +1,7 @@ #include "ReadJob.h" #include "HttpServ.h" -ReadJob::ReadJob(Handle * as, Handle * ad) : s(as), d(ad), current(0) { } +ReadJob::ReadJob(Handle * as, Handle * ad) : s(as), d(ad) { } ReadJob::~ReadJob() { } diff --git a/lib/Socket.cc b/lib/Socket.cc index de6f42e..f3f4c0f 100644 --- a/lib/Socket.cc +++ b/lib/Socket.cc @@ -133,13 +133,13 @@ bool Socket::Listen(void) { return listening; } -Socket Socket::Accept(void) { +Socket Socket::Accept(void) throw (GeneralException) { struct sockaddr inaddr; socklen_t inlen = sizeof(inaddr); int h; if ((h = accept(GetHandle(), &inaddr, &inlen)) < 0) { - return Socket(); + throw GeneralException("Failed accepting."); } else { return Socket(h); } diff --git a/lib/Task.cc b/lib/Task.cc index fd1bf3f..063a983 100644 --- a/lib/Task.cc +++ b/lib/Task.cc @@ -4,7 +4,7 @@ #include "Task.h" #include "String.h" -Task::Task() : state(TASK_ON_HOLD), suspended(false) { +Task::Task() : state(TASK_ON_HOLD), suspended(false), current(0) { TaskMan::AddTask(this); } Task::~Task() { |