aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib/arch/unix/ArchInternetUnix.cpp
blob: fd1e135b8117f4717360a4edc4665003e64164fb (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/*
 * barrier -- mouse and keyboard sharing utility
 * Copyright (C) 2014-2016 Symless Ltd.
 *
 * This package is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * found in the file LICENSE that should have accompanied this file.
 *
 * 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/>.
 */

#include "arch/unix/ArchInternetUnix.h"

#include "arch/XArch.h"
#include "common/Version.h"
#include "base/Log.h"

#include <sstream>
#include <curl/curl.h>

class CurlFacade {
public:
    CurlFacade();
    ~CurlFacade();
    String                get(const String& url);
    String                urlEncode(const String& url);

private:
    CURL*                m_curl;
};

//
// ArchInternetUnix
//

String
ArchInternetUnix::get(const String& url)
{
    CurlFacade curl;
    return curl.get(url);
}

String
ArchInternetUnix::urlEncode(const String& url)
{
    CurlFacade curl;
    return curl.urlEncode(url);
}

//
// CurlFacade
//

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;
}

CurlFacade::CurlFacade() :
    m_curl(NULL)
{
    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.");
    }
}

CurlFacade::~CurlFacade()
{
    if (m_curl != NULL) {
        curl_easy_cleanup(m_curl);
    }

    curl_global_cleanup();
}

String
CurlFacade::get(const String& url)
{
    curl_easy_setopt(m_curl, CURLOPT_URL, url.c_str());
    curl_easy_setopt(m_curl, CURLOPT_WRITEFUNCTION, curlWriteCallback);

    std::stringstream userAgent;
    userAgent << "Barrier ";
    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.");
    }
    
    return result;
}

String
CurlFacade::urlEncode(const String& url)
{
    char* resultCStr = curl_easy_escape(m_curl, url.c_str(), 0);

    if (resultCStr == NULL) {
        throw XArch("CURL escape failed.");
    }
    
    std::string result(resultCStr);
    curl_free(resultCStr);

    return result;
}