实际上,您没有收到分块,但内容是分块的。您必须为自己绘制一张图片,您收到的任何缓冲区可能看起来如何。这不像你当时收到一大块。有时你有前一个块的一些数据,表示新块大小的行,后面是一些块数据。其他时候,您只收到一点块数据。另一次是一些块数据和表示新块的行的一部分等。想象一下最坏的情况,这并不容易。阅读:http ://www.jmarshall.com/easy/http/
在您可以使用以下代码之前接收所有标题,直到空行。内容从缓冲区开始的位置是nContentStart
. 该代码使用了一些我无法共享的内部类,但您应该明白这一点;)据我测试,它按预期工作并且不会泄漏内存。虽然这并不容易,但我不能完全确定!
if (bChunked)
{
int nOffset = nContentStart;
int nChunkLen = 0;
int nCopyLen;
while (true)
{
if (nOffset >= nDataLen)
{pData->SetSize(0); Close(); ASSERTRETURN(false);}
// copy data of previous chunk to caller's buffer
if (nChunkLen > 0)
{
nCopyLen = min(nChunkLen, nDataLen - nOffset);
n = pData->GetSize();
pData->SetSize(n + nCopyLen);
memcpy(pData->GetPtr() + n, buf.GetPtr() + nOffset, nCopyLen);
nChunkLen -= nCopyLen;
ASSERT(nChunkLen >= 0);
nOffset += nCopyLen;
if (nChunkLen == 0)
nOffset += strlen(lpszLineBreak);
ASSERT(nOffset <= nDataLen);
}
// when previous chunk is copied completely, process new chunk
if (nChunkLen == 0 && nOffset < nDataLen)
{
// chunk length is specified on first line
p1 = buf.GetPtr() + nOffset;
p2 = strstr(p1, lpszLineBreak);
while (!p2) // if we can't find the line break receive more data until we do
{
buf.SetSize(nDataLen + RECEIVE_BUFFER_SIZE + 1);
nReceived = m_socket.Receive((BYTE*)buf.GetPtr() + nDataLen, RECEIVE_BUFFER_SIZE);
if (nReceived == -1)
{pData->SetSize(0); Close(); ASSERTRETURN(false);} // connection error
if (nReceived == 0)
{pData->SetSize(0); Close(); ASSERTRETURN(false);} // all data already received but did not find line break
nDataLen += nReceived;
buf[nDataLen] = 0;
p1 = buf.GetPtr() + nOffset; // address of buffer likely changed
p2 = strstr(p1, lpszLineBreak);
}
*p2 = 0;
p2 += strlen(lpszLineBreak);
p3 = strchr(p1, ';');
if (p3)
*p3 = 0;
if (sscanf(p1, "%X", &nChunkLen) != 1)
{pData->SetSize(0); Close(); ASSERTRETURN(false);}
if (nChunkLen < 0)
{pData->SetSize(0); Close(); ASSERTRETURN(false);}
if (nChunkLen == 0)
break; // last chunk received
// copy the following chunk data to caller's buffer
nCopyLen = min(nChunkLen, buf.GetPtr() + nDataLen - p2);
n = pData->GetSize();
pData->SetSize(n + nCopyLen);
memcpy(pData->GetPtr() + n, p2, nCopyLen);
nChunkLen -= nCopyLen;
ASSERT(nChunkLen >= 0);
nOffset = (p2 - buf.GetPtr()) + nCopyLen;
if (nChunkLen == 0)
nOffset += strlen(lpszLineBreak);
if (nChunkLen == 0 && nOffset < nDataLen)
continue; // a new chunk starts in this buffer at nOffset, no need to receive more data
}
// receive more data
buf.SetSize(RECEIVE_BUFFER_SIZE + 1);
nDataLen = m_socket.Receive((BYTE*)buf.GetPtr(), RECEIVE_BUFFER_SIZE);
if (nDataLen == -1)
{pData->SetSize(0); Close(); ASSERTRETURN(false);}
if (nDataLen == 0)
{pData->SetSize(0); Close(); ASSERTRETURN(false);}
buf[nDataLen] = 0;
nOffset = 0;
}
// TODO: receive optional footers and add them to m_headers
}