void Handler::dechunkBody(Client &client)
{
//(1) client.chunk.len 파싱
if (strstr(client.rBuf, "\\r\\n") && client.chunk.found == false)
{
client.chunk.len = _helper.findLen(client);
if (client.chunk.len == 0)
client.chunk.done = true;
else
client.chunk.found = true;
}
//(2) 위에서 구한 client.chunk.len 만큼 body를 파싱
if (client.chunk.found == true)
_helper.fillBody(client);
//(3) body 파싱이 끝나면 모든 변수 초기화. status = CODE.
if (client.chunk.done)
{
client.status = Client::CODE;
memset(client.rBuf, 0, BUFFER_SIZE + 1);
client.chunk.found = false;
client.chunk.done = false;
return ;
}
}
//body 파싱이 끝나지 않았을 경우, getBody()처럼 다시 main 루프를 돌고와서 (2)번 플로우 진행
void Helper::fillBody(Client &client)
{
std::string tmp;
tmp = client.rBuf;
if (tmp.size() > client.chunk.len) //body 파싱 완료
{
client.req.body += tmp.substr(0, client.chunk.len);
tmp = tmp.substr(client.chunk.len + 1);
memset(client.rBuf, 0, BUFFER_SIZE + 1);
strcpy(client.rBuf, tmp.c_str());
client.chunk.len = 0;
client.chunk.found = false;
client.chunk.done = true; //이 부분 추가
}
else //아직 파싱할 body가 남음
{
client.req.body += tmp;
client.chunk.len -= tmp.size();
memset(client.rBuf, 0, BUFFER_SIZE + 1);
}
}