summaryrefslogtreecommitdiff
path: root/src/syncevo/CurlTransportAgent.h
blob: deb457c55e85d1305494bdbe265263af980c067c (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
/*
 * 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
 */

#ifndef INCL_CURLTRANSPORTAGENT
#define INCL_CURLTRANSPORTAGENT

#include "config.h"

#ifdef ENABLE_LIBCURL

#include <syncevo/TransportAgent.h>
#include <curl/curl.h>

#include <syncevo/declarations.h>
SE_BEGIN_CXX


/**
 * message send/receive with curl
 *
 * The simple curl API is used, so sending blocks until the
 * reply is ready.
 */
class CurlTransportAgent : public HTTPTransportAgent
{
 public:
    CurlTransportAgent();
    ~CurlTransportAgent();

    virtual void setURL(const std::string &url);
    virtual void setProxy(const std::string &proxy);
    virtual void setProxyAuth(const std::string &user, const std::string &password);
    virtual void setSSL(const std::string &cacerts,
                        bool verifyServer,
                        bool verifyHost);
    virtual void setContentType(const std::string &type);
    virtual void setUserAgent(const std::string &agent);
    virtual void shutdown();
    virtual void send(const char *data, size_t len);
    virtual void cancel();
    virtual Status wait(bool noReply = false);
    virtual void getReply(const char *&data, size_t &len, std::string &contentType);
    virtual void setTimeout(int seconds);
    int processCallback();
    void setAborting(bool aborting) {m_aborting = aborting;}

 private:
    CURL *m_easyHandle;
    curl_slist *m_slist;
    std::string m_contentType;
    Status m_status;
    bool m_aborting;

    Timespec m_sendStartTime;
    int m_timeoutSeconds;

    /**
     * libcurl < 7.17.0 does not copy strings passed into curl_easy_setopt().
     * These are local copies that remain valid as long as needed.
     */
    std::string m_url, m_proxy, m_auth, m_agent,
        m_cacerts;

    /** message buffer (owned by caller) */
    const char *m_message;
    /** number of valid bytes in m_message */
    size_t m_messageLen;
    /** number of sent bytes in m_message */
    size_t m_messageSent;

    /** reply buffer */
    char *m_reply;
    /** number of valid bytes in m_reply */
    size_t m_replyLen;
    /** total buffer size */
    size_t m_replySize;

    /** error text from curl, set via CURLOPT_ERRORBUFFER */
    char m_curlErrorText[CURL_ERROR_SIZE];

    /** CURLOPT_READFUNCTION, stream == CurlTransportAgent */
    static size_t readDataCallback(void *buffer, size_t size, size_t nmemb, void *stream) throw();
    size_t readData(void *buffer, size_t size) throw();

    /** CURLOPT_WRITEFUNCTION, stream == CurlTransportAgent */
    static size_t writeDataCallback(void *ptr, size_t size, size_t nmemb, void *stream) throw();
    size_t writeData(void *buffer, size_t size) throw();

    /** CURLOPT_PROGRESS callback, use this function to detect user abort */
    static int progressCallback (void *ptr, double dltotal, double dlnow, double uptotal, double upnow);

    /** check curl error code and turn into exception */
    void checkCurl(CURLcode code, bool exception = true);

    /**
     * initialize curl if necessary, return new handle
     *
     * Never returns NULL, instead throws exceptions.
     */
    static CURL *easyInit();
};


SE_END_CXX

#endif // ENABLE_LIBCURL
#endif // INCL_TRANSPORTAGENT