From 394bf8873b80f667aa490dbb745f7da0c51add40 Mon Sep 17 00:00:00 2001 From: Jeremy Poulter Date: Tue, 9 Nov 2021 23:10:29 +0000 Subject: [PATCH 1/4] Adding some debug --- src/http_update.cpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/http_update.cpp b/src/http_update.cpp index c4331573..1f863e5f 100644 --- a/src/http_update.cpp +++ b/src/http_update.cpp @@ -25,8 +25,11 @@ bool http_update_from_url(String url, { request->setMethod(HTTP_GET); + DBUGF("Trying to fetch firmware from %s", url.c_str()); + request->onBody([url,progress,error,request](MongooseHttpClientResponse *response) { + DBUGVAR(response->respCode()); if(response->respCode() == 200) { size_t total = response->contentLength(); From 75dfe097cfc802d9bd63e63e666f8cf3f1997e9d Mon Sep 17 00:00:00 2001 From: Jeremy Poulter Date: Tue, 9 Nov 2021 23:11:04 +0000 Subject: [PATCH 2/4] Enable GitHub certificate --- src/root_ca.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/root_ca.cpp b/src/root_ca.cpp index 932d613f..dbd01b08 100644 --- a/src/root_ca.cpp +++ b/src/root_ca.cpp @@ -29,7 +29,7 @@ const char *root_ca = "" #endif #ifndef CA_GITHUB -#define CA_GITHUB 0 +#define CA_GITHUB 1 #endif #ifndef CA_AMAZON_1 From 14dbee3db3d1649a9cf32b687b073320ab23f9b2 Mon Sep 17 00:00:00 2001 From: Jeremy Poulter Date: Tue, 9 Nov 2021 23:11:59 +0000 Subject: [PATCH 3/4] Added example of gettting the pre-release build --- test/update.http | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/test/update.http b/test/update.http index f62cb7aa..b7d93ad0 100644 --- a/test/update.http +++ b/test/update.http @@ -40,18 +40,24 @@ Content-Type: application/octet-stream curl -F firmware=@.pio/build/openevse_esp-wrover-kit/firmware.bin {{baseUrl}}/update ### - +# Get the latest published relese (not pre-release) # @name latest GET https://api.github.com/repos/OpenEVSE/ESP32_WiFi_V4.x/releases/latest Accept: application/vnd.github.v3+json +### +# Get the latest pre-release +# @name latest +GET https://api.github.com/repos/OpenEVSE/ESP32_WiFi_V4.x/releases/tags/latest +Accept: application/vnd.github.v3+json + ### POST {{baseUrl}}/update Content-Type: application/javascript { - "url": "{{latest.response.body.assets[1].browser_download_url}}" + "url": "{{latest.response.body.assets[0].browser_download_url}}" } ### From 0dc35f396294e2e11b5329f9429cce3c5e546100 Mon Sep 17 00:00:00 2001 From: Jeremy Poulter Date: Wed, 8 Mar 2023 22:47:40 +0000 Subject: [PATCH 4/4] Added support for redirecting in the HTTP pull update to support updating directly from GitHub --- platformio.ini | 3 +++ src/http_update.cpp | 35 ++++++++++++++++++++++++++++------- 2 files changed, 31 insertions(+), 7 deletions(-) diff --git a/platformio.ini b/platformio.ini index f632e4e1..833c247d 100644 --- a/platformio.ini +++ b/platformio.ini @@ -60,6 +60,8 @@ debug_flags = #-D ENABLE_DEBUG_TESLA_CLIENT #-D ENABLE_DEBUG_LIMIT #-D ENABLE_PROFILE + #-D ENABLE_DEBUG_HTTP_UPATE + #-D ENABLE_DEBUG_TIME #-D ENABLE_NOISY_PROFILE #-D ENABLE_DEBUG_MICROTASKS src_build_flags = @@ -95,6 +97,7 @@ build_flags = -D ARDUINO_ARCH_ESP32 -D USE_ESP32 -D USE_ESP32_FRAMEWORK_ARDUINO + -D MG_MAX_HTTP_REQUEST_SIZE=8196 build_partitions = min_spiffs.csv build_partitions_debug = min_spiffs_debug.csv diff --git a/src/http_update.cpp b/src/http_update.cpp index 1f863e5f..4f1ef8ca 100644 --- a/src/http_update.cpp +++ b/src/http_update.cpp @@ -20,6 +20,8 @@ bool http_update_from_url(String url, std::function success, std::function error) { + DBUGF("Update from URL: %s", url.c_str()); + MongooseHttpClientRequest *request = client.beginRequest(url.c_str()); if(request) { @@ -29,7 +31,7 @@ bool http_update_from_url(String url, request->onBody([url,progress,error,request](MongooseHttpClientResponse *response) { - DBUGVAR(response->respCode()); + DBUGF("Update onBody %d", response->respCode()); if(response->respCode() == 200) { size_t total = response->contentLength(); @@ -48,20 +50,39 @@ bool http_update_from_url(String url, } else { error(HTTP_UPDATE_ERROR_FAILED_TO_START_UPDATE); } + } else if (300 <= response->respCode() && response->respCode() < 400) { + // handle 3xx redirects (later) + return; } else { error(response->respCode()); } request->abort(); }); + request->onResponse([progress, error, success, request](MongooseHttpClientResponse *response) + { + DBUGF("Update onResponse %d", response->respCode()); + if(301 == response->respCode() || + 302 == response->respCode()) + { + MongooseString location = response->headers("Location"); + DBUGVAR(location.toString()); + http_update_from_url(location.toString(), progress, success, error); + } + }); + request->onClose([success, error]() { - if(http_update_end()) + DBUGLN("Update onClose"); + if(Update.isRunning()) { - success(HTTP_UPDATE_OK); - restart_system(); - } else { - error(HTTP_UPDATE_ERROR_FAILED_TO_END_UPDATE); + if(http_update_end()) + { + success(HTTP_UPDATE_OK); + restart_system(); + } else { + error(HTTP_UPDATE_ERROR_FAILED_TO_END_UPDATE); + } } }); client.send(request); @@ -115,7 +136,7 @@ bool http_update_write(uint8_t *data, size_t len) lcd.display(text, 0, 1, 10 * 1000, LCD_DISPLAY_NOW); DEBUG_PORT.printf("Update: %d%%\n", percent); - + StaticJsonDocument<128> event; event["ota_progress"] = percent; web_server_event(event);