Improvements to Downloader class to allow for more flexibility in usage
This commit is contained in:
parent
96c451463b
commit
871bee3436
2 changed files with 45 additions and 28 deletions
|
@ -35,18 +35,19 @@ namespace HTTP{
|
||||||
/// Clears all extra/override headers for outgoing requests.
|
/// Clears all extra/override headers for outgoing requests.
|
||||||
void Downloader::clearHeaders(){extraHeaders.clear();}
|
void Downloader::clearHeaders(){extraHeaders.clear();}
|
||||||
|
|
||||||
/// Downloads the given URL into 'H', returns true on success.
|
/// Returns a reference to the internal HTTP class instance.
|
||||||
/// Makes at most 5 attempts, and will wait no longer than 5 seconds without receiving data.
|
Parser &Downloader::getHTTP(){return H;}
|
||||||
bool Downloader::get(const HTTP::URL &link, uint8_t maxRecursiveDepth){
|
|
||||||
if (!link.host.size()){return false;}
|
/// 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"){
|
if (link.protocol != "http"){
|
||||||
FAIL_MSG("Protocol not supported: %s", link.protocol.c_str());
|
FAIL_MSG("Protocol not supported: %s", link.protocol.c_str());
|
||||||
return false;
|
return;
|
||||||
}
|
}
|
||||||
INFO_MSG("Retrieving %s", link.getUrl().c_str());
|
INFO_MSG("Retrieving %s", link.getUrl().c_str());
|
||||||
unsigned int loop = 6; // max 5 attempts
|
|
||||||
|
|
||||||
while (--loop){// loop while we are unsuccessful
|
|
||||||
H.Clean();
|
H.Clean();
|
||||||
// Reconnect if needed
|
// Reconnect if needed
|
||||||
if (!S || link.host != connectedHost || link.getPort() != connectedPort){
|
if (!S || link.host != connectedHost || link.getPort() != connectedPort){
|
||||||
|
@ -73,6 +74,19 @@ namespace HTTP{
|
||||||
}
|
}
|
||||||
H.SendRequest(S);
|
H.SendRequest(S);
|
||||||
H.Clean();
|
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();
|
uint64_t reqTime = Util::bootSecs();
|
||||||
while (S && Util::bootSecs() < reqTime + 5){
|
while (S && Util::bootSecs() < reqTime + 5){
|
||||||
// No data? Wait for a second or so.
|
// No data? Wait for a second or so.
|
||||||
|
|
|
@ -6,6 +6,7 @@ namespace HTTP{
|
||||||
public:
|
public:
|
||||||
Downloader(){progressCallback = 0;}
|
Downloader(){progressCallback = 0;}
|
||||||
std::string &data();
|
std::string &data();
|
||||||
|
void doRequest(const HTTP::URL &link);
|
||||||
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);
|
||||||
std::string getHeader(const std::string &headerName);
|
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.
|
bool (*progressCallback)(); ///< Called every time the socket stalls, up to 4X per second.
|
||||||
void setHeader(const std::string &name, const std::string &val);
|
void setHeader(const std::string &name, const std::string &val);
|
||||||
void clearHeaders();
|
void clearHeaders();
|
||||||
|
Parser &getHTTP();
|
||||||
|
Socket::Connection &getSocket();
|
||||||
|
|
||||||
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
|
||||||
|
|
Loading…
Add table
Reference in a new issue