actualizado el servidor HTTP

This commit is contained in:
Luis Ángel San Martín 2013-06-13 13:53:34 +02:00
parent d4ef318190
commit dee244455e
6 changed files with 25 additions and 6 deletions

View File

@ -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;

View File

@ -9,10 +9,11 @@
#include <QCoreApplication>
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) {

View File

@ -61,7 +61,7 @@ private:
QSettings* settings;
/** Pool of connection handlers */
HttpConnectionHandlerPool pool;
HttpConnectionHandlerPool* pool;
signals:

View File

@ -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) {

View File

@ -121,7 +121,7 @@ private:
QMap<QByteArray,HttpCookie> 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.

View File

@ -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
}