@@ -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 }
0 commit comments