11/* ***************************************************************************************************************************
22 Portenta_H7_AsyncWebResponses.cpp
3-
3+
44 For Portenta_H7 (STM32H7) with Vision-Shield Ethernet
5-
5+
66 Portenta_H7_AsyncWebServer is a library for the Portenta_H7 with with Vision-Shield Ethernet
7-
7+
88 Based on and modified from ESPAsyncWebServer (https://github.com/me-no-dev/ESPAsyncWebServer)
99 Built by Khoi Hoang https://github.com/khoih-prog/Portenta_H7_AsyncWebServer
1010 Licensed under GPLv3 license
11-
11+
1212 Version: 1.4.0
13-
13+
1414 Version Modified By Date Comments
1515 ------- ----------- ---------- -----------
1616 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,
248248 return 0 ;
249249}
250250
251-
252251// RSMOD///////////////////////////////////////////////
253252
254253/*
@@ -308,7 +307,7 @@ void AsyncBasicResponse::_respond(AsyncWebServerRequest *request)
308307 size_t outLen = out.length ();
309308 size_t space = request->client ()->space ();
310309
311- AWS_LOGDEBUG3 (" AsyncAbstractResponse ::_respond : Pre_respond, _contentLength =" , _contentLength, " , out =" , out );
310+ AWS_LOGDEBUG3 (" AsyncBasicResponse ::_respond : Pre_respond, _contentLength =" , _contentLength, " , out =" , out );
312311 AWS_LOGDEBUG3 (" outLen =" , outLen, " , _contentCstr =" , _contentCstr);
313312
314313 if (!_contentLength && space >= outLen)
@@ -322,26 +321,14 @@ void AsyncBasicResponse::_respond(AsyncWebServerRequest *request)
322321 {
323322 AWS_LOGDEBUG (" Step 2" );
324323
325-
326324 if (_contentCstr)
327325 {
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
338327 }
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);
345332
346333 _state = RESPONSE_WAIT_ACK;
347334 }
@@ -353,22 +340,18 @@ void AsyncBasicResponse::_respond(AsyncWebServerRequest *request)
353340
354341 if (_contentCstr)
355342 {
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);
362344 }
363345 else
364346 {
365347 _content = out.substring (space) + _content;
366- _contentLength += outLen - space;
348+ _contentLength += outLen - space;
367349 }
368350
369351 AWS_LOGDEBUG1 (" partial =" , partial);
370352
371353 _writtenLength += request->client ()->write (partial.c_str (), partial.length ());
354+
372355 _state = RESPONSE_CONTENT;
373356 }
374357 else if (space > outLen && space < (outLen + _contentLength))
@@ -384,12 +367,21 @@ void AsyncBasicResponse::_respond(AsyncWebServerRequest *request)
384367 {
385368 char *s = (char *)malloc (shift + 1 );
386369
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" );
391382
392- free (s);
383+ return ;
384+ }
393385 }
394386 else
395387 {
@@ -408,19 +400,18 @@ void AsyncBasicResponse::_respond(AsyncWebServerRequest *request)
408400
409401 if (_contentCstr)
410402 {
411- _partialHeader = out;
412- // memmove(&_contentCstr[outLen], _contentCstr, _contentLength);
413- // memcpy(_contentCstr, out.c_str(), outLen);
403+ _partialHeader = out;
414404 }
415405 else
416406 {
417407 _content = out + _content;
418- _contentLength += outLen;
408+ _contentLength += outLen;
419409 }
410+
420411 _state = RESPONSE_CONTENT;
421412 }
422413
423- AWS_LOGDEBUG3 (" AsyncAbstractResponse ::_respond : Post_respond, _contentLength =" , _contentLength, " , out =" , out );
414+ AWS_LOGDEBUG3 (" AsyncBasicResponse ::_respond : Post_respond, _contentLength =" , _contentLength, " , out =" , out );
424415 AWS_LOGDEBUG3 (" outLen =" , outLen, " , _contentCstr =" , _contentCstr);
425416}
426417
@@ -439,38 +430,41 @@ size_t AsyncBasicResponse::_ack(AsyncWebServerRequest *request, size_t len, uint
439430 String out;
440431 size_t available = _contentLength - _sentLength;
441432 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
467462
468463 AWS_LOGDEBUG3 (" AsyncBasicResponse::_ack : available =" , available, " , space =" , space );
469464
470465 // we can fit in this packet
471466 if (space > available)
472467 {
473- // Serial.println("In space>available");
474468 AWS_LOGDEBUG1 (" AsyncBasicResponse::_ack : Pre_ack, _contentLength =" , _contentLength);
475469
476470 if (_contentCstr)
@@ -485,7 +479,7 @@ size_t AsyncBasicResponse::_ack(AsyncWebServerRequest *request, size_t len, uint
485479 _writtenLength += request->client ()->write (_content.c_str (), available);
486480 _content = String ();
487481 }
488-
482+
489483 _state = RESPONSE_WAIT_ACK;
490484
491485 return available;
@@ -495,11 +489,22 @@ size_t AsyncBasicResponse::_ack(AsyncWebServerRequest *request, size_t len, uint
495489 if (_contentCstr)
496490 {
497491 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+ }
503508 }
504509 else
505510 {
@@ -1008,4 +1013,3 @@ size_t AsyncResponseStream::write(uint8_t data)
10081013}
10091014
10101015// ///////////////////////////////////////////////
1011-
0 commit comments