2014-02-04 19:41:29 +00:00
|
|
|
/*
|
2018-01-27 21:48:17 +00:00
|
|
|
* barrier -- mouse and keyboard sharing utility
|
2016-09-07 14:24:00 +00:00
|
|
|
* Copyright (C) 2014-2016 Symless Ltd.
|
2014-02-04 19:41:29 +00:00
|
|
|
*
|
|
|
|
* This package is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
2015-05-03 02:33:52 +00:00
|
|
|
* found in the file LICENSE that should have accompanied this file.
|
2014-02-04 19:41:29 +00:00
|
|
|
*
|
|
|
|
* This package is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2014-02-28 12:55:14 +00:00
|
|
|
#include "arch/unix/ArchInternetUnix.h"
|
|
|
|
|
2014-02-28 12:36:45 +00:00
|
|
|
#include "arch/XArch.h"
|
|
|
|
#include "common/Version.h"
|
|
|
|
#include "base/Log.h"
|
2014-02-05 14:29:50 +00:00
|
|
|
|
|
|
|
#include <sstream>
|
|
|
|
#include <curl/curl.h>
|
|
|
|
|
2014-03-14 20:34:19 +00:00
|
|
|
class CurlFacade {
|
|
|
|
public:
|
2016-12-28 11:50:32 +00:00
|
|
|
CurlFacade();
|
|
|
|
~CurlFacade();
|
2020-05-30 11:41:35 +00:00
|
|
|
std::string get(const std::string& url);
|
|
|
|
std::string urlEncode(const std::string& url);
|
2014-03-14 20:34:19 +00:00
|
|
|
|
|
|
|
private:
|
2016-12-28 11:50:32 +00:00
|
|
|
CURL* m_curl;
|
2014-03-14 20:34:19 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
//
|
2014-11-11 13:51:47 +00:00
|
|
|
// ArchInternetUnix
|
2014-03-14 20:34:19 +00:00
|
|
|
//
|
|
|
|
|
2020-05-30 11:41:35 +00:00
|
|
|
std::string ArchInternetUnix::get(const std::string& url)
|
2014-03-14 20:34:19 +00:00
|
|
|
{
|
2016-12-28 11:50:32 +00:00
|
|
|
CurlFacade curl;
|
|
|
|
return curl.get(url);
|
2014-03-14 20:34:19 +00:00
|
|
|
}
|
|
|
|
|
2020-05-30 11:41:35 +00:00
|
|
|
std::string ArchInternetUnix::urlEncode(const std::string& url)
|
2014-03-14 20:34:19 +00:00
|
|
|
{
|
2016-12-28 11:50:32 +00:00
|
|
|
CurlFacade curl;
|
|
|
|
return curl.urlEncode(url);
|
2014-03-14 20:34:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// CurlFacade
|
|
|
|
//
|
|
|
|
|
2014-02-05 14:29:50 +00:00
|
|
|
static size_t
|
|
|
|
curlWriteCallback(void *contents, size_t size, size_t nmemb, void *userp)
|
|
|
|
{
|
|
|
|
((std::string*)userp)->append((char*)contents, size * nmemb);
|
|
|
|
return size * nmemb;
|
|
|
|
}
|
2014-02-04 19:41:29 +00:00
|
|
|
|
2014-03-14 20:34:19 +00:00
|
|
|
CurlFacade::CurlFacade() :
|
2016-12-28 11:50:32 +00:00
|
|
|
m_curl(NULL)
|
2014-03-14 20:34:19 +00:00
|
|
|
{
|
2016-12-28 11:50:32 +00:00
|
|
|
CURLcode init = curl_global_init(CURL_GLOBAL_ALL);
|
|
|
|
if (init != CURLE_OK) {
|
|
|
|
throw XArch("CURL global init failed.");
|
|
|
|
}
|
|
|
|
|
|
|
|
m_curl = curl_easy_init();
|
|
|
|
if (m_curl == NULL) {
|
|
|
|
throw XArch("CURL easy init failed.");
|
|
|
|
}
|
2014-03-14 20:34:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
CurlFacade::~CurlFacade()
|
|
|
|
{
|
2016-12-28 11:50:32 +00:00
|
|
|
if (m_curl != NULL) {
|
|
|
|
curl_easy_cleanup(m_curl);
|
|
|
|
}
|
2014-03-14 20:34:19 +00:00
|
|
|
|
2016-12-28 11:50:32 +00:00
|
|
|
curl_global_cleanup();
|
2014-03-14 20:34:19 +00:00
|
|
|
}
|
|
|
|
|
2020-05-30 11:41:35 +00:00
|
|
|
std::string CurlFacade::get(const std::string& url)
|
2014-02-04 19:41:29 +00:00
|
|
|
{
|
2016-12-28 11:50:32 +00:00
|
|
|
curl_easy_setopt(m_curl, CURLOPT_URL, url.c_str());
|
|
|
|
curl_easy_setopt(m_curl, CURLOPT_WRITEFUNCTION, curlWriteCallback);
|
|
|
|
|
|
|
|
std::stringstream userAgent;
|
2018-01-27 21:48:17 +00:00
|
|
|
userAgent << "Barrier ";
|
2016-12-28 11:50:32 +00:00
|
|
|
userAgent << kVersion;
|
|
|
|
curl_easy_setopt(m_curl, CURLOPT_USERAGENT, userAgent.str().c_str());
|
|
|
|
|
|
|
|
std::string result;
|
|
|
|
curl_easy_setopt(m_curl, CURLOPT_WRITEDATA, &result);
|
|
|
|
|
|
|
|
CURLcode code = curl_easy_perform(m_curl);
|
|
|
|
if (code != CURLE_OK) {
|
|
|
|
LOG((CLOG_ERR "curl perform error: %s", curl_easy_strerror(code)));
|
|
|
|
throw XArch("CURL perform failed.");
|
|
|
|
}
|
|
|
|
|
2014-02-05 14:29:50 +00:00
|
|
|
return result;
|
2014-02-04 19:41:29 +00:00
|
|
|
}
|
2014-03-14 20:34:19 +00:00
|
|
|
|
2020-05-30 11:41:35 +00:00
|
|
|
std::string CurlFacade::urlEncode(const std::string& url)
|
2014-03-14 20:34:19 +00:00
|
|
|
{
|
2016-12-28 11:50:32 +00:00
|
|
|
char* resultCStr = curl_easy_escape(m_curl, url.c_str(), 0);
|
2014-03-14 20:34:19 +00:00
|
|
|
|
2016-12-28 11:50:32 +00:00
|
|
|
if (resultCStr == NULL) {
|
|
|
|
throw XArch("CURL escape failed.");
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string result(resultCStr);
|
|
|
|
curl_free(resultCStr);
|
2014-03-14 20:34:19 +00:00
|
|
|
|
2016-12-28 11:50:32 +00:00
|
|
|
return result;
|
2014-03-14 20:34:19 +00:00
|
|
|
}
|