| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * This file is part of the Falcon Player (FPP) and is Copyright (C) | ||
| 3 | * 2013-2024 by the Falcon Player Developers. | ||
| 4 | * | ||
| 5 | * The Falcon Player (FPP) is free software, and is covered under | ||
| 6 | * multiple Open Source licenses. Please see the included 'LICENSES' | ||
| 7 | * file for descriptions of what files are covered by each license. | ||
| 8 | * | ||
| 9 | * This source file is covered under the LGPL v2.1 as described in the | ||
| 10 | * included LICENSE.LGPL file. | ||
| 11 | */ | ||
| 12 | |||
| 13 | #include "fpp-pch.h" | ||
| 14 | |||
| 15 | #include <algorithm> | ||
| 16 | #include <atomic> | ||
| 17 | #include <chrono> | ||
| 18 | #include <cstring> | ||
| 19 | #include <list> | ||
| 20 | #include <memory> | ||
| 21 | #include <mutex> | ||
| 22 | #include <string> | ||
| 23 | #include <thread> | ||
| 24 | #include <vector> | ||
| 25 | |||
| 26 | #include "CurlManager.h" | ||
| 27 | #include "CurlSocket.h" | ||
| 28 | #include "fppversion.h" | ||
| 29 | #include "log.h" | ||
| 30 | |||
| 31 | CurlManager CurlManager::INSTANCE; | ||
| 32 | |||
| 33 | 3252 | CurlManager::CurlManager() { | |
| 34 | 3252 | } | |
| 35 | |||
| 36 | 600 | CurlManager::~CurlManager() { | |
| 37 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 600 times.
|
600 | if (curlMulti) { |
| 38 | ✗ | curl_multi_cleanup(curlMulti); | |
| 39 | } | ||
| 40 | // No handle should still reference these shares by the time the singleton is | ||
| 41 | // torn down at process exit; free whatever cookie sessions were left behind. | ||
| 42 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 600 times.
|
600 | for (auto& p : cookieSessions) { |
| 43 | ✗ | if (p.second->share) { | |
| 44 | ✗ | curl_share_cleanup(p.second->share); | |
| 45 | } | ||
| 46 | ✗ | delete p.second; | |
| 47 | } | ||
| 48 | 600 | cookieSessions.clear(); | |
| 49 | 600 | } | |
| 50 | |||
| 51 | // curl calls these around every access to shared data (only COOKIE is shared | ||
| 52 | // here). The userdata is the session's own std::mutex; a single mutex is | ||
| 53 | // sufficient because only one data class is shared, so curl never nests locks. | ||
| 54 | 68 | static void curlShareLockCB(CURL* handle, curl_lock_data data, curl_lock_access access, void* userptr) { | |
| 55 | 68 | static_cast<std::mutex*>(userptr)->lock(); | |
| 56 | 68 | } | |
| 57 | 68 | static void curlShareUnlockCB(CURL* handle, curl_lock_data data, void* userptr) { | |
| 58 | 68 | static_cast<std::mutex*>(userptr)->unlock(); | |
| 59 | 68 | } | |
| 60 | |||
| 61 | 12 | CURLSH* CurlManager::getCookieShare(const std::string& token) { | |
| 62 |
1/2✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
|
12 | std::unique_lock<std::mutex> l(lock); |
| 63 |
1/2✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
|
12 | CookieSession*& cs = cookieSessions[token]; |
| 64 |
2/2✓ Branch 0 taken 5 times.
✓ Branch 1 taken 7 times.
|
12 | if (cs == nullptr) { |
| 65 |
2/4✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 5 times.
|
5 | cs = new CookieSession(); |
| 66 |
1/2✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
|
5 | cs->share = curl_share_init(); |
| 67 |
1/2✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
|
5 | curl_share_setopt(cs->share, CURLSHOPT_SHARE, CURL_LOCK_DATA_COOKIE); |
| 68 |
1/2✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
|
5 | curl_share_setopt(cs->share, CURLSHOPT_LOCKFUNC, curlShareLockCB); |
| 69 |
1/2✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
|
5 | curl_share_setopt(cs->share, CURLSHOPT_UNLOCKFUNC, curlShareUnlockCB); |
| 70 |
1/2✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
|
5 | curl_share_setopt(cs->share, CURLSHOPT_USERDATA, &cs->mutex); |
| 71 | } | ||
| 72 | 12 | return cs->share; | |
| 73 | 12 | } | |
| 74 | |||
| 75 | 26 | void CurlManager::releaseCookieSession(const std::string& token) { | |
| 76 | 26 | CookieSession* cs = nullptr; | |
| 77 | { | ||
| 78 |
1/2✓ Branch 0 taken 26 times.
✗ Branch 1 not taken.
|
26 | std::unique_lock<std::mutex> l(lock); |
| 79 |
1/2✓ Branch 0 taken 26 times.
✗ Branch 1 not taken.
|
26 | auto it = cookieSessions.find(token); |
| 80 |
2/2✓ Branch 0 taken 21 times.
✓ Branch 1 taken 5 times.
|
26 | if (it == cookieSessions.end()) { |
| 81 | 21 | return; | |
| 82 | } | ||
| 83 | 5 | cs = it->second; | |
| 84 |
1/2✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
|
5 | cookieSessions.erase(it); |
| 85 |
2/2✓ Branch 0 taken 5 times.
✓ Branch 1 taken 21 times.
|
26 | } |
| 86 | // Caller contract: no easy handle still has CURLOPT_SHARE pointing at this | ||
| 87 | // share (the owner's requests have completed or been cancelled). If curl | ||
| 88 | // still sees it in use we must NOT free it - the share holds &cs->mutex, so | ||
| 89 | // deleting cs would leave curl calling into a freed mutex. Leak + shout. | ||
| 90 | 5 | CURLSHcode sc = curl_share_cleanup(cs->share); | |
| 91 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
|
5 | if (sc == CURLSHE_IN_USE) { |
| 92 | ✗ | LogErr(VB_GENERAL, "CurlManager cookie session '%s' still in use at release; leaking share to avoid a UAF\n", token.c_str()); | |
| 93 | ✗ | return; | |
| 94 | } | ||
| 95 |
1/2✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
|
5 | delete cs; |
| 96 | } | ||
| 97 | |||
| 98 | 1465 | void CurlManager::addCURL(const std::string& furl, CURL* curl, std::function<void(CURL*)>&& callback, bool autoCleanCurl, | |
| 99 | const std::string& owner) { | ||
| 100 |
1/2✓ Branch 0 taken 1465 times.
✗ Branch 1 not taken.
|
1465 | std::unique_lock<std::mutex> l(lock); |
| 101 |
2/2✓ Branch 0 taken 192 times.
✓ Branch 1 taken 1273 times.
|
1465 | if (curlMulti == nullptr) { |
| 102 |
1/2✓ Branch 0 taken 192 times.
✗ Branch 1 not taken.
|
192 | curlMulti = curl_multi_init(); |
| 103 |
1/2✓ Branch 0 taken 192 times.
✗ Branch 1 not taken.
|
192 | curl_multi_setopt(curlMulti, CURLMOPT_PIPELINING, CURLPIPE_MULTIPLEX); |
| 104 | } | ||
| 105 | |||
| 106 |
2/4✓ Branch 0 taken 1465 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1465 times.
|
1465 | CurlInfo* i = new CurlInfo; |
| 107 |
1/2✓ Branch 0 taken 1465 times.
✗ Branch 1 not taken.
|
1465 | i->host = getHost(furl); |
| 108 |
1/2✓ Branch 0 taken 1465 times.
✗ Branch 1 not taken.
|
1465 | i->url = furl; |
| 109 |
1/2✓ Branch 0 taken 1465 times.
✗ Branch 1 not taken.
|
1465 | i->callback = callback; |
| 110 | 1465 | i->curl = curl; | |
| 111 | 1465 | i->cleanCurl = autoCleanCurl; | |
| 112 |
1/2✓ Branch 0 taken 1465 times.
✗ Branch 1 not taken.
|
1465 | i->owner = owner; |
| 113 |
1/2✓ Branch 0 taken 1465 times.
✗ Branch 1 not taken.
|
1465 | curl_multi_add_handle(curlMulti, curl); |
| 114 | 1465 | numCurls++; | |
| 115 |
2/2✓ Branch 0 taken 7487 times.
✓ Branch 1 taken 401 times.
|
7888 | for (int x = 0; x < curls.size(); x++) { |
| 116 |
2/2✓ Branch 0 taken 1064 times.
✓ Branch 1 taken 6423 times.
|
7487 | if (curls[x] == nullptr) { |
| 117 | 1064 | curls[x] = i; | |
| 118 | 1064 | return; | |
| 119 | } | ||
| 120 | } | ||
| 121 |
1/2✓ Branch 0 taken 401 times.
✗ Branch 1 not taken.
|
401 | curls.push_back(i); |
| 122 |
2/2✓ Branch 0 taken 401 times.
✓ Branch 1 taken 1064 times.
|
1465 | } |
| 123 | |||
| 124 | 3490 | static size_t urlWriteData(void* buffer, size_t size, size_t nmemb, void* userp) { | |
| 125 | 3490 | std::vector<uint8_t>* str = (std::vector<uint8_t>*)userp; | |
| 126 | 3490 | size_t pos = str->size(); | |
| 127 | 3490 | str->resize(str->size() + size * nmemb); | |
| 128 | 3490 | memcpy(&str->data()[pos], buffer, size * nmemb); | |
| 129 | 3490 | return size * nmemb; | |
| 130 | } | ||
| 131 | |||
| 132 | // The requests FPP makes itself (add*(), do*()) and its discovery probes hold | ||
| 133 | // the whole response in memory, and discovery reaches devices that are merely | ||
| 134 | // on the LAN: one that streamed an endless body grew fppd until the OOM killer | ||
| 135 | // stepped in (the timeout alone allows ~100MB at LAN speed). Returning short | ||
| 136 | // aborts the transfer with CURLE_WRITE_ERROR -- but the HTTP status already | ||
| 137 | // received still reads 200, so the request paths below report that result, | ||
| 138 | // like any other failed transfer, as a failure themselves (failedTransfer()). | ||
| 139 | // A plugin's own handle from createCurl() keeps no | ||
| 140 | // limit: it reads the status and the body itself, and a cut-off body with a | ||
| 141 | // 200 is what its proxy then served (limitResponseSize() opts a handle in). | ||
| 142 | static constexpr size_t MAX_CURL_RESPONSE = 16 * 1024 * 1024; | ||
| 143 | |||
| 144 | 3492 | static size_t urlWriteDataLimited(void* buffer, size_t size, size_t nmemb, void* userp) { | |
| 145 | 3492 | std::vector<uint8_t>* str = (std::vector<uint8_t>*)userp; | |
| 146 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 3490 times.
|
3492 | if (size * nmemb > MAX_CURL_RESPONSE - std::min(str->size(), MAX_CURL_RESPONSE)) { |
| 147 | 2 | return 0; | |
| 148 | } | ||
| 149 | 3490 | return urlWriteData(buffer, size, nmemb, userp); | |
| 150 | } | ||
| 151 | |||
| 152 | 1465 | void CurlManager::limitResponseSize(CURL* c) { | |
| 153 | 1465 | curl_easy_setopt(c, CURLOPT_WRITEFUNCTION, urlWriteDataLimited); | |
| 154 | 1465 | } | |
| 155 | |||
| 156 | // The result of the transfer whose callback is running on this thread, set by | ||
| 157 | // doProcessCurls() around it: the callbacks take only the handle, and the | ||
| 158 | // result isn't one of its infos. | ||
| 159 | static thread_local CURLcode t_completedResult = CURLE_OK; | ||
| 160 | |||
| 161 | // Why a transfer failed, or "" if it completed. CURLINFO_RESPONSE_CODE is | ||
| 162 | // the last status received even when the transfer then failed: a body cut | ||
| 163 | // off by the size cap, a timeout or a reset partway through, or a server | ||
| 164 | // that closed before its Content-Length all read as a 200 with part of the | ||
| 165 | // body. So the requests below go by the transfer's result, not the status. | ||
| 166 | 1041 | static std::string failedTransfer(CURLcode result, const CurlManager::CurlPrivateData* data) { | |
| 167 |
2/2✓ Branch 0 taken 984 times.
✓ Branch 1 taken 57 times.
|
1041 | if (result == CURLE_OK) { |
| 168 |
1/2✓ Branch 0 taken 984 times.
✗ Branch 1 not taken.
|
1968 | return ""; |
| 169 | } | ||
| 170 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 55 times.
|
57 | if (result == CURLE_WRITE_ERROR) { |
| 171 | // only urlWriteDataLimited() returns short on these handles | ||
| 172 |
3/6✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
|
2 | return "the response was larger than " + std::to_string(MAX_CURL_RESPONSE / (1024 * 1024)) + " MB"; |
| 173 | } | ||
| 174 |
2/4✓ Branch 0 taken 55 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 55 times.
✗ Branch 3 not taken.
|
55 | if (data && data->errorResp[0]) { |
| 175 |
1/2✓ Branch 0 taken 55 times.
✗ Branch 1 not taken.
|
110 | return data->errorResp; |
| 176 | } | ||
| 177 | ✗ | return curl_easy_strerror(result); | |
| 178 | } | ||
| 179 | ✗ | static size_t urlReadData(void* ptr, size_t size, size_t nmemb, void* userp) { | |
| 180 | ✗ | size_t buffer_size = size * nmemb; | |
| 181 | ✗ | CurlManager::CurlPrivateData* dt = (CurlManager::CurlPrivateData*)userp; | |
| 182 | ✗ | size_t numb = dt->req->size() - dt->curPos; | |
| 183 | ✗ | if (numb > buffer_size) { | |
| 184 | ✗ | numb = buffer_size; | |
| 185 | } | ||
| 186 | ✗ | if (numb > 0) { | |
| 187 | ✗ | memcpy(ptr, &(*dt->req)[dt->curPos], numb); | |
| 188 | } | ||
| 189 | ✗ | dt->curPos += numb; | |
| 190 | ✗ | return numb; | |
| 191 | } | ||
| 192 | 1465 | CURL* CurlManager::createCurl(const std::string& fullUrl, CurlPrivateData** cpd, bool upload, CURLSH* share) { | |
| 193 |
6/12✓ Branch 0 taken 192 times.
✓ Branch 1 taken 1273 times.
✓ Branch 2 taken 192 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 192 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 192 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 192 times.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
|
1849 | static std::string USERAGENT = std::string("FPP/") + getFPPVersionTriplet(); |
| 194 | |||
| 195 |
1/2✓ Branch 0 taken 1465 times.
✗ Branch 1 not taken.
|
1465 | const std::string host = getHost(fullUrl); |
| 196 | // Under the lock: requests are created from API and command threads as | ||
| 197 | // well as the main loop, and getHostData() inserts into the map. Copy the | ||
| 198 | // credentials out (curl copies string options anyway). | ||
| 199 | 1465 | std::string username; | |
| 200 | 1465 | std::string password; | |
| 201 | { | ||
| 202 |
1/2✓ Branch 0 taken 1465 times.
✗ Branch 1 not taken.
|
1465 | std::unique_lock<std::mutex> l(lock); |
| 203 |
1/2✓ Branch 0 taken 1465 times.
✗ Branch 1 not taken.
|
1465 | HostData* hd = getHostData(host); |
| 204 |
1/2✓ Branch 0 taken 1465 times.
✗ Branch 1 not taken.
|
1465 | username = hd->username; |
| 205 |
1/2✓ Branch 0 taken 1465 times.
✗ Branch 1 not taken.
|
1465 | password = hd->password; |
| 206 | 1465 | } | |
| 207 |
1/2✓ Branch 0 taken 1465 times.
✗ Branch 1 not taken.
|
1465 | CURL* c = curl_easy_init(); |
| 208 |
1/2✓ Branch 0 taken 1465 times.
✗ Branch 1 not taken.
|
1465 | SetCurlCloseOnExec(c); |
| 209 |
1/2✓ Branch 0 taken 1465 times.
✗ Branch 1 not taken.
|
1465 | curl_easy_setopt(c, CURLOPT_URL, fullUrl.c_str()); |
| 210 |
2/2✓ Branch 0 taken 12 times.
✓ Branch 1 taken 1453 times.
|
1465 | if (share) { |
| 211 | // Join the cookie session: arm the cookie engine (empty COOKIEFILE) and | ||
| 212 | // point at the shared jar so cookies persist across handles in the group. | ||
| 213 |
1/2✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
|
12 | curl_easy_setopt(c, CURLOPT_COOKIEFILE, ""); |
| 214 |
1/2✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
|
12 | curl_easy_setopt(c, CURLOPT_SHARE, share); |
| 215 | } | ||
| 216 |
1/2✓ Branch 0 taken 1465 times.
✗ Branch 1 not taken.
|
1465 | curl_easy_setopt(c, CURLOPT_USERAGENT, USERAGENT.c_str()); |
| 217 |
1/2✓ Branch 0 taken 1465 times.
✗ Branch 1 not taken.
|
1465 | curl_easy_setopt(c, CURLOPT_CONNECTTIMEOUT_MS, 2000L); |
| 218 |
1/2✓ Branch 0 taken 1465 times.
✗ Branch 1 not taken.
|
1465 | curl_easy_setopt(c, CURLOPT_TIMEOUT_MS, 10000L); |
| 219 |
1/2✓ Branch 0 taken 1465 times.
✗ Branch 1 not taken.
|
1465 | curl_easy_setopt(c, CURLOPT_ACCEPT_ENCODING, ""); |
| 220 |
1/2✓ Branch 0 taken 1465 times.
✗ Branch 1 not taken.
|
1465 | curl_easy_setopt(c, CURLOPT_NOSIGNAL, 1); |
| 221 | //curl_easy_setopt(c, CURLOPT_VERBOSE, 2L); | ||
| 222 |
1/2✓ Branch 0 taken 1465 times.
✗ Branch 1 not taken.
|
1465 | curl_easy_setopt(c, CURLOPT_UPKEEP_INTERVAL_MS, 5000L); |
| 223 | |||
| 224 |
1/2✓ Branch 0 taken 1465 times.
✗ Branch 1 not taken.
|
1465 | curl_easy_setopt(c, CURLOPT_WRITEFUNCTION, urlWriteData); |
| 225 |
2/4✓ Branch 0 taken 1465 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1465 times.
|
1465 | CurlPrivateData* data = new CurlPrivateData(); |
| 226 |
1/2✓ Branch 0 taken 1465 times.
✗ Branch 1 not taken.
|
1465 | data->host = host; |
| 227 |
1/2✓ Branch 0 taken 1465 times.
✗ Branch 1 not taken.
|
1465 | curl_easy_setopt(c, CURLOPT_ERRORBUFFER, data->errorResp); |
| 228 |
1/2✓ Branch 0 taken 1465 times.
✗ Branch 1 not taken.
|
1465 | curl_easy_setopt(c, CURLOPT_WRITEDATA, &data->resp); |
| 229 |
1/2✓ Branch 0 taken 1465 times.
✗ Branch 1 not taken.
|
1465 | curl_easy_setopt(c, CURLOPT_PRIVATE, data); |
| 230 |
2/4✓ Branch 0 taken 1465 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1465 times.
|
1465 | if (username != "") { |
| 231 | ✗ | curl_easy_setopt(c, CURLOPT_USERNAME, username.c_str()); | |
| 232 | ✗ | curl_easy_setopt(c, CURLOPT_PASSWORD, password.c_str()); | |
| 233 | ✗ | curl_easy_setopt(c, CURLOPT_HTTPAUTH, CURLAUTH_BASIC | CURLAUTH_DIGEST | CURLAUTH_NEGOTIATE); | |
| 234 | } | ||
| 235 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1465 times.
|
1465 | if (upload) { |
| 236 | ✗ | data->req = new std::vector<uint8_t>(); | |
| 237 | ✗ | curl_easy_setopt(c, CURLOPT_READDATA, data); | |
| 238 | ✗ | curl_easy_setopt(c, CURLOPT_READFUNCTION, urlReadData); | |
| 239 | } | ||
| 240 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1465 times.
|
1465 | if (cpd) { |
| 241 | ✗ | *cpd = data; | |
| 242 | } | ||
| 243 | 1465 | return c; | |
| 244 | 1465 | } | |
| 245 | |||
| 246 | 444 | void CurlManager::add(const std::string& furl, const std::string& method, const std::string& data, | |
| 247 | const std::list<std::string>& extraHeaders, | ||
| 248 | std::function<void(int rc, const std::string& resp)>&& callback, | ||
| 249 | const std::string& owner, const std::string& cookieSession) { | ||
| 250 |
3/4✓ Branch 0 taken 432 times.
✓ Branch 1 taken 12 times.
✓ Branch 2 taken 12 times.
✗ Branch 3 not taken.
|
444 | CURLSH* share = cookieSession.empty() ? nullptr : getCookieShare(cookieSession); |
| 251 |
1/2✓ Branch 0 taken 444 times.
✗ Branch 1 not taken.
|
444 | CURL* curl = createCurl(furl, nullptr, false, share); |
| 252 |
1/2✓ Branch 0 taken 444 times.
✗ Branch 1 not taken.
|
444 | limitResponseSize(curl); |
| 253 | |||
| 254 |
3/4✓ Branch 0 taken 444 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 74 times.
✓ Branch 3 taken 370 times.
|
444 | if (method == "POST") { |
| 255 |
1/2✓ Branch 0 taken 74 times.
✗ Branch 1 not taken.
|
74 | curl_easy_setopt(curl, CURLOPT_POST, 1); |
| 256 |
2/4✓ Branch 0 taken 370 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 370 times.
|
370 | } else if (method == "PUT") { |
| 257 | ✗ | curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "PUT"); | |
| 258 |
2/4✓ Branch 0 taken 370 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 370 times.
|
370 | } else if (method == "PATCH") { |
| 259 | ✗ | curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "PATCH"); | |
| 260 |
2/4✓ Branch 0 taken 370 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 370 times.
|
370 | } else if (method == "DELETE") { |
| 261 | ✗ | curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "DELETE"); | |
| 262 | } | ||
| 263 | |||
| 264 |
7/10✓ Branch 0 taken 444 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 370 times.
✓ Branch 3 taken 74 times.
✓ Branch 4 taken 370 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 370 times.
✓ Branch 8 taken 74 times.
✓ Branch 9 taken 370 times.
|
444 | if ((method == "POST") || (method == "PUT")) { |
| 265 |
1/2✓ Branch 0 taken 74 times.
✗ Branch 1 not taken.
|
74 | curl_easy_setopt(curl, CURLOPT_COPYPOSTFIELDS, data.c_str()); |
| 266 | } | ||
| 267 | |||
| 268 | 444 | struct curl_slist* headers = NULL; | |
| 269 |
2/2✓ Branch 0 taken 132 times.
✓ Branch 1 taken 444 times.
|
576 | for (auto& h : extraHeaders) { |
| 270 |
1/2✓ Branch 0 taken 132 times.
✗ Branch 1 not taken.
|
132 | headers = curl_slist_append(headers, h.c_str()); |
| 271 | } | ||
| 272 |
2/2✓ Branch 0 taken 72 times.
✓ Branch 1 taken 372 times.
|
444 | if (headers) { |
| 273 |
1/2✓ Branch 0 taken 72 times.
✗ Branch 1 not taken.
|
72 | curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers); |
| 274 | } | ||
| 275 | |||
| 276 | // The header list is owned by the callback rather than freed inside it, so | ||
| 277 | // it is released whether the request completes or is cancelled (which | ||
| 278 | // destroys the callback without running it). Either way that happens after | ||
| 279 | // curl_easy_cleanup(), which is also the right order - the old code freed | ||
| 280 | // the list while the easy handle still referenced it. | ||
| 281 | 444 | std::shared_ptr<curl_slist> headerList(headers, [](curl_slist* h) { | |
| 282 |
2/2✓ Branch 0 taken 72 times.
✓ Branch 1 taken 372 times.
|
444 | if (h) { |
| 283 | 72 | curl_slist_free_all(h); | |
| 284 | } | ||
| 285 |
1/2✓ Branch 0 taken 444 times.
✗ Branch 1 not taken.
|
444 | }); |
| 286 | |||
| 287 |
3/8✓ Branch 0 taken 444 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 444 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 444 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
|
444 | addCURL(furl, curl, [callback, headerList](CURL* c) { |
| 288 | 424 | CurlPrivateData* data = nullptr; | |
| 289 |
1/2✓ Branch 0 taken 424 times.
✗ Branch 1 not taken.
|
424 | curl_easy_getinfo(c, CURLINFO_PRIVATE, &data); |
| 290 |
1/2✓ Branch 0 taken 424 times.
✗ Branch 1 not taken.
|
424 | std::string failure = failedTransfer(t_completedResult, data); |
| 291 |
2/2✓ Branch 0 taken 55 times.
✓ Branch 1 taken 369 times.
|
424 | if (!failure.empty()) { |
| 292 |
1/2✓ Branch 0 taken 55 times.
✗ Branch 1 not taken.
|
55 | callback(0, failure); |
| 293 | 55 | return; | |
| 294 | } | ||
| 295 | 369 | long rc = 0; | |
| 296 |
1/2✓ Branch 0 taken 369 times.
✗ Branch 1 not taken.
|
369 | curl_easy_getinfo(c, CURLINFO_RESPONSE_CODE, &rc); |
| 297 | |||
| 298 | 369 | char* urlp = nullptr; | |
| 299 |
1/2✓ Branch 0 taken 369 times.
✗ Branch 1 not taken.
|
369 | curl_easy_getinfo(c, CURLINFO_EFFECTIVE_URL, &urlp); |
| 300 |
1/2✓ Branch 0 taken 369 times.
✗ Branch 1 not taken.
|
369 | std::string resp(reinterpret_cast<char*>(data->resp.data()), data->resp.size()); |
| 301 |
1/2✓ Branch 0 taken 369 times.
✗ Branch 1 not taken.
|
369 | callback(rc, resp); |
| 302 |
2/2✓ Branch 0 taken 369 times.
✓ Branch 1 taken 55 times.
|
424 | }, true, owner); |
| 303 | 444 | } | |
| 304 | 340 | void CurlManager::addGet(const std::string& url, std::function<void(int rc, const std::string& resp)>&& callback, | |
| 305 | const std::string& owner, const std::string& cookieSession) { | ||
| 306 |
5/10✓ Branch 0 taken 340 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 340 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 340 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 340 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 340 times.
✗ Branch 9 not taken.
|
2029 | add(url, "GET", "", {}, [callback](int rc, const std::string& resp) { callback(rc, resp); }, owner, cookieSession); |
| 307 | 340 | } | |
| 308 | |||
| 309 | 9 | void CurlManager::addPost(const std::string& url, const std::string& data, const std::string& contentType, | |
| 310 | std::function<void(int rc, const std::string& resp)>&& callback, const std::string& owner) { | ||
| 311 |
8/16✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 9 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 9 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 9 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 9 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 9 times.
✗ Branch 11 not taken.
✓ Branch 12 taken 9 times.
✓ Branch 13 taken 9 times.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
|
63 | add(url, "POST", data, { "Content-Type: " + contentType }, [callback](int rc, const std::string& resp) { callback(rc, resp); }, owner); |
| 312 |
1/6✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
|
18 | } |
| 313 | ✗ | void CurlManager::addPut(const std::string& url, const std::string& data, const std::string& contentType, | |
| 314 | std::function<void(int rc, const std::string& resp)>&& callback, const std::string& owner) { | ||
| 315 | ✗ | add(url, "PUT", data, { "Content-Type: " + contentType }, [callback](int rc, const std::string& resp) { callback(rc, resp); }, owner); | |
| 316 | ✗ | } | |
| 317 | |||
| 318 | 1064 | int CurlManager::cancelRequests(const std::string& owner) { | |
| 319 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1064 times.
|
1064 | if (owner.empty()) { |
| 320 | ✗ | return 0; // "" is FPP's own traffic; cancelling all of it is never wanted | |
| 321 | } | ||
| 322 | 1064 | return cancelMatching(&owner); | |
| 323 | } | ||
| 324 | |||
| 325 | 5840 | int CurlManager::cancelAll() { | |
| 326 |
1/2✓ Branch 0 taken 5840 times.
✗ Branch 1 not taken.
|
5840 | int n = cancelMatching(nullptr); |
| 327 |
1/2✓ Branch 0 taken 5840 times.
✗ Branch 1 not taken.
|
5840 | std::unique_lock<std::mutex> l(lock); |
| 328 |
5/6✓ Branch 0 taken 191 times.
✓ Branch 1 taken 5649 times.
✓ Branch 2 taken 191 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 191 times.
✓ Branch 5 taken 5649 times.
|
5840 | if (curlMulti && numCurls == 0) { |
| 329 |
1/2✓ Branch 0 taken 191 times.
✗ Branch 1 not taken.
|
191 | curl_multi_cleanup(curlMulti); |
| 330 | 191 | curlMulti = nullptr; | |
| 331 | } | ||
| 332 | 5840 | return n; | |
| 333 | 5840 | } | |
| 334 | |||
| 335 | // `owner` null cancels every asynchronous request (cleanCurl: the handle is | ||
| 336 | // ours; a handle the caller keeps is being waited on by it). | ||
| 337 | 6904 | int CurlManager::cancelMatching(const std::string* owner) { | |
| 338 | // The callbacks are moved out and destroyed after the lock is dropped: they | ||
| 339 | // belong to a plugin, and a captured object's destructor that touched | ||
| 340 | // CurlManager again would deadlock on this non-recursive mutex. | ||
| 341 | 6904 | std::vector<std::function<void(CURL*)>> doomed; | |
| 342 | { | ||
| 343 |
1/2✓ Branch 0 taken 6904 times.
✗ Branch 1 not taken.
|
6904 | std::unique_lock<std::mutex> l(lock); |
| 344 |
2/2✓ Branch 0 taken 1047 times.
✓ Branch 1 taken 6904 times.
|
7951 | for (size_t x = 0; x < curls.size(); x++) { |
| 345 | 1047 | CurlInfo* ci = curls[x]; | |
| 346 |
9/10✓ Branch 0 taken 128 times.
✓ Branch 1 taken 919 times.
✓ Branch 2 taken 41 times.
✓ Branch 3 taken 87 times.
✓ Branch 4 taken 33 times.
✓ Branch 5 taken 8 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 87 times.
✓ Branch 8 taken 952 times.
✓ Branch 9 taken 95 times.
|
1047 | if (!ci || (owner ? ci->owner != *owner : !ci->cleanCurl)) { |
| 347 | 952 | continue; | |
| 348 | } | ||
| 349 | 95 | curls[x] = nullptr; | |
| 350 | 95 | --numCurls; | |
| 351 |
1/2✓ Branch 0 taken 95 times.
✗ Branch 1 not taken.
|
95 | curl_multi_remove_handle(curlMulti, ci->curl); |
| 352 |
1/2✓ Branch 0 taken 95 times.
✗ Branch 1 not taken.
|
95 | if (ci->cleanCurl) { |
| 353 | 95 | CurlPrivateData* data = nullptr; | |
| 354 |
1/2✓ Branch 0 taken 95 times.
✗ Branch 1 not taken.
|
95 | curl_easy_getinfo(ci->curl, CURLINFO_PRIVATE, &data); |
| 355 |
1/2✓ Branch 0 taken 95 times.
✗ Branch 1 not taken.
|
95 | if (data) { |
| 356 |
1/2✓ Branch 0 taken 95 times.
✗ Branch 1 not taken.
|
95 | delete data; |
| 357 | } | ||
| 358 |
1/2✓ Branch 0 taken 95 times.
✗ Branch 1 not taken.
|
95 | curl_easy_cleanup(ci->curl); |
| 359 | } | ||
| 360 |
1/2✓ Branch 0 taken 95 times.
✗ Branch 1 not taken.
|
95 | doomed.push_back(std::move(ci->callback)); |
| 361 |
1/2✓ Branch 0 taken 95 times.
✗ Branch 1 not taken.
|
95 | delete ci; |
| 362 | } | ||
| 363 | 6904 | } | |
| 364 |
3/4✓ Branch 0 taken 1064 times.
✓ Branch 1 taken 5840 times.
✓ Branch 2 taken 6904 times.
✗ Branch 3 not taken.
|
6904 | LogDebug(VB_GENERAL, "CurlManager cancelled %d request(s) for %s\n", (int)doomed.size(), owner ? owner->c_str() : "(everyone)"); |
| 365 | 13808 | return (int)doomed.size(); | |
| 366 | 6904 | } | |
| 367 | |||
| 368 | // Runs one synchronous request to completion. The main loop pumps curl | ||
| 369 | // too, and when it completes this request it drops numCurls before calling | ||
| 370 | // the callback: processCurls() here then returned false with `done` still | ||
| 371 | // unset, this frame returned, and the callback wrote `done` into it. The | ||
| 372 | // flag is shared instead, and the wait is for this request only (doPut | ||
| 373 | // waited for every request in flight). | ||
| 374 | // Returns the transfer's result. | ||
| 375 | 617 | static CURLcode runToCompletion(CurlManager& mgr, const std::string& furl, CURL* curl) { | |
| 376 | struct State { | ||
| 377 | std::atomic<bool> done{ false }; | ||
| 378 | CURLcode result = CURLE_OK; // written before done, read after | ||
| 379 | }; | ||
| 380 |
1/2✓ Branch 0 taken 617 times.
✗ Branch 1 not taken.
|
617 | auto state = std::make_shared<State>(); |
| 381 |
3/6✓ Branch 0 taken 617 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 617 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 617 times.
✗ Branch 5 not taken.
|
1234 | mgr.addCURL( |
| 382 | 1851 | furl, curl, [state](CURL* c) { state->result = t_completedResult; state->done = true; }, false); | |
| 383 |
2/2✓ Branch 0 taken 81414 times.
✓ Branch 1 taken 617 times.
|
82031 | while (!state->done) { |
| 384 |
3/4✓ Branch 0 taken 81414 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 530 times.
✓ Branch 3 taken 80884 times.
|
81414 | if (!mgr.processCurls()) { |
| 385 | // another thread holds it between completion and the callback | ||
| 386 |
1/2✓ Branch 0 taken 530 times.
✗ Branch 1 not taken.
|
530 | std::this_thread::sleep_for(std::chrono::milliseconds(1)); |
| 387 | } else { | ||
| 388 | 80884 | std::this_thread::yield(); | |
| 389 | } | ||
| 390 | } | ||
| 391 | 1234 | return state->result; | |
| 392 | 617 | } | |
| 393 | |||
| 394 | 601 | std::string CurlManager::doGet(const std::string& furl, int& rc, const std::string& cookieSession) { | |
| 395 |
1/4✓ Branch 0 taken 601 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
601 | CURLSH* share = cookieSession.empty() ? nullptr : getCookieShare(cookieSession); |
| 396 |
1/2✓ Branch 0 taken 601 times.
✗ Branch 1 not taken.
|
601 | CURL* curl = createCurl(furl, nullptr, false, share); |
| 397 |
1/2✓ Branch 0 taken 601 times.
✗ Branch 1 not taken.
|
601 | limitResponseSize(curl); |
| 398 | |||
| 399 |
1/2✓ Branch 0 taken 601 times.
✗ Branch 1 not taken.
|
601 | CURLcode result = runToCompletion(*this, furl, curl); |
| 400 | |||
| 401 | 601 | CurlPrivateData* data = nullptr; | |
| 402 | 601 | long rc2 = 0; | |
| 403 |
1/2✓ Branch 0 taken 601 times.
✗ Branch 1 not taken.
|
601 | curl_easy_getinfo(curl, CURLINFO_PRIVATE, &data); |
| 404 |
1/2✓ Branch 0 taken 601 times.
✗ Branch 1 not taken.
|
601 | curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &rc2); |
| 405 | // On a failed transfer, rc 0 and the reason, as for one that never connected. | ||
| 406 |
1/2✓ Branch 0 taken 601 times.
✗ Branch 1 not taken.
|
601 | std::string resp = failedTransfer(result, data); |
| 407 |
1/2✓ Branch 0 taken 601 times.
✗ Branch 1 not taken.
|
601 | rc = resp.empty() ? rc2 : 0; |
| 408 |
1/2✓ Branch 0 taken 601 times.
✗ Branch 1 not taken.
|
601 | if (resp.empty()) { |
| 409 |
1/2✓ Branch 0 taken 601 times.
✗ Branch 1 not taken.
|
601 | resp.assign(reinterpret_cast<char*>(data->resp.data()), data->resp.size()); |
| 410 | } | ||
| 411 |
1/2✓ Branch 0 taken 601 times.
✗ Branch 1 not taken.
|
601 | delete data; |
| 412 |
1/2✓ Branch 0 taken 601 times.
✗ Branch 1 not taken.
|
601 | curl_easy_cleanup(curl); |
| 413 | |||
| 414 | 601 | return resp; | |
| 415 | ✗ | } | |
| 416 | |||
| 417 | struct ReadDataInfo { | ||
| 418 | const std::vector<uint8_t>* data; | ||
| 419 | size_t curPos = 0; | ||
| 420 | }; | ||
| 421 | 26 | static size_t read_callback(void* ptr, size_t size, size_t nmemb, void* userp) { | |
| 422 | 26 | size_t buffer_size = size * nmemb; | |
| 423 | 26 | struct ReadDataInfo* dt = (struct ReadDataInfo*)userp; | |
| 424 | 26 | int numb = dt->data->size() - dt->curPos; | |
| 425 |
2/2✓ Branch 0 taken 16 times.
✓ Branch 1 taken 10 times.
|
26 | if (numb > buffer_size) { |
| 426 | 16 | numb = buffer_size; | |
| 427 | } | ||
| 428 | 26 | memcpy(ptr, &(*dt->data)[dt->curPos], numb); | |
| 429 | 26 | dt->curPos += numb; | |
| 430 | 26 | return numb; | |
| 431 | } | ||
| 432 | 8 | std::string CurlManager::doPost(const std::string& furl, const std::string& contentType, const std::vector<uint8_t>& data, int& rc) { | |
| 433 |
1/2✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
|
8 | CURL* curl = createCurl(furl); |
| 434 |
1/2✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
|
8 | limitResponseSize(curl); |
| 435 | |||
| 436 | 8 | struct curl_slist* head = nullptr; | |
| 437 |
1/2✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
|
8 | std::string ct = "Content-Type: " + contentType; |
| 438 |
1/2✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
|
8 | head = curl_slist_append(head, ct.c_str()); |
| 439 |
2/4✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 8 times.
✗ Branch 3 not taken.
|
8 | std::string cl = "Content-Length: " + std::to_string(data.size()); |
| 440 |
1/2✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
|
8 | head = curl_slist_append(head, cl.c_str()); |
| 441 | |||
| 442 |
1/2✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
|
8 | curl_easy_setopt(curl, CURLOPT_POST, 1L); |
| 443 |
1/2✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
|
8 | curl_easy_setopt(curl, CURLOPT_HTTPHEADER, head); |
| 444 | |||
| 445 | 8 | ReadDataInfo dta; | |
| 446 | 8 | dta.data = &data; | |
| 447 | 8 | dta.curPos = 0; | |
| 448 | |||
| 449 |
1/2✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
|
8 | curl_easy_setopt(curl, CURLOPT_READDATA, &dta); |
| 450 |
1/2✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
|
8 | curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_callback); |
| 451 |
1/2✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
|
8 | curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, (long)data.size()); |
| 452 | |||
| 453 |
1/2✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
|
8 | CURLcode result = runToCompletion(*this, furl, curl); |
| 454 | |||
| 455 | 8 | CurlPrivateData* cdata = nullptr; | |
| 456 | 8 | long rc2 = 0; | |
| 457 |
1/2✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
|
8 | curl_easy_getinfo(curl, CURLINFO_PRIVATE, &cdata); |
| 458 |
1/2✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
|
8 | curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &rc2); |
| 459 | // On a failed transfer, rc 0 and the reason, as for one that never connected. | ||
| 460 |
1/2✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
|
8 | std::string resp = failedTransfer(result, cdata); |
| 461 |
2/2✓ Branch 0 taken 7 times.
✓ Branch 1 taken 1 times.
|
8 | rc = resp.empty() ? rc2 : 0; |
| 462 |
2/2✓ Branch 0 taken 7 times.
✓ Branch 1 taken 1 times.
|
8 | if (resp.empty()) { |
| 463 |
1/2✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
|
7 | resp.assign(reinterpret_cast<char*>(cdata->resp.data()), cdata->resp.size()); |
| 464 | } | ||
| 465 |
1/2✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
|
8 | delete cdata; |
| 466 |
1/2✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
|
8 | curl_slist_free_all(head); |
| 467 |
1/2✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
|
8 | curl_easy_cleanup(curl); |
| 468 | 8 | return resp; | |
| 469 | 8 | } | |
| 470 | 8 | std::string CurlManager::doPut(const std::string& furl, const std::string& contentType, const std::vector<uint8_t>& data, int& rc) { | |
| 471 |
1/2✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
|
8 | CURL* curl = createCurl(furl); |
| 472 |
1/2✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
|
8 | limitResponseSize(curl); |
| 473 | |||
| 474 | 8 | struct curl_slist* head = nullptr; | |
| 475 |
1/2✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
|
8 | std::string ct = "Content-Type: " + contentType; |
| 476 |
1/2✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
|
8 | head = curl_slist_append(head, ct.c_str()); |
| 477 |
2/4✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 8 times.
✗ Branch 3 not taken.
|
8 | std::string cl = "Content-Length: " + std::to_string(data.size()); |
| 478 |
1/2✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
|
8 | head = curl_slist_append(head, cl.c_str()); |
| 479 | |||
| 480 |
1/2✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
|
8 | curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L); |
| 481 | //curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "PUT"); | ||
| 482 |
1/2✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
|
8 | curl_easy_setopt(curl, CURLOPT_HTTPHEADER, head); |
| 483 | |||
| 484 | 8 | ReadDataInfo dta; | |
| 485 | 8 | dta.data = &data; | |
| 486 | 8 | dta.curPos = 0; | |
| 487 | |||
| 488 |
1/2✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
|
8 | curl_easy_setopt(curl, CURLOPT_READDATA, &dta); |
| 489 |
1/2✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
|
8 | curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_callback); |
| 490 | // An upload's size is INFILESIZE; POSTFIELDSIZE is ignored for one, and | ||
| 491 | // without a size curl chunks the body under the Content-Length above -- | ||
| 492 | // both framings at once, which RFC 9112 forbids. | ||
| 493 |
1/2✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
|
8 | curl_easy_setopt(curl, CURLOPT_INFILESIZE_LARGE, (curl_off_t)data.size()); |
| 494 | |||
| 495 |
1/2✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
|
8 | CURLcode result = runToCompletion(*this, furl, curl); |
| 496 | |||
| 497 | 8 | CurlPrivateData* cdata = nullptr; | |
| 498 | 8 | long rc2 = 0; | |
| 499 |
1/2✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
|
8 | curl_easy_getinfo(curl, CURLINFO_PRIVATE, &cdata); |
| 500 |
1/2✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
|
8 | curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &rc2); |
| 501 | // On a failed transfer, rc 0 and the reason, as for one that never connected. | ||
| 502 |
1/2✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
|
8 | std::string resp = failedTransfer(result, cdata); |
| 503 |
2/2✓ Branch 0 taken 7 times.
✓ Branch 1 taken 1 times.
|
8 | rc = resp.empty() ? rc2 : 0; |
| 504 |
2/2✓ Branch 0 taken 7 times.
✓ Branch 1 taken 1 times.
|
8 | if (resp.empty()) { |
| 505 |
1/2✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
|
7 | resp.assign(reinterpret_cast<char*>(cdata->resp.data()), cdata->resp.size()); |
| 506 | } | ||
| 507 |
1/2✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
|
8 | delete cdata; |
| 508 |
1/2✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
|
8 | curl_slist_free_all(head); |
| 509 |
1/2✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
|
8 | curl_easy_cleanup(curl); |
| 510 | 8 | return resp; | |
| 511 | 8 | } | |
| 512 | |||
| 513 | 90758 | bool CurlManager::doProcessCurls() { | |
| 514 | CURLMcode mc; | ||
| 515 | 90758 | int stillRunning = 0; | |
| 516 |
1/2✓ Branch 0 taken 90758 times.
✗ Branch 1 not taken.
|
90758 | std::unique_lock<std::mutex> l(lock); |
| 517 |
1/2✓ Branch 0 taken 90758 times.
✗ Branch 1 not taken.
|
90758 | mc = curl_multi_perform(curlMulti, &stillRunning); |
| 518 |
2/2✓ Branch 0 taken 951 times.
✓ Branch 1 taken 89807 times.
|
90758 | if (stillRunning != numCurls) { |
| 519 | 951 | CURLMsg* m = nullptr; | |
| 520 | do { | ||
| 521 | 2321 | int msgq = 0; | |
| 522 |
1/2✓ Branch 0 taken 2321 times.
✗ Branch 1 not taken.
|
2321 | m = curl_multi_info_read(curlMulti, &msgq); |
| 523 |
3/4✓ Branch 0 taken 1370 times.
✓ Branch 1 taken 951 times.
✓ Branch 2 taken 1370 times.
✗ Branch 3 not taken.
|
2321 | if (m && (m->msg == CURLMSG_DONE)) { |
| 524 | 1370 | CURL* e = m->easy_handle; | |
| 525 | // m is invalid once the handle is removed | ||
| 526 | 1370 | CURLcode result = m->data.result; | |
| 527 |
1/2✓ Branch 0 taken 1370 times.
✗ Branch 1 not taken.
|
1370 | curl_multi_remove_handle(curlMulti, e); |
| 528 | 1370 | CurlInfo* ci = nullptr; | |
| 529 |
1/2✓ Branch 0 taken 6614 times.
✗ Branch 1 not taken.
|
6614 | for (int x = 0; x < curls.size(); x++) { |
| 530 |
6/6✓ Branch 0 taken 5498 times.
✓ Branch 1 taken 1116 times.
✓ Branch 2 taken 1370 times.
✓ Branch 3 taken 4128 times.
✓ Branch 4 taken 1370 times.
✓ Branch 5 taken 5244 times.
|
6614 | if (curls[x] && curls[x]->curl == e) { |
| 531 | 1370 | ci = curls[x]; | |
| 532 | 1370 | curls[x] = nullptr; | |
| 533 | 1370 | --numCurls; | |
| 534 | 1370 | break; | |
| 535 | } | ||
| 536 | } | ||
| 537 |
1/2✓ Branch 0 taken 1370 times.
✗ Branch 1 not taken.
|
1370 | if (ci) { |
| 538 |
1/2✓ Branch 0 taken 1370 times.
✗ Branch 1 not taken.
|
1370 | l.unlock(); |
| 539 | 1370 | t_completedResult = result; | |
| 540 | // The callbacks parse what a remote or a probed device sent | ||
| 541 | // back, on the main loop with nothing above to catch: one | ||
| 542 | // jsoncpp throw on a field of the wrong type ({"Platform":{}} | ||
| 543 | // from a MultiSync remote, {"majorVersion":"9"} from a | ||
| 544 | // discovery probe) ended fppd. Contain it here, for every | ||
| 545 | // callback, and carry on with the cleanup. | ||
| 546 | try { | ||
| 547 |
1/2✓ Branch 0 taken 1370 times.
✗ Branch 1 not taken.
|
1370 | ci->callback(e); |
| 548 | ✗ | } catch (const std::exception& ex) { | |
| 549 | ✗ | char* url = nullptr; | |
| 550 | ✗ | curl_easy_getinfo(e, CURLINFO_EFFECTIVE_URL, &url); | |
| 551 | ✗ | LogWarn(VB_GENERAL, "Ignoring the reply from %s: handling it failed: %s\n", | |
| 552 | url ? url : "(unknown)", ex.what()); | ||
| 553 | ✗ | } catch (...) { | |
| 554 | ✗ | char* url = nullptr; | |
| 555 | ✗ | curl_easy_getinfo(e, CURLINFO_EFFECTIVE_URL, &url); | |
| 556 | ✗ | LogWarn(VB_GENERAL, "Ignoring the reply from %s: handling it failed\n", url ? url : "(unknown)"); | |
| 557 | ✗ | } | |
| 558 | 1370 | t_completedResult = CURLE_OK; | |
| 559 |
1/2✓ Branch 0 taken 1370 times.
✗ Branch 1 not taken.
|
1370 | l.lock(); |
| 560 |
2/2✓ Branch 0 taken 753 times.
✓ Branch 1 taken 617 times.
|
1370 | if (ci->cleanCurl) { |
| 561 | 753 | CurlPrivateData* data = nullptr; | |
| 562 |
1/2✓ Branch 0 taken 753 times.
✗ Branch 1 not taken.
|
753 | curl_easy_getinfo(e, CURLINFO_PRIVATE, &data); |
| 563 |
1/2✓ Branch 0 taken 753 times.
✗ Branch 1 not taken.
|
753 | if (data) { |
| 564 |
1/2✓ Branch 0 taken 753 times.
✗ Branch 1 not taken.
|
753 | delete data; |
| 565 | } | ||
| 566 |
1/2✓ Branch 0 taken 753 times.
✗ Branch 1 not taken.
|
753 | curl_easy_cleanup(e); |
| 567 | } | ||
| 568 |
1/2✓ Branch 0 taken 1370 times.
✗ Branch 1 not taken.
|
1370 | delete ci; |
| 569 | } | ||
| 570 | } | ||
| 571 |
2/2✓ Branch 0 taken 1370 times.
✓ Branch 1 taken 951 times.
|
2321 | } while (m); |
| 572 | } | ||
| 573 | 181516 | return numCurls > 0; | |
| 574 | 90758 | } | |
| 575 | |||
| 576 | 1465 | CurlManager::HostData* CurlManager::getHostData(const std::string& host) { | |
| 577 | 1465 | HostData* h = hostData[host]; | |
| 578 |
2/2✓ Branch 0 taken 822 times.
✓ Branch 1 taken 643 times.
|
1465 | if (h == nullptr) { |
| 579 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 822 times.
|
822 | h = new HostData(); |
| 580 | 822 | hostData[host] = h; | |
| 581 | } | ||
| 582 | 1465 | return h; | |
| 583 | } | ||
| 584 | |||
| 585 | ✗ | void CurlManager::setHostUsernamePassword(const std::string& host, const std::string& username, const std::string password) { | |
| 586 | ✗ | std::unique_lock<std::mutex> l(lock); | |
| 587 | ✗ | HostData* h = getHostData(host); | |
| 588 | ✗ | h->username = username; | |
| 589 | ✗ | h->password = password; | |
| 590 | ✗ | } | |
| 591 | |||
| 592 | 2930 | std::string CurlManager::getHost(const std::string& url) { | |
| 593 | 2930 | int idx = url.find("://"); | |
| 594 | 2930 | std::string host = url.substr(idx + 3); | |
| 595 | 2930 | idx = host.find("/"); | |
| 596 |
1/2✓ Branch 0 taken 2930 times.
✗ Branch 1 not taken.
|
2930 | host = host.substr(0, idx); |
| 597 | 2930 | return host; | |
| 598 | ✗ | } | |
| 599 |