• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

mavlink / MAVSDK / 29547914454

17 Jul 2026 01:37AM UTC coverage: 50.413%. First build
29547914454

Pull #2936

github

web-flow
Merge 46af30374 into 23fd28534
Pull Request #2936: Backport various bugfixes to v3

162 of 257 new or added lines in 24 files covered. (63.04%)

19304 of 38292 relevant lines covered (50.41%)

668.85 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

79.01
/src/mavsdk/core/curl_wrapper.cpp
1
#include "log.h"
2
#include "curl_wrapper.h"
3
#include "unused.h"
4
#include <iostream>
5
#include <cstdio>
6
#include <fstream>
7
#include <string>
8

9
namespace mavsdk {
10

11
// converts curl output to string
12
// taken from
13
// https://stackoverflow.com/questions/9786150/save-curl-content-result-into-a-string-in-c
14
static size_t write_callback(void* contents, size_t size, size_t nmemb, void* userp)
1✔
15
{
16
    reinterpret_cast<std::string*>(userp)->append(reinterpret_cast<char*>(contents), size * nmemb);
1✔
17
    return size * nmemb;
1✔
18
}
19

20
bool CurlWrapper::download_text(const std::string& url, std::string& content)
2✔
21
{
22
    auto curl = std::shared_ptr<CURL>(curl_easy_init(), curl_easy_cleanup);
2✔
23
    std::string readBuffer;
2✔
24

25
    if (nullptr != curl) {
2✔
26
        CURLcode res;
27

28
        curl_easy_setopt(curl.get(), CURLOPT_CONNECTTIMEOUT, 5L);
2✔
29
        curl_easy_setopt(curl.get(), CURLOPT_URL, url.c_str());
2✔
30
        curl_easy_setopt(curl.get(), CURLOPT_WRITEFUNCTION, write_callback);
2✔
31
        curl_easy_setopt(curl.get(), CURLOPT_WRITEDATA, &readBuffer);
2✔
32
        curl_easy_setopt(curl.get(), CURLOPT_SSL_VERIFYPEER, 1L);
2✔
33
        curl_easy_setopt(curl.get(), CURLOPT_FOLLOWLOCATION, 1L);
2✔
34
        res = curl_easy_perform(curl.get());
2✔
35
        content = readBuffer;
2✔
36

37
        if (res == CURLcode::CURLE_OK) {
2✔
38
            return true;
1✔
39
        } else {
40
            LogErr() << "Error while downloading text, curl error code: "
1✔
41
                     << curl_easy_strerror(res);
1✔
42
            return false;
1✔
43
        }
44
    } else {
45
        LogErr() << "Error: cannot start uploading because of curl initialization error. ";
×
46
        return false;
×
47
    }
48
}
2✔
49

50
size_t get_file_size(const std::string& path)
×
51
{
52
    std::streampos begin, end;
×
53
    std::ifstream my_file(path.c_str(), std::ios::binary);
×
54
    begin = my_file.tellg();
×
55
    my_file.seekg(0, std::ios::end);
×
56
    end = my_file.tellg();
×
57
    my_file.close();
×
58
    return ((end - begin) > 0) ? (end - begin) : 0;
×
59
}
×
60

61
template<typename T> std::string to_string(T value)
62
{
63
    std::ostringstream os;
64
    os << value;
65
    return os.str();
66
}
67

68
static int download_progress_update(
45✔
69
    void* p, curl_off_t dltotal, curl_off_t dlnow, curl_off_t ultotal, curl_off_t ulnow)
70
{
71
    UNUSED(ultotal);
45✔
72
    UNUSED(ulnow);
45✔
73

74
    auto* myp = reinterpret_cast<struct UpProgress*>(p);
45✔
75

76
    if (myp->progress_callback == nullptr) {
45✔
77
        return 0;
24✔
78
    }
79

80
    if (dltotal == 0 || dlnow == 0) {
21✔
81
        myp->progress_callback(0, HttpStatus::Idle, CURLcode::CURLE_OK);
17✔
82
    } else {
83
        int percentage = static_cast<int>(100 * dlnow / dltotal);
4✔
84

85
        if (percentage > myp->progress_in_percentage) {
4✔
86
            myp->progress_in_percentage = percentage;
1✔
87
            myp->progress_callback(percentage, HttpStatus::Downloading, CURLcode::CURLE_OK);
1✔
88
        }
89
    }
90

91
    return 0;
21✔
92
}
93

94
bool CurlWrapper::download_file_to_path(
4✔
95
    const std::string& url, const std::string& path, const ProgressCallback& progress_callback)
96
{
97
    auto curl = std::shared_ptr<CURL>(curl_easy_init(), curl_easy_cleanup);
4✔
98
    FILE* fp;
99

100
    if (nullptr != curl) {
4✔
101
        CURLcode res;
102
        struct UpProgress progress;
4✔
103
        progress.progress_callback = progress_callback;
4✔
104

105
        fp = fopen(path.c_str(), "wb");
4✔
106
        if (fp == nullptr) {
4✔
NEW
107
            LogErr() << "Error: cannot open file for writing: " << path;
×
NEW
108
            if (nullptr != progress_callback) {
×
NEW
109
                progress_callback(0, HttpStatus::Error, CURLcode::CURLE_WRITE_ERROR);
×
110
            }
NEW
111
            return false;
×
112
        }
113
        curl_easy_setopt(curl.get(), CURLOPT_CONNECTTIMEOUT, 5L);
4✔
114
        curl_easy_setopt(curl.get(), CURLOPT_XFERINFOFUNCTION, download_progress_update);
4✔
115
        curl_easy_setopt(curl.get(), CURLOPT_PROGRESSDATA, &progress);
4✔
116
        curl_easy_setopt(curl.get(), CURLOPT_URL, url.c_str());
4✔
117
        curl_easy_setopt(curl.get(), CURLOPT_WRITEFUNCTION, NULL);
4✔
118
        curl_easy_setopt(curl.get(), CURLOPT_WRITEDATA, fp);
4✔
119
        curl_easy_setopt(curl.get(), CURLOPT_NOPROGRESS, 0L);
4✔
120
        curl_easy_setopt(curl.get(), CURLOPT_SSL_VERIFYPEER, 1L);
4✔
121
        curl_easy_setopt(curl.get(), CURLOPT_FOLLOWLOCATION, 1L);
4✔
122
        res = curl_easy_perform(curl.get());
4✔
123
        fclose(fp);
4✔
124

125
        if (res == CURLcode::CURLE_OK) {
4✔
126
            if (nullptr != progress_callback) {
2✔
127
                progress_callback(100, HttpStatus::Finished, res);
1✔
128
            }
129
            return true;
2✔
130
        } else {
131
            if (nullptr != progress_callback) {
2✔
132
                progress_callback(0, HttpStatus::Error, res);
1✔
133
            }
134
            remove(path.c_str());
2✔
135
            LogErr() << "Error while downloading file, curl error code: "
2✔
136
                     << curl_easy_strerror(res);
2✔
137
            return false;
2✔
138
        }
139
    } else {
4✔
140
        LogErr() << "Error: cannot start downloading file because of curl initialization error. ";
×
141
        return false;
×
142
    }
143
}
4✔
144

145
} // namespace mavsdk
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc