summaryrefslogtreecommitdiff
path: root/src/mongo/util/net/http_client_curl.cpp
blob: 008161f6d0c96edb0abff2f2368dfa853977764d (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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
/**
 *    Copyright (C) 2018-present MongoDB, Inc.
 *
 *    This program is free software: you can redistribute it and/or modify
 *    it under the terms of the Server Side Public License, version 1,
 *    as published by MongoDB, Inc.
 *
 *    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
 *    Server Side Public License for more details.
 *
 *    You should have received a copy of the Server Side Public License
 *    along with this program. If not, see
 *    <http://www.mongodb.com/licensing/server-side-public-license>.
 *
 *    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 Server Side 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/bson/bsonobjbuilder.h"
#include "mongo/platform/mutex.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:
    // No copying and no moving because we give libcurl the address of our members.
    // In practice, we'll never want to copy/move this instance anyway,
    // but if that ever changes, we can write trivial implementations to deal with it.
    CurlLibraryManager(const CurlLibraryManager&) = delete;
    CurlLibraryManager& operator=(const CurlLibraryManager&) = delete;
    CurlLibraryManager(CurlLibraryManager&&) = delete;
    CurlLibraryManager& operator=(CurlLibraryManager&&) = delete;

    CurlLibraryManager() = default;
    ~CurlLibraryManager() {
        if (_share) {
            curl_share_cleanup(_share);
        }
        // Ordering matters: curl_global_cleanup() must happen last.
        if (_initialized) {
            curl_global_cleanup();
        }
    }

    Status initialize() {
        auto status = _initializeGlobal();
        if (!status.isOK()) {
            return status;
        }

        status = _initializeShare();
        if (!status.isOK()) {
            return status;
        }

        return Status::OK();
    }

    CURLSH* getShareHandle() const {
        return _share;
    }

private:
    Status _initializeGlobal() {
        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();
    }

    Status _initializeShare() {
        invariant(_initialized);
        if (_share) {
            return Status::OK();
        }

        _share = curl_share_init();
        curl_share_setopt(_share, CURLSHOPT_SHARE, CURL_LOCK_DATA_DNS);
        curl_share_setopt(_share, CURLSHOPT_SHARE, CURL_LOCK_DATA_CONNECT);
        curl_share_setopt(_share, CURLSHOPT_SHARE, CURL_LOCK_DATA_SSL_SESSION);
        curl_share_setopt(_share, CURLSHOPT_USERDATA, this);
        curl_share_setopt(_share, CURLSHOPT_LOCKFUNC, _lockShare);
        curl_share_setopt(_share, CURLSHOPT_UNLOCKFUNC, _unlockShare);

        return Status::OK();
    }


    static void _lockShare(CURL*, curl_lock_data lock_data, curl_lock_access, void* ctx) {
        // curl_lock_access maps to shared and single access, i.e. a read and exclusive lock
        // except the unlock method does not have curl_lock_access as a parameter so we map
        // all lock requests to regular mutexes
        switch (lock_data) {
            case CURL_LOCK_DATA_SHARE:
                reinterpret_cast<CurlLibraryManager*>(ctx)->_mutexShare.lock();
                break;
            case CURL_LOCK_DATA_DNS:
                reinterpret_cast<CurlLibraryManager*>(ctx)->_mutexDns.lock();
                break;
            case CURL_LOCK_DATA_SSL_SESSION:
                reinterpret_cast<CurlLibraryManager*>(ctx)->_mutexSSLSession.lock();
                break;
            case CURL_LOCK_DATA_CONNECT:
                reinterpret_cast<CurlLibraryManager*>(ctx)->_mutexConnect.lock();
                break;
            default:
                fassert(5185801, "Unsupported curl lock type");
        }
    }

    static void _unlockShare(CURL*, curl_lock_data lock_data, void* ctx) {
        switch (lock_data) {
            case CURL_LOCK_DATA_SHARE:
                reinterpret_cast<CurlLibraryManager*>(ctx)->_mutexShare.unlock();
                break;
            case CURL_LOCK_DATA_DNS:
                reinterpret_cast<CurlLibraryManager*>(ctx)->_mutexDns.unlock();
                break;
            case CURL_LOCK_DATA_SSL_SESSION:
                reinterpret_cast<CurlLibraryManager*>(ctx)->_mutexSSLSession.unlock();
                break;
            case CURL_LOCK_DATA_CONNECT:
                reinterpret_cast<CurlLibraryManager*>(ctx)->_mutexConnect.unlock();
                break;
            default:
                fassert(5185802, "Unsupported curl unlock type");
        }
    }

private:
    bool _initialized = false;
    CURLSH* _share = nullptr;

    Mutex _mutexDns = MONGO_MAKE_LATCH("CurlLibraryManager::ShareDns");
    Mutex _mutexConnect = MONGO_MAKE_LATCH("CurlLibraryManager::ShareConnect");
    Mutex _mutexSSLSession = MONGO_MAKE_LATCH("CurlLibraryManager::ShareSSLSession");
    Mutex _mutexShare = MONGO_MAKE_LATCH("CurlLibraryManager::ShareLock");
} 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->advanceNoThrow(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);
#ifdef CURLOPT_TCP_KEEPALIVE
        curl_easy_setopt(_handle.get(), CURLOPT_TCP_KEEPALIVE, 1);
#endif
        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());
        curl_easy_setopt(handle, CURLOPT_SHARE, curlLibraryManager.getShareHandle());

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

        curl_slist* chunk = curl_slist_append(nullptr, "Connection: keep-alive");
        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>();
}

BSONObj HttpClient::getServerStatus() {

    BSONObjBuilder info;
    info.append("type", "curl");

    {
        BSONObjBuilder v(info.subobjStart("compiled"));
        v.append("version", LIBCURL_VERSION);
        v.append("version_num", LIBCURL_VERSION_NUM);
    }

    {
        auto* curl_info = curl_version_info(CURLVERSION_NOW);

        BSONObjBuilder v(info.subobjStart("running"));
        v.append("version", curl_info->version);
        v.append("version_num", curl_info->version_num);
    }

    return info.obj();
}

}  // namespace mongo