diff options
Diffstat (limited to 'lib')
-rw-r--r-- | lib/ChainTasks.cc | 23 | ||||
-rw-r--r-- | lib/HttpServ.cc | 55 |
2 files changed, 68 insertions, 10 deletions
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() { |