⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 clienthttp.cpp

📁 支持CMWAP 的HTTP socket封装类。
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/*******************************************************************************
 * (C) Copyright 2008 Giant Electronics LTD 
 * 
 * These computer program listings and specifications are the property of Giant 
 * Electronics LTD and shall not be reproduced or copied or used in whole or in 
 * part without written permission from Giant Electronics LTD .
 *
 * Project:      IViewer 
 * File Name:	ClientHTTP.cpp
 * Programer(s):	Ben Zhan
 * Created:      20080729 
 * Description:	implementation of encapsulating HTTP protocol
 * This class encapsulates HTTP protocol and provides the functionality to request 
 and read files on an HTTP server.
 *******************************************************************************/
#include "stdafx.h"
#include "ClientHTTP.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CClientHTTP::CClientHTTP()
{	
}

CClientHTTP::~CClientHTTP()
{
}

//*******************************************************************************************************   
//   ValidHostChar:     
// Return   TRUE   if   the   specified   character   is   valid   
// for   a   host   name,   i.e.   A-Z   or   0-9   or   -.:     
//*******************************************************************************************************   
BOOL   CClientHTTP::ValidHostChar(TCHAR   ch)   
{   
    return(   iswalpha(ch)   ||   iswdigit(ch)||   ch==('-')   ||   ch==('.')   ||   ch==(':')   );
}

void   CClientHTTP::ParseURL(LPCSTR   url,LPSTR   protocol,int   lprotocol,LPSTR   host,int   lhost,LPSTR   request,int   lrequest,int   *port)   
{   
    LPSTR   work,ptr,ptr2;
    //added by bpan
    //reason: to avoid buffer overflow
    //2008/09/29
    int iMinLen; 
    *protocol = *host = *request = 0;   
    *port=80;

    work   =   _strdup(url);
    //	_strupr(work);  

    ptr   =   strchr(work,':'); //   find   protocol   if   any   
    if(ptr!=NULL)   
    {   
	*(ptr++)   =   0;
	iMinLen = min(lprotocol-1, strlen(work));
	strncpy(protocol,work, iMinLen);
    }   
    else   
    {   
	iMinLen = min(lprotocol -1, 4);
	strncpy(protocol,"HTTP", iMinLen);
	ptr   =   work;   
    }   
    protocol[iMinLen] = TCHAR('\0');

    if(   (*ptr=='/')   &&   (*(ptr+1)=='/')   ) //   skip   past   opening   /'s     
	ptr+=2;   

    ptr2   =   ptr; //   find   host   
    while(   ValidHostChar(*ptr2)   &&   *ptr2   )   
	ptr2++;   

    *ptr2=0;   
    iMinLen = min(lhost -1, strlen(ptr));
    strncpy(host,ptr, iMinLen);
    host[iMinLen] = TCHAR('\0');
    //lstrcpyn(host,ptr,lhost);   
    iMinLen = min(lrequest -1, strlen(url + (ptr2 - work))); 
    strncpy(request,url+(ptr2-work), iMinLen); //   find   the   request   
    request[iMinLen] = TCHAR('\0');
    if( strlen((LPSTR)request) ==0 )
	strcpy(request,"/");



    ptr   =   strchr(host,':'); //   find   the   port   number,   if   any   
    if(ptr!=NULL)   
    {   
	*ptr=0;   
	*port   =   atoi(ptr+1);   
    }   

    free(work);   
}
int   CClientHTTP::SendGETRequest(LPCSTR   lpszURL,HANDLE hCancelEvent)
{
    char acProtocol[20],acHost[256],acRequest[256*4];   
    int  iPort;		
    long nLength;

    memset(m_RequestHeaderBuf,0,HTTPHEADER_MAX_SIZE); 


    ParseURL(lpszURL,acProtocol,sizeof(acProtocol),acHost,sizeof(acHost),
	    acRequest,sizeof(acRequest),&iPort);  

	SetConnectTimeout(30000);

	if (m_sProxyInfo.m_nProxyType != ProxyType_NONE)
	{
		strcpy(m_sProxyInfo.m_acServer,acHost);
		m_sProxyInfo.m_nServerPort = iPort;

		FormatGETHeader_HTTPPROXY(acHost,acRequest,nLength);

		if (!Connect(m_sProxyInfo.m_acProxyIP,m_sProxyInfo.m_nProxyPort,hCancelEvent))
			return E_HTTP_CONNECT_FAILED ;
	}
	else
	{
		FormatGETHeader(acHost,acRequest,nLength);

		if (!Connect(acHost,iPort,hCancelEvent))
			return E_HTTP_CONNECT_FAILED ;
	}

	
    if (Send((char*)m_RequestHeaderBuf,nLength) != S_SOCKET_OK )
		return E_HTTP_REQUEST_FAILED ;

	// send the last empty line according to HTTP protocol
    if (Send("\r\n",2) != S_SOCKET_OK )
		return E_HTTP_REQUEST_FAILED ;

    return   E_HTTP_SUCCEED ;
}

