@@ -259,6 +259,7 @@ AsyncBasicResponse::AsyncBasicResponse(int code, const String& contentType, cons
259
259
_content = String (" " );
260
260
_contentCstr = (char *)content; // RSMOD
261
261
_contentType = contentType;
262
+ _partialHeader = String ();
262
263
263
264
int iLen;
264
265
@@ -284,6 +285,7 @@ AsyncBasicResponse::AsyncBasicResponse(int code, const String& contentType, cons
284
285
_content = content;
285
286
_contentCstr = nullptr ; // RSMOD
286
287
_contentType = contentType;
288
+ _partialHeader = String ();
287
289
288
290
if (_content.length ())
289
291
{
@@ -319,22 +321,26 @@ void AsyncBasicResponse::_respond(AsyncWebServerRequest *request)
319
321
{
320
322
AWS_LOGDEBUG (" Step 2" );
321
323
324
+
322
325
if (_contentCstr)
323
326
{
327
+ /*
324
328
memmove(&_contentCstr[outLen], _contentCstr, _contentLength);
325
329
memcpy(_contentCstr, out.c_str(), outLen);
326
330
outLen += _contentLength;
327
331
328
332
AWS_LOGDEBUG1("_contentCstr =", _contentCstr);
329
333
330
334
_writtenLength += request->client()->write(_contentCstr, outLen);
335
+ */
336
+ _content = String (_contentCstr); // short _contentCstr - so just send as Arduino String - not much of a penalty - fall into below
331
337
}
332
- else
333
- {
338
+ // else
339
+ // {
334
340
out += _content;
335
341
outLen += _contentLength;
336
342
_writtenLength += request->client ()->write (out.c_str (), outLen);
337
- }
343
+ // }
338
344
339
345
_state = RESPONSE_WAIT_ACK;
340
346
}
@@ -346,18 +352,19 @@ void AsyncBasicResponse::_respond(AsyncWebServerRequest *request)
346
352
347
353
if (_contentCstr)
348
354
{
349
- int deltaLen = out.length () - partial.length ();
355
+ // int deltaLen = out.length() - partial.length();
356
+
357
+ _partialHeader = out.substring (space);
350
358
351
- memmove (&_contentCstr[deltaLen], _contentCstr, deltaLen);
352
- memcpy (_contentCstr, out.substring (space).c_str (), deltaLen);
359
+ // memmove(&_contentCstr[deltaLen], _contentCstr, deltaLen);
360
+ // memcpy(_contentCstr, out.substring(space).c_str(), deltaLen);
353
361
}
354
362
else
355
363
{
356
364
_content = out.substring (space) + _content;
365
+ _contentLength += outLen - space;
357
366
}
358
367
359
- _contentLength += outLen - space;
360
-
361
368
AWS_LOGDEBUG1 (" partial =" , partial);
362
369
363
370
_writtenLength += request->client ()->write (partial.c_str (), partial.length ());
@@ -400,15 +407,15 @@ void AsyncBasicResponse::_respond(AsyncWebServerRequest *request)
400
407
401
408
if (_contentCstr)
402
409
{
403
- memmove (&_contentCstr[outLen], _contentCstr, _contentLength);
404
- memcpy (_contentCstr, out.c_str (), outLen);
410
+ _partialHeader = out;
411
+ // memmove(&_contentCstr[outLen], _contentCstr, _contentLength);
412
+ // memcpy(_contentCstr, out.c_str(), outLen);
405
413
}
406
414
else
407
415
{
408
416
_content = out + _content;
417
+ _contentLength += outLen;
409
418
}
410
-
411
- _contentLength += outLen;
412
419
_state = RESPONSE_CONTENT;
413
420
}
414
421
@@ -431,6 +438,31 @@ size_t AsyncBasicResponse::_ack(AsyncWebServerRequest *request, size_t len, uint
431
438
String out;
432
439
size_t available = _contentLength - _sentLength;
433
440
size_t space = request->client ()->space ();
441
+
442
+ if (_partialHeader.length () > 0 ) {
443
+ if (_partialHeader.length () > space) {
444
+ // Header longer than space - send a piece of it, and make the _partialHeader = to what remains
445
+ String _subHeader;
446
+ String tmpString;
447
+
448
+ _subHeader = _partialHeader.substring (0 , space);
449
+ tmpString = _partialHeader.substring (space);
450
+ _partialHeader = tmpString;
451
+
452
+ _writtenLength += request->client ()->write (_subHeader.c_str (), space);
453
+
454
+ return (_partialHeader.length ());
455
+ } else {
456
+ // _partialHeader is <= space length - therefore send the whole thing, and make the remaining length = to the _contrentLength
457
+ _writtenLength += request->client ()->write (_partialHeader.c_str (), _partialHeader.length ());
458
+
459
+ _partialHeader = String ();
460
+
461
+ return (_contentLength);
462
+ }
463
+ }
464
+
465
+ // if we are here - there is no _partialHJeader to send
434
466
435
467
AWS_LOGDEBUG3 (" AsyncAbstractResponse::_ack : available =" , available, " , space =" , space );
436
468
0 commit comments