From 5745e598e392f98e41d0c246b0601af125d7d6cf Mon Sep 17 00:00:00 2001 From: jpirnay Date: Wed, 25 Mar 2026 20:22:01 +0100 Subject: [PATCH] fix: migrate KOSync registerUser to esp_http_client --- lib/KOReaderSync/KOReaderSyncClient.cpp | 49 ++++++++++++------------- 1 file changed, 24 insertions(+), 25 deletions(-) diff --git a/lib/KOReaderSync/KOReaderSyncClient.cpp b/lib/KOReaderSync/KOReaderSyncClient.cpp index 56827a87..b6d593dc 100644 --- a/lib/KOReaderSync/KOReaderSyncClient.cpp +++ b/lib/KOReaderSync/KOReaderSyncClient.cpp @@ -2,6 +2,8 @@ #include #include +#include +#include #include #include @@ -110,22 +112,7 @@ KOReaderSyncClient::Error KOReaderSyncClient::registerUser() { } std::string url = KOREADER_STORE.getBaseUrl() + "/users/create"; - LOG_DBG("KOSync", "Registering user: %s", url.c_str()); - - HTTPClient http; - std::unique_ptr secureClient; - WiFiClient plainClient; - - if (isHttpsUrl(url)) { - secureClient.reset(new WiFiClientSecure); - secureClient->setInsecure(); - http.begin(*secureClient, url.c_str()); - } else { - http.begin(plainClient, url.c_str()); - } - - http.addHeader("Accept", "application/vnd.koreader.v1+json"); - http.addHeader("Content-Type", "application/json"); + LOG_DBG("KOSync", "Registering user: %s (heap: %u)", url.c_str(), (unsigned)ESP.getFreeHeap()); JsonDocument doc; doc["username"] = KOREADER_STORE.getUsername(); @@ -135,11 +122,24 @@ KOReaderSyncClient::Error KOReaderSyncClient::registerUser() { LOG_DBG("KOSync", "Register request body: "); - const int httpCode = http.POST(body.c_str()); - const String responseBody = http.getString(); - http.end(); + ResponseBuffer buf; + esp_http_client_handle_t client = createClient(url.c_str(), &buf, HTTP_METHOD_POST); + if (!client) return NETWORK_ERROR; - LOG_DBG("KOSync", "Register response: %d | body: %s", httpCode, responseBody.c_str()); + esp_http_client_set_header(client, "Content-Type", "application/json"); + esp_http_client_set_post_field(client, body.c_str(), body.length()); + + esp_err_t err = esp_http_client_perform(client); + const int httpCode = esp_http_client_get_status_code(client); + lastHttpCode = httpCode; + esp_http_client_cleanup(client); + + LOG_DBG("KOSync", "Register response: %d (err: %d) | body: %s", httpCode, err, + buf.data ? buf.data : ""); + + if (err != ESP_OK) { + return NETWORK_ERROR; + } if (httpCode == 201) { return OK; @@ -149,17 +149,16 @@ KOReaderSyncClient::Error KOReaderSyncClient::registerUser() { } else if (httpCode == 402) { // Both "user already exists" (error 2002) and "registration disabled" (error 2005) // return HTTP 402 on the original kosync server. Distinguish them by body text. - String lowerBody = responseBody; - lowerBody.toLowerCase(); - if (lowerBody.indexOf("already") >= 0) { + std::string lowerBody = buf.data ? buf.data : ""; + std::transform(lowerBody.begin(), lowerBody.end(), lowerBody.begin(), + [](unsigned char c) { return static_cast(std::tolower(c)); }); + if (lowerBody.find("already") != std::string::npos) { return USER_EXISTS; } return REGISTRATION_DISABLED; } else if (httpCode == 409) { // korrosync returns 409 for existing users return USER_EXISTS; - } else if (httpCode < 0) { - return NETWORK_ERROR; } return SERVER_ERROR; }