diff options
author | pixel <pixel> | 2007-04-23 15:00:49 +0000 |
---|---|---|
committer | pixel <pixel> | 2007-04-23 15:00:49 +0000 |
commit | cdc089b41563a216f9a1a9dfa27ba486d1484990 (patch) | |
tree | 723e8989383086b76a5a53f7220f1648801e9533 /lib/HttpServ.cc | |
parent | 6ee340fa32c42dfaa69604e67f9848c918675af7 (diff) |
First steps towards a new HTTP backend.
Diffstat (limited to 'lib/HttpServ.cc')
-rw-r--r-- | lib/HttpServ.cc | 76 |
1 files changed, 76 insertions, 0 deletions
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"; + } +} |