@@ -4,14 +4,13 @@ const char* const TAG = "HTTPRequestManager";
44
55#include " Common.h"
66#include " Core.h"
7- #include " http/HTTPClient.h"
87#include " Logging.h"
98#include " RateLimiter.h"
109#include " SimpleMutex.h"
1110#include " util/HexUtils.h"
1211#include " util/StringUtils.h"
1312
14- #include " WiFiClient.h "
13+ #include < esp_http_client.h >
1514
1615#include < algorithm>
1716#include < memory>
@@ -350,29 +349,35 @@ StreamReaderResult _readStreamData(HTTP::HTTPClient& client, WiFiClient* stream,
350349}
351350
352351HTTP::Response<std::size_t > _doGetStream (
353- HTTP::HTTPClient& client,
352+ esp_http_client_handle_t client,
354353 const char * url,
355- const std::map<String, String >& headers,
354+ const std::map<std::string, std::string >& headers,
356355 tcb::span<const uint16_t > acceptedCodes,
357356 std::shared_ptr<OpenShock::RateLimiter> rateLimiter,
358357 HTTP::GotContentLengthCallback contentLengthCallback,
359358 HTTP::DownloadCallback downloadCallback,
360359 int timeoutMs
361360)
362361{
362+ esp_err_t err;
363+
363364 int64_t begin = OpenShock::millis ();
364365
365366 for (auto & header : headers) {
366- client.SetHeader (header.first , header.second );
367+ err = esp_http_client_set_header (client, header.first .c_str (), header.second .c_str ());
368+ if (err != ESP_OK) {
369+ // TODO: Handle error
370+ }
371+ return {HTTP::RequestResult::RequestFailed, 0 , 0 };
367372 }
368373
369- auto response = client.Get (url);
370- if (!response.IsValid ()) {
371- esp_err_t err = response.GetError ();
374+ err = esp_http_client_open (client, 0 );
375+ if (err != ESP_OK) {
372376 OS_LOGE (TAG, " Failed to begin HTTP request" );
373377 return {HTTP::RequestResult::RequestFailed, 0 , 0 };
374378 }
375379
380+ err = esp_http_
376381 auto responseCode = response.ResponseCode ();
377382 if (responseCode == HTTP_CODE_REQUEST_TIMEOUT || begin + timeoutMs < OpenShock::millis ()) {
378383 OS_LOGW (TAG, " Request timed out" );
@@ -383,7 +388,7 @@ HTTP::Response<std::size_t> _doGetStream(
383388 // https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Retry-After
384389
385390 // Get "Retry-After" header
386- String retryAfterStr = client.header (" Retry-After" );
391+ std::string retryAfterStr = client.header (" Retry-After" );
387392
388393 // Try to parse it as an integer (delay-seconds)
389394 long retryAfter = 0 ;
@@ -444,7 +449,7 @@ HTTP::Response<std::size_t> _doGetStream(
444449 return {result.result , responseCode, result.nWritten };
445450}
446451
447- HTTP::Response<std::size_t > HTTP::Download (const char * url, const std::map<String, String >& headers, HTTP::GotContentLengthCallback contentLengthCallback, HTTP::DownloadCallback downloadCallback, tcb::span<const uint16_t > acceptedCodes, uint32_t timeoutMs)
452+ HTTP::Response<std::size_t > HTTP::Download (const char * url, const std::map<std::string, std::string >& headers, HTTP::GotContentLengthCallback contentLengthCallback, HTTP::DownloadCallback downloadCallback, tcb::span<const uint16_t > acceptedCodes, uint32_t timeoutMs)
448453{
449454 std::shared_ptr<OpenShock::RateLimiter> rateLimiter = _getRateLimiter (url);
450455 if (rateLimiter == nullptr ) {
@@ -455,12 +460,29 @@ HTTP::Response<std::size_t> HTTP::Download(const char* url, const std::map<Strin
455460 return {RequestResult::RateLimited, 0 , 0 };
456461 }
457462
458- HTTP::HTTPClient client (url);
463+ esp_http_client_config_t cfg = {
464+ .url = url,
465+ .user_agent = OpenShock::Constants::FW_USERAGENT,
466+ .timeout_ms = 10'000 ,
467+ .disable_auto_redirect = true ,
468+ .event_handler = HTTPClient::EventHandler,
469+ .user_data = reinterpret_cast <void *>(this ),
470+ .is_async = true ,
471+ .use_global_ca_store = true ,
472+ };
473+ esp_http_client_handle_t client = esp_http_client_init (&cfg);
474+
475+ auto result = _doGetStream (client, url, headers, acceptedCodes, rateLimiter, contentLengthCallback, downloadCallback, timeoutMs);
476+
477+ esp_err_t err = esp_http_client_cleanup (client);
478+ if (err != ESP_OK) {
479+ // TODO: Handle error
480+ }
459481
460- return _doGetStream (client, url, headers, acceptedCodes, rateLimiter, contentLengthCallback, downloadCallback, timeoutMs) ;
482+ return result ;
461483}
462484
463- HTTP::Response<std::string> HTTP::GetString (const char * url, const std::map<String, String >& headers, tcb::span<const uint16_t > acceptedCodes, uint32_t timeoutMs)
485+ HTTP::Response<std::string> HTTP::GetString (const char * url, const std::map<std::string, std::string >& headers, tcb::span<const uint16_t > acceptedCodes, uint32_t timeoutMs)
464486{
465487 std::string result;
466488
0 commit comments