Pastebin is 300% more awesome when you are logged in. Sign Up, it's FREE!

definition of my class httpproxythreadbrowser

By: trueleowdeo on Mar 9th, 2012  |  syntax: C++ (with QT extensions)  |  size: 5.83 KB  |  hits: 216  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #include "httpproxythreadbrowser.h"
  2. #include "proxyconstants.h"
  3.  
  4. HttpProxyThreadBrowser::HttpProxyThreadBrowser(QTcpSocket  outgoingSocket,QTcpSocket browserSocket,QObject *parent) :
  5.     QThread(parent)
  6. {
  7.     this->outgoingSocket.setSocketDescriptor(outgoingSocket.socketDescriptor());
  8.     this->browserSocket.setSocketDescriptor(browserSocket.socketDescriptor());
  9. }
  10.  
  11. void HttpProxyThreadBrowser::run(){
  12.     QByteArray outgoingdata;
  13.  
  14.     QDataStream in(&browserSocket);
  15.     QDataStream out(&outgoingdata, QIODevice::WriteOnly);
  16.     in.setVersion(QDataStream::Qt_4_0);
  17.     out.setVersion(QDataStream::Qt_4_0);
  18.  
  19.     QString header("");//incomming header from browser eg internet explorer, firefox, chrome ,etc
  20.     //its our duty to package this header and send it to our remote php script
  21.  
  22.     qDebug()<<"HttpProxyThreadBrowser started ...";
  23.     QByteArray proxyAuth("");
  24.     if(ORGANIZATION_HTTP_PROXY_HOST !=QString(""))
  25.     {
  26.         QByteArray authString= QString(ORGANIZATION_HTTP_PROXY_USER_NAME).toLatin1()
  27.                 + QString(":").toLatin1()
  28.                 + QString(ORGANIZATION_HTTP_PROXY_USER_PASS).toLatin1();
  29.         proxyAuth= QByteArray("Basic "+authString.toBase64());
  30.         qDebug()<<"Organisation http_proxy found. Auth string is "<<proxyAuth;
  31.     }
  32.     qint64 rdl;
  33.     while(true)//while browser is still sending us its request, read the request into incomingdata variable
  34.     {
  35.         if(!browserSocket.waitForReadyRead(30*1000))
  36.         {
  37.             return;//overwaited for browser request. Giving up (30 seconds)
  38.         }
  39.         if(browserSocket.bytesAvailable()< sizeof(qint64))continue;//wait for buffer to fill up
  40.         in>>rdl;//try to read data from browser
  41.         if(rdl == -1)break;//returns -1 when there is an error or data over
  42.  
  43.         header.append(char(rdl));
  44.         if(header.indexOf("\r\n\r\n") != -1) break;// in http , a header ends with double \r\n. So it means
  45.         //browser is done telling us its what it wants
  46.     }
  47.     qDebug()<<"INITIAL header LENGTH IS(cf with content-length:xxx) "<<header.length();
  48.     qDebug()<<"HttpProxyThreadBrowser request header is "<<header;
  49.     QString host="";
  50.     QString port="";
  51.     QString tmphost="";
  52.     int indexOf=header.toLower().indexOf("host:");
  53.     if(indexOf!= -1)
  54.     {
  55.         int immediateNewLineChar= header.toLower().indexOf("\r\n", header.toLower().indexOf("host:"));
  56.         tmphost= header.mid(indexOf + 5, immediateNewLineChar - indexOf);//QString.mid(startpos, numberofchars)
  57.         tmphost=tmphost.trimmed();
  58.         qDebug()<<"tmp host is "<<tmphost;
  59.         int isPortThere= tmphost.indexOf(":");
  60.         if(isPortThere != -1)
  61.         {
  62.             host= tmphost.mid(0,tmphost.indexOf(":"));
  63.             port= tmphost.mid(tmphost.indexOf(":")+1);
  64.             qDebug()<<"there is a port in this tmphost. True host is "<<host<<" and true port is "<<port;
  65.         }
  66.         else
  67.         {
  68.             port="80";
  69.             host=tmphost;
  70.         }
  71.  
  72.         //added since rapidshare not opening making it a relative request
  73.         QString modifyget= header.toLower();
  74.         if(modifyget.startsWith("get http://"))
  75.         {
  76.             int i2= modifyget.indexOf("/",11);
  77.             header= header.replace(4, i2, "");
  78.         }
  79.         if(modifyget.startsWith("post http://"))
  80.         {
  81.             int i2= modifyget.indexOf("/",12);
  82.             header= header.replace(5,i2,"");
  83.         }
  84.         qDebug()<<"modified header (bcoz of rapidshare not  working) is "<<header;
  85.  
  86.         QString proxyServerUrl= webPHP_URL_HTTP;//our php script
  87.         QString isSecure="";
  88.         QString proxyServerHost= webPHP_HOST_HTTP;
  89.         if(header.indexOf("X-IS-SSL-RECURSIVE:") == -1)
  90.         {
  91.             isSecure="N";
  92.         }
  93.         else
  94.         {
  95.             isSecure="Y";
  96.             // Now detect which Port 443 or 8443 ?
  97.             // Like : abcd X-IS-SSL-RECURSIVE: 8443
  98.             int p1 = header.indexOf("X-IS-SSL-RECURSIVE: ");
  99.             port = header.mid(p1 + 20, 4);
  100.         }
  101.         qDebug()<<"isSecure= "<<isSecure<<" Port is "<<port;
  102.         qDebug()<<"----------HttpProxyThreadBrowser started. Host="<<host<<" Port="<<port<<" ProxyServerUrl="<<proxyServerUrl<<" headerHost= "<<proxyServerHost;
  103.         //get content length
  104.         QString contentLength="";
  105.         int contentIdx= header.toLower().indexOf("content-length: ");
  106.         if (contentIdx != -1)
  107.         {
  108.             int endI = header.indexOf("\r\n", contentIdx + 17);
  109.             contentLength = header.mid(contentIdx + 16, endI - (contentIdx+16));
  110.             qDebug()<<"contentLength is "<<contentLength;
  111.         }
  112.         QString data= header;
  113.         //data= data.replace("\r\n\r\n","\r\nConnection: Close\r\n\r\n");//emulate replacefirst 3with regex todo fix regex
  114.         data = data.replace("keep-alive", "Close");
  115.  
  116.         int totallength = 0;
  117.         if (contentLength != "") {//removed 61+1 coz don know what its doing
  118.             totallength = contentLength.toInt() + data.length();
  119.         } else {
  120.             totallength = data.length();
  121.         }
  122.         QString header1 = "";
  123.         header1 = header1 + "POST " + proxyServerUrl + " HTTP/1.1\r\n";
  124.         header1 = header1 + "Host: " + proxyServerHost + "\r\n";
  125.         header1 = header1 + "Connection: Close\r\n";
  126.         header1 = header1 + "Content-Length: \r\n";
  127.         header1 = header1 + "Cache-Control: no-cache\r\n";
  128.  
  129.         //now insert total content length
  130.         totallength=totallength+header1.length();
  131.         header1= header1.insert(header1.toLower().indexOf("content-length: "), QString::number(double(totallength)));
  132.         qDebug()<<"FIXED header1 ="<<header1;
  133.  
  134.         if (ORGANIZATION_HTTP_PROXY_USER_NAME !=QString("")) {
  135.             header1 = header1 + "Proxy-Authorization: " + proxyAuth
  136.                     + "\r\n";
  137.         }
  138.  
  139.  
  140.     }
  141. }