Skip to content

Commit

Permalink
- handle optional carriage returns
Browse files Browse the repository at this point in the history
  • Loading branch information
aschnell committed Jul 8, 2024
1 parent fd03468 commit 622bb23
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 0 deletions.
13 changes: 13 additions & 0 deletions stomp/Stomp.cc
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ namespace Stomp
{
string line;
getline(is, line);
line = strip_cr(line);

if (state == State::Start)
{
Expand Down Expand Up @@ -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)
{
Expand Down
2 changes: 2 additions & 0 deletions stomp/Stomp.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
18 changes: 18 additions & 0 deletions stomp/testsuite/read1.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 19 additions & 0 deletions stomp/testsuite/strip.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@

#define BOOST_TEST_DYN_LINK
#define BOOST_TEST_MODULE snapper

#include <boost/test/unit_test.hpp>

#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");
}

0 comments on commit 622bb23

Please sign in to comment.