fix: chunk logic
This commit is contained in:
+23
-10
@@ -134,6 +134,9 @@ struct HttpStream
|
||||
size_t headersEnd = std::string::npos;
|
||||
int statusCode = 0;
|
||||
|
||||
size_t currentChunkIdx = 0;
|
||||
std::string payload;
|
||||
|
||||
void reset()
|
||||
{
|
||||
isReceivingBody = false;
|
||||
@@ -141,6 +144,8 @@ struct HttpStream
|
||||
contentLength = -1;
|
||||
headersEnd = std::string::npos;
|
||||
statusCode = 0;
|
||||
currentChunkIdx = 0;
|
||||
payload.clear();
|
||||
}
|
||||
|
||||
bool parseHeaders()
|
||||
@@ -420,21 +425,25 @@ void Proxy::handleClient(SOCKET clientSocket)
|
||||
|
||||
if (clientStream.isChunked)
|
||||
{
|
||||
size_t idx = clientStream.headersEnd + 4;
|
||||
while (idx < clientStream.buffer.size())
|
||||
if (clientStream.currentChunkIdx == 0)
|
||||
clientStream.currentChunkIdx = clientStream.headersEnd + 4;
|
||||
|
||||
while (clientStream.currentChunkIdx < clientStream.buffer.size())
|
||||
{
|
||||
size_t idx = clientStream.currentChunkIdx;
|
||||
size_t le = clientStream.buffer.find("\r\n", idx);
|
||||
if (le == std::string::npos) break;
|
||||
int cs = stoiSafe(clientStream.buffer.substr(idx, le - idx), -1, 16);
|
||||
if (cs < 0) return;
|
||||
|
||||
if (idx + (le - idx) + 2 + cs + 2 > clientStream.buffer.size()) break;
|
||||
if (cs > 0) fullBody.append(clientStream.buffer, le + 2, cs);
|
||||
idx = le + 2 + cs + 2;
|
||||
if (cs > 0) clientStream.payload.append(clientStream.buffer, le + 2, cs);
|
||||
clientStream.currentChunkIdx = le + 2 + cs + 2;
|
||||
if (cs == 0)
|
||||
{
|
||||
fullBody = std::move(clientStream.payload);
|
||||
complete = true;
|
||||
totalRequestSize = idx;
|
||||
totalRequestSize = clientStream.currentChunkIdx;
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -500,21 +509,25 @@ void Proxy::handleClient(SOCKET clientSocket)
|
||||
}
|
||||
else if (serverStream.isChunked)
|
||||
{
|
||||
size_t idx = serverStream.headersEnd + 4;
|
||||
while (idx < serverStream.buffer.size())
|
||||
if (serverStream.currentChunkIdx == 0)
|
||||
serverStream.currentChunkIdx = serverStream.headersEnd + 4;
|
||||
|
||||
while (serverStream.currentChunkIdx < serverStream.buffer.size())
|
||||
{
|
||||
size_t idx = serverStream.currentChunkIdx;
|
||||
size_t le = serverStream.buffer.find("\r\n", idx);
|
||||
if (le == std::string::npos) break;
|
||||
int cs = stoiSafe(serverStream.buffer.substr(idx, le - idx), -1, 16);
|
||||
if (cs < 0) return;
|
||||
|
||||
if (idx + (le - idx) + 2 + cs + 2 > serverStream.buffer.size()) break;
|
||||
if (cs > 0) fullBody.append(serverStream.buffer, le + 2, cs);
|
||||
idx = le + 2 + cs + 2;
|
||||
if (cs > 0) serverStream.payload.append(serverStream.buffer, le + 2, cs);
|
||||
serverStream.currentChunkIdx = le + 2 + cs + 2;
|
||||
if (cs == 0)
|
||||
{
|
||||
fullBody = std::move(serverStream.payload);
|
||||
complete = true;
|
||||
totalResponseSize = idx;
|
||||
totalResponseSize = serverStream.currentChunkIdx;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user