From f4aaaa24626226903a6ef8a4daa08530409b4f26 Mon Sep 17 00:00:00 2001 From: Stephan Hadinger Date: Thu, 27 Jan 2022 18:48:57 +0100 Subject: [PATCH] Berry add limit to tcp reads --- tasmota/xdrv_52_3_berry_webclient.ino | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/tasmota/xdrv_52_3_berry_webclient.ino b/tasmota/xdrv_52_3_berry_webclient.ino index 625c54622..71b0aaab4 100644 --- a/tasmota/xdrv_52_3_berry_webclient.ino +++ b/tasmota/xdrv_52_3_berry_webclient.ino @@ -325,10 +325,17 @@ extern "C" { int32_t wc_tcp_read(struct bvm *vm); int32_t wc_tcp_read(struct bvm *vm) { WiFiClient * tcp = wc_getwificlient(vm); + int32_t max_read = -1; // by default read as much as we can + if (be_top(vm) >= 2 && be_isint(vm, 2)) { + max_read = be_toint(vm, 2); + } int32_t btr = tcp->available(); if (btr <= 0) { be_pushstring(vm, ""); } else { + if ((max_read >= 0) && (btr > max_read)) { + btr = max_read; + } char * buf = (char*) be_pushbuffer(vm, btr); int32_t btr2 = tcp->read((uint8_t*) buf, btr); be_pushnstring(vm, buf, btr2); @@ -340,10 +347,17 @@ extern "C" { int32_t wc_tcp_readbytes(struct bvm *vm); int32_t wc_tcp_readbytes(struct bvm *vm) { WiFiClient * tcp = wc_getwificlient(vm); + int32_t max_read = -1; // by default read as much as we can + if (be_top(vm) >= 2 && be_isint(vm, 2)) { + max_read = be_toint(vm, 2); + } int32_t btr = tcp->available(); if (btr <= 0) { be_pushbytes(vm, nullptr, 0); } else { + if ((max_read >= 0) && (btr > max_read)) { + btr = max_read; + } uint8_t * buf = (uint8_t*) be_pushbuffer(vm, btr); int32_t btr2 = tcp->read(buf, btr); be_pushbytes(vm, buf, btr2);