diff --git a/YACReaderLibrary/server/lib/bfHttpServer/httpconnectionhandler.cpp b/YACReaderLibrary/server/lib/bfHttpServer/httpconnectionhandler.cpp index f3d2ef89..3ed0956c 100644 --- a/YACReaderLibrary/server/lib/bfHttpServer/httpconnectionhandler.cpp +++ b/YACReaderLibrary/server/lib/bfHttpServer/httpconnectionhandler.cpp @@ -31,6 +31,9 @@ HttpConnectionHandler::HttpConnectionHandler(QSettings* settings, HttpRequestHan HttpConnectionHandler::~HttpConnectionHandler() { + socket.close(); + quit(); + wait(); qDebug("HttpConnectionHandler (%p): destroyed", this); } @@ -53,6 +56,7 @@ void HttpConnectionHandler::handleConnection(int socketDescriptor) { qDebug("HttpConnectionHandler (%p): handle new connection", this); busy = true; Q_ASSERT(socket.isOpen()==false); // if not, then the handler is already busy + if (!socket.setSocketDescriptor(socketDescriptor)) { qCritical("HttpConnectionHandler (%p): cannot initialize socket: %s", this,qPrintable(socket.errorString())); return; diff --git a/YACReaderLibrary/server/lib/bfHttpServer/httplistener.cpp b/YACReaderLibrary/server/lib/bfHttpServer/httplistener.cpp index da26376f..dccdb33c 100644 --- a/YACReaderLibrary/server/lib/bfHttpServer/httplistener.cpp +++ b/YACReaderLibrary/server/lib/bfHttpServer/httplistener.cpp @@ -9,10 +9,11 @@ #include HttpListener::HttpListener(QSettings* settings, HttpRequestHandler* requestHandler, QObject *parent) - : QTcpServer(parent), pool(settings,requestHandler) + : QTcpServer(parent) { Q_ASSERT(settings!=0); this->settings=settings; + pool=new HttpConnectionHandlerPool(settings,requestHandler); // Start listening int port=settings->value("port",8080).toInt(); listen(QHostAddress::Any, port); @@ -34,6 +35,8 @@ HttpListener::HttpListener(QSettings* settings, HttpRequestHandler* requestHandl HttpListener::~HttpListener() { close(); qDebug("HttpListener: closed"); + delete pool; + qDebug("HttpListener: destroyed"); } @@ -41,7 +44,7 @@ void HttpListener::incomingConnection(int socketDescriptor) { #ifdef SUPERVERBOSE qDebug("HttpListener: New connection"); #endif - HttpConnectionHandler* freeHandler=pool.getConnectionHandler(); + HttpConnectionHandler* freeHandler=pool->getConnectionHandler(); // Let the handler process the new connection. if (freeHandler) { diff --git a/YACReaderLibrary/server/lib/bfHttpServer/httplistener.h b/YACReaderLibrary/server/lib/bfHttpServer/httplistener.h index 614e3c5e..c53d81bb 100644 --- a/YACReaderLibrary/server/lib/bfHttpServer/httplistener.h +++ b/YACReaderLibrary/server/lib/bfHttpServer/httplistener.h @@ -61,7 +61,7 @@ private: QSettings* settings; /** Pool of connection handlers */ - HttpConnectionHandlerPool pool; + HttpConnectionHandlerPool* pool; signals: diff --git a/YACReaderLibrary/server/lib/bfHttpServer/httpresponse.cpp b/YACReaderLibrary/server/lib/bfHttpServer/httpresponse.cpp index a1347d5c..434afc1e 100644 --- a/YACReaderLibrary/server/lib/bfHttpServer/httpresponse.cpp +++ b/YACReaderLibrary/server/lib/bfHttpServer/httpresponse.cpp @@ -56,14 +56,20 @@ void HttpResponse::writeHeaders() { sentHeaders=true; } -void HttpResponse::writeToSocket(QByteArray data) { +bool HttpResponse::writeToSocket(QByteArray data) { int remaining=data.size(); char* ptr=data.data(); while (socket->isOpen() && remaining>0) { - int written=socket->write(data); + // Wait until the previous buffer content is written out, otherwise it could become very large + socket->waitForBytesWritten(-1); + int written=socket->write(ptr,remaining); + if (written==-1) { + return false; + } ptr+=written; remaining-=written; } + return true; } void HttpResponse::write(QByteArray data, bool lastPart) { diff --git a/YACReaderLibrary/server/lib/bfHttpServer/httpresponse.h b/YACReaderLibrary/server/lib/bfHttpServer/httpresponse.h index 14c54a88..2450a319 100644 --- a/YACReaderLibrary/server/lib/bfHttpServer/httpresponse.h +++ b/YACReaderLibrary/server/lib/bfHttpServer/httpresponse.h @@ -121,7 +121,7 @@ private: QMap cookies; /** Write raw data to the socket. This method blocks until all bytes have been passed to the TCP buffer */ - void writeToSocket(QByteArray data); + bool writeToSocket(QByteArray data); /** Write the response HTTP status and headers to the socket. diff --git a/YACReaderLibrary/server/lib/bfHttpServer/staticfilecontroller.cpp b/YACReaderLibrary/server/lib/bfHttpServer/staticfilecontroller.cpp index 1a7b29ce..ec9b220f 100644 --- a/YACReaderLibrary/server/lib/bfHttpServer/staticfilecontroller.cpp +++ b/YACReaderLibrary/server/lib/bfHttpServer/staticfilecontroller.cpp @@ -140,6 +140,12 @@ void StaticFileController::setContentType(QString fileName, HttpResponse& respon else if (fileName.endsWith(".html") || fileName.endsWith(".htm")) { response.setHeader("Content-Type", qPrintable("text/html; charset="+encoding)); } + else if (fileName.endsWith(".css")) { + response.setHeader("Content-Type", "text/css"); + } + else if (fileName.endsWith(".js")) { + response.setHeader("Content-Type", "text/javascript"); + } // Todo: add all of your content types }