diff --git a/lib/downloader.cpp b/lib/downloader.cpp index b682f3ff..b2ace65c 100644 --- a/lib/downloader.cpp +++ b/lib/downloader.cpp @@ -8,19 +8,24 @@ namespace HTTP{ Downloader::Downloader(){ progressCallback = 0; connectedPort = 0; + dataTimeout = 5; + retryCount = 5; ssl = false; proxied = false; char *p = getenv("http_proxy"); if (p){ proxyUrl = HTTP::URL(p); proxied = true; - INFO_MSG("Proxying through %s", proxyUrl.getUrl().c_str()); + MEDIUM_MSG("Proxying through %s", proxyUrl.getUrl().c_str()); } } /// Returns a reference to the internal HTTP::Parser body element std::string &Downloader::data(){return H.body;} + /// Returns a const reference to the internal HTTP::Parser body element + const std::string &Downloader::const_data() const{return H.body;} + /// Returns the status text of the HTTP Request. std::string &Downloader::getStatusText(){return H.method;} @@ -64,7 +69,6 @@ namespace HTTP{ void Downloader::doRequest(const HTTP::URL &link, const std::string &method, const std::string &body){ if (!canRequest(link)){return;} bool needSSL = (link.protocol == "https"); - INFO_MSG("Retrieving %s", link.getUrl().c_str()); H.Clean(); // Reconnect if needed if (!proxied || needSSL){ @@ -138,11 +142,12 @@ namespace HTTP{ /// Makes at most 5 attempts, and will wait no longer than 5 seconds without receiving data. bool Downloader::get(const HTTP::URL &link, uint8_t maxRecursiveDepth){ if (!canRequest(link)){return false;} - unsigned int loop = 6; // max 5 attempts + unsigned int loop = retryCount+1; // max 5 attempts while (--loop){// loop while we are unsuccessful + MEDIUM_MSG("Retrieving %s (%lu/%lu)", link.getUrl().c_str(), retryCount-loop+1, retryCount); doRequest(link); uint64_t reqTime = Util::bootSecs(); - while (getSocket() && Util::bootSecs() < reqTime + 5){ + while (getSocket() && Util::bootSecs() < reqTime + dataTimeout){ // No data? Wait for a second or so. if (!getSocket().spool()){ if (progressCallback != 0){ @@ -170,12 +175,14 @@ namespace HTTP{ } return true; // Success! } - // reset the 5 second timeout + // reset the data timeout reqTime = Util::bootSecs(); } if (getSocket()){ - FAIL_MSG("Timeout while retrieving %s", link.getUrl().c_str()); - return false; + FAIL_MSG("Timeout while retrieving %s (%lu/%lu)", link.getUrl().c_str(), retryCount-loop+1, retryCount); + getSocket().close(); + }else{ + FAIL_MSG("Lost connection while retrieving %s (%lu/%lu)", link.getUrl().c_str(), retryCount-loop+1, retryCount); } Util::sleep(500); // wait a bit before retrying } @@ -185,13 +192,14 @@ namespace HTTP{ bool Downloader::post(const HTTP::URL &link, const std::string &payload, bool sync, uint8_t maxRecursiveDepth){ if (!canRequest(link)){return false;} - unsigned int loop = 6; // max 5 attempts + unsigned int loop = retryCount; // max 5 attempts while (--loop){// loop while we are unsuccessful + MEDIUM_MSG("Posting to %s (%lu/%lu)", link.getUrl().c_str(), retryCount-loop+1, retryCount); doRequest(link, "POST", payload); //Not synced? Ignore the response and immediately return false. if (!sync){return false;} uint64_t reqTime = Util::bootSecs(); - while (getSocket() && Util::bootSecs() < reqTime + 5){ + while (getSocket() && Util::bootSecs() < reqTime + dataTimeout){ // No data? Wait for a second or so. if (!getSocket().spool()){ if (progressCallback != 0){ @@ -219,7 +227,7 @@ namespace HTTP{ } return true; // Success! } - // reset the 5 second timeout + // reset the data timeout reqTime = Util::bootSecs(); } if (getSocket()){ diff --git a/lib/downloader.h b/lib/downloader.h index 85f7f835..d5895dc0 100644 --- a/lib/downloader.h +++ b/lib/downloader.h @@ -6,6 +6,7 @@ namespace HTTP{ public: Downloader(); std::string &data(); + const std::string &const_data() const; void doRequest(const HTTP::URL &link, const std::string &method="", const std::string &body=""); bool get(const std::string &link); bool get(const HTTP::URL &link, uint8_t maxRecursiveDepth = 6); @@ -22,6 +23,7 @@ namespace HTTP{ bool canRequest(const HTTP::URL &link); Parser &getHTTP(); Socket::Connection &getSocket(); + uint32_t retryCount, dataTimeout; private: std::map extraHeaders; ///< Holds extra headers to sent with request diff --git a/lib/http_parser.cpp b/lib/http_parser.cpp index 088a6a9a..bbad592d 100644 --- a/lib/http_parser.cpp +++ b/lib/http_parser.cpp @@ -156,6 +156,14 @@ uint32_t HTTP::URL::getDefaultPort() const{ return 0; } +/// Returns the file extension of the URL, or an empty string if none. +std::string HTTP::URL::getExt() const{ + if (path.rfind('.') == std::string::npos){ + return ""; + } + return path.substr(path.rfind('.')+1); +} + /// Returns the full URL in string format std::string HTTP::URL::getUrl() const{ std::string ret; diff --git a/lib/http_parser.h b/lib/http_parser.h index 4b73ffbd..8880690b 100644 --- a/lib/http_parser.h +++ b/lib/http_parser.h @@ -78,6 +78,7 @@ namespace HTTP{ URL(const std::string &url = ""); uint32_t getPort() const; uint32_t getDefaultPort() const; + std::string getExt() const; std::string getUrl() const; std::string getFilePath() const; std::string getBareUrl() const; diff --git a/src/input/input_dtsc.cpp b/src/input/input_dtsc.cpp index 2d008435..1c3e22a8 100644 --- a/src/input/input_dtsc.cpp +++ b/src/input/input_dtsc.cpp @@ -173,10 +173,6 @@ namespace Mist { std::string givenStream = config->getString("streamname"); if (streamName == "") { streamName = givenStream; - }else{ - if (givenStream.find("+") != std::string::npos){ - streamName += givenStream.substr(givenStream.find("+")); - } } srcConn = Socket::Connection(host, port, true); if (!srcConn.connected()){