예제 #1
0
//************************************
// Method: ProxyThread
// FullName: ProxyThread
// Access: public
// Returns: unsigned int __stdcall
// Qualifier: 线程执行函数
// Parameter: LPVOID lpParameter
//************************************
unsigned int __stdcall ProxyThread(LPVOID lpParameter){
    char Buffer[MAXSIZE];
    char *CacheBuffer;
    ZeroMemory(Buffer,MAXSIZE);
    SOCKADDR_IN clientAddr;
    int length = sizeof(SOCKADDR_IN);
    int recvSize;
    int ret;
    recvSize = recv(((ProxyParam*)lpParameter)->clientSocket,Buffer,MAXSIZE,0);
    if(recvSize <= 0){
        goto error;
    }
    HttpHeader* httpHeader = new HttpHeader();
    CacheBuffer = new char[recvSize + 1];
    ZeroMemory(CacheBuffer,recvSize + 1);
    memcpy(CacheBuffer,Buffer,recvSize);
    ParseHttpHead(CacheBuffer,httpHeader);
    delete CacheBuffer;
    if(!ConnectToServer(&((ProxyParam*)lpParameter)->serverSocket,httpHeader->host)) {
        goto error;
    }
    printf("代理连接主机 %s 成功\n",httpHeader->host);
    //将客户端发送的 HTTP 数据报文直接转发给目标服务器
    ret = send(((ProxyParam *)lpParameter)->serverSocket,Buffer,strlen(Buffer) + 1,0);
    //等待目标服务器返回数据
    recvSize = recv(((ProxyParam*)lpParameter)->serverSocket,Buffer,MAXSIZE,0);
    if(recvSize <= 0){
        goto error;
    }
    //将目标服务器返回的数据直接转发给客户端
    ret = send(((ProxyParam*)lpParameter)->clientSocket,Buffer,sizeof(Buffer),0);
    //错误处理
    error:
        printf("关闭套接字\n");
        Sleep(200);
        closesocket(((ProxyParam*)lpParameter)->clientSocket);
        closesocket(((ProxyParam*)lpParameter)->serverSocket);
        delete lpParameter;
        _endthreadex(0);
    return 0;
}
예제 #2
0
//************************************
// Method: ProxyThread
// FullName: ProxyThread
// Access: public
// Returns: unsigned int __stdcall
// Qualifier: 线程执行函数
// Parameter: LPVOID lpParameter
//************************************
unsigned int __stdcall ProxyThread(LPVOID lpParameter){
	char *Buffer;
	Buffer = new char[MAXSIZE];
	char *CacheBuffer;
	ZeroMemory(Buffer, MAXSIZE);
	SOCKADDR_IN clientAddr;
	int length = sizeof(SOCKADDR_IN);
	int recvSize;
	int ret;
	//从客户端接收http请求
	recvSize = recv(((ProxyParam *)lpParameter)->clientSocket, Buffer, MAXSIZE, 0);
	if (recvSize <= 0){
		printf("连接客户端!!!\n");
		goto error;
	}
	/*
	1.先判断缓存是否命中
	1.1若没有命中则直接请求对象,获取对象后,查看cache-control字段
	1.1.1若可以缓存,则保存到本地(完整的报文),并发送回客户端
	1.1.2若不可以缓存,则直接发送回客户端
	1.2若缓存命中(是否过期:DATE+max-age与当前时间的对比)
	1.2.1若没有过期,直接从缓存中读取,并发回客户端
	1.2.2若过期,向服务器发送IF-MODIFIED-SINCE:LAST MODIFIED中的时间
	1.2.2.1若返回200,则更新缓存,并返回客户端
	1.2.2.2若返回304,则更新缓存,并返回客户端

	*/
	HttpHeader* httpHeader = new HttpHeader();
	CacheBuffer = new char[recvSize + 1];
	ZeroMemory(CacheBuffer, recvSize + 1);
	memcpy(CacheBuffer, Buffer, recvSize);
	ParseHttpHead(CacheBuffer, httpHeader);
	delete CacheBuffer;
	if (!ConnectToServer(&((ProxyParam *)lpParameter)->serverSocket, httpHeader->host)) {
		printf("连接目标服务器!!!\n");
		goto error;
	}
	printf("代理连接主机 %s 成功\n", httpHeader->host);
	//将客户端发送的 HTTP 数据报文直接转发给目标服务器
	ret = send(((ProxyParam *)lpParameter)->serverSocket, Buffer, strlen(Buffer), 0);
	//printf("向目标服务器发送:%s\nOVER...", Buffer);
	//等待目标服务器返回数据
	ZeroMemory(Buffer, MAXSIZE);
	recvSize = recv(((ProxyParam *)lpParameter)->serverSocket, Buffer, MAXSIZE, 0);
	if (recvSize <= 0){
		printf("接受数据出错!!!\n");
		goto error;
	}
	//printf("目标服务器返回:%s\nOVER...", Buffer);
	//将目标服务器返回的数据直接转发给客户端
	ret = send(((ProxyParam *)lpParameter)->clientSocket, Buffer, recvSize, 0);
	//错误处理
	goto success;
error:
	printf("关闭套接字\n");
	//Sleep(200);
	closesocket(((ProxyParam*)lpParameter)->clientSocket);
	closesocket(((ProxyParam*)lpParameter)->serverSocket);
	delete lpParameter;
	_endthreadex(0);
	return 0;
success:
	printf("关闭套接字\n");
	//Sleep();
	closesocket(((ProxyParam*)lpParameter)->clientSocket);
	closesocket(((ProxyParam*)lpParameter)->serverSocket);
	delete lpParameter;
	_endthreadex(0);
	return 0;
}
예제 #3
0
int CHttpThread::ReadHttpReq()
{
    int iRet;
	int pos;
	int len;
	char * p;
	int contentpos;
	int i,j;

	m_szHttpHead[0]='\0';
    pos=0;
	p=strstr(m_szHttpHead,"\r\n\r\n");
    while (p==NULL)
    {
        len=recv(m_sockfd,&m_szHttpHead[pos],1023,0);
		if(len<=0)
		{
		    return -1;
		}
        //printf("len=%d\n",len);
        pos+=len;
		m_szHttpHead[pos]=0;
		p=strstr(m_szHttpHead,"\r\n\r\n");
    }
	if(pos==0)
	{
	    return -1;
	}
	m_szHttpHead[pos]=0;
	contentpos=p - m_szHttpHead + 4;
	p[2]=0;

	printf("========================http head begin =================================\n");
	printf("%s",m_szHttpHead);
	printf("========================http head end =================================\n");

	m_szHttpContent[0]=0;
	for(j=0,i=contentpos;i<pos;i++)
	{
	    m_szHttpContent[j]=m_szHttpHead[i];
		j++;
	}
	m_szHttpContent[j]=0;
	iRet= ParseHttpHead(m_szHttpHead,&m_HttpHead);

	if(m_HttpHead.m_ContentLength<0 || m_HttpHead.m_ContentLength>MAXHTTPPACKETLEN)
	{
	    return -1;
	}

	if(m_HttpHead.m_ContentLength==0)
	{
	    return 0;
	}

	if(m_HttpHead.m_ContentLength - j>0)
	{
		iRet=SocketRead(m_sockfd,&m_szHttpContent[j],m_HttpHead.m_ContentLength - j);
		if(iRet<=0)
		{
			return -1;
		}
		m_szHttpContent[iRet+j]=0;
	}

	printf("============================http content begin ===============================\n");
	printf("%s\n",m_szHttpContent);
	printf("============================http content end   ===============================\n");

	return 0;
}