From 0e0da824f71cf73d618febd8bd88a0cd45105eed Mon Sep 17 00:00:00 2001 From: Thulinma Date: Tue, 9 May 2017 11:31:24 +0200 Subject: [PATCH] Robustified stream/protocol related API calls --- lib/json.cpp | 14 ++++++++++++++ lib/json.h | 1 + src/controller/controller_api.cpp | 3 +++ src/controller/controller_streams.cpp | 12 ++++++++++++ 4 files changed, 30 insertions(+) diff --git a/lib/json.cpp b/lib/json.cpp index 1c4b9483..5f5752f1 100644 --- a/lib/json.cpp +++ b/lib/json.cpp @@ -1294,6 +1294,20 @@ void JSON::Value::removeMember(const std::map::iterator & i objVal.erase(it); } +void JSON::Value::removeNullMembers(){ + bool again = true; + while (again){ + again = false; + jsonForEach(*this, m){ + if (m.key().size() && m->isNull()){ + removeMember(m.key()); + again = true; + break; + } + } + } +} + /// For object JSON::Value objects, returns true if the /// given name is a member. Returns false otherwise. bool JSON::Value::isMember(const std::string & name) const { diff --git a/lib/json.h b/lib/json.h index 6ec14341..21966ec2 100644 --- a/lib/json.h +++ b/lib/json.h @@ -84,6 +84,7 @@ namespace JSON { void removeMember(const std::string & name); void removeMember(const std::deque::iterator & it); void removeMember(const std::map::iterator & it); + void removeNullMembers(); bool isMember(const std::string & name) const; bool isInt() const; bool isString() const; diff --git a/src/controller/controller_api.cpp b/src/controller/controller_api.cpp index 27a4233a..423fdd9d 100644 --- a/src/controller/controller_api.cpp +++ b/src/controller/controller_api.cpp @@ -167,6 +167,9 @@ int Controller::handleAPIConnection(Socket::Connection & conn){ /// Local-only helper function that checks for duplicate protocols and removes them static void removeDuplicateProtocols(){ JSON::Value & P = Controller::Storage["config"]["protocols"]; + jsonForEach(P, it){ + it->removeNullMembers(); + } std::set ignores; ignores.insert("online"); bool reloop = true; diff --git a/src/controller/controller_streams.cpp b/src/controller/controller_streams.cpp index 768242eb..565eb769 100644 --- a/src/controller/controller_streams.cpp +++ b/src/controller/controller_streams.cpp @@ -132,11 +132,23 @@ namespace Controller { if (out.isMember(jit.key())){ if ( !streamsEqual((*jit), out[jit.key()])){ out[jit.key()] = (*jit); + out[jit.key()].removeNullMembers(); out[jit.key()]["name"] = jit.key(); Log("STRM", std::string("Updated stream ") + jit.key()); } }else{ + std::string checked = jit.key(); + Util::sanitizeName(checked); + if (checked != jit.key() || !checked.size()){ + if (!checked.size()){ + FAIL_MSG("Invalid stream name '%s'", jit.key().c_str()); + }else{ + FAIL_MSG("Invalid stream name '%s'. Suggested alternative: '%s'", jit.key().c_str(), checked.c_str()); + } + continue; + } out[jit.key()] = (*jit); + out[jit.key()].removeNullMembers(); out[jit.key()]["name"] = jit.key(); Log("STRM", std::string("New stream ") + jit.key()); }