summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--include/HttpServ.h1
-rw-r--r--lib/HttpServ.cc76
2 files changed, 55 insertions, 22 deletions
diff --git a/include/HttpServ.h b/include/HttpServ.h
index 34de345..96d8016 100644
--- a/include/HttpServ.h
+++ b/include/HttpServ.h
@@ -26,6 +26,7 @@ class HttpResponse : public Base {
virtual ~HttpResponse();
Task * BuildResponse(Handle * out);
void PrepareResponse(Handle * b = 0);
+ bool Prepared();
String mime_type;
String location;
diff --git a/lib/HttpServ.cc b/lib/HttpServ.cc
index 4ca4a4c..57809fd 100644
--- a/lib/HttpServ.cc
+++ b/lib/HttpServ.cc
@@ -213,13 +213,16 @@ int ProcessRequest::Do() throw(GeneralException) {
if (a) a->Stop();
// std::cerr << "---- Sending header buffer.\n";
- c = new CopyJob(&b, &s, -1, false);
- WaitFor(c);
- current = 3;
- Suspend();
+ if (!d) {
+ c = new CopyJob(&b, &s, -1, false);
+ WaitFor(c);
+ current = 3;
+ Suspend();
+ }
case 3:
- delete c;
+ if (!d)
+ delete c;
if (a) {
// std::cerr << "---- Sending contents.\n";
@@ -498,6 +501,43 @@ String HttpServ::GetName() {
return String("Mini HTTP-Server '") + name + "'";
}
+class BuildHttpResponse : public Task {
+ public:
+ BuildHttpResponse(HttpResponse * _hr, Handle * _out) : hr(_hr), out(_out) { SetBurst(); }
+ virtual ~BuildHttpResponse() { }
+ virtual String GetName() { return "BuildHttpResponse"; }
+ virtual int Do() throw (GeneralException) {
+ Buffer * b;
+
+ switch (current) {
+ case 0:
+ if (hr->Prepared()) {
+ b = &hr->contents;
+ } else {
+ b = new Buffer();
+ hr->PrepareResponse(b);
+ t = new CopyJob(&hr->contents, b);
+ t->DryRun();
+ delete t;
+ }
+
+ t = new CopyJob(b, out, -1, b != &hr->contents);
+ current = 1;
+ WaitFor(t);
+ Suspend(TASK_ON_HOLD);
+
+ case 1:
+ delete t;
+ }
+
+ return TASK_DONE;
+ }
+ private:
+ HttpResponse * hr;
+ Handle * out;
+ Task * t;
+};
+
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) {
}
@@ -505,26 +545,15 @@ HttpResponse::~HttpResponse() {
}
Task * HttpResponse::BuildResponse(Handle * out) {
- 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);
+ Task * t;
+
+ t = new BuildHttpResponse(this, out);
if (builder) {
- l.push_back(t);
l.push_back(builder);
- return new ChainTasks(l);
+ l.push_back(t);
+ t = new ChainTasks(l);
}
return t;
@@ -575,7 +604,10 @@ void HttpResponse::PrepareResponse(Handle * b) {
}
(*b) << "\r\n";
-
+}
+
+bool HttpResponse::Prepared() {
+ return already_prepared;
}
String HttpResponse::code_to_string() {