summaryrefslogtreecommitdiff
path: root/lib/HttpServ.cc
diff options
context:
space:
mode:
Diffstat (limited to 'lib/HttpServ.cc')
-rw-r--r--lib/HttpServ.cc55
1 files changed, 45 insertions, 10 deletions
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() {