Improvements to Downloader class to allow for more flexibility in usage

This commit is contained in:
Thulinma 2017-07-25 11:50:04 +02:00
parent 96c451463b
commit 871bee3436
2 changed files with 45 additions and 28 deletions

View file

@ -35,18 +35,19 @@ namespace HTTP{
/// Clears all extra/override headers for outgoing requests.
void Downloader::clearHeaders(){extraHeaders.clear();}
/// Downloads the given URL into 'H', returns true on success.
/// 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 (!link.host.size()){return false;}
/// Returns a reference to the internal HTTP class instance.
Parser &Downloader::getHTTP(){return H;}
/// Returns a reference to the internal Socket::Connection class instance.
Socket::Connection &Downloader::getSocket(){return S;}
/// Sends a request for the given URL, does no waiting.
void Downloader::doRequest(const HTTP::URL &link){
if (link.protocol != "http"){
FAIL_MSG("Protocol not supported: %s", link.protocol.c_str());
return false;
return;
}
INFO_MSG("Retrieving %s", link.getUrl().c_str());
unsigned int loop = 6; // max 5 attempts
while (--loop){// loop while we are unsuccessful
H.Clean();
// Reconnect if needed
if (!S || link.host != connectedHost || link.getPort() != connectedPort){
@ -73,6 +74,19 @@ namespace HTTP{
}
H.SendRequest(S);
H.Clean();
}
/// Downloads the given URL into 'H', returns true on success.
/// 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 (!link.host.size()){return false;}
if (link.protocol != "http"){
FAIL_MSG("Protocol not supported: %s", link.protocol.c_str());
return false;
}
unsigned int loop = 6; // max 5 attempts
while (--loop){// loop while we are unsuccessful
doRequest(link);
uint64_t reqTime = Util::bootSecs();
while (S && Util::bootSecs() < reqTime + 5){
// No data? Wait for a second or so.

View file

@ -6,6 +6,7 @@ namespace HTTP{
public:
Downloader(){progressCallback = 0;}
std::string &data();
void doRequest(const HTTP::URL &link);
bool get(const std::string &link);
bool get(const HTTP::URL &link, uint8_t maxRecursiveDepth = 6);
std::string getHeader(const std::string &headerName);
@ -15,6 +16,8 @@ namespace HTTP{
bool (*progressCallback)(); ///< Called every time the socket stalls, up to 4X per second.
void setHeader(const std::string &name, const std::string &val);
void clearHeaders();
Parser &getHTTP();
Socket::Connection &getSocket();
private:
std::map<std::string, std::string> extraHeaders; ///< Holds extra headers to sent with request