Skip to content

Commit f088b54

Browse files
committed
Fix chunked data decoding issue
1 parent 1408949 commit f088b54

File tree

4 files changed

+38
-20
lines changed

4 files changed

+38
-20
lines changed

examples/FirestoreDatabase/Documents/Commit/AppendArray/AppendArray.ino

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@
2525
#define FIREBASE_PROJECT_ID "PROJECT_ID"
2626

2727
void processData(AsyncResult &aResult);
28-
void append_array_async();
29-
void append_array_async2();
28+
void append_array_async(Writes &writes);
29+
void append_array_async2(Writes &writes);
3030
void append_array_await();
3131

3232
SSL_CLIENT ssl_client;

examples/FirestoreDatabase/Documents/Commit/AppendMapValue/AppendMapValue.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ void append_map_value_async(Writes &writes)
149149
Docs.commit(aClient, Firestore::Parent(FIREBASE_PROJECT_ID), writes, processData, "commitTask");
150150
}
151151

152-
void append_map_value_async(Writes &writes)
152+
void append_map_value_async2(Writes &writes)
153153
{
154154
Serial.println("Appending the map value in the document... ");
155155

src/core/AsyncClient/AsyncClient.h

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,11 @@ class AsyncClientClass PUBLIC_DATABASE_RESULT_IMPL_BASE
204204
// Handles raw data sending process.
205205
function_return_type sendImpl(async_data *sData, const uint8_t *data, size_t len, size_t size, async_state state = astate_send_payload)
206206
{
207+
if (state == astate_send_header && (sData->response.respCtx.stage == res_handler::response_stage_undefined || sData->response.respCtx.stage == res_handler::response_stage_finished))
208+
{
209+
initResponse(sData);
210+
}
211+
207212
sys_idle();
208213
sData->state = state;
209214
if (data && len && sman.client)
@@ -456,11 +461,18 @@ class AsyncClientClass PUBLIC_DATABASE_RESULT_IMPL_BASE
456461

457462
String *payload = &sData->response.val[resns::payload];
458463

459-
if (sData->auth_used)
460-
sData->response.auth_data_available = true;
461-
462-
if (!sData->response.flags.sse)
464+
if (!sData->response.flags.sse && payload->length())
465+
{
463466
sData->aResult.setPayload(*payload);
467+
if (sData->auth_used)
468+
sData->response.auth_data_available = true;
469+
else
470+
{
471+
clear(*payload);
472+
sData->response.flags.payload_available = true;
473+
sman.returnResult(sData, true);
474+
}
475+
}
464476

465477
if (sData->aResult.download_data.total > 0)
466478
sData->aResult.data_log.reset();
@@ -487,7 +499,6 @@ class AsyncClientClass PUBLIC_DATABASE_RESULT_IMPL_BASE
487499

488500
if (((*payload)[len - 1] == '\n' && sData->response.tcpAvailable() == 0) || partial)
489501
{
490-
491502
setRefPayload(&sData->aResult.rtdbResult, payload);
492503
parseSSE(&sData->aResult.rtdbResult);
493504

@@ -946,8 +957,6 @@ class AsyncClientClass PUBLIC_DATABASE_RESULT_IMPL_BASE
946957
sData->request.val[reqns::header] += "access_token_auth: true\r\n";
947958
}
948959

949-
initResponse(sData);
950-
951960
if (method == reqns::http_get || method == reqns::http_delete)
952961
sData->request.addNewLine();
953962
}
@@ -966,6 +975,14 @@ class AsyncClientClass PUBLIC_DATABASE_RESULT_IMPL_BASE
966975
sData->response.respCtx.isSSE = sData->sse;
967976
sData->response.respCtx.isUpload = sData->upload;
968977
sData->response.respCtx.location = location;
978+
sData->response.httpCode = 0;
979+
sData->response.payloadLen = 0;
980+
sData->response.payloadRead = 0;
981+
sData->response.flags.reset();
982+
sData->response.toFill = nullptr;
983+
sData->response.toFillIndex = 0;
984+
sData->response.toFillLen = 0;
985+
sData->response.auth_data_available = false;
969986
}
970987

971988
void returnResult(async_data *sData) { *sData->refResult = sData->aResult; }
@@ -1017,15 +1034,13 @@ class AsyncClientClass PUBLIC_DATABASE_RESULT_IMPL_BASE
10171034
{
10181035
sman.stop();
10191036
sData->state = astate_send_header;
1020-
initResponse(sData);
10211037
}
10221038

10231039
// Resume incomplete async task from previously stopped.
10241040
if (!sman.conn.async && sData->async && !sData->complete)
10251041
{
10261042
sData->state = astate_send_header;
10271043
sman.conn.async = sData->async;
1028-
initResponse(sData);
10291044
}
10301045

10311046
bool sending = false;
@@ -1104,8 +1119,7 @@ class AsyncClientClass PUBLIC_DATABASE_RESULT_IMPL_BASE
11041119

11051120
handleReadTimeout(sData);
11061121

1107-
bool allRead = sData->response.httpCode > 0 && sData->response.httpCode != FIREBASE_ERROR_HTTP_CODE_OK && sData->response.respCtx.stage == res_handler::response_stage_finished;
1108-
if (allRead && sData->response.httpCode >= FIREBASE_ERROR_HTTP_CODE_BAD_REQUEST)
1122+
if (sData->response.respCtx.stage == res_handler::response_stage_finished && sData->response.httpCode >= FIREBASE_ERROR_HTTP_CODE_BAD_REQUEST)
11091123
{
11101124
if (sData->sse)
11111125
{
@@ -1115,9 +1129,10 @@ class AsyncClientClass PUBLIC_DATABASE_RESULT_IMPL_BASE
11151129
#endif
11161130
}
11171131
sData->return_type = ret_failure;
1132+
sman.setAsyncError(sData, sData->state, sData->response.httpCode, !sData->sse, false);
11181133
}
11191134

1120-
if (sData->async || allRead || sData->return_type == ret_failure)
1135+
if (sData->async || sData->return_type == ret_failure)
11211136
break;
11221137
}
11231138
}

src/core/AsyncClient/ResponseHandler.h

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ struct res_handler
6666
{
6767
public:
6868
const int bufSize = 512; // Maximum size of the buffer (including null terminator).
69-
uint8_t *buf = nullptr; // Byte array to store the read data.
69+
uint8_t *buf = nullptr; // Byte array to store the read data.
7070
const int hdrSize = 512;
7171
char *hdr = nullptr;
7272
String *location = nullptr;
@@ -453,10 +453,13 @@ struct res_handler
453453
}
454454
else
455455
{
456-
payloadRead += len;
457-
respCtx.buf[len] = '\0';
458-
reserveString();
459-
val[resns::payload] += (const char *)respCtx.buf;
456+
if (len > 0)
457+
{
458+
payloadRead += len;
459+
respCtx.buf[len] = '\0';
460+
reserveString();
461+
val[resns::payload] += (const char *)respCtx.buf;
462+
}
460463

461464
if (!respCtx.isChunked || (respCtx.isChunked && len == 0))
462465
{

0 commit comments

Comments
 (0)