#include "httpproxythreadbrowser.h"
#include "proxyconstants.h"
HttpProxyThreadBrowser::HttpProxyThreadBrowser(QTcpSocket outgoingSocket,QTcpSocket browserSocket,QObject *parent) :
QThread(parent)
{
this->outgoingSocket.setSocketDescriptor(outgoingSocket.socketDescriptor());
this->browserSocket.setSocketDescriptor(browserSocket.socketDescriptor());
}
void HttpProxyThreadBrowser::run(){
QByteArray outgoingdata;
QDataStream in(&browserSocket);
QDataStream out(&outgoingdata, QIODevice::WriteOnly);
in.setVersion(QDataStream::Qt_4_0);
out.setVersion(QDataStream::Qt_4_0);
QString header("");//incomming header from browser eg internet explorer, firefox, chrome ,etc
//its our duty to package this header and send it to our remote php script
qDebug()<<"HttpProxyThreadBrowser started ...";
QByteArray proxyAuth("");
if(ORGANIZATION_HTTP_PROXY_HOST !=QString(""))
{
QByteArray authString= QString(ORGANIZATION_HTTP_PROXY_USER_NAME).toLatin1()
+ QString(":").toLatin1()
+ QString(ORGANIZATION_HTTP_PROXY_USER_PASS).toLatin1();
proxyAuth= QByteArray("Basic "+authString.toBase64());
qDebug()<<"Organisation http_proxy found. Auth string is "<<proxyAuth;
}
qint64 rdl;
while(true)//while browser is still sending us its request, read the request into incomingdata variable
{
if(!browserSocket.waitForReadyRead(30*1000))
{
return;//overwaited for browser request. Giving up (30 seconds)
}
if(browserSocket.bytesAvailable()< sizeof(qint64))continue;//wait for buffer to fill up
in>>rdl;//try to read data from browser
if(rdl == -1)break;//returns -1 when there is an error or data over
header.append(char(rdl));
if(header.indexOf("\r\n\r\n") != -1) break;// in http , a header ends with double \r\n. So it means
//browser is done telling us its what it wants
}
qDebug()<<"INITIAL header LENGTH IS(cf with content-length:xxx) "<<header.length();
qDebug()<<"HttpProxyThreadBrowser request header is "<<header;
QString host="";
QString port="";
QString tmphost="";
int indexOf=header.toLower().indexOf("host:");
if(indexOf!= -1)
{
int immediateNewLineChar= header.toLower().indexOf("\r\n", header.toLower().indexOf("host:"));
tmphost= header.mid(indexOf + 5, immediateNewLineChar - indexOf);//QString.mid(startpos, numberofchars)
tmphost=tmphost.trimmed();
qDebug()<<"tmp host is "<<tmphost;
int isPortThere= tmphost.indexOf(":");
if(isPortThere != -1)
{
host= tmphost.mid(0,tmphost.indexOf(":"));
port= tmphost.mid(tmphost.indexOf(":")+1);
qDebug()<<"there is a port in this tmphost. True host is "<<host<<" and true port is "<<port;
}
else
{
port="80";
host=tmphost;
}
//added since rapidshare not opening making it a relative request
QString modifyget= header.toLower();
if(modifyget.startsWith("get http://"))
{
int i2= modifyget.indexOf("/",11);
header= header.replace(4, i2, "");
}
if(modifyget.startsWith("post http://"))
{
int i2= modifyget.indexOf("/",12);
header= header.replace(5,i2,"");
}
qDebug()<<"modified header (bcoz of rapidshare not working) is "<<header;
QString proxyServerUrl= webPHP_URL_HTTP;//our php script
QString isSecure="";
QString proxyServerHost= webPHP_HOST_HTTP;
if(header.indexOf("X-IS-SSL-RECURSIVE:") == -1)
{
isSecure="N";
}
else
{
isSecure="Y";
// Now detect which Port 443 or 8443 ?
// Like : abcd X-IS-SSL-RECURSIVE: 8443
int p1 = header.indexOf("X-IS-SSL-RECURSIVE: ");
port = header.mid(p1 + 20, 4);
}
qDebug()<<"isSecure= "<<isSecure<<" Port is "<<port;
qDebug()<<"----------HttpProxyThreadBrowser started. Host="<<host<<" Port="<<port<<" ProxyServerUrl="<<proxyServerUrl<<" headerHost= "<<proxyServerHost;
//get content length
QString contentLength="";
int contentIdx= header.toLower().indexOf("content-length: ");
if (contentIdx != -1)
{
int endI = header.indexOf("\r\n", contentIdx + 17);
contentLength = header.mid(contentIdx + 16, endI - (contentIdx+16));
qDebug()<<"contentLength is "<<contentLength;
}
QString data= header;
//data= data.replace("\r\n\r\n","\r\nConnection: Close\r\n\r\n");//emulate replacefirst 3with regex todo fix regex
data = data.replace("keep-alive", "Close");
int totallength = 0;
if (contentLength != "") {//removed 61+1 coz don know what its doing
totallength = contentLength.toInt() + data.length();
} else {
totallength = data.length();
}
QString header1 = "";
header1 = header1 + "POST " + proxyServerUrl + " HTTP/1.1\r\n";
header1 = header1 + "Host: " + proxyServerHost + "\r\n";
header1 = header1 + "Connection: Close\r\n";
header1 = header1 + "Content-Length: \r\n";
header1 = header1 + "Cache-Control: no-cache\r\n";
//now insert total content length
totallength=totallength+header1.length();
header1= header1.insert(header1.toLower().indexOf("content-length: "), QString::number(double(totallength)));
qDebug()<<"FIXED header1 ="<<header1;
if (ORGANIZATION_HTTP_PROXY_USER_NAME !=QString("")) {
header1 = header1 + "Proxy-Authorization: " + proxyAuth
+ "\r\n";
}
}
}