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

mavlink / MAVSDK / 11588874603

30 Oct 2024 07:37AM UTC coverage: 38.621% (+0.7%) from 37.918%
11588874603

Pull #2394

github

web-flow
Merge 991248b3a into cebb708a4
Pull Request #2394: Consolidate CI

12034 of 31159 relevant lines covered (38.62%)

243.25 hits per line

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

81.94
/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
        res = curl_easy_perform(curl.get());
2✔
33
        content = readBuffer;
2✔
34

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

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

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

66
static int download_progress_update(
93✔
67
    void* p, curl_off_t dltotal, curl_off_t dlnow, curl_off_t ultotal, curl_off_t ulnow)
68
{
69
    UNUSED(ultotal);
93✔
70
    UNUSED(ulnow);
93✔
71

72
    auto* myp = reinterpret_cast<struct UpProgress*>(p);
93✔
73

74
    if (myp->progress_callback == nullptr) {
93✔
75
        return 0;
47✔
76
    }
77

78
    if (dltotal == 0 || dlnow == 0) {
46✔
79
        return myp->progress_callback(0, HttpStatus::Idle, CURLcode::CURLE_OK);
42✔
80
    }
81

82
    int percentage = static_cast<int>(100 / dltotal * dlnow);
4✔
83

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

89
    return 0;
3✔
90
}
91

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

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

103
        fp = fopen(path.c_str(), "wb");
4✔
104
        curl_easy_setopt(curl.get(), CURLOPT_CONNECTTIMEOUT, 5L);
4✔
105
        curl_easy_setopt(curl.get(), CURLOPT_XFERINFOFUNCTION, download_progress_update);
4✔
106
        curl_easy_setopt(curl.get(), CURLOPT_PROGRESSDATA, &progress);
4✔
107
        curl_easy_setopt(curl.get(), CURLOPT_URL, url.c_str());
4✔
108
        curl_easy_setopt(curl.get(), CURLOPT_WRITEFUNCTION, NULL);
4✔
109
        curl_easy_setopt(curl.get(), CURLOPT_WRITEDATA, fp);
4✔
110
        curl_easy_setopt(curl.get(), CURLOPT_NOPROGRESS, 0L);
4✔
111
        res = curl_easy_perform(curl.get());
4✔
112
        fclose(fp);
4✔
113

114
        if (res == CURLcode::CURLE_OK) {
4✔
115
            if (nullptr != progress_callback) {
2✔
116
                progress_callback(100, HttpStatus::Finished, res);
1✔
117
            }
118
            return true;
2✔
119
        } else {
120
            if (nullptr != progress_callback) {
2✔
121
                progress_callback(0, HttpStatus::Error, res);
1✔
122
            }
123
            remove(path.c_str());
2✔
124
            LogErr() << "Error while downloading file, curl error code: "
4✔
125
                     << curl_easy_strerror(res);
4✔
126
            return false;
2✔
127
        }
128
    } else {
4✔
129
        LogErr() << "Error: cannot start downloading file because of curl initialization error. ";
×
130
        return false;
×
131
    }
132
}
4✔
133

134
} // 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

© 2025 Coveralls, Inc