Message Format

- Relaxed message parsing slightly.
This commit is contained in:
Paul Beckingham 2013-07-07 14:57:02 -04:00
parent 82c4b05fe4
commit 12809fd0bf
2 changed files with 20 additions and 15 deletions

View file

@ -145,26 +145,25 @@ bool Msg::parse (const std::string& input)
_header.clear ();
_payload = "";
std::vector <std::string> lines;
split (lines, input.substr (0, input.size ()), '\n');
std::string::size_type separator = input.find ("\n\n");
if (separator == std::string::npos)
throw std::string ("ERROR: Malformed message");
// Parse header.
std::vector <std::string> lines;
split (lines, input.substr (0, separator), '\n');
std::vector <std::string>::iterator i;
bool tripped = false;
for (i = lines.begin (); i != lines.end (); ++i)
{
if (*i == "")
tripped = true;
else if (tripped)
_payload += *i + "\n";
else
{
std::string::size_type delim = i->find (": ");
if (delim != std::string::npos)
_header[i->substr (0, delim)] = i->substr (delim + 2);
else
std::string::size_type delimiter = i->find (':');
if (delimiter == std::string::npos)
throw std::string ("ERROR: Malformed message header '") + *i + "'";
_header[trim (i->substr (0, delimiter))] = trim (i->substr (delimiter + 1));
}
}
// Parse payload.
_payload = input.substr (separator + 2);
return true;
}

View file

@ -36,7 +36,7 @@ Context context;
////////////////////////////////////////////////////////////////////////////////
int main (int argc, char** argv)
{
UnitTest t (8);
UnitTest t (12);
Msg m;
t.is (m.serialize (), std::string ("client: ") + PACKAGE_STRING + "\n\n\n", "Msg::serialize '' --> '\\n\\n'");
@ -55,6 +55,12 @@ int main (int argc, char** argv)
t.is (m2.get ("foo"), "bar", "Msg::get");
t.is (m2.get ("name"), "value", "Msg::get");
t.is (m2.getPayload (), "payload\n", "Msg::getPayload");
Msg m3;
t.ok (m3.parse ("foo:bar\nname: value\n\npayload\n"), "Msg::parse ok");
t.is (m3.get ("foo"), "bar", "Msg::get");
t.is (m3.get ("name"), "value", "Msg::get");
t.is (m3.getPayload (), "payload\n", "Msg::getPayload");
return 0;
}