## Summary * **What is the goal of this PR?** Fix KOReader sync failing on HTTPS servers due to TLS out-of-memory on ESP32-C3 * **What changes are included?** - Replace `WiFiClientSecure`/`HTTPClient` with `esp_http_client` (ESP-IDF native API) for all KOSync HTTP requests - Use 2KB TLS buffers instead of the default 16KB — KOSync payloads are tiny JSON (<1KB), so this is more than sufficient - Use `esp_crt_bundle_attach` for proper TLS certificate verification (replaces `setInsecure()`) - Strip trailing slashes from server URL to prevent double-slash in API paths (e.g. `https://server.com//users/auth`) - Add `lastHttpCode` static field for diagnostics - Add free heap logging to help debug memory issues ### Problem The ESP32-C3 has ~46KB free heap after WiFi is initialized. `WiFiClientSecure` allocates 16KB for TLS RX + 16KB for TLS TX = 32KB just for the TLS buffers, leaving almost no room for the actual TLS handshake (which needs additional dynamic allocations). This causes KOReader sync to: 1. Fail silently with network errors on HTTPS servers (including the default `sync.koreader.rocks`) 2. Occasionally crash with heap exhaustion ### Solution `esp_http_client` (the ESP-IDF native HTTP client) allows configuring `buffer_size` and `buffer_size_tx` independently. Setting both to 2KB (total 4KB) leaves plenty of heap for the TLS handshake while still being sufficient for KOSync's small JSON payloads. This also fixes the `setInsecure()` anti-pattern — `esp_crt_bundle_attach` provides proper certificate verification using the ESP-IDF's built-in CA bundle, so credentials and reading history are no longer sent over unverified TLS connections. ### Files changed | File | Change | |------|--------| | `lib/KOReaderSync/KOReaderSyncClient.cpp` | Replace WiFiClientSecure/HTTPClient with esp_http_client; add ResponseBuffer, base64 encoder, createClient helper | | `lib/KOReaderSync/KOReaderSyncClient.h` | Add `lastHttpCode` static field | | `lib/KOReaderSync/KOReaderCredentialStore.cpp` | Strip trailing slashes from base URL | ## Additional Context Tested on a CrossPoint X4 (ESP32-C3 with 4MB flash). Before this change, KOSync auth to `sync.koreader.rocks` (HTTPS) would fail ~80% of the time. After: works reliably. The `base64Encode` helper is needed because `esp_http_client` doesn't have a built-in `setAuthorization()` method like `HTTPClient` does. This is used for the HTTP Basic Auth header required by Calibre-Web-Automated KOSync servers. Fixes #581 --- ### AI Usage While CrossPoint doesn't have restrictions on AI tools in contributing, please be transparent about their usage as it helps set the right context for reviewers. Did you use AI tools to help write this code? _**PARTIALLY**_ AI assisted with the esp_http_client migration pattern and base64 encoder. The root cause analysis (TLS buffer OOM) and solution design were manual. --------- Co-authored-by: trilwu <trilwu@users.noreply.github.com> Co-authored-by: Justin Mitchell <justin@jmitch.com>
63 lines
1.8 KiB
C++
63 lines
1.8 KiB
C++
#pragma once
|
|
#include <string>
|
|
|
|
/**
|
|
* Progress data from KOReader sync server.
|
|
*/
|
|
struct KOReaderProgress {
|
|
std::string document; // Document hash
|
|
std::string progress; // XPath-like progress string
|
|
float percentage; // Progress percentage (0.0 to 1.0)
|
|
std::string device; // Device name
|
|
std::string deviceId; // Device ID
|
|
int64_t timestamp; // Unix timestamp of last update
|
|
};
|
|
|
|
/**
|
|
* HTTP client for KOReader sync API.
|
|
*
|
|
* Base URL: https://sync.koreader.rocks:443/
|
|
*
|
|
* API Endpoints:
|
|
* GET /users/auth - Authenticate (validate credentials)
|
|
* GET /syncs/progress/:document - Get progress for a document
|
|
* PUT /syncs/progress - Update progress for a document
|
|
*
|
|
* Authentication:
|
|
* x-auth-user: username
|
|
* x-auth-key: MD5 hash of password
|
|
*/
|
|
class KOReaderSyncClient {
|
|
public:
|
|
enum Error { OK = 0, NO_CREDENTIALS, NETWORK_ERROR, AUTH_FAILED, SERVER_ERROR, JSON_ERROR, NOT_FOUND };
|
|
|
|
/**
|
|
* Authenticate with the sync server (validate credentials).
|
|
* @return OK on success, error code on failure
|
|
*/
|
|
static Error authenticate();
|
|
|
|
/**
|
|
* Get reading progress for a document.
|
|
* @param documentHash The document hash (from KOReaderDocumentId)
|
|
* @param outProgress Output: the progress data
|
|
* @return OK on success, NOT_FOUND if no progress exists, error code on failure
|
|
*/
|
|
static Error getProgress(const std::string& documentHash, KOReaderProgress& outProgress);
|
|
|
|
/**
|
|
* Update reading progress for a document.
|
|
* @param progress The progress data to upload
|
|
* @return OK on success, error code on failure
|
|
*/
|
|
static Error updateProgress(const KOReaderProgress& progress);
|
|
|
|
/**
|
|
* Get human-readable error message.
|
|
*/
|
|
static const char* errorString(Error error);
|
|
|
|
/** HTTP status code from the last request (for diagnostics). */
|
|
static int lastHttpCode;
|
|
};
|