summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--include/HttpServ.h38
-rw-r--r--lib/HttpServ.cc76
2 files changed, 111 insertions, 3 deletions
diff --git a/include/HttpServ.h b/include/HttpServ.h
index fc58246..db454e0 100644
--- a/include/HttpServ.h
+++ b/include/HttpServ.h
@@ -4,15 +4,16 @@
#include <Socket.h>
#include <BString.h>
#include <Variables.h>
-#include <Action.h>
#include <Task.h>
+#include <Buffer.h>
+#include <Handle.h>
#include <Exceptions.h>
+class Action;
class HttpServ : public Task {
public:
- HttpServ(Action *, int = 1500, const String & = String("GruiK Server v0.1")) throw (GeneralException);
+ HttpServ(Action *, int = 1500, const String & = String("GruiK Server v0.2")) throw (GeneralException);
virtual ~HttpServ();
- void SetMenu(Action *);
virtual String GetName();
protected:
@@ -25,6 +26,37 @@ class HttpServ : public Task {
int localport;
};
+class HttpResponse : public Base {
+ public:
+ typedef enum {
+ HTTP_200_OK = 200,
+ HTTP_301_PERM_MOVED = 301,
+ HTTP_302_FOUND = 302,
+ HTTP_400_BAD_REQUEST = 400,
+ HTTP_401_UNAUTHORIZED = 401,
+ HTTP_403_FORBIDDEN = 403,
+ HTTP_404_NOT_FOUND = 404,
+ HTTP_500_INTERNAL_ERROR = 500,
+ HTTP_503_SERVICE_UNAVAILABLE = 503,
+ } return_code_t;
+ HttpResponse();
+ virtual ~HttpResponse();
+ Task * BuildResponse(Handle * out);
+
+ String mime_type;
+ String location;
+ String server_name;
+ return_code_t return_code;
+ time_t last_modified;
+ Buffer contents;
+ bool cache;
+
+ public:
+ String code_to_string();
+};
+
extern String endhl, endnl;
+#include <Action.h>
+
#endif
diff --git a/lib/HttpServ.cc b/lib/HttpServ.cc
index 37c1e28..1559efd 100644
--- a/lib/HttpServ.cc
+++ b/lib/HttpServ.cc
@@ -465,3 +465,79 @@ int HttpServ::Do() throw (GeneralException) {
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() {
+}
+
+Task * HttpResponse::BuildResponse(Handle * out) {
+ Buffer * b = new Buffer();
+ Task * t;
+ char buf[1025];
+
+ time_t cur_time = time(NULL);
+ struct tm * ft = gmtime(&cur_time);
+
+ strftime(buf, 1024, "%a, %d %b %Y %H:%M:%S GMT", ft);
+
+ (*b) << "HTTP/1.1 " << return_code << " " << code_to_string() << "\r\n"
+ << "Server: " << server_name << "\r\n"
+ << "Date: " << buf << "\r\n";
+
+ if (!cache)
+ (*b) << "Cache-Control: no-cache\r\n";
+
+ (*b) << "Connection: closed\r\n"
+ << "Content-Type: " << mime_type << "\r\n";
+
+
+ if (location != "") {
+ switch (return_code) {
+ case HTTP_301_PERM_MOVED:
+ case HTTP_302_FOUND:
+ (*b) << "Location: " << location << "\r\n";
+ break;
+ case HTTP_401_UNAUTHORIZED:
+ (*b) << "WWW-Authenticate: Basic realm=\"" << location << "\"\r\n";
+ break;
+ }
+ }
+ if (last_modified >=0) {
+ ft = gmtime(&last_modified);
+ strftime(buf, 1024, "%a, %d %b %Y %H:%M:%S GMT", ft);
+ (*b) << "Last-Modified: " << buf << "\r\n";
+ }
+
+ (*b) << "\r\n";
+
+ t = new CopyJob(&contents, b);
+ t->DryRun();
+ delete t;
+
+ return new CopyJob(b, out, -1, true);
+}
+
+String HttpResponse::code_to_string() {
+ switch (return_code) {
+ case HTTP_200_OK:
+ return "OK";
+ case HTTP_301_PERM_MOVED:
+ return "Moved Permanently";
+ case HTTP_302_FOUND:
+ return "Found";
+ case HTTP_400_BAD_REQUEST:
+ return "Bad Request";
+ case HTTP_401_UNAUTHORIZED:
+ return "Authorization Required";
+ case HTTP_403_FORBIDDEN:
+ return "Forbidden";
+ case HTTP_404_NOT_FOUND:
+ return "Not Found";
+ case HTTP_500_INTERNAL_ERROR:
+ return "Internal Error";
+ case HTTP_503_SERVICE_UNAVAILABLE:
+ return "Service Unavailable";
+ }
+}