summaryrefslogtreecommitdiff
path: root/src/syncevo/CurlTransportAgent.cpp
blob: 2e520527b5ce271f3fed6435dcd809d7a5bb55d3 (plain)
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
/*
 * Copyright (C) 2009 Intel Corporation
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) version 3.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301  USA
 */

#include <syncevo/CurlTransportAgent.h>
#include <syncevo/SuspendFlags.h>

#ifdef ENABLE_LIBCURL

#include <algorithm>
#include <ctime>
#include <syncevo/util.h>

#include <syncevo/declarations.h>
SE_BEGIN_CXX


CurlTransportAgent::CurlTransportAgent() :
    m_easyHandle(easyInit()),
    m_slist(NULL),
    m_status(INACTIVE),
    m_timeoutSeconds(0),
    m_reply(NULL),
    m_replyLen(0),
    m_replySize(0)
{
#ifdef ENABLE_MAEMO /* hack because Maemo doesn't support IPv6 yet */
    curl_easy_setopt(m_easyHandle, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
#endif
    /*
     * set up for post where message is pushed into curl via
     * its read callback and reply is stored in write callback
     */
    CURLcode code;
    if ((code = curl_easy_setopt(m_easyHandle, CURLOPT_NOPROGRESS, false)) ||
        (code = curl_easy_setopt(m_easyHandle, CURLOPT_PROGRESSFUNCTION, progressCallback)) ||
        (code = curl_easy_setopt(m_easyHandle, CURLOPT_WRITEFUNCTION, writeDataCallback)) ||
        (code = curl_easy_setopt(m_easyHandle, CURLOPT_WRITEDATA, (void *)this)) ||
        (code = curl_easy_setopt(m_easyHandle, CURLOPT_READFUNCTION, readDataCallback)) ||
        (code = curl_easy_setopt(m_easyHandle, CURLOPT_READDATA, (void *)this)) ||
        (code = curl_easy_setopt(m_easyHandle, CURLOPT_ERRORBUFFER, this->m_curlErrorText )) ||
        (code = curl_easy_setopt(m_easyHandle, CURLOPT_AUTOREFERER, true)) ||
        (code = curl_easy_setopt(m_easyHandle, CURLOPT_POST, true)) ||
        (code = curl_easy_setopt(m_easyHandle, CURLOPT_FOLLOWLOCATION, true))) {
        /* error encountered, throw exception */
        curl_easy_cleanup(m_easyHandle);
        checkCurl(code);
    }
}

CURL *CurlTransportAgent::easyInit()
{
    static bool initialized = false;
    static CURLcode initres;

    if (!initialized) {
        initres = curl_global_init(CURL_GLOBAL_ALL);
        initialized = true;
    }

    if (initres) {
        SE_THROW_EXCEPTION(TransportException, "global curl initialization failed");
    }
    CURL *handle = curl_easy_init();
    if (!handle) {
        SE_THROW_EXCEPTION(TransportException, "no curl handle");
    }
    return handle;
}

CurlTransportAgent::~CurlTransportAgent()
{
    if (m_reply) {
        free(m_reply);
    }
    curl_easy_cleanup(m_easyHandle);
    curl_slist_free_all(m_slist);
}

void CurlTransportAgent::setURL(const std::string &url)
{
    m_url = url;
    CURLcode code = curl_easy_setopt(m_easyHandle, CURLOPT_URL, m_url.c_str());
    checkCurl(code);
}

void CurlTransportAgent::setProxy(const std::string &proxy)
{
    m_proxy = proxy;
    CURLcode code = curl_easy_setopt(m_easyHandle, CURLOPT_PROXY, m_proxy.c_str());
    checkCurl(code);
}

void CurlTransportAgent::setProxyAuth(const std::string &user, const std::string &password)
{
    m_auth = user + ":" + password;
    CURLcode code = curl_easy_setopt(m_easyHandle, CURLOPT_PROXYUSERPWD,
                                     m_auth.c_str());
    checkCurl(code);
}

void CurlTransportAgent::setContentType(const std::string &type)
{
    m_contentType = type;
}

void CurlTransportAgent::setUserAgent(const std::string &agent)
{
    m_agent = agent;
    CURLcode code = curl_easy_setopt(m_easyHandle, CURLOPT_USERAGENT,
                                     m_agent.c_str());
    checkCurl(code);
}

void CurlTransportAgent::setSSL(const std::string &cacerts,
                                bool verifyServer,
                                bool verifyHost)
{
    m_cacerts = cacerts;
    CURLcode code = CURLE_OK;

    if (!m_cacerts.empty()) {
        if (isDir(m_cacerts)) {
            // libcurl + OpenSSL does not work with a directory set in CURLOPT_CAINFO.
            // Must set the directory name as CURLOPT_CAPATH.
            //
            // Hopefully libcurl NSS also finds the directory name
            // here ("NSS-powered libcurl provides the option only for
            // backward compatibility. ").
            code = curl_easy_setopt(m_easyHandle, CURLOPT_CAPATH, m_cacerts.c_str());
        } else {
            code = curl_easy_setopt(m_easyHandle, CURLOPT_CAINFO, m_cacerts.c_str());
        }
    }
    if (!code) {
        code = curl_easy_setopt(m_easyHandle, CURLOPT_SSL_VERIFYPEER, (long)verifyServer);
    }
    if (!code) {
        code = curl_easy_setopt(m_easyHandle, CURLOPT_SSL_VERIFYHOST, (long)(verifyHost ? 2 : 0));
    }
    checkCurl(code);
}

void CurlTransportAgent::setTimeout(int seconds)
{
    m_timeoutSeconds = seconds;
}

void CurlTransportAgent::shutdown()
{
}

void CurlTransportAgent::send(const char *data, size_t len)
{
    CURLcode code;

    m_replyLen = 0;
    m_message = data;
    m_messageSent = 0;
    m_messageLen = len;

    curl_slist_free_all(m_slist);
    m_slist = NULL;
    
    // Setting Expect explicitly prevents problems with certain
    // proxies: if curl is allowed to depend on Expect, then it will
    // send the POST header and wait for the servers reply that it is
    // allowed to continue. This will always be the case with a correctly
    // configured SyncML and because some proxies reject unknown Expect
    // requests, it is better not used.
    m_slist = curl_slist_append(m_slist, "Expect:");

    std::string contentHeader("Content-Type: ");
    contentHeader += m_contentType;
    m_slist = curl_slist_append(m_slist, contentHeader.c_str());

    m_status = ACTIVE;
    if (m_timeoutSeconds) {
        m_sendStartTime = Timespec::monotonic();
    }
    m_aborting = false;
    if ((code = curl_easy_setopt(m_easyHandle, CURLOPT_PROGRESSDATA, static_cast<void *> (this)))||
        (code = curl_easy_setopt(m_easyHandle, CURLOPT_HTTPHEADER, m_slist)) ||
        (code = curl_easy_setopt(m_easyHandle, CURLOPT_POSTFIELDSIZE, len))
       ){
        m_status = CANCELED;
        checkCurl(code);
    }

    if ((code = curl_easy_perform(m_easyHandle))) {
        m_status = FAILED;
        checkCurl(code, false);
    } else {
        m_status = GOT_REPLY;
    }
}

void CurlTransportAgent::cancel()
{
    /* nothing to do */
}

TransportAgent::Status CurlTransportAgent::wait(bool noReply)
{
    return m_status;
}

void CurlTransportAgent::getReply(const char *&data, size_t &len, std::string &contentType)
{
    data = m_reply;
    len = m_replyLen;
    const char *curlContentType;
    if (!curl_easy_getinfo(m_easyHandle, CURLINFO_CONTENT_TYPE, &curlContentType) &&
        curlContentType) {
        contentType = curlContentType;
    } else {
        // unknown
        contentType = "";
    }
}

size_t CurlTransportAgent::writeDataCallback(void *buffer, size_t size, size_t nmemb, void *stream) throw()
{
    return static_cast<CurlTransportAgent *>(stream)->writeData(buffer, size * nmemb);
}

size_t CurlTransportAgent::writeData(void *buffer, size_t size) throw()
{
    bool increase = false;
    while (m_replyLen + size > m_replySize) {
        m_replySize = m_replySize ? m_replySize * 2 : 64 * 1024;
        increase = true;
    }

    if (increase) {
        m_reply = (char *)realloc(m_reply, m_replySize);
        if (!m_reply) {
            m_replySize = 0;
            m_replyLen = 0;
            return 0;
        }
    }

    memcpy(m_reply + m_replyLen,
           buffer,
           size);
    m_replyLen += size;
    return size;
}

size_t CurlTransportAgent::readDataCallback(void *buffer, size_t size, size_t nmemb, void *stream) throw()
{
    return static_cast<CurlTransportAgent *>(stream)->readData(buffer, size * nmemb);
}

size_t CurlTransportAgent::readData(void *buffer, size_t size) throw()
{
    size_t curr = std::min(size, m_messageLen - m_messageSent);

    memcpy(buffer, m_message + m_messageSent, curr);
    m_messageSent += curr;
    return curr;
}

void CurlTransportAgent::checkCurl(CURLcode code, bool exception)
{
    if (code) {
        if(exception){
            SE_THROW_EXCEPTION(TransportException, m_curlErrorText);
        }else {
            SE_LOG_INFO(NULL, "CurlTransport Failure: %s", m_curlErrorText);
        }
    }
}

int CurlTransportAgent::progressCallback(void* transport, double, double, double, double)
{
    CurlTransportAgent *agent = static_cast<CurlTransportAgent *> (transport);
    SuspendFlags &flags = SuspendFlags::getSuspendFlags();
    // check signals and abort transfer?
    flags.printSignals();
    if (flags.getState() == SuspendFlags::ABORT) {
        agent->setAborting (true);
        return -1;
    }
    return agent->processCallback();
}

int CurlTransportAgent::processCallback()
{
    if (m_timeoutSeconds) {
        Timespec curTime = Timespec::monotonic();
        if (curTime > m_sendStartTime + m_timeoutSeconds) {
            m_status = TIME_OUT;
            return -1;
        }
    }
    return 0;
}

SE_END_CXX

#endif // ENABLE_LIBCURL