From: Arvin Schnell Date: Mon, 8 Jul 2024 09:43:07 +0000 (+0200) Subject: - handle optional carriage returns X-Git-Tag: v0.11.2~4^2~1 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=622bb2300c659b10ae7e7f5553f7368006557e16;p=thirdparty%2Fsnapper.git - handle optional carriage returns --- diff --git a/stomp/Stomp.cc b/stomp/Stomp.cc index ebdd2af9..6da1827b 100644 --- a/stomp/Stomp.cc +++ b/stomp/Stomp.cc @@ -48,6 +48,7 @@ namespace Stomp { string line; getline(is, line); + line = strip_cr(line); if (state == State::Start) { @@ -150,6 +151,18 @@ namespace Stomp } + std::string + strip_cr(const std::string& in) + { + string::size_type length = in.size(); + + if (length > 0 && in[length - 1] == '\r') + return in.substr(0, length - 1); + + return in; + } + + std::string escape_header(const std::string& in) { diff --git a/stomp/Stomp.h b/stomp/Stomp.h index 2220d8dc..1a024da6 100644 --- a/stomp/Stomp.h +++ b/stomp/Stomp.h @@ -51,6 +51,8 @@ namespace Stomp Message ack(); Message nack(); + std::string strip_cr(const std::string& in); + std::string escape_header(const std::string& in); std::string unescape_header(const std::string& in); diff --git a/stomp/testsuite/read1.cc b/stomp/testsuite/read1.cc index 916ea0f8..a5c31eb4 100644 --- a/stomp/testsuite/read1.cc +++ b/stomp/testsuite/read1.cc @@ -55,6 +55,24 @@ BOOST_AUTO_TEST_CASE(test2) } +BOOST_AUTO_TEST_CASE(cr1) +{ + // optional carriage returns + + istringstream s1("HELLO\r\nkey:value\r\n\r\nWORLD" + null); + istream s2(s1.rdbuf()); + + Message msg = read_message(s2); + + BOOST_CHECK_EQUAL(msg.command, "HELLO"); + + BOOST_CHECK_EQUAL(msg.headers.size(), 1); + BOOST_CHECK_EQUAL(msg.headers["key"], "value"); + + BOOST_CHECK_EQUAL(msg.body, "WORLD"); +} + + BOOST_AUTO_TEST_CASE(escape1) { // special characters in header diff --git a/stomp/testsuite/strip.cc b/stomp/testsuite/strip.cc new file mode 100644 index 00000000..685a285d --- /dev/null +++ b/stomp/testsuite/strip.cc @@ -0,0 +1,19 @@ + +#define BOOST_TEST_DYN_LINK +#define BOOST_TEST_MODULE snapper + +#include + +#include "../Stomp.h" + + +using namespace std; +using namespace Stomp; + + +BOOST_AUTO_TEST_CASE(cr) +{ + BOOST_CHECK_EQUAL(Stomp::strip_cr("hello"), "hello"); + BOOST_CHECK_EQUAL(Stomp::strip_cr("hello\r"), "hello"); + BOOST_CHECK_EQUAL(Stomp::strip_cr("hello\r\n"), "hello\r\n"); +}