int   CClientHTTP::SendPOSTRequest(LPCSTR lpszURL, LPBYTE lpPost, DWORD dwPostLength, HANDLE hCancelEvent)
{ 
    char acProtocol[20],acHost[256],acRequest[256*4];   
    int  iPort;		
    long nLength;
	
    memset(m_RequestHeaderBuf,0,HTTPHEADER_MAX_SIZE); 
	
	
    ParseURL(lpszURL,acProtocol,sizeof(acProtocol),acHost,sizeof(acHost),
		acRequest,sizeof(acRequest),&iPort);  
	
	SetConnectTimeout(30000);
	
	if (m_sProxyInfo.m_nProxyType != ProxyType_NONE)
	{
		strcpy(m_sProxyInfo.m_acServer,acHost);
		m_sProxyInfo.m_nServerPort = iPort;
		
		FormatPOSTHeader_HTTPPROXY(acHost,acRequest,dwPostLength,nLength);		
		
		if (!Connect(m_sProxyInfo.m_acProxyIP,m_sProxyInfo.m_nProxyPort,hCancelEvent))
			return E_HTTP_CONNECT_FAILED ;
	}
	else
	{
		FormatPOSTHeader(acHost,acRequest,dwPostLength,nLength);
		
		if (!Connect(acHost,iPort,hCancelEvent))
			return E_HTTP_CONNECT_FAILED ;
	}	
	
    if (Send((char*)m_RequestHeaderBuf,nLength) != S_SOCKET_OK )
		return E_HTTP_REQUEST_FAILED ;


	// send post data
    if (Send((char*)lpPost, dwPostLength) != S_SOCKET_OK)
		return E_HTTP_REQUEST_FAILED ;

	// send the last empty line according to HTTP protocol
    //if (Send("\r\n\r\n",strlen("\r\n\r\n")) != S_SOCKET_OK)
		//return E_HTTP_REQUEST_FAILED ;
	
    return   E_HTTP_SUCCEED ;
} 



///根据请求的相对URL输出HTTP请求头
const char *CClientHTTP::FormatGETHeader_HTTPPROXY(LPSTR lpszServer,
	LPSTR lpszObject,long &lLength,
	LPSTR lpszCookie,LPSTR lpszReferer,
	long lFrom,long lTo,
	int nServerType)
{

    char szTemp[80];

    memset(m_RequestHeaderBuf,0,HTTPHEADER_MAX_SIZE); 

    ///第1行:方法,请求的路径,版本
    int iIndex = 0 ;
    memcpy(m_RequestHeaderBuf+iIndex,(BYTE*)"GET ",4);
    iIndex +=4;


	memcpy(m_RequestHeaderBuf+iIndex,(BYTE*)"HTTP://",7);
	iIndex += 7;

	memcpy(m_RequestHeaderBuf+iIndex,m_sProxyInfo.m_acServer,strlen(m_sProxyInfo.m_acServer));
	iIndex += strlen(m_sProxyInfo.m_acServer);

	sprintf(szTemp,":%d",m_sProxyInfo.m_nProxyPort);
    memcpy(m_RequestHeaderBuf+iIndex,(BYTE*)szTemp,strlen(szTemp));
    iIndex +=strlen(szTemp);
	

    memcpy(m_RequestHeaderBuf+iIndex,(BYTE*)lpszObject,strlen(lpszObject));
    iIndex +=strlen(lpszObject);

    memcpy(m_RequestHeaderBuf+iIndex,(BYTE*)" HTTP/1.1\r\n",strlen(" HTTP/1.1\r\n"));
    iIndex +=strlen(" HTTP/1.1\r\n");


    ///第2行:主机
    memcpy(m_RequestHeaderBuf+iIndex,(BYTE*)"Host: ",6);
    iIndex +=6;
    memcpy(m_RequestHeaderBuf+iIndex,(BYTE*)lpszServer,strlen(lpszServer));
    iIndex +=strlen(lpszServer);

	sprintf(szTemp,":%d\r\n",m_sProxyInfo.m_nServerPort);
    memcpy(m_RequestHeaderBuf+iIndex,(BYTE*)szTemp,strlen(szTemp));
    iIndex +=strlen(szTemp);



    ///第5行:浏览器类型
    char* pstr = "User-Agent:Mozilla/4.0 (compatible; MSIE 5.00; Windows 98)";
    memcpy(m_RequestHeaderBuf+iIndex,(BYTE*)pstr,strlen(pstr));
    iIndex +=strlen(pstr);
    memcpy(m_RequestHeaderBuf+iIndex,(BYTE*)"\r\n",strlen("\r\n"));
    iIndex +=strlen("\r\n");


    ///返回结果
    lLength= iIndex ;
    return m_RequestHeaderBuf;

}


