Ok, but it is as I said.
Qt Code:
  1. WebProxy::WebProxy(QObject *parent,int port): QTcpServer(parent)
  2.  
  3. {
  4. qDebug()<<" Listen...";
  5. authMethod = "";
  6. proxyServer = new QTcpServer;
  7. if (!proxyServer->listen(QHostAddress::Any, port)) {
  8. emit error(1);
  9.  
  10. return;
  11. }
  12.  
  13. connect(proxyServer, SIGNAL(newConnection()), this, SLOT(go()));
  14. qDebug() << "Proxy server running at port" << proxyServer->serverPort() << proxyServer->serverAddress();
  15.  
  16. thr = 0;
  17. }
  18. void WebProxy::addFilter(const QHash<QString, QString> &user, const QHash<QString, QString> &pass, const QHash<QString, QString> &u_format, const QHash<QString, QString> &p_format, const QHash<QString, int> &dvr,const QHash<QString,QString > &submit, const QHash<QString,QString> &search)
  19. {
  20. qDebug() << "Server status..............";
  21. qDebug() << "Server status"<<proxyServer->isListening();
  22. hashUsername = user;
  23. hashPassword = pass;
  24. hashUFormat = u_format;
  25. hashPFormat = p_format;
  26. hashDvr = dvr;
  27. hashSubmit = submit;
  28. hashSearch = search;
  29.  
  30.  
  31. }
  32.  
  33. void WebProxy::setAddress(const QHostAddress &a, int port)
  34. {
  35. proxyAddress = a;
  36. proxyPort = port;
  37. port = port < 0 ? 8081 : port;
  38.  
  39.  
  40. }
  41. void WebProxy::go()
  42. {
  43. // Create a new Thread
  44. int sck = socketDescriptor();
  45. WebProxyThread *wpt = new WebProxyThread(sck,
  46. hashUsername,
  47. hashPassword,
  48. hashUFormat,
  49. hashPFormat,
  50. hashDvr,
  51. hashSubmit,
  52. hashSearch,this);
  53. connect(wpt, SIGNAL(finished()), wpt, SLOT(deleteLater()));
  54. wpt->start();
  55. qDebug() << "New thread"<<thr++;
  56.  
  57.  
  58. }
To copy to clipboard, switch view to plain text mode 

