From cd7ef6b57aeb1114d52c026385ba371e4d5ffcc1 Mon Sep 17 00:00:00 2001 From: zjp8369 <605146581@qq.com> Date: Tue, 6 Jan 2026 13:58:38 +0800 Subject: [PATCH] Update IXWebSocketHttpHeaders.cpp MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修改header中值为空时,程序崩溃问题 --- ixwebsocket/IXWebSocketHttpHeaders.cpp | 33 ++++++++++++++++++++++---- 1 file changed, 29 insertions(+), 4 deletions(-) diff --git a/ixwebsocket/IXWebSocketHttpHeaders.cpp b/ixwebsocket/IXWebSocketHttpHeaders.cpp index 55634301..7d66ff08 100644 --- a/ixwebsocket/IXWebSocketHttpHeaders.cpp +++ b/ixwebsocket/IXWebSocketHttpHeaders.cpp @@ -12,6 +12,23 @@ namespace ix { + static inline void ltrim(std::string& s) { + s.erase(s.begin(), std::find_if(s.begin(), s.end(), [](int ch) { + return !std::isspace(ch); + })); + } + + static inline void rtrim(std::string& s) { + s.erase(std::find_if(s.rbegin(), s.rend(), [](int ch) { + return !std::isspace(ch); + }).base(), s.end()); + } + + static inline void trim(std::string& s) { + ltrim(s); + rtrim(s); + } + std::pair parseHttpHeaders( std::unique_ptr& socket, const CancellationRequest& isCancellationRequested) { @@ -62,10 +79,18 @@ namespace ix start++; } - std::string name(lineStr.substr(0, colon)); - std::string value(lineStr.substr(start, lineStr.size() - start - 2)); - - headers[name] = value; + int nSize = std::min((size_t)1024, lineStr.size()); + if (colon <= nSize) + { + std::string name(lineStr.substr(0, colon)); + std::string value; + if (start < nSize) + { + value = lineStr.substr(start); + trim(value); + } + headers[name] = value; + } } }