Skip to content

Commit

Permalink
Fix chenked data decoding issue #175 introduced by PR #172
Browse files Browse the repository at this point in the history
  • Loading branch information
mobizt committed Nov 18, 2024
1 parent eddf6f4 commit 5eafe56
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 25 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

![GitHub Actions Workflow Status](https://img.shields.io/github/actions/workflow/status/mobizt/FirebaseClient/.github%2Fworkflows%2Fcompile_library.yml?logo=github&label=compile) [![Github Stars](https://img.shields.io/github/stars/mobizt/FirebaseClient?logo=github)](https://github.com/mobizt/FirebaseClient/stargazers) ![Github Issues](https://img.shields.io/github/issues/mobizt/FirebaseClient?logo=github)

![GitHub Release](https://img.shields.io/github/v/release/mobizt/FirebaseClient) ![Arduino](https://img.shields.io/badge/Arduino-v1.4.7-57C207?logo=arduino) ![PlatformIO](https://badges.registry.platformio.org/packages/mobizt/library/FirebaseClient.svg) ![GitHub Release Date](https://img.shields.io/github/release-date/mobizt/FirebaseClient)
![GitHub Release](https://img.shields.io/github/v/release/mobizt/FirebaseClient) ![Arduino](https://img.shields.io/badge/Arduino-v1.4.8-57C207?logo=arduino) ![PlatformIO](https://badges.registry.platformio.org/packages/mobizt/library/FirebaseClient.svg) ![GitHub Release Date](https://img.shields.io/github/release-date/mobizt/FirebaseClient)

[![GitHub Sponsors](https://img.shields.io/github/sponsors/mobizt?logo=github)](https://github.com/sponsors/mobizt)

Revision `2024-11-10T07:42:41Z`
Revision `2024-11-18T18:48:41Z`

## Table of Contents

Expand Down
2 changes: 1 addition & 1 deletion library.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "FirebaseClient",
"version": "1.4.7",
"version": "1.4.8",
"keywords": "communication, REST, esp32, esp8266, arduino",
"description": "Async Firebase Client library for Arduino.",
"repository": {
Expand Down
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name=FirebaseClient

version=1.4.7
version=1.4.8

author=Mobizt

Expand Down
37 changes: 17 additions & 20 deletions src/core/AsyncClient/AsyncClient.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Created October 25, 2024
* Created November 19, 2024
*
* For MCU build target (CORE_ARDUINO_XXXX), see Options.h.
*
Expand Down Expand Up @@ -1026,10 +1026,10 @@ class AsyncClientClass : public ResultBase, RTDBResultBase
}
}

int getChunkSize(async_data_item_t *sData, Client *client)
int getChunkSize(async_data_item_t *sData, Client *client, String &line)
{
String line;
readLine(sData, line);
if (line.length() == 0)
readLine(sData, line);
int p = line.indexOf(";");
if (p == -1)
p = line.indexOf("\r\n");
Expand All @@ -1044,7 +1044,8 @@ class AsyncClientClass : public ResultBase, RTDBResultBase
{
if (!client || !sData || !out)
return 0;
int res = 0;
int res = 0, read = 0;
String line;

// because chunks might span multiple reads, we need to keep track of where we are in the chunk
// chunkInfo.dataLen is our current position in the chunk
Expand All @@ -1059,7 +1060,7 @@ class AsyncClientClass : public ResultBase, RTDBResultBase
sData->response.chunkInfo.phase = async_response_handler_t::READ_CHUNK_DATA;
sData->response.chunkInfo.chunkSize = -1;
sData->response.chunkInfo.dataLen = 0;
res = getChunkSize(sData, client);
res = getChunkSize(sData, client, line);
sData->response.payloadLen += res > -1 ? res : 0;
}
// read chunk-data and CRLF
Expand All @@ -1069,51 +1070,47 @@ class AsyncClientClass : public ResultBase, RTDBResultBase
// if chunk-size is 0, it's the last chunk, and can be skipped
if (sData->response.chunkInfo.chunkSize > 0)
{
String chunk;
int read = readLine(sData, chunk);

read = readLine(sData, line);

// if we read till a CRLF, we have a chunk (or the rest of it)
// if the last two bytes are NOT CRLF, we have a partial chunk
// if we read 0 bytes, read next chunk size

// check for \n and \r, remove them if present (they're part of the protocol, not the data)
if (read >= 2 && chunk[read - 2] == '\r' && chunk[read - 1] == '\n')
if (read >= 2 && line[read - 2] == '\r' && line[read - 1] == '\n')
{
// remove the \r\n
chunk.remove(chunk.length() - 2);
line.remove(line.length() - 2);
read -= 2;
}

// if we still have data, append it and update the chunkInfo
if (read)
{
*out += chunk;
*out += line;
sData->response.chunkInfo.dataLen += read;
sData->response.payloadRead += read;

// check if we're done reading this chunk
if (sData->response.chunkInfo.dataLen == sData->response.chunkInfo.chunkSize)
{
sData->response.chunkInfo.phase = async_response_handler_t::READ_CHUNK_SIZE;
}
}
// if we read 0 bytes, read next chunk size
else
{
sData->response.chunkInfo.phase = async_response_handler_t::READ_CHUNK_SIZE;
}
}
}
else
{
// last chunk, read until the final CRLF so the rest of the logic based on available() works
String chunk;
int read = readLine(sData, chunk);
read = readLine(sData, line);

// if we read a CRLF, we're done
if (read == 2 && chunk[0] == '\r' && chunk[1] == '\n')
{
if (read == 2 && line[0] == '\r' && line[1] == '\n')
res = -1;
}
else // that could be a last chunk size, continue to read the last chunk-data and CRLF
getChunkSize(sData, client, line);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/core/Core.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#undef FIREBASE_CLIENT_VERSION
#endif

#define FIREBASE_CLIENT_VERSION "1.4.7"
#define FIREBASE_CLIENT_VERSION "1.4.8"

static void sys_idle()
{
Expand Down

0 comments on commit 5eafe56

Please sign in to comment.