and the threaded proxy is in another class:
Qt Code:
  1. #include "webproxythreaded.h"
  2. #include <QTcpServer>
  3. #include <QTcpSocket>
  4. #include <QtGui>
  5.  
  6.  
  7. WebProxyThread::WebProxyThread(int socketDescriptor,
  8. const QHash<QString, QString> &user,
  9. const QHash<QString, QString> &pass,
  10. const QHash<QString, QString> &u_format,
  11. const QHash<QString, QString> &p_format,
  12. const QHash<QString, int> &dvr,
  13. const QHash<QString, QString> &submit_str,
  14. const QHash<QString, QString> &submit_search,
  15. QObject *parent) :
  16. QThread(parent), socketDescriptor(socketDescriptor)
  17. {
  18. hashUsername = user;
  19. hashPassword = pass;
  20. hashUFormat = u_format;
  21. hashPFormat = p_format;
  22. hashDvr = dvr;
  23. hashSubmit = submit_str;
  24. hashSearch = submit_search;
  25. }
  26.  
  27.  
  28.  
  29.  
  30. void WebProxyThread::run()
  31. {
  32. QTcpServer *proxyServer = qobject_cast<QTcpServer*>(sender());
  33. QTcpSocket *socket = proxyServer->nextPendingConnection();
  34. connect(socket, SIGNAL(readyRead()), this, SLOT(processQuery()));
  35. connect(socket, SIGNAL(disconnected()), socket, SLOT(deleteLater()));
  36.  
  37. qDebug()<<"New connection started..run." ;
  38. //! [1] //! [2]
  39.  
  40.  
  41.  
  42.  
  43. }
  44. void WebProxyThread::manageQuery() {
  45. QTcpServer *proxyServer = qobject_cast<QTcpServer*>(sender());
  46. QTcpSocket *socket = proxyServer->nextPendingConnection();
  47. connect(socket, SIGNAL(readyRead()), this, SLOT(processQuery()));
  48. connect(socket, SIGNAL(disconnected()), socket, SLOT(deleteLater()));
  49. qDebug()<<"New connection started..."<<socket->peerAddress();
  50. }
  51. QUrl WebProxyThread::getUrl(QList<QByteArray > &entries)
  52. {
  53.  
  54. QByteArray method = entries.value(0);
  55. QByteArray address = entries.value(1);
  56. QByteArray version = entries.value(2);
  57.  
  58. qDebug()<<method;
  59. qDebug()<<address;
  60. qDebug()<<version;
  61. QUrl url = QUrl::fromEncoded(address);
  62. if (!url.isValid()) {
  63.  
  64. qWarning() << "Invalid URL:" << url;
  65.  
  66. return QString();
  67. }
  68.  
  69.  
  70. return url;
  71. }
  72.  
  73. void WebProxyThread::processQuery() {
  74.  
  75.  
  76. QTcpSocket *socket = qobject_cast<QTcpSocket*>(sender());
  77. QByteArray requestData = socket->readAll();
  78. int pos = requestData.indexOf("\r\n");
  79. QByteArray requestLine = requestData.left(pos);
  80. requestData.remove(0, pos + 2);
  81. QList<QByteArray> entries = requestLine.split(' ');
  82. QByteArray method = entries.value(0);
  83. QByteArray address = entries.value(1);
  84. QByteArray version = entries.value(2);
  85.  
  86. QUrl url = QUrl::fromEncoded(address);
  87. if (!url.isValid()) {
  88. qWarning() << "Invalid URL:" << url;
  89. socket->disconnectFromHost();
  90. return;
  91. }
  92.  
  93. QString host = url.host();
  94.  
  95. int port = (url.port() <= 0) ? 80 : url.port();
  96. QByteArray req = url.encodedPath();
  97. if (url.hasQuery())
  98. req.append('?').append(url.encodedQuery());
  99.  
  100. requestLine = method + " " + req + " " + version + "\r\n";
  101. requestData.prepend(requestLine);
  102. QString key = host + ':' + QString::number(port);
  103. QTcpSocket *proxySocket = socket->findChild<QTcpSocket*>(key);
  104. if (proxySocket) {
  105. proxySocket->setObjectName(key);
  106. proxySocket->setProperty("url", url);
  107. proxySocket->setProperty("requestData", requestData);
  108. proxySocket->write(requestData);
  109. } else {
  110. proxySocket = new QTcpSocket(socket);
  111. proxySocket->setObjectName(key);
  112. proxySocket->setProperty("url", url);
  113. proxySocket->setProperty("requestData", requestData);
  114. connect(proxySocket, SIGNAL(connected()), this, SLOT(sendRequest()));
  115. connect(proxySocket, SIGNAL(readyRead()), this, SLOT(transferData()));
  116. connect(proxySocket, SIGNAL(disconnected()), this, SLOT(closeConnection()));
  117. connect(proxySocket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(closeConnection()));
  118. proxySocket->connectToHost(host, port);
  119. }
  120. }
  121.  
  122. void WebProxyThread::sendRequest() {
  123. QTcpSocket *proxySocket = qobject_cast<QTcpSocket*>(sender());
  124. QByteArray requestData = proxySocket->property("requestData").toByteArray();
  125. proxySocket->write(requestData);
  126. }
  127.  
  128. void WebProxyThread::transferData() {
  129. QTcpSocket *proxySocket = qobject_cast<QTcpSocket*>(sender());
  130. QByteArray data = proxySocket->readAll();
  131. QString host = proxySocket->peerAddress().toString();
  132. QByteArray filtered(data);
  133. if (hashDvr.value(host) == 1)
  134. {
  135.  
  136. QString name = hashUFormat.value(host);
  137. QString pass = hashPFormat.value(host);
  138. QString repU = "name=" + name + " value=" + hashUsername.value(host);
  139. QString repP = "name="+ pass + " value=" + hashPassword.value(host);
  140. QString submit = hashSubmit.value(host);
  141. QString search = hashSearch.value(host);
  142. filtered.replace("name=" + name,repU.toLocal8Bit());
  143. filtered.replace("name=" + pass,repP.toLocal8Bit());
  144. filtered.replace(search.toLocal8Bit(),submit.toLocal8Bit());
  145. }
  146. QTcpSocket *socket = qobject_cast<QTcpSocket*>(proxySocket->parent());
  147. socket->write(filtered);
  148. }
  149.  
  150. void WebProxyThread::closeConnection() {
  151. QTcpSocket *proxySocket = qobject_cast<QTcpSocket*>(sender());
  152. if (proxySocket) {
  153. QTcpSocket *socket = qobject_cast<QTcpSocket*>(proxySocket->parent());
  154. if (socket)
  155. socket->disconnectFromHost();
  156. if (proxySocket->error() != QTcpSocket::RemoteHostClosedError)
  157. qWarning() << "Error for:" << proxySocket->property("url").toUrl()
  158. << proxySocket->errorString();
  159. proxySocket->deleteLater();;
  160. }
  161. }
To copy to clipboard, switch view to plain text mode