Rename httpClient

This commit is contained in:
Jm Casler 2022-01-05 19:44:21 -08:00
parent d5fc905402
commit dbdbe75e9f

View File

@ -43,7 +43,7 @@ using namespace httpsserver;
#include <HTTPClient.h> #include <HTTPClient.h>
#include <WiFiClientSecure.h> #include <WiFiClientSecure.h>
HTTPClient http; HTTPClient httpClient;
#define DEST_FS_USES_SPIFFS #define DEST_FS_USES_SPIFFS
#include <ESP32-targz.h> #include <ESP32-targz.h>
@ -73,22 +73,22 @@ WiFiClient *getTarHTTPClientPtr(WiFiClientSecure *client, const char *url, const
client->setCACert(cert); client->setCACert(cert);
} }
const char *UserAgent = "ESP32-HTTP-GzUpdater-Client"; const char *UserAgent = "ESP32-HTTP-GzUpdater-Client";
http.setReuse(true); // handle 301 redirects gracefully httpClient.setReuse(true); // handle 301 redirects gracefully
http.setUserAgent(UserAgent); httpClient.setUserAgent(UserAgent);
http.setConnectTimeout(10000); // 10s timeout = 10000 httpClient.setConnectTimeout(10000); // 10s timeout = 10000
if (!http.begin(*client, url)) { if (!httpClient.begin(*client, url)) {
log_e("Can't open url %s", url); log_e("Can't open url %s", url);
return nullptr; return nullptr;
} }
const char *headerKeys[] = {"location", "redirect", "Content-Type", "Content-Length", "Content-Disposition"}; const char *headerKeys[] = {"location", "redirect", "Content-Type", "Content-Length", "Content-Disposition"};
const size_t numberOfHeaders = 5; const size_t numberOfHeaders = 5;
http.collectHeaders(headerKeys, numberOfHeaders); httpClient.collectHeaders(headerKeys, numberOfHeaders);
int httpCode = http.GET(); int httpCode = httpClient.GET();
// file found at server // file found at server
if (httpCode == HTTP_CODE_FOUND || httpCode == HTTP_CODE_MOVED_PERMANENTLY) { if (httpCode == HTTP_CODE_FOUND || httpCode == HTTP_CODE_MOVED_PERMANENTLY) {
String newlocation = ""; String newlocation = "";
String headerLocation = http.header("location"); String headerLocation = httpClient.header("location");
String headerRedirect = http.header("redirect"); String headerRedirect = httpClient.header("redirect");
if (headerLocation != "") { if (headerLocation != "") {
newlocation = headerLocation; newlocation = headerLocation;
Serial.printf("302 (location): %s => %s\n", url, headerLocation.c_str()); Serial.printf("302 (location): %s => %s\n", url, headerLocation.c_str());
@ -96,7 +96,7 @@ WiFiClient *getTarHTTPClientPtr(WiFiClientSecure *client, const char *url, const
Serial.printf("301 (redirect): %s => %s\n", url, headerLocation.c_str()); Serial.printf("301 (redirect): %s => %s\n", url, headerLocation.c_str());
newlocation = headerRedirect; newlocation = headerRedirect;
} }
http.end(); httpClient.end();
if (newlocation != "") { if (newlocation != "") {
log_w("Found 302/301 location header: %s", newlocation.c_str()); log_w("Found 302/301 location header: %s", newlocation.c_str());
return getTarHTTPClientPtr(client, newlocation.c_str(), cert); return getTarHTTPClientPtr(client, newlocation.c_str(), cert);
@ -107,7 +107,7 @@ WiFiClient *getTarHTTPClientPtr(WiFiClientSecure *client, const char *url, const
} }
if (httpCode != 200) if (httpCode != 200)
return nullptr; return nullptr;
return http.getStreamPtr(); return httpClient.getStreamPtr();
} }
void registerHandlers(HTTPServer *insecureServer, HTTPSServer *secureServer) void registerHandlers(HTTPServer *insecureServer, HTTPSServer *secureServer)
@ -700,12 +700,12 @@ void handleUpdateSPIFFS(HTTPRequest *req, HTTPResponse *res)
File root = SPIFFS.open("/"); File root = SPIFFS.open("/");
File file = root.openNextFile(); File file = root.openNextFile();
DEBUG_MSG("Deleting files from /static\n"); DEBUG_MSG("Deleting files from /static : \n");
while (file) { while (file) {
String filePath = String(file.name()); String filePath = String(file.name());
if (filePath.indexOf("/static") == 0) { if (filePath.indexOf("/static") == 0) {
DEBUG_MSG("%s\n", file.name()); DEBUG_MSG(" %s\n", file.name());
SPIFFS.remove(file.name()); SPIFFS.remove(file.name());
} }
file = root.openNextFile(); file = root.openNextFile();
@ -724,7 +724,7 @@ void handleUpdateSPIFFS(HTTPRequest *req, HTTPResponse *res)
BaseUnpacker::defaultTarStatusProgressCallback); // print the filenames as they're expanded BaseUnpacker::defaultTarStatusProgressCallback); // print the filenames as they're expanded
TARUnpacker->setTarMessageCallback(BaseUnpacker::targzPrintLoggerCallback); // tar log verbosity TARUnpacker->setTarMessageCallback(BaseUnpacker::targzPrintLoggerCallback); // tar log verbosity
String contentLengthStr = http.header("Content-Length"); String contentLengthStr = httpClient.header("Content-Length");
contentLengthStr.trim(); contentLengthStr.trim();
int64_t streamSize = -1; int64_t streamSize = -1;
if (contentLengthStr != "") { if (contentLengthStr != "") {
@ -739,14 +739,16 @@ void handleUpdateSPIFFS(HTTPRequest *req, HTTPResponse *res)
return; return;
} else { } else {
/*
// print leftover bytes if any (probably zero-fill from the server) // print leftover bytes if any (probably zero-fill from the server)
while (http.connected()) { while (httpClient.connected()) {
size_t streamSize = streamptr->available(); size_t streamSize = streamptr->available();
if (streamSize) { if (streamSize) {
Serial.printf("%02x ", streamptr->read()); Serial.printf("%02x ", streamptr->read());
} else } else
break; break;
} }
*/
} }
} else { } else {