diff options
-rw-r--r-- | include/ChainTasks.h | 21 | ||||
-rw-r--r-- | include/Domain.h | 2 | ||||
-rw-r--r-- | include/HttpServ.h | 6 | ||||
-rw-r--r-- | lib/ChainTasks.cc | 23 | ||||
-rw-r--r-- | lib/HttpServ.cc | 55 |
5 files changed, 94 insertions, 13 deletions
diff --git a/include/ChainTasks.h b/include/ChainTasks.h new file mode 100644 index 0000000..428c964 --- /dev/null +++ b/include/ChainTasks.h @@ -0,0 +1,21 @@ +#ifndef __CHAINTASKS_H__ +#define __CHAINTASKS_H__ + +#include <vector> + +#include <Task.h> + +class ChainTasks : public Task { + public: + typedef std::vector<Task *> tasklist_t; + ChainTasks(tasklist_t); + virtual ~ChainTasks(); + virtual String GetName(); + virtual int Do() throw (GeneralException); + private: + tasklist_t tasklist; + typedef std::vector<Task *>::iterator tasklist_iter_t; + tasklist_iter_t pos; +}; + +#endif diff --git a/include/Domain.h b/include/Domain.h index ef103c2..3a51d9f 100644 --- a/include/Domain.h +++ b/include/Domain.h @@ -10,7 +10,7 @@ class Domain : public Base { virtual ~Domain(); static Domain * find_domain(const String & url); void OnTop(); - virtual void Do(const HttpRequest &) = 0; + virtual void Do(const HttpRequest &, HttpResponse *) throw (GeneralException) = 0; private: Domain * find_domain_r(const String & url); static Domain * head; diff --git a/include/HttpServ.h b/include/HttpServ.h index aec3aad..34de345 100644 --- a/include/HttpServ.h +++ b/include/HttpServ.h @@ -25,6 +25,7 @@ class HttpResponse : public Base { HttpResponse(); virtual ~HttpResponse(); Task * BuildResponse(Handle * out); + void PrepareResponse(Handle * b = 0); String mime_type; String location; @@ -33,15 +34,17 @@ class HttpResponse : public Base { time_t last_modified; Buffer contents; bool cache; + Task * builder; private: String code_to_string(); + bool already_prepared; }; class HttpRequest : public Base { public: Variables * vars, * headers; - String url; + String uri; }; class Action; @@ -61,7 +64,6 @@ class HttpServ : public Task { String name; int localport; String root; - HttpResponse response; }; extern String endhl, endnl; diff --git a/lib/ChainTasks.cc b/lib/ChainTasks.cc new file mode 100644 index 0000000..033cadf --- /dev/null +++ b/lib/ChainTasks.cc @@ -0,0 +1,23 @@ +#include "ChainTasks.h" + + +ChainTasks::ChainTasks(tasklist_t _tasklist) : tasklist(_tasklist) { + SetBurst(); + pos = tasklist.begin(); +} + +ChainTasks::~ChainTasks() { +} + +String ChainTasks::GetName() { + return "ChainTask"; +} + +int ChainTasks::Do() throw (GeneralException) { + if (pos == tasklist.end()) + return TASK_DONE; + + WaitFor(*pos); + pos++; + return TASK_ON_HOLD; +} diff --git a/lib/HttpServ.cc b/lib/HttpServ.cc index 69d4b2f..4ca4a4c 100644 --- a/lib/HttpServ.cc +++ b/lib/HttpServ.cc @@ -7,6 +7,7 @@ #include "Buffer.h" #include "ReadJob.h" #include "CopyJob.h" +#include "ChainTasks.h" #include "Task.h" #include "Base64.h" #include "Domain.h" @@ -41,6 +42,8 @@ class ProcessRequest : public Task { String name, host, gvars, login, password, root; Variables * Vars, * Heads; bool bad, hasvars, post; + + HttpResponse response; }; ProcessRequest::ProcessRequest(Action * ap, const Socket & as, const String & aname, int aport, const String & aroot) : localport(aport), p(ap), s(as), name(aname), root(aroot) { @@ -196,7 +199,12 @@ int ProcessRequest::Do() throw(GeneralException) { std::cerr << _("File not found, error shown.\n"); } } else if (d) { -// d->Do(&response); + HttpRequest request; + request.vars = Vars; + request.headers = Heads; + request.uri = Uri; + d->Do(request, &response); + a = response.BuildResponse(&s); } else { ShowError(&b); } @@ -204,8 +212,6 @@ int ProcessRequest::Do() throw(GeneralException) { if (a) a->Stop(); - delete Vars; - delete Heads; // std::cerr << "---- Sending header buffer.\n"; c = new CopyJob(&b, &s, -1, false); WaitFor(c); @@ -225,6 +231,8 @@ int ProcessRequest::Do() throw(GeneralException) { case 4: if (a) delete a; + delete Vars; + delete Heads; // std::cerr << "---- End of Request.\n"; } return TASK_DONE; @@ -490,15 +498,47 @@ String HttpServ::GetName() { return String("Mini HTTP-Server '") + name + "'"; } -HttpResponse::HttpResponse() : mime_type("text/html; charset=iso8859-1"), location(""), server_name("GruiK Server v0.2"), return_code(HTTP_200_OK), last_modified(time(NULL)), cache(true) { +HttpResponse::HttpResponse() : mime_type("text/html; charset=iso8859-1"), location(""), server_name("GruiK Server v0.2"), return_code(HTTP_200_OK), last_modified(time(NULL)), cache(true), already_prepared(false), builder(0) { } HttpResponse::~HttpResponse() { } Task * HttpResponse::BuildResponse(Handle * out) { - Buffer * b = new Buffer(); + Buffer * b; Task * t; + ChainTasks::tasklist_t l; + + if (already_prepared) { + b = &contents; + } else { + b = new Buffer(); + PrepareResponse(b); + t = new CopyJob(&contents, b); + t->DryRun(); + delete t; + } + + t = new CopyJob(b, out, -1, b != &contents); + + if (builder) { + l.push_back(t); + l.push_back(builder); + return new ChainTasks(l); + } + + return t; +} + +void HttpResponse::PrepareResponse(Handle * b) { + if (already_prepared) + return; + + if (!b) + b = &contents; + + already_prepared = true; + char buf[1025]; time_t cur_time = time(NULL); @@ -536,11 +576,6 @@ Task * HttpResponse::BuildResponse(Handle * out) { (*b) << "\r\n"; - t = new CopyJob(&contents, b); - t->DryRun(); - delete t; - - return new CopyJob(b, out, -1, true); } String HttpResponse::code_to_string() { |