-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Server Checker: check short URLs asynchronously
- Loading branch information
Showing
6 changed files
with
128 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
#include "CheckShortUrlTask.h" | ||
|
||
namespace ServersListTool { | ||
|
||
CheckShortUrlTask::CheckShortUrlTask(std::shared_ptr<INetworkClientFactory> factory, std::string url, std::string shortUrl) | ||
: factory_(std::move(factory)) | ||
, url_(std::move(url)) | ||
, shortUrl_(std::move(shortUrl)) | ||
{ | ||
} | ||
|
||
void CheckShortUrlTask::run() | ||
{ | ||
if (isCanceled_) { | ||
onTaskFinished(this, false); | ||
return; | ||
} | ||
isInProgress_ = true; | ||
auto client = factory_->create(); | ||
|
||
client->setCurlOptionInt(CURLOPT_FOLLOWLOCATION, 0); | ||
|
||
bool ok = false; | ||
std::string targetUrl = url_; | ||
int i = 0; //counter for limiting max redirects | ||
if (!targetUrl.empty()) { | ||
int responseCode = 0; | ||
|
||
do { | ||
if (isCanceled_) { | ||
break; | ||
} | ||
client->setCurlOptionInt(CURLOPT_FOLLOWLOCATION, 0); | ||
client->doGet(targetUrl); | ||
|
||
responseCode = client->responseCode(); | ||
if (responseCode == 302 || responseCode == 301) { | ||
targetUrl = client->getCurlInfoString(CURLINFO_REDIRECT_URL); | ||
} | ||
|
||
i++; | ||
} while (i < 6 && !targetUrl.empty() && (responseCode == 302 || responseCode == 301) && targetUrl != shortUrl_); | ||
|
||
if (!targetUrl.empty() && targetUrl == shortUrl_) { | ||
ok = true; | ||
} | ||
} | ||
|
||
isInProgress_ = false; | ||
onTaskFinished(this, ok); | ||
} | ||
|
||
void CheckShortUrlTask::cancel() | ||
{ | ||
isCanceled_ = true; | ||
} | ||
|
||
bool CheckShortUrlTask::isCanceled() | ||
{ | ||
return isCanceled_; | ||
} | ||
|
||
bool CheckShortUrlTask::isInProgress() | ||
{ | ||
return isInProgress_; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
#pragma once | ||
|
||
#include <atomic> | ||
#include <string> | ||
#include <memory> | ||
|
||
#include <boost/signals2.hpp> | ||
|
||
#include "Core/TaskDispatcher.h" | ||
#include "Core/Network/INetworkClient.h" | ||
|
||
namespace ServersListTool { | ||
|
||
class CheckShortUrlTask : public CancellableTask { | ||
public: | ||
explicit CheckShortUrlTask(std::shared_ptr<INetworkClientFactory> factory, std::string url, std::string shortUrl); | ||
void run() override; | ||
void cancel() override; | ||
bool isCanceled() override; | ||
bool isInProgress() override; | ||
|
||
boost::signals2::signal<void(CheckShortUrlTask*, bool)> onTaskFinished; | ||
|
||
private: | ||
std::atomic<bool> isCanceled_; | ||
std::atomic<bool> isInProgress_; | ||
std::shared_ptr<INetworkClientFactory> factory_; | ||
std::string url_, shortUrl_; | ||
}; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters