From f532d965bf9858fabe9681ba20c3e73bf97d26d3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stephan=20A=C3=9Fmus?= Date: Thu, 2 Oct 2014 22:41:45 +0200 Subject: [PATCH] HaikuDepot: Escape strings in the JSON builder Fixes uploading comments which use any char that needs to be escaped. Such as line-breaks, " or \. --- src/apps/haikudepot/model/WebAppInterface.cpp | 23 ++++++++++++++----- 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/src/apps/haikudepot/model/WebAppInterface.cpp b/src/apps/haikudepot/model/WebAppInterface.cpp index 9bf7bc7a9b..0fb21f42df 100644 --- a/src/apps/haikudepot/model/WebAppInterface.cpp +++ b/src/apps/haikudepot/model/WebAppInterface.cpp @@ -91,8 +91,7 @@ public: fString << ",\""; else fString << '"'; - // TODO: Escape item - fString << item; + fString << _EscapeString(item); fString << '"'; } fInList = true; @@ -112,8 +111,7 @@ public: fString << "null"; } else { fString << '"'; - // TODO: Escape value - fString << value; + fString << _EscapeString(value); fString << '"'; } fInList = true; @@ -152,11 +150,24 @@ private: fString << ",\""; else fString << '"'; - // TODO: Escape name - fString << name; + fString << _EscapeString(name); fString << "\":"; } + BString _EscapeString(const char* original) const + { + BString string(original); + string.ReplaceAll("\\", "\\\\"); + string.ReplaceAll("\"", "\\\""); + string.ReplaceAll("/", "\\/"); + string.ReplaceAll("\b", "\\b"); + string.ReplaceAll("\f", "\\f"); + string.ReplaceAll("\n", "\\n"); + string.ReplaceAll("\r", "\\r"); + string.ReplaceAll("\t", "\\t"); + return string; + } + private: BString fString; bool fInList;