mirror of
https://github.com/YACReader/yacreader
synced 2025-06-04 01:28:55 -04:00
actualizado el servidor HTTP
This commit is contained in:
parent
d4ef318190
commit
dee244455e
@ -31,6 +31,9 @@ HttpConnectionHandler::HttpConnectionHandler(QSettings* settings, HttpRequestHan
|
|||||||
|
|
||||||
|
|
||||||
HttpConnectionHandler::~HttpConnectionHandler() {
|
HttpConnectionHandler::~HttpConnectionHandler() {
|
||||||
|
socket.close();
|
||||||
|
quit();
|
||||||
|
wait();
|
||||||
qDebug("HttpConnectionHandler (%p): destroyed", this);
|
qDebug("HttpConnectionHandler (%p): destroyed", this);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -53,6 +56,7 @@ void HttpConnectionHandler::handleConnection(int socketDescriptor) {
|
|||||||
qDebug("HttpConnectionHandler (%p): handle new connection", this);
|
qDebug("HttpConnectionHandler (%p): handle new connection", this);
|
||||||
busy = true;
|
busy = true;
|
||||||
Q_ASSERT(socket.isOpen()==false); // if not, then the handler is already busy
|
Q_ASSERT(socket.isOpen()==false); // if not, then the handler is already busy
|
||||||
|
|
||||||
if (!socket.setSocketDescriptor(socketDescriptor)) {
|
if (!socket.setSocketDescriptor(socketDescriptor)) {
|
||||||
qCritical("HttpConnectionHandler (%p): cannot initialize socket: %s", this,qPrintable(socket.errorString()));
|
qCritical("HttpConnectionHandler (%p): cannot initialize socket: %s", this,qPrintable(socket.errorString()));
|
||||||
return;
|
return;
|
||||||
|
@ -9,10 +9,11 @@
|
|||||||
#include <QCoreApplication>
|
#include <QCoreApplication>
|
||||||
|
|
||||||
HttpListener::HttpListener(QSettings* settings, HttpRequestHandler* requestHandler, QObject *parent)
|
HttpListener::HttpListener(QSettings* settings, HttpRequestHandler* requestHandler, QObject *parent)
|
||||||
: QTcpServer(parent), pool(settings,requestHandler)
|
: QTcpServer(parent)
|
||||||
{
|
{
|
||||||
Q_ASSERT(settings!=0);
|
Q_ASSERT(settings!=0);
|
||||||
this->settings=settings;
|
this->settings=settings;
|
||||||
|
pool=new HttpConnectionHandlerPool(settings,requestHandler);
|
||||||
// Start listening
|
// Start listening
|
||||||
int port=settings->value("port",8080).toInt();
|
int port=settings->value("port",8080).toInt();
|
||||||
listen(QHostAddress::Any, port);
|
listen(QHostAddress::Any, port);
|
||||||
@ -34,6 +35,8 @@ HttpListener::HttpListener(QSettings* settings, HttpRequestHandler* requestHandl
|
|||||||
HttpListener::~HttpListener() {
|
HttpListener::~HttpListener() {
|
||||||
close();
|
close();
|
||||||
qDebug("HttpListener: closed");
|
qDebug("HttpListener: closed");
|
||||||
|
delete pool;
|
||||||
|
qDebug("HttpListener: destroyed");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -41,7 +44,7 @@ void HttpListener::incomingConnection(int socketDescriptor) {
|
|||||||
#ifdef SUPERVERBOSE
|
#ifdef SUPERVERBOSE
|
||||||
qDebug("HttpListener: New connection");
|
qDebug("HttpListener: New connection");
|
||||||
#endif
|
#endif
|
||||||
HttpConnectionHandler* freeHandler=pool.getConnectionHandler();
|
HttpConnectionHandler* freeHandler=pool->getConnectionHandler();
|
||||||
|
|
||||||
// Let the handler process the new connection.
|
// Let the handler process the new connection.
|
||||||
if (freeHandler) {
|
if (freeHandler) {
|
||||||
|
@ -61,7 +61,7 @@ private:
|
|||||||
QSettings* settings;
|
QSettings* settings;
|
||||||
|
|
||||||
/** Pool of connection handlers */
|
/** Pool of connection handlers */
|
||||||
HttpConnectionHandlerPool pool;
|
HttpConnectionHandlerPool* pool;
|
||||||
|
|
||||||
signals:
|
signals:
|
||||||
|
|
||||||
|
@ -56,14 +56,20 @@ void HttpResponse::writeHeaders() {
|
|||||||
sentHeaders=true;
|
sentHeaders=true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void HttpResponse::writeToSocket(QByteArray data) {
|
bool HttpResponse::writeToSocket(QByteArray data) {
|
||||||
int remaining=data.size();
|
int remaining=data.size();
|
||||||
char* ptr=data.data();
|
char* ptr=data.data();
|
||||||
while (socket->isOpen() && remaining>0) {
|
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;
|
ptr+=written;
|
||||||
remaining-=written;
|
remaining-=written;
|
||||||
}
|
}
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void HttpResponse::write(QByteArray data, bool lastPart) {
|
void HttpResponse::write(QByteArray data, bool lastPart) {
|
||||||
|
@ -121,7 +121,7 @@ private:
|
|||||||
QMap<QByteArray,HttpCookie> cookies;
|
QMap<QByteArray,HttpCookie> cookies;
|
||||||
|
|
||||||
/** Write raw data to the socket. This method blocks until all bytes have been passed to the TCP buffer */
|
/** 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.
|
Write the response HTTP status and headers to the socket.
|
||||||
|
@ -140,6 +140,12 @@ void StaticFileController::setContentType(QString fileName, HttpResponse& respon
|
|||||||
else if (fileName.endsWith(".html") || fileName.endsWith(".htm")) {
|
else if (fileName.endsWith(".html") || fileName.endsWith(".htm")) {
|
||||||
response.setHeader("Content-Type", qPrintable("text/html; charset="+encoding));
|
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
|
// Todo: add all of your content types
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user