summaryrefslogtreecommitdiff
path: root/src/HttpServer.cc
diff options
context:
space:
mode:
authorTatsuhiro Tsujikawa <tatsuhiro.t@gmail.com>2009-01-25 10:55:27 +0000
committerTatsuhiro Tsujikawa <tatsuhiro.t@gmail.com>2009-01-25 10:55:27 +0000
commit5def96906fd17d9b48e9fda192617c6ad1b95683 (patch)
tree7907dd734fbaa2a43636c2a7fea342bb5ef773e5 /src/HttpServer.cc
parent0742e3921f185613f98c2b31cd03182d8aa1d6f5 (diff)
2009-01-25 Tatsuhiro Tsujikawa <t-tujikawa@users.sourceforge.net>
Persist connection between the built-in HTTP server and a client if a client supports keep-alive. Fixed the bug that aria2 exits when the HTTP server receives EOF from a client. * src/HttpServer.cc * src/HttpServer.h * src/HttpServerCommand.cc * src/HttpServerCommand.h * src/HttpServerResponseCommand.cc
Diffstat (limited to 'src/HttpServer.cc')
-rw-r--r--src/HttpServer.cc39
1 files changed, 33 insertions, 6 deletions
diff --git a/src/HttpServer.cc b/src/HttpServer.cc
index 14cf6192..cae0a076 100644
--- a/src/HttpServer.cc
+++ b/src/HttpServer.cc
@@ -39,6 +39,8 @@
#include "DlAbortEx.h"
#include "message.h"
#include "Util.h"
+#include "LogFactory.h"
+#include "Logger.h"
namespace aria2 {
@@ -47,7 +49,8 @@ HttpServer::HttpServer(const SharedHandle<SocketCore>& socket,
_socket(socket),
_socketBuffer(socket),
_e(e),
- _headerProcessor(new HttpHeaderProcessor())
+ _headerProcessor(new HttpHeaderProcessor()),
+ _logger(LogFactory::getInstance())
{}
HttpServer::~HttpServer() {}
@@ -69,16 +72,40 @@ SharedHandle<HttpHeader> HttpServer::receiveRequest()
size -= putbackDataLength;
_socket->readData(buf, size);
- return _headerProcessor->getHttpRequestHeader();
+ SharedHandle<HttpHeader> header = _headerProcessor->getHttpRequestHeader();
+ if(!header.isNull()) {
+ _logger->info("HTTP Server received request\n%s",
+ _headerProcessor->getHeaderString().c_str());
+ _lastRequestHeader = header;
+ _headerProcessor->clear();
+ }
+
+ return header;
+}
+
+bool HttpServer::supportsPersistentConnection() const
+{
+ std::string connection =
+ Util::toLower(_lastRequestHeader->getFirst(HttpHeader::CONNECTION));
+
+ return connection.find(HttpHeader::CLOSE) == std::string::npos &&
+ (_lastRequestHeader->getVersion() == HttpHeader::HTTP_1_1 ||
+ connection.find("keep-alive") != std::string::npos);
}
void HttpServer::feedResponse(const std::string& text)
{
- std::string header = "HTTP/1.0 200 OK\r\n"
+ std::string header = "HTTP/1.1 200 OK\r\n"
"Content-Type: text/html\r\n"
- "Content-Length: "+Util::uitos(text.size())+"\r\n"
- "Connection: close\r\n"
- "\r\n";
+ "Content-Length: "+Util::uitos(text.size())+"\r\n";
+
+ if(!supportsPersistentConnection()) {
+ header += "Connection: close\r\n";
+ }
+ header += "\r\n";
+
+ _logger->debug("HTTP Server sends response:\n%s", header.c_str());
+
_socketBuffer.feedSendBuffer(header);
_socketBuffer.feedSendBuffer(text);
}