///根据请求的相对URL输出HTTP请求头
const char *CClientHTTP::FormatGETHeader(LPSTR lpszServer,
	LPSTR lpszObject,long &lLength,
	LPSTR lpszCookie,LPSTR lpszReferer,
	long lFrom,long lTo,
	int nServerType)
{

    char szTemp[20];
    char* pstr;

    memset(m_RequestHeaderBuf,0,HTTPHEADER_MAX_SIZE); 

    ///第1行:方法,请求的路径,版本
    int iIndex = 0 ;
    memcpy(m_RequestHeaderBuf+iIndex,(BYTE*)"GET ",4);
    iIndex +=4;
	

    memcpy(m_RequestHeaderBuf+iIndex,(BYTE*)lpszObject,strlen(lpszObject));
    iIndex +=strlen(lpszObject);

    memcpy(m_RequestHeaderBuf+iIndex,(BYTE*)" HTTP/1.1\r\n",strlen(" HTTP/1.1\r\n"));
    iIndex +=strlen(" HTTP/1.1\r\n");


    ///第2行:主机
    memcpy(m_RequestHeaderBuf+iIndex,(BYTE*)"Host:",5);
    iIndex +=5;
    memcpy(m_RequestHeaderBuf+iIndex,(BYTE*)lpszServer,strlen(lpszServer));
    iIndex +=strlen(lpszServer);
    memcpy(m_RequestHeaderBuf+iIndex,(BYTE*)"\r\n",strlen("\r\n"));
    iIndex +=strlen("\r\n");


    ///第3行:
    if(lpszReferer != NULL)
    {
		memcpy(m_RequestHeaderBuf+iIndex,(BYTE*)"Referer:",8);
		iIndex +=8;
		memcpy(m_RequestHeaderBuf+iIndex,(BYTE*)lpszReferer,strlen(lpszReferer));
		iIndex +=strlen(lpszReferer);
		memcpy(m_RequestHeaderBuf+iIndex,(BYTE*)"\r\n",strlen("\r\n"));
		iIndex +=strlen("\r\n");
    }

    ///第4行:接收的数据类型
    memcpy(m_RequestHeaderBuf+iIndex,(BYTE*)"Accept:*/*",strlen("Accept:*/*"));
    iIndex +=strlen("Accept:*/*");
    memcpy(m_RequestHeaderBuf+iIndex,(BYTE*)"\r\n",strlen("\r\n"));
    iIndex +=strlen("\r\n");


    ///第5行:浏览器类型
    pstr="User-Agent:Mozilla/4.0 (compatible; MSIE 5.00; Windows 98)";
    memcpy(m_RequestHeaderBuf+iIndex,(BYTE*)pstr,strlen(pstr));
    iIndex +=strlen(pstr);
    memcpy(m_RequestHeaderBuf+iIndex,(BYTE*)"\r\n",strlen("\r\n"));
    iIndex +=strlen("\r\n");


    ///第6行:连接设置,保持
    pstr="Connection:Keep-Alive";
    memcpy(m_RequestHeaderBuf+iIndex,(BYTE*)pstr,strlen(pstr));
    iIndex +=strlen(pstr);
    memcpy(m_RequestHeaderBuf+iIndex,(BYTE*)"\r\n",strlen("\r\n"));
    iIndex +=strlen("\r\n");


    ///第7行:Cookie.
    if(lpszCookie != NULL)
    {
		pstr="Set Cookie:0";
		memcpy(m_RequestHeaderBuf+iIndex,(BYTE*)pstr,strlen(pstr));
		iIndex +=strlen(pstr);
		memcpy(m_RequestHeaderBuf+iIndex,(BYTE*)lpszCookie,strlen(lpszCookie));
		iIndex +=strlen(lpszCookie);
		memcpy(m_RequestHeaderBuf+iIndex,(BYTE*)"\r\n",strlen("\r\n"));
		iIndex +=strlen("\r\n");
    }

    ///第8行:请求的数据起始字节位置(断点续传的关键)
    if(lFrom > 0)
    {

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -