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