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
/*
@@ -308,7 +307,7 @@ void AsyncBasicResponse::_respond(AsyncWebServerRequest *request)
308
307
size_t outLen = out.length ();
309
308
size_t space = request->client ()->space ();
310
309
311
- AWS_LOGDEBUG3 (" AsyncAbstractResponse ::_respond : Pre_respond, _contentLength =" , _contentLength, " , out =" , out );
310
+ AWS_LOGDEBUG3 (" AsyncBasicResponse ::_respond : Pre_respond, _contentLength =" , _contentLength, " , out =" , out );
312
311
AWS_LOGDEBUG3 (" outLen =" , outLen, " , _contentCstr =" , _contentCstr);
313
312
314
313
if (!_contentLength && space >= outLen)
@@ -322,26 +321,14 @@ void AsyncBasicResponse::_respond(AsyncWebServerRequest *request)
322
321
{
323
322
AWS_LOGDEBUG (" Step 2" );
324
323
325
-
326
324
if (_contentCstr)
327
325
{
328
- /*
329
- memmove(&_contentCstr[outLen], _contentCstr, _contentLength);
330
- memcpy(_contentCstr, out.c_str(), outLen);
331
- outLen += _contentLength;
332
-
333
- AWS_LOGDEBUG1("_contentCstr =", _contentCstr);
334
-
335
- _writtenLength += request->client()->write(_contentCstr, outLen);
336
- */
337
- _content = String (_contentCstr); // short _contentCstr - so just send as Arduino String - not much of a penalty - fall into below
326
+ _content = String (_contentCstr); // short _contentCstr - so just send as Arduino String - not much of a penalty - fall into below
338
327
}
339
- // else
340
- // {
341
- out += _content;
342
- outLen += _contentLength;
343
- _writtenLength += request->client ()->write (out.c_str (), outLen);
344
- // }
328
+
329
+ out += _content;
330
+ outLen += _contentLength;
331
+ _writtenLength += request->client ()->write (out.c_str (), outLen);
345
332
346
333
_state = RESPONSE_WAIT_ACK;
347
334
}
@@ -353,22 +340,18 @@ void AsyncBasicResponse::_respond(AsyncWebServerRequest *request)
353
340
354
341
if (_contentCstr)
355
342
{
356
- // int deltaLen = out.length() - partial.length();
357
-
358
- _partialHeader = out.substring (space);
359
-
360
- // memmove(&_contentCstr[deltaLen], _contentCstr, deltaLen);
361
- // memcpy(_contentCstr, out.substring(space).c_str(), deltaLen);
343
+ _partialHeader = out.substring (space);
362
344
}
363
345
else
364
346
{
365
347
_content = out.substring (space) + _content;
366
- _contentLength += outLen - space;
348
+ _contentLength += outLen - space;
367
349
}
368
350
369
351
AWS_LOGDEBUG1 (" partial =" , partial);
370
352
371
353
_writtenLength += request->client ()->write (partial.c_str (), partial.length ());
354
+
372
355
_state = RESPONSE_CONTENT;
373
356
}
374
357
else if (space > outLen && space < (outLen + _contentLength))
@@ -384,12 +367,21 @@ void AsyncBasicResponse::_respond(AsyncWebServerRequest *request)
384
367
{
385
368
char *s = (char *)malloc (shift + 1 );
386
369
387
- strncpy (s, _contentCstr, shift);
388
- s[shift] = ' \0 ' ;
389
- out += String (s);
390
- _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" );
391
382
392
- free (s);
383
+ return ;
384
+ }
393
385
}
394
386
else
395
387
{
@@ -408,19 +400,18 @@ void AsyncBasicResponse::_respond(AsyncWebServerRequest *request)
408
400
409
401
if (_contentCstr)
410
402
{
411
- _partialHeader = out;
412
- // memmove(&_contentCstr[outLen], _contentCstr, _contentLength);
413
- // memcpy(_contentCstr, out.c_str(), outLen);
403
+ _partialHeader = out;
414
404
}
415
405
else
416
406
{
417
407
_content = out + _content;
418
- _contentLength += outLen;
408
+ _contentLength += outLen;
419
409
}
410
+
420
411
_state = RESPONSE_CONTENT;
421
412
}
422
413
423
- AWS_LOGDEBUG3 (" AsyncAbstractResponse ::_respond : Post_respond, _contentLength =" , _contentLength, " , out =" , out );
414
+ AWS_LOGDEBUG3 (" AsyncBasicResponse ::_respond : Post_respond, _contentLength =" , _contentLength, " , out =" , out );
424
415
AWS_LOGDEBUG3 (" outLen =" , outLen, " , _contentCstr =" , _contentCstr);
425
416
}
426
417
@@ -439,38 +430,41 @@ size_t AsyncBasicResponse::_ack(AsyncWebServerRequest *request, size_t len, uint
439
430
String out;
440
431
size_t available = _contentLength - _sentLength;
441
432
size_t space = request->client ()->space ();
442
-
443
- if (_partialHeader.length () > 0 ) {
444
- if (_partialHeader.length () > space) {
445
- // Header longer than space - send a piece of it, and make the _partialHeader = to what remains
446
- String _subHeader;
447
- String tmpString;
448
-
449
- _subHeader = _partialHeader.substring (0 , space);
450
- tmpString = _partialHeader.substring (space);
451
- _partialHeader = tmpString;
452
-
453
- _writtenLength += request->client ()->write (_subHeader.c_str (), space);
454
-
455
- return (_partialHeader.length ());
456
- } else {
457
- // _partialHeader is <= space length - therefore send the whole thing, and make the remaining length = to the _contrentLength
458
- _writtenLength += request->client ()->write (_partialHeader.c_str (), _partialHeader.length ());
459
-
460
- _partialHeader = String ();
461
-
462
- return (_contentLength);
463
- }
464
- }
465
-
466
- // if we are here - there is no _partialHJeader to send
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
467
462
468
463
AWS_LOGDEBUG3 (" AsyncBasicResponse::_ack : available =" , available, " , space =" , space );
469
464
470
465
// we can fit in this packet
471
466
if (space > available)
472
467
{
473
- // Serial.println("In space>available");
474
468
AWS_LOGDEBUG1 (" AsyncBasicResponse::_ack : Pre_ack, _contentLength =" , _contentLength);
475
469
476
470
if (_contentCstr)
@@ -485,7 +479,7 @@ size_t AsyncBasicResponse::_ack(AsyncWebServerRequest *request, size_t len, uint
485
479
_writtenLength += request->client ()->write (_content.c_str (), available);
486
480
_content = String ();
487
481
}
488
-
482
+
489
483
_state = RESPONSE_WAIT_ACK;
490
484
491
485
return available;
@@ -495,11 +489,22 @@ size_t AsyncBasicResponse::_ack(AsyncWebServerRequest *request, size_t len, uint
495
489
if (_contentCstr)
496
490
{
497
491
char *s = (char *)malloc (space + 1 );
498
- strncpy (s, _contentCstr, space);
499
- s[space] = ' \0 ' ;
500
- out = String (s);
501
- _contentCstr += space;
502
- 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
+ }
503
508
}
504
509
else
505
510
{
@@ -1008,4 +1013,3 @@ size_t AsyncResponseStream::write(uint8_t data)
1008
1013
}
1009
1014
1010
1015
// ///////////////////////////////////////////////
1011
-
0 commit comments