2020-05-26 16:34:33 +00:00
|
|
|
//
|
2020-06-21 12:25:28 +00:00
|
|
|
// HttpReq.cpp
|
2020-05-26 16:34:33 +00:00
|
|
|
//
|
2020-06-21 12:25:28 +00:00
|
|
|
// HTTP request functions.
|
|
|
|
// Used by Scraper, GamesDBJSONScraper, GamesDBJSONScraperResources and
|
|
|
|
// ScreenScraper to download game information and media files.
|
2020-05-26 16:34:33 +00:00
|
|
|
//
|
|
|
|
|
2013-09-15 17:56:47 +00:00
|
|
|
#include "HttpReq.h"
|
2017-11-01 22:21:10 +00:00
|
|
|
|
2018-01-09 22:55:09 +00:00
|
|
|
#include "utils/FileSystemUtil.h"
|
2013-09-15 17:56:47 +00:00
|
|
|
#include "Log.h"
|
2018-01-09 22:55:09 +00:00
|
|
|
#include <assert.h>
|
2013-09-15 17:56:47 +00:00
|
|
|
|
2013-10-10 18:11:01 +00:00
|
|
|
CURLM* HttpReq::s_multi_handle = curl_multi_init();
|
|
|
|
|
|
|
|
std::map<CURL*, HttpReq*> HttpReq::s_requests;
|
2013-09-15 17:56:47 +00:00
|
|
|
|
2013-09-24 07:02:14 +00:00
|
|
|
std::string HttpReq::urlEncode(const std::string &s)
|
|
|
|
{
|
2020-05-26 16:34:33 +00:00
|
|
|
const std::string unreserved =
|
2020-06-21 12:25:28 +00:00
|
|
|
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_.~";
|
2013-09-24 07:02:14 +00:00
|
|
|
|
|
|
|
std::string escaped="";
|
2020-05-26 16:34:33 +00:00
|
|
|
for (size_t i=0; i<s.length(); i++) {
|
|
|
|
if (unreserved.find_first_of(s[i]) != std::string::npos) {
|
2013-09-24 07:02:14 +00:00
|
|
|
escaped.push_back(s[i]);
|
|
|
|
}
|
2020-05-26 16:34:33 +00:00
|
|
|
else {
|
2013-09-24 07:02:14 +00:00
|
|
|
escaped.append("%");
|
|
|
|
char buf[3];
|
2014-11-29 20:42:30 +00:00
|
|
|
sprintf(buf, "%.2X", (unsigned char)s[i]);
|
2013-09-24 07:02:14 +00:00
|
|
|
escaped.append(buf);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return escaped;
|
|
|
|
}
|
|
|
|
|
2013-10-10 00:50:42 +00:00
|
|
|
bool HttpReq::isUrl(const std::string& str)
|
|
|
|
{
|
2020-06-21 12:25:28 +00:00
|
|
|
// The worst guess.
|
|
|
|
return (!str.empty() && !Utils::FileSystem::exists(str) &&
|
|
|
|
(str.find("http://") != std::string::npos || str.find("https://") !=
|
|
|
|
std::string::npos || str.find("www.") != std::string::npos));
|
2013-10-10 00:50:42 +00:00
|
|
|
}
|
|
|
|
|
2020-06-06 11:10:33 +00:00
|
|
|
HttpReq::HttpReq(const std::string& url) : mStatus(REQ_IN_PROGRESS), mHandle(NULL)
|
2013-09-20 23:55:05 +00:00
|
|
|
{
|
2020-06-21 12:25:28 +00:00
|
|
|
mHandle = curl_easy_init();
|
|
|
|
|
|
|
|
if (mHandle == NULL) {
|
|
|
|
mStatus = REQ_IO_ERROR;
|
|
|
|
onError("curl_easy_init failed");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the url.
|
|
|
|
CURLcode err = curl_easy_setopt(mHandle, CURLOPT_URL, url.c_str());
|
|
|
|
if (err != CURLE_OK) {
|
|
|
|
mStatus = REQ_IO_ERROR;
|
|
|
|
onError(curl_easy_strerror(err));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set curl to handle redirects.
|
|
|
|
err = curl_easy_setopt(mHandle, CURLOPT_FOLLOWLOCATION, 1L);
|
|
|
|
if (err != CURLE_OK) {
|
|
|
|
mStatus = REQ_IO_ERROR;
|
|
|
|
onError(curl_easy_strerror(err));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set curl max redirects.
|
|
|
|
err = curl_easy_setopt(mHandle, CURLOPT_MAXREDIRS, 2L);
|
|
|
|
if (err != CURLE_OK) {
|
|
|
|
mStatus = REQ_IO_ERROR;
|
|
|
|
onError(curl_easy_strerror(err));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set curl restrict redirect protocols.
|
|
|
|
err = curl_easy_setopt(mHandle, CURLOPT_REDIR_PROTOCOLS, CURLPROTO_HTTP | CURLPROTO_HTTPS);
|
|
|
|
if (err != CURLE_OK) {
|
|
|
|
mStatus = REQ_IO_ERROR;
|
|
|
|
onError(curl_easy_strerror(err));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Tell curl how to write the data.
|
|
|
|
err = curl_easy_setopt(mHandle, CURLOPT_WRITEFUNCTION, &HttpReq::write_content);
|
|
|
|
if (err != CURLE_OK) {
|
|
|
|
mStatus = REQ_IO_ERROR;
|
|
|
|
onError(curl_easy_strerror(err));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Give curl a pointer to this HttpReq so we know where to write the
|
|
|
|
// data *to* in our write function.
|
|
|
|
err = curl_easy_setopt(mHandle, CURLOPT_WRITEDATA, this);
|
|
|
|
if (err != CURLE_OK) {
|
|
|
|
mStatus = REQ_IO_ERROR;
|
|
|
|
onError(curl_easy_strerror(err));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add the handle to our multi.
|
|
|
|
CURLMcode merr = curl_multi_add_handle(s_multi_handle, mHandle);
|
|
|
|
if (merr != CURLM_OK) {
|
|
|
|
mStatus = REQ_IO_ERROR;
|
|
|
|
onError(curl_multi_strerror(merr));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
s_requests[mHandle] = this;
|
2013-09-15 17:56:47 +00:00
|
|
|
}
|
|
|
|
|
2013-10-10 18:11:01 +00:00
|
|
|
HttpReq::~HttpReq()
|
2013-09-15 17:56:47 +00:00
|
|
|
{
|
2020-06-21 12:25:28 +00:00
|
|
|
if (mHandle) {
|
|
|
|
s_requests.erase(mHandle);
|
2013-09-15 17:56:47 +00:00
|
|
|
|
2020-06-21 12:25:28 +00:00
|
|
|
CURLMcode merr = curl_multi_remove_handle(s_multi_handle, mHandle);
|
2013-09-15 17:56:47 +00:00
|
|
|
|
2020-06-25 17:52:38 +00:00
|
|
|
if (merr != CURLM_OK) {
|
2020-06-21 12:25:28 +00:00
|
|
|
LOG(LogError) << "Error removing curl_easy handle from curl_multi: " <<
|
|
|
|
curl_multi_strerror(merr);
|
2020-06-25 17:52:38 +00:00
|
|
|
}
|
2013-09-15 17:56:47 +00:00
|
|
|
|
2020-06-21 12:25:28 +00:00
|
|
|
curl_easy_cleanup(mHandle);
|
|
|
|
}
|
2013-09-15 17:56:47 +00:00
|
|
|
}
|
|
|
|
|
2013-10-10 18:11:01 +00:00
|
|
|
HttpReq::Status HttpReq::status()
|
2013-09-15 17:56:47 +00:00
|
|
|
{
|
2020-06-21 12:25:28 +00:00
|
|
|
if (mStatus == REQ_IN_PROGRESS) {
|
|
|
|
int handle_count;
|
|
|
|
CURLMcode merr = curl_multi_perform(s_multi_handle, &handle_count);
|
|
|
|
if (merr != CURLM_OK && merr != CURLM_CALL_MULTI_PERFORM) {
|
|
|
|
mStatus = REQ_IO_ERROR;
|
|
|
|
onError(curl_multi_strerror(merr));
|
|
|
|
return mStatus;
|
|
|
|
}
|
|
|
|
|
|
|
|
int msgs_left;
|
|
|
|
CURLMsg* msg;
|
|
|
|
while ((msg = curl_multi_info_read(s_multi_handle, &msgs_left)) != nullptr) {
|
|
|
|
if (msg->msg == CURLMSG_DONE) {
|
|
|
|
HttpReq* req = s_requests[msg->easy_handle];
|
|
|
|
|
|
|
|
if (req == NULL) {
|
|
|
|
LOG(LogError) << "Cannot find easy handle!";
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (msg->data.result == CURLE_OK) {
|
|
|
|
req->mStatus = REQ_SUCCESS;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
req->mStatus = REQ_IO_ERROR;
|
|
|
|
req->onError(curl_easy_strerror(msg->data.result));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return mStatus;
|
2013-09-15 17:56:47 +00:00
|
|
|
}
|
|
|
|
|
2014-03-19 00:55:37 +00:00
|
|
|
std::string HttpReq::getContent() const
|
2013-09-15 17:56:47 +00:00
|
|
|
{
|
2020-06-21 12:25:28 +00:00
|
|
|
assert(mStatus == REQ_SUCCESS);
|
|
|
|
return mContent.str();
|
2013-09-15 17:56:47 +00:00
|
|
|
}
|
|
|
|
|
2013-10-10 18:11:01 +00:00
|
|
|
void HttpReq::onError(const char* msg)
|
2013-09-15 17:56:47 +00:00
|
|
|
{
|
2020-06-21 12:25:28 +00:00
|
|
|
mErrorMsg = msg;
|
2013-09-15 17:56:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string HttpReq::getErrorMsg()
|
|
|
|
{
|
2020-06-21 12:25:28 +00:00
|
|
|
return mErrorMsg;
|
2013-09-15 17:56:47 +00:00
|
|
|
}
|
2013-10-10 18:11:01 +00:00
|
|
|
|
2020-05-26 16:34:33 +00:00
|
|
|
// Used as a curl callback.
|
|
|
|
// size = size of an element, nmemb = number of elements.
|
|
|
|
// Return value is number of elements successfully read.
|
2013-10-10 18:11:01 +00:00
|
|
|
size_t HttpReq::write_content(void* buff, size_t size, size_t nmemb, void* req_ptr)
|
|
|
|
{
|
2020-06-21 12:25:28 +00:00
|
|
|
std::stringstream& ss = ((HttpReq*)req_ptr)->mContent;
|
|
|
|
ss.write((char*)buff, size * nmemb);
|
2013-10-10 18:11:01 +00:00
|
|
|
|
2020-06-21 12:25:28 +00:00
|
|
|
return nmemb;
|
2013-10-10 18:11:01 +00:00
|
|
|
}
|
|
|
|
|
2020-05-26 16:34:33 +00:00
|
|
|
// Used as a curl callback.
|
|
|
|
//int HttpReq::update_progress(void* req_ptr, double dlTotal,
|
|
|
|
// double dlNow, double ulTotal, double ulNow)
|
|
|
|
//{
|
|
|
|
//}
|