|
@@ -2,6 +2,9 @@
|
|
|
|
|
|
#include <freertos/task.h>
|
|
|
#include <esp_log.h>
|
|
|
+#include <esp_tls.h>
|
|
|
+
|
|
|
+#include <memory>
|
|
|
|
|
|
#include "kbf/exception.h"
|
|
|
#include "kbf/http/exception.h"
|
|
@@ -16,12 +19,12 @@ using std::nullopt;
|
|
|
using std::map;
|
|
|
|
|
|
http::Client::Client(int timeoutMs, bool async) : timeoutMs(timeoutMs), async(async), buffer() {
|
|
|
- ESP_LOGD(TAG, "Client()");
|
|
|
+ ESP_LOGD(TAG, "%s()", __func__);
|
|
|
init();
|
|
|
}
|
|
|
|
|
|
void http::Client::init() {
|
|
|
- ESP_LOGD(TAG, "init()");
|
|
|
+ ESP_LOGD(TAG, "%s()", __func__);
|
|
|
esp_http_client_config_t config{};
|
|
|
config.event_handler = handleHttpEvent;
|
|
|
config.user_data = this;
|
|
@@ -31,18 +34,19 @@ void http::Client::init() {
|
|
|
config.max_redirection_count = 0;
|
|
|
config.timeout_ms = timeoutMs;
|
|
|
config.is_async = async;
|
|
|
+ config.use_global_ca_store = true;
|
|
|
handle = esp_http_client_init(&config);
|
|
|
}
|
|
|
|
|
|
http::Client::~Client() {
|
|
|
- ESP_LOGD(TAG, "~Client()");
|
|
|
+ ESP_LOGD(TAG, "%s()", __func__);
|
|
|
CHECK_ABORT(esp_http_client_cleanup(handle));
|
|
|
}
|
|
|
|
|
|
std::shared_ptr<kbf::http::Response>
|
|
|
http::Client::performRequest(const kbf::http::Method method, const std::string &url, const nlohmann::json *postData,
|
|
|
const optional<const map<string, string>> &headers) {
|
|
|
- ESP_LOGD(TAG, "performRequest(); method: %d, url: %s", method, url.c_str());
|
|
|
+ ESP_LOGD(TAG, "%s(); method: %d, url: %s", __func__, method, url.c_str());
|
|
|
|
|
|
if (running) {
|
|
|
ESP_LOGE(TAG, "request already in progress");
|
|
@@ -50,7 +54,7 @@ http::Client::performRequest(const kbf::http::Method method, const std::string &
|
|
|
}
|
|
|
running = true;
|
|
|
|
|
|
- response = shared_ptr<Response>(new Response()); // TODO why doesn't make_shared work here? :/
|
|
|
+ response = std::make_shared<Response>();
|
|
|
buffer.clear();
|
|
|
string body;
|
|
|
|
|
@@ -159,7 +163,7 @@ esp_err_t http::Client::handleHttpEvent(esp_http_client_event_t *event) {
|
|
|
updateResponse(*client);
|
|
|
break;
|
|
|
case HTTP_EVENT_DISCONNECTED:
|
|
|
- ESP_LOGW(TAG, "disconnected");
|
|
|
+ ESP_LOGI(TAG, "disconnected");
|
|
|
client->running = false;
|
|
|
break;
|
|
|
}
|
|
@@ -182,13 +186,29 @@ void http::Client::updateResponse(http::Client &client) {
|
|
|
|
|
|
shared_ptr<http::Response>
|
|
|
http::Client::get(const string &url, const optional<const map<string, string>> &headers) {
|
|
|
- ESP_LOGD(TAG, "get(%s)", url.c_str());
|
|
|
+ ESP_LOGD(TAG, "%s(%s)", __func__, url.c_str());
|
|
|
return performRequest(GET, url, nullptr, headers);
|
|
|
}
|
|
|
|
|
|
shared_ptr<http::Response>
|
|
|
http::Client::post(const string &url, const nlohmann::json &data, const optional<const map<string, string>> &headers) {
|
|
|
- ESP_LOGD(TAG, "post(%s)", url.c_str());
|
|
|
+ ESP_LOGD(TAG, "%s(%s)", __func__, url.c_str());
|
|
|
return performRequest(POST, url, &data, headers);
|
|
|
}
|
|
|
|
|
|
+void http::Client::addCert(const unsigned char *const start, const unsigned char *const end) {
|
|
|
+ ESP_LOGI(TAG, "%s()", __func__);
|
|
|
+ ESP_LOGD(TAG, "certificate:\n%s", start);
|
|
|
+
|
|
|
+ CHECK(esp_tls_set_global_ca_store(start, end - start));
|
|
|
+}
|
|
|
+
|
|
|
+void http::Client::clearCerts() {
|
|
|
+ ESP_LOGI(TAG, "%s()", __func__);
|
|
|
+ esp_tls_free_global_ca_store();
|
|
|
+}
|
|
|
+
|
|
|
+void http::Client::disconnect() {
|
|
|
+ ESP_LOGD(TAG, "%s()", __func__);
|
|
|
+ CHECK(esp_http_client_close(handle));
|
|
|
+}
|