#include "thread.h"
extern quint64 Served;
// Constructor
Thread
::Thread(int socketDescriptor,
QObject *parent
) : QThread(parent
), socketDescriptor
(socketDescriptor
) {
this->isTLS = 0;
this->setTerminationEnabled(true);
qDebug() << "Socket" << socketDescriptor;
//qDebug()<< QThread::idealThreadCount();
}
void Thread::test(){
qDebug() << "Finished";
}
// When its a normal connection
bool Thread::NormalSocket(){
// Create the Socket
socket = new QSslSocket;
if (!socket->setSocketDescriptor(socketDescriptor)) {
emit error(socket->error());
delete socket;
return false;
}
//qDebug() << "In Normal Socket";
return true;
}
// SSL Connection hence SSL Socket
bool Thread::SSLSocket(){
// Create the Socket
socket = new QSslSocket;
//qDebug() << Certificate;
//qDebug() << SslKey;
connect(socket, SIGNAL(encrypted()),
this, SLOT(handshakeComplete()));
connect(socket, SIGNAL(sslErrors(QList<QSslError>)),
this, SLOT(sslErrors(QList<QSslError>)));
// Set the protocol and certificates
socket->setProtocol(QSsl::AnyProtocol);
socket->setLocalCertificate(Certificate);
socket->setPrivateKey(SslKey);
// Set the descriptor
if (!socket->setSocketDescriptor(socketDescriptor)) {
emit error(socket->error());
delete socket;
return false;
}
// No need to verify the peer
socket->setPeerVerifyMode(QSslSocket::VerifyNone);
// Start the encryption
socket->startServerEncryption();
// Wait for the excryption to start
socket->waitForEncrypted(1000);
return true;
}
// Called when the thread is started
void Thread::run(){
//this->exec();
Served++;
qDebug() << "Num Req : " << Served;
// SSL Socket Start
if(isTLS > 0){
qDebug() << "SSL";
if(!SSLSocket()){
return;
}
// Normal Socket
}else{
if(!NormalSocket()){
return;
}
}
// Wait for it to become read ready for a MAX of 1 second
socket->waitForReadyRead(1000);
//qDebug()<<"Wait";
QMap<QByteArray, QByteArray> headers;
//qDebug() << "Enc : " << socket->isEncrypted();
//qDebug() << "Errors : " << socket->sslErrors();
/////////////////////////////////////
// Determine its an DATA / HTTP Call
/////////////////////////////////////
// Get the Request Header
req = socket->read(81920);
//req = socket->readAll();
//qDebug() << "Req : " << req;
"Content-Type: text/html; charset=\"utf-8\"\r\n"
"\r\n"
"<form method=\"post\" action=\"\" name=\"input\">"
"<input type=\"text\" value=\"test\">"
"<input type=\"submit\" value=\"test\">"
"</form>\n"
"Test"
"\r\n"
"\r\n"
"\r\n";
this->write(block);
/*unsigned char * test = (unsigned char *)malloc(134217728);
this->sleep(10);
free(test);*/
//this->write(block);
cleanup();
//delete socket;
//emit finished();
}
socket->write(text);
return socket->flush();
}
void Thread::cleanup(){
socket->disconnectFromHost();
socket->waitForDisconnected();
}
//qDebug() << "Cleaned";
// Clear the socket
delete socket;
// Delete the thread
//this->exit(0);
//this->wait();
//this->deleteLater();
}
if(outputStarted){
return false;
}
return this->socketWrite(text);
}
// Set that output has been started
outputStarted = true;
return this->socketWrite(text);
}
// Set that output has been started
outputStarted = true;
return this->socketWrite(text);
}
// Called when there are SSL errors
void Thread::sslErrors(const QList<QSslError> &errors){
foreach (const QSslError &error, errors){
qDebug() << "[Backup::sslErrors] " << error.errorString();
}
// Ignore Errors
//socket->ignoreSslErrors();
}
// SSL handshake complete
void Thread::handshakeComplete(){
qDebug() << "[Thread::handshakeComplete]";
}