mirror of
https://github.com/YACReader/yacreader
synced 2025-07-14 02:54:46 -04:00
Update QtWebapp to 1.7.11
This commit is contained in:
@ -6,8 +6,10 @@
|
||||
#include "httpconnectionhandler.h"
|
||||
#include "httpresponse.h"
|
||||
|
||||
HttpConnectionHandler::HttpConnectionHandler(QSettings* settings, HttpRequestHandler* requestHandler, QSslConfiguration* sslConfiguration)
|
||||
: QThread()
|
||||
using namespace stefanfrings;
|
||||
|
||||
HttpConnectionHandler::HttpConnectionHandler(const QSettings *settings, HttpRequestHandler *requestHandler, const QSslConfiguration* sslConfiguration)
|
||||
: QObject()
|
||||
{
|
||||
Q_ASSERT(settings!=nullptr);
|
||||
Q_ASSERT(requestHandler!=nullptr);
|
||||
@ -17,43 +19,56 @@ HttpConnectionHandler::HttpConnectionHandler(QSettings* settings, HttpRequestHan
|
||||
currentRequest=nullptr;
|
||||
busy=false;
|
||||
|
||||
// Create TCP or SSL socket
|
||||
createSocket();
|
||||
// execute signals in a new thread
|
||||
thread = new QThread();
|
||||
thread->start();
|
||||
qDebug("HttpConnectionHandler (%p): thread started", static_cast<void*>(this));
|
||||
moveToThread(thread);
|
||||
readTimer.moveToThread(thread);
|
||||
readTimer.setSingleShot(true);
|
||||
|
||||
// execute signals in my own thread
|
||||
moveToThread(this);
|
||||
socket->moveToThread(this);
|
||||
readTimer.moveToThread(this);
|
||||
// Create TCP or SSL socket
|
||||
createSocket();
|
||||
socket->moveToThread(thread);
|
||||
|
||||
// Connect signals
|
||||
connect(socket, SIGNAL(readyRead()), SLOT(read()));
|
||||
connect(socket, SIGNAL(disconnected()), SLOT(disconnected()));
|
||||
connect(&readTimer, SIGNAL(timeout()), SLOT(readTimeout()));
|
||||
readTimer.setSingleShot(true);
|
||||
connect(thread, SIGNAL(finished()), this, SLOT(thread_done()));
|
||||
|
||||
qDebug("HttpConnectionHandler (%p): constructed", this);
|
||||
this->start();
|
||||
qDebug("HttpConnectionHandler (%p): constructed", static_cast<void*>(this));
|
||||
}
|
||||
|
||||
|
||||
void HttpConnectionHandler::thread_done()
|
||||
{
|
||||
readTimer.stop();
|
||||
socket->close();
|
||||
delete socket;
|
||||
qDebug("HttpConnectionHandler (%p): thread stopped", static_cast<void*>(this));
|
||||
}
|
||||
|
||||
|
||||
HttpConnectionHandler::~HttpConnectionHandler()
|
||||
{
|
||||
quit();
|
||||
wait();
|
||||
qDebug("HttpConnectionHandler (%p): destroyed", this);
|
||||
thread->quit();
|
||||
thread->wait();
|
||||
thread->deleteLater();
|
||||
qDebug("HttpConnectionHandler (%p): destroyed", static_cast<void*>(this));
|
||||
}
|
||||
|
||||
|
||||
void HttpConnectionHandler::createSocket()
|
||||
{
|
||||
// If SSL is supported and configured, then create an instance of QSslSocket
|
||||
#ifndef QT_NO_OPENSSL
|
||||
#ifndef QT_NO_SSL
|
||||
if (sslConfiguration)
|
||||
{
|
||||
QSslSocket* sslSocket=new QSslSocket();
|
||||
sslSocket->setSslConfiguration(*sslConfiguration);
|
||||
socket=sslSocket;
|
||||
qDebug("HttpConnectionHandler (%p): SSL is enabled", this);
|
||||
qDebug("HttpConnectionHandler (%p): SSL is enabled", static_cast<void*>(this));
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
@ -62,27 +77,9 @@ void HttpConnectionHandler::createSocket()
|
||||
}
|
||||
|
||||
|
||||
void HttpConnectionHandler::run()
|
||||
{
|
||||
qDebug("HttpConnectionHandler (%p): thread started", this);
|
||||
try
|
||||
{
|
||||
exec();
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
qCritical("HttpConnectionHandler (%p): an uncatched exception occured in the thread",this);
|
||||
}
|
||||
socket->close();
|
||||
delete socket;
|
||||
readTimer.stop();
|
||||
qDebug("HttpConnectionHandler (%p): thread stopped", this);
|
||||
}
|
||||
|
||||
|
||||
void HttpConnectionHandler::handleConnection(tSocketDescriptor socketDescriptor)
|
||||
{
|
||||
qDebug("HttpConnectionHandler (%p): handle new connection", this);
|
||||
qDebug("HttpConnectionHandler (%p): handle new connection", static_cast<void*>(this));
|
||||
busy = true;
|
||||
Q_ASSERT(socket->isOpen()==false); // if not, then the handler is already busy
|
||||
|
||||
@ -93,16 +90,17 @@ void HttpConnectionHandler::handleConnection(tSocketDescriptor socketDescriptor)
|
||||
|
||||
if (!socket->setSocketDescriptor(socketDescriptor))
|
||||
{
|
||||
qCritical("HttpConnectionHandler (%p): cannot initialize socket: %s", this,qPrintable(socket->errorString()));
|
||||
qCritical("HttpConnectionHandler (%p): cannot initialize socket: %s",
|
||||
static_cast<void*>(this),qPrintable(socket->errorString()));
|
||||
return;
|
||||
}
|
||||
|
||||
#ifndef QT_NO_OPENSSL
|
||||
#ifndef QT_NO_SSL
|
||||
// Switch on encryption, if SSL is configured
|
||||
if (sslConfiguration)
|
||||
{
|
||||
qDebug("HttpConnectionHandler (%p): Starting encryption", this);
|
||||
((QSslSocket*)socket)->startServerEncryption();
|
||||
qDebug("HttpConnectionHandler (%p): Starting encryption", static_cast<void*>(this));
|
||||
(static_cast<QSslSocket*>(socket))->startServerEncryption();
|
||||
}
|
||||
#endif
|
||||
|
||||
@ -128,12 +126,12 @@ void HttpConnectionHandler::setBusy()
|
||||
|
||||
void HttpConnectionHandler::readTimeout()
|
||||
{
|
||||
qDebug("HttpConnectionHandler (%p): read timeout occured",this);
|
||||
qDebug("HttpConnectionHandler (%p): read timeout occured",static_cast<void*>(this));
|
||||
|
||||
//Commented out because QWebView cannot handle this.
|
||||
//socket->write("HTTP/1.1 408 request timeout\r\nConnection: close\r\n\r\n408 request timeout\r\n");
|
||||
|
||||
socket->flush();
|
||||
while(socket->bytesToWrite()) socket->waitForBytesWritten();
|
||||
socket->disconnectFromHost();
|
||||
delete currentRequest;
|
||||
currentRequest=nullptr;
|
||||
@ -142,7 +140,7 @@ void HttpConnectionHandler::readTimeout()
|
||||
|
||||
void HttpConnectionHandler::disconnected()
|
||||
{
|
||||
qDebug("HttpConnectionHandler (%p): disconnected", this);
|
||||
qDebug("HttpConnectionHandler (%p): disconnected", static_cast<void*>(this));
|
||||
socket->close();
|
||||
readTimer.stop();
|
||||
busy = false;
|
||||
@ -154,7 +152,7 @@ void HttpConnectionHandler::read()
|
||||
while (socket->bytesAvailable())
|
||||
{
|
||||
#ifdef SUPERVERBOSE
|
||||
qDebug("HttpConnectionHandler (%p): read input",this);
|
||||
qDebug("HttpConnectionHandler (%p): read input",static_cast<void*>(this));
|
||||
#endif
|
||||
|
||||
// Create new HttpRequest object if necessary
|
||||
@ -180,7 +178,7 @@ void HttpConnectionHandler::read()
|
||||
if (currentRequest->getStatus()==HttpRequest::abort)
|
||||
{
|
||||
socket->write("HTTP/1.1 413 entity too large\r\nConnection: close\r\n\r\n413 Entity too large\r\n");
|
||||
socket->flush();
|
||||
while(socket->bytesToWrite()) socket->waitForBytesWritten();
|
||||
socket->disconnectFromHost();
|
||||
delete currentRequest;
|
||||
currentRequest=nullptr;
|
||||
@ -191,7 +189,7 @@ void HttpConnectionHandler::read()
|
||||
if (currentRequest->getStatus()==HttpRequest::complete)
|
||||
{
|
||||
readTimer.stop();
|
||||
qDebug("HttpConnectionHandler (%p): received request",this);
|
||||
qDebug("HttpConnectionHandler (%p): received request",static_cast<void*>(this));
|
||||
|
||||
// Copy the Connection:close header to the response
|
||||
HttpResponse response(socket);
|
||||
@ -220,7 +218,8 @@ void HttpConnectionHandler::read()
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
qCritical("HttpConnectionHandler (%p): An uncatched exception occured in the request handler",this);
|
||||
qCritical("HttpConnectionHandler (%p): An uncatched exception occured in the request handler",
|
||||
static_cast<void*>(this));
|
||||
}
|
||||
|
||||
// Finalize sending the response if not already done
|
||||
@ -229,7 +228,7 @@ void HttpConnectionHandler::read()
|
||||
response.write(QByteArray(),true);
|
||||
}
|
||||
|
||||
qDebug("HttpConnectionHandler (%p): finished request",this);
|
||||
qDebug("HttpConnectionHandler (%p): finished request",static_cast<void*>(this));
|
||||
|
||||
// Find out whether the connection must be closed
|
||||
if (!closeConnection)
|
||||
@ -259,7 +258,7 @@ void HttpConnectionHandler::read()
|
||||
// Close the connection or prepare for the next request on the same connection.
|
||||
if (closeConnection)
|
||||
{
|
||||
socket->flush();
|
||||
while(socket->bytesToWrite()) socket->waitForBytesWritten();
|
||||
socket->disconnectFromHost();
|
||||
}
|
||||
else
|
||||
|
Reference in New Issue
Block a user