Added retryCount and dataTimeout variables as well as a constant data accessor to HTTP::Downloader class

This commit is contained in:
Thulinma 2018-05-02 15:31:58 +02:00
parent f934d15a78
commit d7c021106f
2 changed files with 20 additions and 10 deletions

View file

@ -8,19 +8,24 @@ namespace HTTP{
Downloader::Downloader(){ Downloader::Downloader(){
progressCallback = 0; progressCallback = 0;
connectedPort = 0; connectedPort = 0;
dataTimeout = 5;
retryCount = 5;
ssl = false; ssl = false;
proxied = false; proxied = false;
char *p = getenv("http_proxy"); char *p = getenv("http_proxy");
if (p){ if (p){
proxyUrl = HTTP::URL(p); proxyUrl = HTTP::URL(p);
proxied = true; 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 /// Returns a reference to the internal HTTP::Parser body element
std::string &Downloader::data(){return H.body;} 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. /// Returns the status text of the HTTP Request.
std::string &Downloader::getStatusText(){return H.method;} 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){ void Downloader::doRequest(const HTTP::URL &link, const std::string &method, const std::string &body){
if (!canRequest(link)){return;} if (!canRequest(link)){return;}
bool needSSL = (link.protocol == "https"); bool needSSL = (link.protocol == "https");
INFO_MSG("Retrieving %s", link.getUrl().c_str());
H.Clean(); H.Clean();
// Reconnect if needed // Reconnect if needed
if (!proxied || needSSL){ 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. /// 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){ bool Downloader::get(const HTTP::URL &link, uint8_t maxRecursiveDepth){
if (!canRequest(link)){return false;} 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 while (--loop){// loop while we are unsuccessful
MEDIUM_MSG("Retrieving %s (%lu/%lu)", link.getUrl().c_str(), retryCount-loop+1, retryCount);
doRequest(link); doRequest(link);
uint64_t reqTime = Util::bootSecs(); 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. // No data? Wait for a second or so.
if (!getSocket().spool()){ if (!getSocket().spool()){
if (progressCallback != 0){ if (progressCallback != 0){
@ -170,12 +175,14 @@ namespace HTTP{
} }
return true; // Success! return true; // Success!
} }
// reset the 5 second timeout // reset the data timeout
reqTime = Util::bootSecs(); reqTime = Util::bootSecs();
} }
if (getSocket()){ if (getSocket()){
FAIL_MSG("Timeout while retrieving %s", link.getUrl().c_str()); FAIL_MSG("Timeout while retrieving %s (%lu/%lu)", link.getUrl().c_str(), retryCount-loop+1, retryCount);
return false; 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 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){ bool Downloader::post(const HTTP::URL &link, const std::string &payload, bool sync, uint8_t maxRecursiveDepth){
if (!canRequest(link)){return false;} 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 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); doRequest(link, "POST", payload);
//Not synced? Ignore the response and immediately return false. //Not synced? Ignore the response and immediately return false.
if (!sync){return false;} if (!sync){return false;}
uint64_t reqTime = Util::bootSecs(); 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. // No data? Wait for a second or so.
if (!getSocket().spool()){ if (!getSocket().spool()){
if (progressCallback != 0){ if (progressCallback != 0){
@ -219,7 +227,7 @@ namespace HTTP{
} }
return true; // Success! return true; // Success!
} }
// reset the 5 second timeout // reset the data timeout
reqTime = Util::bootSecs(); reqTime = Util::bootSecs();
} }
if (getSocket()){ if (getSocket()){

View file

@ -6,6 +6,7 @@ namespace HTTP{
public: public:
Downloader(); Downloader();
std::string &data(); std::string &data();
const std::string &const_data() const;
void doRequest(const HTTP::URL &link, const std::string &method="", const std::string &body=""); void doRequest(const HTTP::URL &link, const std::string &method="", const std::string &body="");
bool get(const std::string &link); bool get(const std::string &link);
bool get(const HTTP::URL &link, uint8_t maxRecursiveDepth = 6); bool get(const HTTP::URL &link, uint8_t maxRecursiveDepth = 6);
@ -22,6 +23,7 @@ namespace HTTP{
bool canRequest(const HTTP::URL &link); bool canRequest(const HTTP::URL &link);
Parser &getHTTP(); Parser &getHTTP();
Socket::Connection &getSocket(); Socket::Connection &getSocket();
uint32_t retryCount, dataTimeout;
private: private:
std::map<std::string, std::string> extraHeaders; ///< Holds extra headers to sent with request std::map<std::string, std::string> extraHeaders; ///< Holds extra headers to sent with request