1
1
/* ***************************************************************************************************************************
2
2
Portenta_H7_AsyncWebResponses.cpp
3
-
3
+
4
4
For Portenta_H7 (STM32H7) with Vision-Shield Ethernet
5
-
5
+
6
6
Portenta_H7_AsyncWebServer is a library for the Portenta_H7 with with Vision-Shield Ethernet
7
-
7
+
8
8
Based on and modified from ESPAsyncWebServer (https://github.com/me-no-dev/ESPAsyncWebServer)
9
9
Built by Khoi Hoang https://github.com/khoih-prog/Portenta_H7_AsyncWebServer
10
10
Licensed under GPLv3 license
11
-
11
+
12
12
Version: 1.4.0
13
-
13
+
14
14
Version Modified By Date Comments
15
15
------- ----------- ---------- -----------
16
16
1.0.0 K Hoang 06/10/2021 Initial coding for Portenta_H7 (STM32H7) with Vision-Shield Ethernet
@@ -248,7 +248,6 @@ size_t AsyncWebServerResponse::_ack(AsyncWebServerRequest *request, size_t len,
248
248
return 0 ;
249
249
}
250
250
251
-
252
251
// RSMOD///////////////////////////////////////////////
253
252
254
253
/*
@@ -260,6 +259,7 @@ AsyncBasicResponse::AsyncBasicResponse(int code, const String& contentType, cons
260
259
_content = String (" " );
261
260
_contentCstr = (char *)content; // RSMOD
262
261
_contentType = contentType;
262
+ _partialHeader = String ();
263
263
264
264
int iLen;
265
265
@@ -285,6 +285,7 @@ AsyncBasicResponse::AsyncBasicResponse(int code, const String& contentType, cons
285
285
_content = content;
286
286
_contentCstr = nullptr ; // RSMOD
287
287
_contentType = contentType;
288
+ _partialHeader = String ();
288
289
289
290
if (_content.length ())
290
291
{
@@ -306,7 +307,7 @@ void AsyncBasicResponse::_respond(AsyncWebServerRequest *request)
306
307
size_t outLen = out.length ();
307
308
size_t space = request->client ()->space ();
308
309
309
- AWS_LOGDEBUG3 (" AsyncAbstractResponse ::_respond : Pre_respond, _contentLength =" , _contentLength, " , out =" , out );
310
+ AWS_LOGDEBUG3 (" AsyncBasicResponse ::_respond : Pre_respond, _contentLength =" , _contentLength, " , out =" , out );
310
311
AWS_LOGDEBUG3 (" outLen =" , outLen, " , _contentCstr =" , _contentCstr);
311
312
312
313
if (!_contentLength && space >= outLen)
@@ -322,21 +323,13 @@ void AsyncBasicResponse::_respond(AsyncWebServerRequest *request)
322
323
323
324
if (_contentCstr)
324
325
{
325
- memmove (&_contentCstr[outLen], _contentCstr, _contentLength);
326
- memcpy (_contentCstr, out.c_str (), outLen);
327
- outLen += _contentLength;
328
-
329
- AWS_LOGDEBUG1 (" _contentCstr =" , _contentCstr);
330
-
331
- _writtenLength += request->client ()->write (_contentCstr, outLen);
332
- }
333
- else
334
- {
335
- out += _content;
336
- outLen += _contentLength;
337
- _writtenLength += request->client ()->write (out.c_str (), outLen);
326
+ _content = String (_contentCstr); // short _contentCstr - so just send as Arduino String - not much of a penalty - fall into below
338
327
}
339
328
329
+ out += _content;
330
+ outLen += _contentLength;
331
+ _writtenLength += request->client ()->write (out.c_str (), outLen);
332
+
340
333
_state = RESPONSE_WAIT_ACK;
341
334
}
342
335
else if (space && space < outLen)
@@ -347,21 +340,18 @@ void AsyncBasicResponse::_respond(AsyncWebServerRequest *request)
347
340
348
341
if (_contentCstr)
349
342
{
350
- int deltaLen = out.length () - partial.length ();
351
-
352
- memmove (&_contentCstr[deltaLen], _contentCstr, deltaLen);
353
- memcpy (_contentCstr, out.substring (space).c_str (), deltaLen);
343
+ _partialHeader = out.substring (space);
354
344
}
355
345
else
356
346
{
357
347
_content = out.substring (space) + _content;
348
+ _contentLength += outLen - space;
358
349
}
359
350
360
- _contentLength += outLen - space;
361
-
362
351
AWS_LOGDEBUG1 (" partial =" , partial);
363
352
364
353
_writtenLength += request->client ()->write (partial.c_str (), partial.length ());
354
+
365
355
_state = RESPONSE_CONTENT;
366
356
}
367
357
else if (space > outLen && space < (outLen + _contentLength))
@@ -377,12 +367,21 @@ void AsyncBasicResponse::_respond(AsyncWebServerRequest *request)
377
367
{
378
368
char *s = (char *)malloc (shift + 1 );
379
369
380
- strncpy (s, _contentCstr, shift);
381
- s[shift] = ' \0 ' ;
382
- out += String (s);
383
- _contentCstr += shift;
370
+ if (s != nullptr )
371
+ {
372
+ strncpy (s, _contentCstr, shift);
373
+ s[shift] = ' \0 ' ;
374
+ out += String (s);
375
+ _contentCstr += shift;
376
+
377
+ free (s);
378
+ }
379
+ else
380
+ {
381
+ AWS_LOGERROR (" AsyncBasicResponse::_respond: Out of heap" );
384
382
385
- free (s);
383
+ return ;
384
+ }
386
385
}
387
386
else
388
387
{
@@ -401,19 +400,18 @@ void AsyncBasicResponse::_respond(AsyncWebServerRequest *request)
401
400
402
401
if (_contentCstr)
403
402
{
404
- memmove (&_contentCstr[outLen], _contentCstr, _contentLength);
405
- memcpy (_contentCstr, out.c_str (), outLen);
403
+ _partialHeader = out;
406
404
}
407
405
else
408
406
{
409
407
_content = out + _content;
408
+ _contentLength += outLen;
410
409
}
411
-
412
- _contentLength += outLen;
410
+
413
411
_state = RESPONSE_CONTENT;
414
412
}
415
413
416
- AWS_LOGDEBUG3 (" AsyncAbstractResponse ::_respond : Post_respond, _contentLength =" , _contentLength, " , out =" , out );
414
+ AWS_LOGDEBUG3 (" AsyncBasicResponse ::_respond : Post_respond, _contentLength =" , _contentLength, " , out =" , out );
417
415
AWS_LOGDEBUG3 (" outLen =" , outLen, " , _contentCstr =" , _contentCstr);
418
416
}
419
417
@@ -433,12 +431,40 @@ size_t AsyncBasicResponse::_ack(AsyncWebServerRequest *request, size_t len, uint
433
431
size_t available = _contentLength - _sentLength;
434
432
size_t space = request->client ()->space ();
435
433
434
+ if (_partialHeader.length () > 0 )
435
+ {
436
+ if (_partialHeader.length () > space)
437
+ {
438
+ // Header longer than space - send a piece of it, and make the _partialHeader = to what remains
439
+ String _subHeader;
440
+ String tmpString;
441
+
442
+ _subHeader = _partialHeader.substring (0 , space);
443
+ tmpString = _partialHeader.substring (space);
444
+ _partialHeader = tmpString;
445
+
446
+ _writtenLength += request->client ()->write (_subHeader.c_str (), space);
447
+
448
+ return (_partialHeader.length ());
449
+ }
450
+ else
451
+ {
452
+ // _partialHeader is <= space length - therefore send the whole thing, and make the remaining length = to the _contrentLength
453
+ _writtenLength += request->client ()->write (_partialHeader.c_str (), _partialHeader.length ());
454
+
455
+ _partialHeader = String ();
456
+
457
+ return (_contentLength);
458
+ }
459
+ }
460
+
461
+ // if we are here - there is no _partialHJeader to send
462
+
436
463
AWS_LOGDEBUG3 (" AsyncBasicResponse::_ack : available =" , available, " , space =" , space );
437
464
438
465
// we can fit in this packet
439
466
if (space > available)
440
467
{
441
- // Serial.println("In space>available");
442
468
AWS_LOGDEBUG1 (" AsyncBasicResponse::_ack : Pre_ack, _contentLength =" , _contentLength);
443
469
444
470
if (_contentCstr)
@@ -453,7 +479,7 @@ size_t AsyncBasicResponse::_ack(AsyncWebServerRequest *request, size_t len, uint
453
479
_writtenLength += request->client ()->write (_content.c_str (), available);
454
480
_content = String ();
455
481
}
456
-
482
+
457
483
_state = RESPONSE_WAIT_ACK;
458
484
459
485
return available;
@@ -463,11 +489,22 @@ size_t AsyncBasicResponse::_ack(AsyncWebServerRequest *request, size_t len, uint
463
489
if (_contentCstr)
464
490
{
465
491
char *s = (char *)malloc (space + 1 );
466
- strncpy (s, _contentCstr, space);
467
- s[space] = ' \0 ' ;
468
- out = String (s);
469
- _contentCstr += space;
470
- free (s);
492
+
493
+ if (s != nullptr )
494
+ {
495
+ strncpy (s, _contentCstr, space);
496
+ s[space] = ' \0 ' ;
497
+ out = String (s);
498
+ _contentCstr += space;
499
+
500
+ free (s);
501
+ }
502
+ else
503
+ {
504
+ AWS_LOGERROR (" AsyncBasicResponse::_ack: Out of heap" );
505
+
506
+ return 0 ;
507
+ }
471
508
}
472
509
else
473
510
{
@@ -976,4 +1013,3 @@ size_t AsyncResponseStream::write(uint8_t data)
976
1013
}
977
1014
978
1015
// ///////////////////////////////////////////////
979
-
0 commit comments