@@ -286,9 +286,9 @@ size_t AsyncBasicResponse::write_send_buffs(AsyncWebServerRequest *request, size
286286 size_t const pcb_written = request->client ()->add (_assembled_headers.c_str () + _writtenHeadersLength, _assembled_headers.length () - _writtenHeadersLength);
287287 _writtenLength += pcb_written;
288288 _writtenHeadersLength += pcb_written;
289- if (_writtenHeadersLength < _assembled_headers.length ()){
289+ if (_writtenHeadersLength < _assembled_headers.length ()) {
290290 // we were not able to fit all headers in current buff, send this part here and return later for the rest
291- if (!request->client ()->send ()){
291+ if (!request->client ()->send ()) {
292292 // something is wrong, what should we do here?
293293 request->client ()->close ();
294294 return 0 ;
@@ -303,10 +303,10 @@ size_t AsyncBasicResponse::write_send_buffs(AsyncWebServerRequest *request, size
303303
304304 if (_state == RESPONSE_CONTENT) {
305305 size_t const pcb_written = request->client ()->write (_content.c_str () + _sentLength, _content.length () - _sentLength);
306- _writtenLength += pcb_written; // total written data (hdrs + body)
307- _sentLength += pcb_written; // body written data
308- payloadlen += pcb_written; // data writtent in current buff
309- if (_sentLength >= _content.length ()){
306+ _writtenLength += pcb_written; // total written data (hdrs + body)
307+ _sentLength += pcb_written; // body written data
308+ payloadlen += pcb_written; // data writtent in current buff
309+ if (_sentLength >= _content.length ()) {
310310 // we've just sent all the (remainder) data in current buff, complete the response
311311 _state = RESPONSE_END;
312312 }
@@ -322,7 +322,7 @@ size_t AsyncBasicResponse::write_send_buffs(AsyncWebServerRequest *request, size
322322
323323/*
324324 * Abstract Response
325- *
325+ *
326326 */
327327AsyncAbstractResponse::AsyncAbstractResponse (AwsTemplateProcessor callback) : _callback(callback) {
328328 // In case of template processing, we're unable to determine real response size
@@ -363,7 +363,7 @@ size_t AsyncAbstractResponse::write_send_buffs(AsyncWebServerRequest *request, s
363363 _in_flight -= std::min (len, _in_flight);
364364 }
365365
366- if (_chunked || !_sendContentLength || (_sentLength > CONFIG_LWIP_TCP_WND_DEFAULT) ) {
366+ if (_chunked || !_sendContentLength || (_sentLength > CONFIG_LWIP_TCP_WND_DEFAULT)) {
367367 if (!_in_flight_credit || (ASYNC_RESPONCE_BUFF_SIZE > request->client ()->space ())) {
368368 // async_ws_log_d("defer user call in_flight:%u, tcpwin:%u", _in_flight, request->client()->space());
369369 // take the credit back since we are ignoring this ack and rely on other inflight data acks
@@ -386,13 +386,13 @@ size_t AsyncAbstractResponse::write_send_buffs(AsyncWebServerRequest *request, s
386386 size_t const pcb_written = request->client ()->add (_assembled_headers.c_str () + _writtenHeadersLength, _assembled_headers.length () - _writtenHeadersLength);
387387 _writtenLength += pcb_written;
388388 _writtenHeadersLength += pcb_written;
389- if (_writtenHeadersLength < _assembled_headers.length ()){
390- // we were not able to fit all headers in current buff, send this part here and return later for the rest
391- #if ASYNCWEBSERVER_USE_CHUNK_INFLIGHT
389+ if (_writtenHeadersLength < _assembled_headers.length ()) {
390+ // we were not able to fit all headers in current buff, send this part here and return later for the rest
391+ #if ASYNCWEBSERVER_USE_CHUNK_INFLIGHT
392392 _in_flight += pcb_written;
393393 --_in_flight_credit; // take a credit
394- #endif
395- if (!request->client ()->send ()){
394+ #endif
395+ if (!request->client ()->send ()) {
396396 // something is wrong, what should we do here?
397397 request->client ()->close ();
398398 return 0 ;
@@ -408,82 +408,87 @@ size_t AsyncAbstractResponse::write_send_buffs(AsyncWebServerRequest *request, s
408408 // send content body
409409 if (_state == RESPONSE_CONTENT) {
410410 do {
411- if (_send_buffer_len && _send_buffer){
411+ if (_send_buffer_len && _send_buffer) {
412412 // data is pending in buffer from a previous call or previous iteration
413- size_t const added_len = request->client ()->add (reinterpret_cast <char *>(_send_buffer->data () + _send_buffer_offset), _send_buffer_len - _send_buffer_offset);
414- if (added_len != _send_buffer_len - _send_buffer_offset){
413+ size_t const added_len =
414+ request->client ()->add (reinterpret_cast <char *>(_send_buffer->data () + _send_buffer_offset), _send_buffer_len - _send_buffer_offset);
415+ if (added_len != _send_buffer_len - _send_buffer_offset) {
415416 // we were not able to add entire buffer's content to tcp buffs, leave it for later
416417 // (this should not happen normally unless connection's TCP window suddenly changed from remote or mem pressure)
417418 _send_buffer_offset += added_len;
418419 break ;
419- } else
420- _send_buffer_len = _send_buffer_offset = 0 ; // consider buffer empty
420+ } else {
421+ _send_buffer_len = _send_buffer_offset = 0 ; // consider buffer empty
422+ }
421423 payloadlen += added_len;
422424 }
423425
424426 auto tcp_win = request->client ()->space ();
425- if (tcp_win == 0 || _state == RESPONSE_END)
427+ if (tcp_win == 0 || _state == RESPONSE_END) {
426428 break ; // no room left or no more data
429+ }
427430
428431 if ((_chunked || !_sendContentLength) && (tcp_win < CONFIG_LWIP_TCP_MSS / 2 )) {
429432 // available window size is not enough to send a new chunk sized half of tcp mss, let's wait for better chance and reduce pressure to AsyncTCP's event Q
430433 break ;
431434 }
432435
433- if (!_send_buffer){
436+ if (!_send_buffer) {
434437 auto p = new (std::nothrow) std::array<uint8_t , ASYNC_RESPONCE_BUFF_SIZE>;
435- if (p){
438+ if (p) {
436439 _send_buffer.reset (p);
437440 _send_buffer_len = _send_buffer_offset = 0 ;
438- } else
439- break ; // OOM
441+ } else {
442+ break ; // OOM
443+ }
440444 }
441445
442446 if (_chunked) {
443447 // HTTP 1.1 allows leading zeros in chunk length. Or spaces may be added.
444448 // See https://datatracker.ietf.org/doc/html/rfc9112#section-7.1
445- size_t const readLen = _fillBufferAndProcessTemplates (_send_buffer->data () + 6 , std::min (_send_buffer->size (), tcp_win) - 8 ); // reserve 8 bytes for chunk size data
449+ size_t const readLen =
450+ _fillBufferAndProcessTemplates (_send_buffer->data () + 6 , std::min (_send_buffer->size (), tcp_win) - 8 ); // reserve 8 bytes for chunk size data
446451 if (readLen != RESPONSE_TRY_AGAIN) {
447- sprintf (reinterpret_cast <char *>(_send_buffer->data ()), " %04x\r\n " , readLen); // print chunk size in buffer
452+ sprintf (reinterpret_cast <char *>(_send_buffer->data ()), " %04x\r\n " , readLen); // print chunk size in buffer
448453 _send_buffer->at (readLen + 6 ) = ' \r ' ;
449454 _send_buffer->at (readLen + 7 ) = ' \n ' ;
450- _send_buffer_len += readLen + 8 ; // set buffers's size to match added data
451- _sentLength += readLen; // data is not sent yet, but we won't get a chance to count this later properly for chunked data
452- if (!readLen){
455+ _send_buffer_len += readLen + 8 ; // set buffers's size to match added data
456+ _sentLength += readLen; // data is not sent yet, but we won't get a chance to count this later properly for chunked data
457+ if (!readLen) {
453458 // last chunk?
454459 _state = RESPONSE_END;
455460 }
456461 }
457462 } else {
458463 size_t const readLen = _fillBufferAndProcessTemplates (_send_buffer->data (), std::min (_send_buffer->size (), tcp_win));
459- if (readLen == 0 ){
464+ if (readLen == 0 ) {
460465 // no more data to send
461466 _state = RESPONSE_END;
462467 } else if (readLen != RESPONSE_TRY_AGAIN) {
463- _send_buffer_len += readLen; // set buffers's size to match added data
464- _sentLength += readLen; // data is not sent yet, but we need it to uderstand that it would be last block
465- if (_sendContentLength && (_sentLength == _contentLength)){
468+ _send_buffer_len += readLen; // set buffers's size to match added data
469+ _sentLength += readLen; // data is not sent yet, but we need it to uderstand that it would be last block
470+ if (_sendContentLength && (_sentLength == _contentLength)) {
466471 // it was last piece of content
467472 _state = RESPONSE_END;
468473 }
469474 }
470475 }
471- } while (_send_buffer_len); // go on till we have something in buffer pending to send
476+ } while (_send_buffer_len); // go on till we have something in buffer pending to send
472477
473478 // execute sending whatever we have in sock buffs now
474479 request->client ()->send ();
475480 _writtenLength += payloadlen;
476- #if ASYNCWEBSERVER_USE_CHUNK_INFLIGHT
481+ #if ASYNCWEBSERVER_USE_CHUNK_INFLIGHT
477482 _in_flight += payloadlen;
478483 --_in_flight_credit; // take a credit
479- #endif
480- if (_send_buffer_len == 0 ){
484+ #endif
485+ if (_send_buffer_len == 0 ) {
481486 // buffer empty, we can release mem, otherwise need to keep it till next run (should not happen under normal conditions)
482487 _send_buffer.reset ();
483488 }
484489 return payloadlen;
485- } // (_state == RESPONSE_CONTENT)
486-
490+ } // (_state == RESPONSE_CONTENT)
491+
487492 // implicit check
488493 if (_state == RESPONSE_WAIT_ACK) {
489494 // we do not need to wait for any acks actually if we won't send any more data,
@@ -517,7 +522,7 @@ size_t AsyncAbstractResponse::_fillBufferAndProcessTemplates(uint8_t *data, size
517522 // Now we've read 'len' bytes, either from cache or from file
518523 // Search for template placeholders
519524 uint8_t *pTemplateStart = data;
520- while ((pTemplateStart < &data[len]) && (pTemplateStart = (uint8_t *)memchr (pTemplateStart, TEMPLATE_PLACEHOLDER, &data[len - 1 ] - pTemplateStart + 1 )) ) {
525+ while ((pTemplateStart < &data[len]) && (pTemplateStart = (uint8_t *)memchr (pTemplateStart, TEMPLATE_PLACEHOLDER, &data[len - 1 ] - pTemplateStart + 1 ))) {
521526 // data[0] ... data[len - 1]
522527 uint8_t *pTemplateEnd =
523528 (pTemplateStart < &data[len - 1 ]) ? (uint8_t *)memchr (pTemplateStart + 1 , TEMPLATE_PLACEHOLDER, &data[len - 1 ] - pTemplateStart) : nullptr ;
0 commit comments