summaryrefslogtreecommitdiff
path: root/src/mongo/util/net/http_client_curl.cpp
blob: 0a1e7cce6d6f9277cac2a68abcab58ae650a09a1 (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
/**
 * Copyright (C) 2018 MongoDB Inc.
 *
 * This program is free software: you can redistribute it and/or  modify
 * it under the terms of the GNU Affero General Public License, version 3,
 * as published by the Free Software Foundation.
 *
 * This program 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 Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * As a special exception, the copyright holders give permission to link the
 * code of portions of this program with the OpenSSL library under certain
 * conditions as described in each individual source file and distribute
 * linked combinations including the program with the OpenSSL library. You
 * must comply with the GNU Affero General Public License in all respects
 * for all of the code used other than as permitted herein. If you modify
 * file(s) with this exception, you may extend this exception to your
 * version of the file(s), but you are not obligated to do so. If you do not
 * wish to do so, delete this exception statement from your version. If you
 * delete this exception statement from all source files in the program,
 * then also delete it in the license file.
 */

#define MONGO_LOG_DEFAULT_COMPONENT ::mongo::logger::LogComponent::kNetwork

#include "mongo/platform/basic.h"

#include <cstddef>
#include <curl/curl.h>
#include <curl/easy.h>
#include <string>

#include "mongo/base/data_builder.h"
#include "mongo/base/data_range.h"
#include "mongo/base/data_range_cursor.h"
#include "mongo/base/init.h"
#include "mongo/base/status.h"
#include "mongo/base/string_data.h"
#include "mongo/bson/bsonobj.h"
#include "mongo/util/assert_util.h"
#include "mongo/util/log.h"
#include "mongo/util/net/http_client.h"

namespace mongo {

namespace {

class CurlLibraryManager {
public:
    ~CurlLibraryManager() {
        if (_initialized) {
            curl_global_cleanup();
        }
    }

    Status initialize() {
        if (_initialized) {
            return Status::OK();
        }

        CURLcode ret = curl_global_init(CURL_GLOBAL_ALL);
        if (ret != CURLE_OK) {
            return {ErrorCodes::InternalError,
                    str::stream() << "Failed to initialize CURL: " << static_cast<int64_t>(ret)};
        }

        curl_version_info_data* version_data = curl_version_info(CURLVERSION_NOW);
        if (!(version_data->features & CURL_VERSION_SSL)) {
            return {ErrorCodes::InternalError, "Curl lacks SSL support, cannot continue"};
        }

        _initialized = true;
        return Status::OK();
    }

private:
    bool _initialized = false;
} curlLibraryManager;

/**
 * Receives data from the remote side.
 */
size_t WriteMemoryCallback(void* ptr, size_t size, size_t nmemb, void* data) {
    const size_t realsize = size * nmemb;

    auto* mem = reinterpret_cast<DataBuilder*>(data);
    if (!mem->writeAndAdvance(ConstDataRange(reinterpret_cast<const char*>(ptr),
                                             reinterpret_cast<const char*>(ptr) + realsize))
             .isOK()) {
        // Cause curl to generate a CURLE_WRITE_ERROR by returning a different number than how much
        // data there was to write.
        return 0;
    }

    return realsize;
}

/**
 * Sends data to the remote side
 */
size_t ReadMemoryCallback(char* buffer, size_t size, size_t nitems, void* instream) {

    auto* cdrc = reinterpret_cast<ConstDataRangeCursor*>(instream);

    size_t ret = 0;

    if (cdrc->length() > 0) {
        size_t readSize = std::min(size * nitems, cdrc->length());
        memcpy(buffer, cdrc->data(), readSize);
        invariant(cdrc->advance(readSize).isOK());
        ret = readSize;
    }

    return ret;
}

struct CurlEasyCleanup {
    void operator()(CURL* handle) {
        if (handle) {
            curl_easy_cleanup(handle);
        }
    }
};
using CurlHandle = std::unique_ptr<CURL, CurlEasyCleanup>;

struct CurlSlistFreeAll {
    void operator()(curl_slist* list) {
        if (list) {
            curl_slist_free_all(list);
        }
    }
};
using CurlSlist = std::unique_ptr<curl_slist, CurlSlistFreeAll>;

class CurlHttpClient : public HttpClient {
public:
    CurlHttpClient() {
        // Initialize a base handle with common settings.
        // Methods like requireHTTPS() will operate on this
        // base handle.
        _handle.reset(curl_easy_init());
        uassert(ErrorCodes::InternalError, "Curl initialization failed", _handle);

        curl_easy_setopt(_handle.get(), CURLOPT_CONNECTTIMEOUT, longSeconds(kConnectionTimeout));
        curl_easy_setopt(_handle.get(), CURLOPT_FOLLOWLOCATION, 0);
        curl_easy_setopt(_handle.get(), CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
        curl_easy_setopt(_handle.get(), CURLOPT_NOSIGNAL, 1);
        curl_easy_setopt(_handle.get(), CURLOPT_PROTOCOLS, CURLPROTO_HTTPS);
        curl_easy_setopt(_handle.get(), CURLOPT_TIMEOUT, longSeconds(kTotalRequestTimeout));
#if LIBCURL_VERSION_NUM > 0x072200
        // Requires >= 7.34.0
        curl_easy_setopt(_handle.get(), CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1_2);
#endif
        curl_easy_setopt(_handle.get(), CURLOPT_WRITEFUNCTION, WriteMemoryCallback);

        // TODO: CURLOPT_EXPECT_100_TIMEOUT_MS?
        // TODO: consider making this configurable
        // curl_easy_setopt(_handle.get(), CURLOPT_VERBOSE, 1);
        // curl_easy_setopt(_handle.get(), CURLOPT_DEBUGFUNCTION , ???);
    }

    ~CurlHttpClient() final = default;

    void allowInsecureHTTP(bool allow) final {
        if (allow) {
            curl_easy_setopt(_handle.get(), CURLOPT_PROTOCOLS, CURLPROTO_HTTPS | CURLPROTO_HTTP);
        } else {
            curl_easy_setopt(_handle.get(), CURLOPT_PROTOCOLS, CURLPROTO_HTTPS);
        }
    }

    void setHeaders(const std::vector<std::string>& headers) final {
        // Can't set on base handle because cURL doesn't deep-dup this field
        // and we don't want it getting overwritten while another thread is using it.
        _headers = headers;
    }

    void setTimeout(Seconds timeout) final {
        curl_easy_setopt(_handle.get(), CURLOPT_TIMEOUT, longSeconds(timeout));
    }

    void setConnectTimeout(Seconds timeout) final {
        curl_easy_setopt(_handle.get(), CURLOPT_CONNECTTIMEOUT, longSeconds(timeout));
    }

    DataBuilder get(StringData url) const final {
        // Make a local copy of the base handle for this request.
        CurlHandle myHandle(curl_easy_duphandle(_handle.get()));
        uassert(ErrorCodes::InternalError, "Curl initialization failed", myHandle);

        return doRequest(myHandle.get(), url);
    }

    DataBuilder post(StringData url, ConstDataRange cdr) const final {
        // Make a local copy of the base handle for this request.
        CurlHandle myHandle(curl_easy_duphandle(_handle.get()));
        uassert(ErrorCodes::InternalError, "Curl initialization failed", myHandle);

        curl_easy_setopt(myHandle.get(), CURLOPT_POST, 1);

        ConstDataRangeCursor cdrc(cdr);
        curl_easy_setopt(myHandle.get(), CURLOPT_READFUNCTION, ReadMemoryCallback);
        curl_easy_setopt(myHandle.get(), CURLOPT_READDATA, &cdrc);
        curl_easy_setopt(myHandle.get(), CURLOPT_POSTFIELDSIZE, (long)cdrc.length());

        return doRequest(myHandle.get(), url);
    }

private:
    /**
     * Helper for use with curl_easy_setopt which takes a vararg list,
     * and expects a long, not the long long durationCount() returns.
     */
    long longSeconds(Seconds tm) {
        return static_cast<long>(durationCount<Seconds>(tm));
    }

    DataBuilder doRequest(CURL* handle, StringData url) const {
        const auto urlString = url.toString();
        curl_easy_setopt(handle, CURLOPT_URL, urlString.c_str());

        DataBuilder dataBuilder(4096);
        curl_easy_setopt(handle, CURLOPT_WRITEDATA, &dataBuilder);

        curl_slist* chunk = nullptr;
        for (const auto& header : _headers) {
            chunk = curl_slist_append(chunk, header.c_str());
        }
        curl_easy_setopt(handle, CURLOPT_HTTPHEADER, chunk);
        CurlSlist _headers(chunk);

        CURLcode result = curl_easy_perform(handle);
        uassert(ErrorCodes::OperationFailed,
                str::stream() << "Bad HTTP response from API server: "
                              << curl_easy_strerror(result),
                result == CURLE_OK);

        long statusCode;
        result = curl_easy_getinfo(handle, CURLINFO_RESPONSE_CODE, &statusCode);
        uassert(ErrorCodes::OperationFailed,
                str::stream() << "Unexpected error retrieving response: "
                              << curl_easy_strerror(result),
                result == CURLE_OK);

        uassert(ErrorCodes::OperationFailed,
                str::stream() << "Unexpected http status code from server: " << statusCode,
                statusCode == 200);

        return dataBuilder;
    }

private:
    CurlHandle _handle;
    std::vector<std::string> _headers;
};

}  // namespace

// Transitional API used by blockstore to trigger libcurl init
// until it's been migrated to use the HTTPClient API.
Status curlLibraryManager_initialize() {
    return curlLibraryManager.initialize();
}

std::unique_ptr<HttpClient> HttpClient::create() {
    uassertStatusOK(curlLibraryManager.initialize());
    return std::make_unique<CurlHttpClient>();
}

}  // namespace mongo