PDA

View Full Version : Problems with development proxy server.



Xhunter
8th July 2012, 19:27
Hi. I decided to write a proxy server. The problems are - the application shortly after the launch fails - QThread: Destroyed while thread is still running, and in general a proxy is acting funny, retarding the page is refreshed.
Source code here - _http :/ / zalil.ru/33553766. Can anybody help me?

ChrisW67
8th July 2012, 23:54
Can anybody help me?
Probably not until you ask a smart question (http://www.catb.org/~esr/faqs/smart-questions.html#beprecise) and don't expect everyone else to download a RAR file from a Russian site (that is unlikely to be known and trusted outside Russia) just to have a chance of debugging your program code (http://www.catb.org/~esr/faqs/smart-questions.html#code).

Xhunter
10th July 2012, 09:51
Hi. The problem now was. Through a proxy I can get one site, if I go to the other site, I get 404 - Not Found, but if I restart the proxy and again I come to this second site, then it works fine, but if I once again I go to the next site, I get 404 - Not Found again. That is, it turns the proxy works for one site. I always clear out browser cache, internet works fine. I tested proxy in a few browsers, the result is the same. How can I fix it? Thanks. Source:

///////////////////
main.cpp

#include <QtGui/QApplication>
#include "dialog.h"

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
dialog d;
d.show();
return a.exec();
}
****************
dialog.h

#ifndef DIALOG_H
#define DIALOG_H

#include <QWidget>
#include "socketserver.h"
#include <QtGui>

class dialog : public QWidget
{
Q_OBJECT
public:
explicit dialog(QWidget *parent = 0);

private:
SocketServer m_SocketServer;

QLabel *statusLabel;
signals:

public slots:
};

#endif // DIALOG_H
***************
dialog.cpp

#include "dialog.h"
#include <QtGui>
#include <QtNetwork>

dialog::dialog(QWidget *parent) :
QWidget(parent)
{
if (!m_SocketServer.listen(QHostAddress::LocalHost,10 001)) {
QMessageBox::critical(this, tr("Proxy Sever"),
tr("Unable to start the server: %1.")
.arg(m_SocketServer.errorString()));
close();
return;
}

statusLabel = new QLabel();
statusLabel->setWordWrap(true);

QString str;
QTextStream(&str) << "Listent ip - ";
QTextStream(&str) << m_SocketServer.serverAddress().toString();
QTextStream(&str) << " \n\rPort - ";
QTextStream(&str) << m_SocketServer.serverPort();

statusLabel->setText(str);

QVBoxLayout * mainLayout = new QVBoxLayout;
mainLayout->addWidget(statusLabel);
setLayout(mainLayout);
}

Hi. The problem now was. Through a proxy I can get one site, if I go to the other site, I get 404 - Not Found, but if I restart the proxy and again I come to this second site, then it works fine, but if I once again I go to the next site, I get 404 - Not Found again. That is, it turns the proxy works for one site. I always clear out browser cache, internet works fine. I tested proxy in a few browsers, the result is the same. How can I fix it? Thanks. Source:

///////////////////
main.cpp

#include <QtGui/QApplication>
#include "dialog.h"

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
dialog d;
d.show();
return a.exec();
}
****************
dialog.h

#ifndef DIALOG_H
#define DIALOG_H

#include <QWidget>
#include "socketserver.h"
#include <QtGui>

class dialog : public QWidget
{
Q_OBJECT
public:
explicit dialog(QWidget *parent = 0);

private:
SocketServer m_SocketServer;

QLabel *statusLabel;
signals:

public slots:
};

#endif // DIALOG_H
***************
dialog.cpp

#include "dialog.h"
#include <QtGui>
#include <QtNetwork>

dialog::dialog(QWidget *parent) :
QWidget(parent)
{
if (!m_SocketServer.listen(QHostAddress::LocalHost,10 001)) {
QMessageBox::critical(this, tr("Proxy Sever"),
tr("Unable to start the server: %1.")
.arg(m_SocketServer.errorString()));
close();
return;
}

statusLabel = new QLabel();
statusLabel->setWordWrap(true);

QString str;
QTextStream(&str) << "Listent ip - ";
QTextStream(&str) << m_SocketServer.serverAddress().toString();
QTextStream(&str) << " \n\rPort - ";
QTextStream(&str) << m_SocketServer.serverPort();

statusLabel->setText(str);

QVBoxLayout * mainLayout = new QVBoxLayout;
mainLayout->addWidget(statusLabel);
setLayout(mainLayout);
}

