From: Miod Vallat Date: Mon, 4 May 2026 09:46:09 +0000 (+0200) Subject: Maintain a "current size of received body" counter. X-Git-Tag: auth-5.1.0~85^2 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=edbcfd30481b60cbca5a44afeee51a0bc9e0ce18;p=thirdparty%2Fpdns.git Maintain a "current size of received body" counter. This allows us to get rid of synthesizing partial body contents as std::string objects, only to check for their length being still within allowed bounds. Signed-off-by: Miod Vallat --- diff --git a/ext/yahttp/yahttp/reqresp.cpp b/ext/yahttp/yahttp/reqresp.cpp index 7df4b4be8b..b774c87961 100644 --- a/ext/yahttp/yahttp/reqresp.cpp +++ b/ext/yahttp/yahttp/reqresp.cpp @@ -201,20 +201,25 @@ namespace YaHTTP { if (buffer.size() < chunk_size+2 || buffer.at(chunk_size+1) != '\n') return false; // expect newline after carriage return crlf=2; } else if (buffer.at(chunk_size) != '\n') return false; - if (bodybuf.str().length() + chunk_size > maxbody) { + if (bodysize + chunk_size > maxbody) { throw ParseError("Chunked body is too large"); } std::string tmp = buffer.substr(0, chunk_size); buffer.erase(buffer.begin(), buffer.begin()+chunk_size+crlf); bodybuf << tmp; + bodysize += chunk_size; chunk_size = 0; if (buffer.size() == 0) break; // just in case } } else { - if (bodybuf.str().length() + buffer.length() > maxbody) + if (bodysize + buffer.length() > maxbody) { bodybuf << buffer.substr(0, maxbody - bodybuf.str().length()); - else + bodysize = maxbody; + } + else { bodybuf << buffer; + bodysize += buffer.length(); + } buffer = ""; } } diff --git a/ext/yahttp/yahttp/reqresp.hpp b/ext/yahttp/yahttp/reqresp.hpp index b44bcd91b8..bb55a0737c 100644 --- a/ext/yahttp/yahttp/reqresp.hpp +++ b/ext/yahttp/yahttp/reqresp.hpp @@ -313,7 +313,8 @@ public: std::ostringstream bodybuf; //target->initialize(); }; // 1 && - (!hasBody || - (bodybuf.str().size() <= maxbody && - bodybuf.str().size() >= minbody) - ) - ); + (!hasBody || (bodysize <= maxbody && bodysize >= minbody))); }; //headers.find("content-type"); if (cpos != target->headers.end() && Utility::iequals(cpos->second, "application/x-www-form-urlencoded", 32)) { - target->postvars = Utility::parseUrlParameters(bodybuf.str()); + target->postvars = Utility::parseUrlParameters(body); } - target->body = bodybuf.str(); + target->body = std::move(body); } bodybuf.str(""); + bodysize = 0; this->target = NULL; }; //