MCPcopy Create free account
hub / github.com/TactilityProject/Tactility / download

Function download

Tactility/Source/network/Http.cpp:17–107  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

15static const auto LOGGER = Logger("HTTP");
16
17void download(
18 const std::string& url,
19 const std::string& certFilePath,
20 const std::string &downloadFilePath,
21 const std::function<void()>& onSuccess,
22 const std::function<void(const char* errorMessage)>& onError
23) {
24 service::gui::warnIfRunningOnGuiTask("HTTP");
25 LOGGER.info("Downloading from {} to {}", url, downloadFilePath);
26#ifdef ESP_PLATFORM
27 getMainDispatcher().dispatch([url, certFilePath, downloadFilePath, onSuccess, onError] {
28 LOGGER.info("Loading certificate");
29 auto certificate = file::readString(certFilePath);
30 if (certificate == nullptr) {
31 onError("Failed to read certificate");
32 return;
33 }
34
35 auto certificate_length = strlen(reinterpret_cast<const char*>(certificate.get())) + 1;
36
37 // TODO: Fix for missing initializer warnings
38 auto config = std::make_unique<esp_http_client_config_t>();
39 memset(config.get(), 0, sizeof(esp_http_client_config_t));
40 config->url = url.c_str();
41 config->auth_type = HTTP_AUTH_TYPE_NONE;
42 config->cert_pem = reinterpret_cast<const char*>(certificate.get());
43 config->cert_len = certificate_length;
44 config->tls_version = ESP_HTTP_CLIENT_TLS_VER_TLS_1_2;
45 config->method = HTTP_METHOD_GET;
46 config->timeout_ms = 5000;
47 config->transport_type = HTTP_TRANSPORT_OVER_SSL;
48
49 auto client = std::make_unique<EspHttpClient>();
50 if (!client->init(std::move(config))) {
51 onError("Failed to initialize client");
52 return;
53 }
54
55 if (!client->open()) {
56 onError("Failed to open connection");
57 return;
58 }
59
60 if (!client->fetchHeaders()) {
61 onError("Failed to get request headers");
62 return;
63 }
64
65 if (!client->isStatusCodeOk()) {
66 onError("Server response is not OK");
67 return;
68 }
69
70 auto bytes_left = client->getContentLength();
71
72 auto lock = file::getLock(downloadFilePath)->asScopedLock();
73 lock.lock();
74 LOGGER.info("opening {}", downloadFilePath);

Callers 2

doInstallMethod · 0.85
refreshMethod · 0.85

Calls 14

warnIfRunningOnGuiTaskFunction · 0.85
readStringFunction · 0.85
getLockFunction · 0.85
infoMethod · 0.80
fetchHeadersMethod · 0.80
isStatusCodeOkMethod · 0.80
getContentLengthMethod · 0.80
asScopedLockMethod · 0.80
dispatchMethod · 0.45
getMethod · 0.45
initMethod · 0.45
openMethod · 0.45

Tested by

no test coverage detected