Skip to content

Commit

Permalink
Remove goto label logic in favor of while loop
Browse files Browse the repository at this point in the history
  • Loading branch information
hhvrc committed Jan 15, 2025
1 parent f14282c commit 40ad185
Showing 1 changed file with 26 additions and 29 deletions.
55 changes: 26 additions & 29 deletions src/http/HTTPRequestManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -271,41 +271,38 @@ StreamReaderResult _readStreamDataChunked(HTTPClient& client, WiFiClient* stream

bufferCursor += bytesRead;

parseMore:
state = _parseChunk(buffer, bufferCursor, payloadPos, payloadSize);
if (state == ParserState::Invalid) {
OS_LOGE(TAG, "Failed to parse chunk");
result = HTTP::RequestResult::RequestFailed;
break;
}
OS_LOGD(TAG, "Chunk parsed: %zu %zu", payloadPos, payloadSize);

if (state == ParserState::NeedMoreData) {
if (bufferCursor == HTTP_BUFFER_SIZE) {
OS_LOGE(TAG, "Chunk too large");
while (bufferCursor > 0) {
state = _parseChunk(buffer, bufferCursor, payloadPos, payloadSize);
if (state == ParserState::Invalid) {
OS_LOGE(TAG, "Failed to parse chunk");
result = HTTP::RequestResult::RequestFailed;
break;
}
continue;
}

// Check for zero chunk size (end of transfer)
if (payloadSize == 0) {
break;
}
OS_LOGD(TAG, "Chunk parsed: %zu %zu", payloadPos, payloadSize);

if (state == ParserState::NeedMoreData) {
if (bufferCursor == HTTP_BUFFER_SIZE) {
OS_LOGE(TAG, "Chunk too large");
result = HTTP::RequestResult::RequestFailed;
break;
}
continue;
}

if (!downloadCallback(totalWritten, buffer + payloadPos, payloadSize)) {
result = HTTP::RequestResult::Cancelled;
break;
}
// Check for zero chunk size (end of transfer)
if (payloadSize == 0) {
break;
}

totalWritten += payloadSize;
_alignChunk(buffer, bufferCursor, payloadPos, payloadSize);
payloadSize = 0;
payloadPos = 0;
if (!downloadCallback(totalWritten, buffer + payloadPos, payloadSize)) {
result = HTTP::RequestResult::Cancelled;
break;
}

if (bufferCursor > 0) {
goto parseMore;
totalWritten += payloadSize;
_alignChunk(buffer, bufferCursor, payloadPos, payloadSize);
payloadSize = 0;
payloadPos = 0;
}

vTaskDelay(pdMS_TO_TICKS(5));
Expand Down

0 comments on commit 40ad185

Please sign in to comment.