From 917ce2d45294e5b3f3721e5f4a2a11a20a545291 Mon Sep 17 00:00:00 2001 From: Thulinma Date: Wed, 22 May 2019 00:08:35 +0200 Subject: [PATCH 1/4] Fixed Downloader class progress callbacks, added HTTP::Parser progress calculation function. --- lib/downloader.cpp | 20 ++++++++++++++++++-- lib/http_parser.cpp | 7 +++++++ lib/http_parser.h | 1 + 3 files changed, 26 insertions(+), 2 deletions(-) diff --git a/lib/downloader.cpp b/lib/downloader.cpp index 3168f9e5..de4e2626 100644 --- a/lib/downloader.cpp +++ b/lib/downloader.cpp @@ -183,7 +183,15 @@ namespace HTTP{ return true; // Success! } // reset the data timeout - reqTime = Util::bootSecs(); + if (reqTime != Util::bootSecs()){ + if (progressCallback != 0){ + if (!progressCallback()){ + WARN_MSG("Download aborted by callback"); + return false; + } + } + reqTime = Util::bootSecs(); + } } if (getSocket()){ FAIL_MSG("Timeout while retrieving %s (%zu/%" PRIu32 ")", link.getUrl().c_str(), @@ -239,7 +247,15 @@ namespace HTTP{ return true; // Success! } // reset the data timeout - reqTime = Util::bootSecs(); + if (reqTime != Util::bootSecs()){ + if (progressCallback != 0){ + if (!progressCallback()){ + WARN_MSG("Download aborted by callback"); + return false; + } + } + reqTime = Util::bootSecs(); + } } if (getSocket()){ FAIL_MSG("Timeout while retrieving %s", link.getUrl().c_str()); diff --git a/lib/http_parser.cpp b/lib/http_parser.cpp index fe62e30d..11522373 100644 --- a/lib/http_parser.cpp +++ b/lib/http_parser.cpp @@ -751,6 +751,13 @@ bool HTTP::Parser::Read(std::string &strbuf){ return parse(strbuf); }// HTTPReader::Read +/// Checks download completion percentage. +/// Returns zero if that doesn't make sense at the time or cannot be determined. +uint8_t HTTP::Parser::getPercentage() const{ + if (!seenHeaders || length < 1){return 0;} + return ((body.length() * 100) / length); +} + /// Attempt to read a whole HTTP response or request from a data buffer. /// If succesful, fills its own fields with the proper data and removes the response/request /// from the data buffer. diff --git a/lib/http_parser.h b/lib/http_parser.h index cbb0e265..a2080cb1 100644 --- a/lib/http_parser.h +++ b/lib/http_parser.h @@ -24,6 +24,7 @@ namespace HTTP{ const std::string &GetHeader(const std::string &i) const; bool hasHeader(const std::string &i) const; void clearHeader(const std::string &i); + uint8_t getPercentage() const; const std::string &GetVar(const std::string &i) const; std::string getUrl(); std::string allVars(); From d0868d0f42a57612789016a9d38f793bec500bf6 Mon Sep 17 00:00:00 2001 From: Thulinma Date: Wed, 22 May 2019 00:09:56 +0200 Subject: [PATCH 2/4] Added "version" element to JSON outputs of binaries, added version check in controller (ignores mismatched binaries now) --- src/controller/controller_capabilities.cpp | 18 +++++++++++++----- src/input/input.cpp | 1 + src/output/mist_out.cpp | 1 + 3 files changed, 15 insertions(+), 5 deletions(-) diff --git a/src/controller/controller_capabilities.cpp b/src/controller/controller_capabilities.cpp index 215db106..063fc7ff 100644 --- a/src/controller/controller_capabilities.cpp +++ b/src/controller/controller_capabilities.cpp @@ -36,17 +36,25 @@ namespace Controller { if ((*it).substr(0, 7) == "MistOut"){ arg_one = Util::getMyPath() + (*it); conn_args[0] = arg_one.c_str(); - capabilities["connectors"][(*it).substr(7)] = JSON::fromString(Util::Procs::getOutputOf((char**)conn_args)); - if (capabilities["connectors"][(*it).substr(7)].size() < 1){ - capabilities["connectors"].removeMember((*it).substr(7)); + std::string entryName = (*it).substr(7); + capabilities["connectors"][entryName] = JSON::fromString(Util::Procs::getOutputOf((char**)conn_args)); + if (capabilities["connectors"][entryName].size() < 1){ + capabilities["connectors"].removeMember(entryName); + }else if (capabilities["connectors"][entryName]["version"].asStringRef() != PACKAGE_VERSION){ + WARN_MSG("Output %s version mismatch (%s != " PACKAGE_VERSION ")", entryName.c_str(), capabilities["connectors"][entryName]["version"].asStringRef().c_str()); + capabilities["connectors"].removeMember(entryName); } } if ((*it).substr(0, 6) == "MistIn" && (*it) != "MistInfo"){ arg_one = Util::getMyPath() + (*it); conn_args[0] = arg_one.c_str(); - capabilities["inputs"][(*it).substr(6)] = JSON::fromString(Util::Procs::getOutputOf((char**)conn_args)); - if (capabilities["inputs"][(*it).substr(6)].size() < 1){ + std::string entryName = (*it).substr(6); + capabilities["inputs"][entryName] = JSON::fromString(Util::Procs::getOutputOf((char**)conn_args)); + if (capabilities["inputs"][entryName].size() < 1){ capabilities["inputs"].removeMember((*it).substr(6)); + }else if (capabilities["inputs"][entryName]["version"].asStringRef() != PACKAGE_VERSION){ + WARN_MSG("Input %s version mismatch (%s != " PACKAGE_VERSION ")", entryName.c_str(), capabilities["inputs"][entryName]["version"].asStringRef().c_str()); + capabilities["inputs"].removeMember(entryName); } } } diff --git a/src/input/input.cpp b/src/input/input.cpp index efd66668..b78b6f83 100644 --- a/src/input/input.cpp +++ b/src/input/input.cpp @@ -98,6 +98,7 @@ namespace Mist { Util::Config::streamName = streamName; if (config->getBool("json")) { + capa["version"] = PACKAGE_VERSION; std::cout << capa.toString() << std::endl; return 0; } diff --git a/src/output/mist_out.cpp b/src/output/mist_out.cpp index b5d02796..a0432cce 100644 --- a/src/output/mist_out.cpp +++ b/src/output/mist_out.cpp @@ -15,6 +15,7 @@ int main(int argc, char * argv[]) { mistOut::init(&conf); if (conf.parseArgs(argc, argv)) { if (conf.getBool("json")) { + mistOut::capa["version"] = PACKAGE_VERSION; std::cout << mistOut::capa.toString() << std::endl; return -1; } From 335a26df60b918a949371ac9af7f286f36931b24 Mon Sep 17 00:00:00 2001 From: Thulinma Date: Wed, 22 May 2019 00:10:10 +0200 Subject: [PATCH 3/4] Removed non-existing function from procs library header --- lib/procs.h | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/procs.h b/lib/procs.h index c0f7bb7f..e7edb4d0 100644 --- a/lib/procs.h +++ b/lib/procs.h @@ -20,7 +20,6 @@ namespace Util { static bool thread_handler;///< True while thread handler should be running. static void childsig_handler(int signum); static void exit_handler(); - static void runCmd(std::string & cmd); static char* const* dequeToArgv(std::deque & argDeq); static void grim_reaper(void * n); public: From 8d17d5f6c4fd95fddd74f5ccd62043babc223fa0 Mon Sep 17 00:00:00 2001 From: Thulinma Date: Wed, 22 May 2019 15:15:54 +0200 Subject: [PATCH 4/4] Removed FAIL message when trying ::accept() an already closed Socket::Server. --- lib/socket.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/socket.cpp b/lib/socket.cpp index 1a62cf85..4ab377fe 100644 --- a/lib/socket.cpp +++ b/lib/socket.cpp @@ -1272,7 +1272,9 @@ Socket::Connection Socket::Server::accept(bool nonblock){ // we could do this through accept4 with a flag, but that call is non-standard... if (r < 0){ if ((errno != EWOULDBLOCK) && (errno != EAGAIN) && (errno != EINTR)){ - FAIL_MSG("Error during accept: %s. Closing server socket %d.", strerror(errno), sock); + if (errno != EINVAL){ + FAIL_MSG("Error during accept: %s. Closing server socket %d.", strerror(errno), sock); + } close(); } return Socket::Connection();