www.pudn.com > HttpServerusingWinSocket.rar > MyHttpBlockSocket.cpp
// MyHttpBlockSocket.cpp: implementation of the CMyHttpBlockSocket class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "HttpServer.h"
#include "MyHttpBlockSocket.h"
#include "MyBlockSocketException.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
IMPLEMENT_DYNAMIC(CMyHttpBlockSocket,CMyBlockSocket)
CMyHttpBlockSocket::CMyHttpBlockSocket()
{
m_pReadBuf=new char[nSizeRecv];
m_nReadBuf=0;
}
//读取整个头信息,并在结尾加上字符串的结束标识
int CMyHttpBlockSocket::ReadHttpHeaderLine(char *pch,const int nSize,const int nSecs){
int nBytesThisTime=m_nReadBuf;
int nLineLength=0;
char * pch1=m_pReadBuf;
char * pch2;
do{
if((pch2=(char *)memchr(pch1,'\n',nBytesThisTime))!=NULL){
ASSERT((pch2)>m_pReadBuf);
ASSERT(*(pch2-1)=='\r');
nLineLength=(pch2-m_pReadBuf)+1;
if(nLineLength>=nSize){
nLineLength=nSize-1;
}
memcpy(pch,m_pReadBuf,nLineLength);
m_nReadBuf-=nLineLength;
memmove(m_pReadBuf,pch2+1,m_nReadBuf);
break;
}
pch1+=nBytesThisTime;
nBytesThisTime=Receive(m_pReadBuf+m_nReadBuf,nSizeRecv-m_nReadBuf,nSecs);
if(nBytesThisTime<=0){
throw new CMyBlockSocketException("获取头部信息ReadHttpHeaderLine");
}
m_nReadBuf+=nBytesThisTime;
}while(TRUE);
*(pch+nLineLength)='\0';
return nLineLength;
}
//获取传输的信息的其它部分(假定头部已经读取)
int CMyHttpBlockSocket::ReadHttpResponse(char *pch,const int nSize,const int nSecs){
int nBytesToRead,nBytesThisTime,nBytesRead=0;
if(m_nReadBuf>0){
memcpy(pch,m_pReadBuf,m_nReadBuf);
pch+=m_nReadBuf;
nBytesRead=m_nReadBuf;
m_nReadBuf=0;
}
do{
nBytesToRead=min(nSizeRecv,nSize-nBytesRead);
nBytesThisTime=Receive(pch,nBytesToRead,nSecs);
if(nBytesThisTime<=0) break;
pch+=nBytesThisTime;
nBytesRead+=nBytesThisTime;
}while(nBytesRead<=nSize);
return nBytesRead;
}
CMyHttpBlockSocket::~CMyHttpBlockSocket()
{
delete [] m_pReadBuf;
}