***************************
socketserver.h

#ifndef SOCKETSERVER_H
#define SOCKETSERVER_H

#include <QTcpServer>
#include "socketclientthread.h"

class SocketServer : public QTcpServer
{
Q_OBJECT
public:
explicit SocketServer(QObject *parent = 0);
//QThread *thread;
protected:
void incomingConnection(int socketDescriptor);
void customEvent(QEvent * pe);
signals:
//quitThread();
public slots:

};

#endif // SOCKETSERVER_H
**************************
socketserver.cpp

#include "socketserver.h"
#include "socketclientthread.h"
#include "threadevent.h"

SocketServer::SocketServer(QObject *parent) :
QTcpServer(parent)
{
}

void SocketServer::incomingConnection(int socketDescriptor)
{
SocketClientThread * thread = new SocketClientThread(socketDescriptor,this);
//connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater()));

thread->start();
}

void SocketServer::customEvent(QEvent * pe)
{
// if ((int)pe->type() == ThreadEvent::ThreadType) {
// if(thread != NULL) thread->quit();
// }

//QWidget::customEvent(pe);
}

************************
socketclientthread.h

#ifndef SOCKETCLIENTTHREAD_H
#define SOCKETCLIENTTHREAD_H
#include <QThread>
#include <QtNetwork>
#include <QTcpSocket>
#include "socketserver.h"

class SocketClientThread : public QThread
{
public:

Q_OBJECT

public:
SocketClientThread(int socketDescriptor, QObject *parent);

void run();

signals:
void error(QTcpSocket::SocketError socketError);
public slots:
void slotReadClient1();
void slotReadClient2();
void disconnectedHandler1();
// void disconnectedHandler2();
private slots:
void errorHandler1(QAbstractSocket::SocketError);
void errorHandler2(QAbstractSocket::SocketError);

private:
int socketDescriptor;
QTcpSocket * m_tcpSocket1;
QTcpSocket * m_tcpSocket2;

public:
//SocketServer * m_socketServer;
QTcpSocket * m_socketServer;
QString getHost(QByteArray &arrayData);
};

#endif // SOCKETCLIENTTHREAD_H

*****************************
socketclientthread.cpp

#include "socketclientthread.h"

#include <QtNetwork>
#include <QApplication>
#include "threadevent.h"

SocketClientThread::SocketClientThread(int socketDescriptor, QObject *parent)
: QThread(parent), socketDescriptor(socketDescriptor),m_socketServer( (QTcpSocket * )parent)
{

}

QString SocketClientThread::getHost(QByteArray &arrayData)
{
QString host;
QString str = arrayData.data();
QRegExp rx("host\\s*:\\s*([A-Za-zА-Я0-9\.]+)[\n\r]+");

if(rx.indexIn(str.toLower()) != 1) host = rx.cap(1);

return host;
}

void SocketClientThread::run()
{
m_tcpSocket1 = new QTcpSocket();
m_tcpSocket2 = new QTcpSocket();

if (!m_tcpSocket1->setSocketDescriptor(socketDescriptor)) {
emit error(m_tcpSocket1->error());
return;
}

connect(m_tcpSocket1, SIGNAL(disconnected()),
m_tcpSocket1, SIGNAL(destroyed()));


connect(m_tcpSocket1, SIGNAL(disconnected()),
this, SIGNAL(finished()));

connect(m_tcpSocket1, SIGNAL(error(QAbstractSocket::SocketError)),
this, SLOT(errorHandler1(QAbstractSocket::SocketError))
,Qt::DirectConnection);

connect(m_tcpSocket1, SIGNAL(readyRead()),
this, SLOT(slotReadClient1())
,Qt::DirectConnection);


connect(m_tcpSocket2, SIGNAL(disconnected()),
m_tcpSocket2, SIGNAL(destroyed()));

connect(m_tcpSocket2, SIGNAL(readyRead()),
this, SLOT(slotReadClient2())
,Qt::DirectConnection);

connect(m_tcpSocket2, SIGNAL(error(QAbstractSocket::SocketError)),
this, SLOT(errorHandler2(QAbstractSocket::SocketError))
,Qt::DirectConnection);
exec();

}

void SocketClientThread::disconnectedHandler1()
{
// if(m_tcpSocket1 != NULL)
// {
// // m_tcpSocket1->deleteLater();
// m_tcpSocket1->close();
// delete m_tcpSocket1;
// }

// if(m_tcpSocket2 != NULL)
// {
// //m_tcpSocket2->deleteLater();
// m_tcpSocket2->close();
// delete m_tcpSocket2;

// }
// emit finished();
//this->exit(0);
// if(m_socketServer != NULL)
// {
// ThreadEvent * thEvent = new ThreadEvent() ;
// QApplication::postEvent(m_socketServer,thEvent);
// }
}


void SocketClientThread::errorHandler1(QAbstractSocket: :SocketError socketError)
{

QString filePath;
QTextStream(&filePath) << QDir::currentPath();
QTextStream(&filePath) << "\\errorsLog.txt";
QFile file(filePath);

file.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Append);

if(file.isOpen() && file.isWritable())
{
QString error = m_tcpSocket1->errorString();
error = "m_tcpSocket1 - " + error + "\n";

file.write(error.toLatin1().data());
file.close();
}

}

void SocketClientThread::errorHandler2(QAbstractSocket: :SocketError socketError)
{
QString filePath;
QTextStream(&filePath) << QDir::currentPath();
QTextStream(&filePath) << "\\errorsLog.txt";
QFile file(filePath);

file.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Append);

if(file.isOpen() && file.isWritable())
{
QString error = m_tcpSocket2->errorString();
error = "m_tcpSocket2 - " + error + "\n";
file.write(error.toLatin1().data());
file.close();
}
}

void SocketClientThread::slotReadClient1()
{
QByteArray arrayData = m_tcpSocket1->readAll();
QString filePath;
QTextStream(&filePath) << QDir::currentPath();
QTextStream(&filePath) << "\\logs.txt";
QFile file(filePath);

file.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Append);

if(file.isOpen() && file.isWritable())
{
file.write(arrayData);
file.close();
}

QString host;
QString str = arrayData.data();
host = getHost(arrayData);

if(m_tcpSocket2 == NULL)
{
m_tcpSocket2 = new QTcpSocket();

connect(m_tcpSocket2, SIGNAL(disconnected()),
m_tcpSocket2, SIGNAL(destroyed()));

connect(m_tcpSocket2, SIGNAL(readyRead()),
this, SLOT(slotReadClient2())
,Qt::DirectConnection);

connect(m_tcpSocket2, SIGNAL(error(QAbstractSocket::SocketError)),
this, SLOT(errorHandler2(QAbstractSocket::SocketError))
,Qt::DirectConnection);
}

if(!m_tcpSocket2->isOpen()) m_tcpSocket2->connectToHost(host,80);

if(m_tcpSocket2->isOpen())
{
m_tcpSocket2->write(arrayData);
}
}

void SocketClientThread::slotReadClient2()
{
QByteArray arrayData = m_tcpSocket2->readAll();
QString filePath;
QTextStream(&filePath) << QDir::currentPath();
QTextStream(&filePath) << "\\logs.txt";
QFile file(filePath);

file.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Append);

if(file.isOpen() && file.isWritable())
{
file.write(arrayData);
file.close();
}

m_tcpSocket1->write(arrayData);
}

***************************
threadevent.h

#ifndef THREADEVENT_H
#define THREADEVENT_H
#include <QtGui>

class ThreadEvent : public QEvent
{
public:
enum {ThreadType = User + 1};
ThreadEvent() : QEvent((Type)ThreadType)
{
}
};

#endif // THREADEVENT_H

*******************
ProxyServer.pro

TEMPLATE = app
SOURCES = main.cpp \
socketclientthread.cpp \
socketserver.cpp \
dialog.cpp \

QT += network
HEADERS = \
socketclientthread.h \
socketserver.h \
dialog.h \
threadevent.h

win32:TARGET = ProxyServer

ChrisW67
11th July 2012, 00:39
The 404 error is coming from which host? The first one you connect to, the second you attempt to connect to, or the proxy itself?

Without delving into the logic of your program I can see you have fallen into a common trap. Your SocketClientThread::slotReadClient1() is connected to readyRead() and assumes that it always receives a complete line for getHost() to match. This may not be the case, and if this happens you will discard the partial request you have received. You need to accumulate received bytes until you can see a full request.