From fe6a0777a56f4bf41e0db89d13a1f1512a309004 Mon Sep 17 00:00:00 2001 From: Thulinma Date: Thu, 17 Jun 2021 14:44:24 +0200 Subject: [PATCH] Added socket lib strict mode spool, used in WebSocket::readFrame --- lib/socket.cpp | 4 ++-- lib/socket.h | 2 +- lib/websocket.cpp | 6 +++--- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/socket.cpp b/lib/socket.cpp index 8676fa9d..8120e4ef 100644 --- a/lib/socket.cpp +++ b/lib/socket.cpp @@ -932,9 +932,9 @@ uint64_t Socket::Connection::dataDown(){ /// Updates the downbuffer internal variable. /// Returns true if new data was received, false otherwise. -bool Socket::Connection::spool(){ +bool Socket::Connection::spool(bool strictMode){ /// \todo Provide better mechanism to prevent overbuffering. - if (downbuffer.size() > 10000){ + if (!strictMode && downbuffer.size() > 10000){ return true; }else{ return iread(downbuffer); diff --git a/lib/socket.h b/lib/socket.h index 9f06ca10..177e1edc 100644 --- a/lib/socket.h +++ b/lib/socket.h @@ -139,7 +139,7 @@ namespace Socket{ bool isAddress(const std::string &addr); bool isLocal(); ///< Returns true if remote address is a local address. // buffered i/o methods - bool spool(); ///< Updates the downbufferinternal variables. + bool spool(bool strictMode = false); ///< Updates the downbufferinternal variables. bool peek(); ///< Clears the downbuffer and fills it with peek Buffer &Received(); ///< Returns a reference to the download buffer. const Buffer &Received() const; ///< Returns a reference to the download buffer. diff --git a/lib/websocket.cpp b/lib/websocket.cpp index a0980f0b..05703b55 100644 --- a/lib/websocket.cpp +++ b/lib/websocket.cpp @@ -130,7 +130,7 @@ namespace HTTP{ while (true){ // Check if we can receive the minimum frame size (2 header bytes, 0 payload) if (!C.Received().available(2)){ - if (C.spool()){continue;} + if (C.spool(true)){continue;} return false; } std::string head = C.Received().copy(2); @@ -141,7 +141,7 @@ namespace HTTP{ if (headSize > 2){ // Check if we can receive the whole header if (!C.Received().available(headSize)){ - if (C.spool()){continue;} + if (C.spool(true)){continue;} return false; } // Read entire header, re-read real payload length @@ -154,7 +154,7 @@ namespace HTTP{ } // Check if we can receive the whole frame (header + payload) if (!C.Received().available(headSize + payLen)){ - if (C.spool()){continue;} + if (C.spool(true)){continue;} return false; } C.Received().remove(headSize); // delete the header