Skip to content

A Hardware Hacking Tool with Web-Based CLI That Speaks Every Protocol

rework http service

Geo68587a3c5767
diff --git a/src/Services/HttpService.cpp b/src/Services/HttpService.cpp
index 8383eb20..af173846 100644
--- a/src/Services/HttpService.cpp
+++ b/src/Services/HttpService.cpp
@@ -1,56 +1,166 @@
-// Not used for now, commented
-
-// #include "HttpService.h"
-
-// static inline bool starts_with(const std::string& s, const char* p) {
-// return s.rfind(p, 0) == 0;
-// }
-
-// esp_err_t HttpService::evt(esp_http_client_event_t *e) {
-// if (!e || !e->user_data) return ESP_OK;
-// auto* headers = reinterpret_cast<std::string*>(e->user_data);
-// if (e->event_id == HTTP_EVENT_ON_HEADER && e->header_key && e->header_value) {
-// headers->append(e->header_key);
-// headers->append(": ");
-// headers->append(e->header_value);
-// headers->append("\r\n");
-// }
-// return ESP_OK;
-// }
-
-// std::string HttpService::get(const std::string& hostOrUrl, int timeout_ms) {
-// // Force http:// if no prefix
-// std::string url = hostOrUrl;
-// if (!starts_with(url, "http://") && !starts_with(url, "https://")) {
-// url = "https://" + url;
-// }
-
-// std::string headers;
-// esp_http_client_config_t cfg = {};
-// cfg.url = url.c_str();
-// cfg.method = HTTP_METHOD_HEAD;
-// cfg.timeout_ms = timeout_ms;
-// cfg.disable_auto_redirect = false;
-// cfg.max_redirection_count = 5;
-// cfg.event_handler = &HttpService::evt;
-// cfg.user_data = &headers;
-
-// esp_http_client_handle_t h = esp_http_client_init(&cfg);
-// if (!h) return "ERROR: esp_http_client_init failed";
-
-// esp_err_t err = esp_http_client_perform(h);
-// if (err != ESP_OK) {
-// std::string out = "ERROR: ";
-// out += esp_err_to_name(err);
-// esp_http_client_cleanup(h);
-// return out;
-// }
-
-// int status = esp_http_client_get_status_code(h);
-// esp_http_client_cleanup(h);
-
-// // Compose final string
-// std::string out = "HTTP/1.1 " + std::to_string(status) + "\r\n";
-// out += headers;
-// return out;
-// }
+#include "HttpService.h"
+
+struct HttpGetParams {
+ std::string url;
+ int timeout_ms;
+ int bodyMaxBytes;
+ bool insecure;
+ HttpService* self;
+};
+
+void HttpService::startGetTask(const std::string& url, int timeout_ms, int bodyMaxBytes, bool insecure,
+ int stack_bytes, int core)
+{
+ ready = false;
+ auto* p = new HttpGetParams{url, timeout_ms, bodyMaxBytes, insecure, this};
+ xTaskCreatePinnedToCore(&HttpService::getTask, "HttpGet", stack_bytes/sizeof(StackType_t),
+ p, 1, nullptr, core);
+}
+
+void HttpService::getTask(void* pv)
+{
+ auto* p = static_cast<HttpGetParams*>(pv);
+ auto* self = p->self;
+
+ bool isHttps = (p->url.rfind("https://",0)==0);
+
+ // Guard to prevent concurrent requests
+ bool expected = false;
+ if (!self->inFlight.compare_exchange_strong(expected, true, std::memory_order_acq_rel)) {
+ return; // already in flight
+ }
+
+ // Mark as not ready
+ self->ready.store(false, std::memory_order_release);
+
+ std::unique_ptr<WiFiClient> client;
+ if (isHttps) {
+ auto *c = new WiFiClientSecure();
+ if (p->insecure) c->setInsecure();
+ c->setTimeout(p->timeout_ms / 1000);
+ client.reset(c);
+ } else {
+ auto *c = new WiFiClient();
+ c->setTimeout(p->timeout_ms / 1000);
+ client.reset(c);
+ }
+
+ HTTPClient http;
+ http.setTimeout(p->timeout_ms);
+
+ std::string result;
+
+ if (!http.begin(*client, p->url.c_str())) {
+ result = "ERROR: begin failed";
+ } else {
+ http.collectHeaders(HttpService::headerKeys,
+ sizeof(HttpService::headerKeys) / sizeof(HttpService::headerKeys[0]));
+
+ // GET
+ http.addHeader("Accept-Encoding", "identity"); // no gzip
+ http.addHeader("Connection", "close"); // close socket
+ int code = http.GET();
+
+ // Check response code
+ if (code > 0) {
+
+ // Construct headers
+ result = "HTTP/1.1 " + std::to_string(code) + "\r\n";
+ int n = http.headers();
+ for (int i=0; i<n; i++) {
+ result += http.headerName(i).c_str();
+ result += ": ";
+ result += http.header(i).c_str();
+ result += "\r\n";
+ }
+
+ // Json content type
+ String ct = http.header("Content-Type");
+ bool isJson = ct.length() && (ct.indexOf("json") >= 0);
+
+ // If json, get the body
+ if (isJson) {
+ result += "\r\nJSON BODY:\n";
+ result += HttpService::getJsonBody(http, p->bodyMaxBytes);
+ }
+
+ } else {
+ result = "ERROR: " + std::string(http.errorToString(code).c_str());
+ }
+ http.getStream().stop();
+ http.end();
+ vTaskDelay(pdMS_TO_TICKS(50));
+ }
+
+ // Ready to fetch the response
+ self->response = result;
+ self->ready.store(true, std::memory_order_release);
+ self->inFlight.store(false, std::memory_order_release);
+ delete p;
+ vTaskDelete(nullptr);
+}
+
+std::string HttpService::getJsonBody(HTTPClient& http, int bodyMaxBytes) {
+ std::string out;
+ if (bodyMaxBytes <= 0) return out; // no length
+
+ WiFiClient* stream = http.getStreamPtr();
+
+ // Max bytes to read
+ size_t budget = static_cast<size_t>(bodyMaxBytes);
+
+ // Expected size if known, -1 if chunked/unknown
+ int size = http.getSize();
+ size_t target = (size > 0) ? (size_t)size : budget;
+ if (size > 0 && target > budget) target = budget;
+
+ // Small chunks to limit RAM spikes
+ static constexpr size_t CHUNK = 256;
+ uint8_t buf[CHUNK];
+
+ // Small initial reserve (limits reallocs)
+ out.reserve(std::min(target, (size_t)128));
+
+ const unsigned long IDLE_TIMEOUT_MS = 1200;
+ unsigned long lastDataMs = millis();
+ size_t readTotal = 0;
+
+ // Read condition
+ auto canContinue = [&]() -> bool {
+ // Continue as long as there is budget left and the socket is alive
+ if (budget == 0) return false;
+ if (stream->available() > 0) return true;
+ // if content length is known, we can wait until everything is read
+ if ((size < 0) && !stream->connected() && stream->available() == 0) return false;
+ // idle timeout
+ return (millis() - lastDataMs) < IDLE_TIMEOUT_MS;
+ };
+
+ // Read loop
+ while (canContinue()) {
+ int avail = stream->available();
+ if (avail <= 0) { delay(1); continue; }
+
+ size_t toRead = (size_t)avail;
+ if (toRead > CHUNK) toRead = CHUNK;
+ if (toRead > budget) toRead = budget;
+
+ int n = stream->read(buf, (int)toRead);
+ if (n <= 0) { delay(1); continue; }
+
+ size_t oldSz = out.size();
+ out.resize(oldSz + (size_t)n);
+ memcpy(&out[oldSz], buf, (size_t)n);
+
+ readTotal += (size_t)n;
+ budget -= (size_t)n;
+ lastDataMs = millis();
+
+ if (size > 0 && readTotal >= (size_t)size) break; // all received
+ if (budget == 0) {
+ out += "...[TRUNCATED]";
+ break; // budget exceeded
+ }
+ }
+ return out;
+}
diff --git a/src/Services/HttpService.h b/src/Services/HttpService.h
index bd12bbe5..6296395e 100644
--- a/src/Services/HttpService.h
+++ b/src/Services/HttpService.h
@@ -1,17 +1,61 @@
-// #pragma once
-// #include <string>
-
-// extern "C" {
-// #include "esp_http_client.h"
-// #if __has_include("esp_crt_bundle.h")
-// #include "esp_crt_bundle.h"
-// #endif
-// }
-
-// class HttpService {
-// public:
-// std::string get(const std::string& hostOrUrl, int timeout_ms = 5000);
-
-// private:
-// static esp_err_t evt(esp_http_client_event_t *e);
-// };
+#pragma once
+#undef INADDR_NONE
+#undef IPADDR_NONE
+#include <HTTPClient.h>
+#include <vector>
+#include <string>
+#include <atomic>
+
+class HttpService {
+public:
+ // ============ GET ================
+
+ // Http get task on url
+ void startGetTask(const std::string& url,
+ int timeout_ms,
+ int bodyMaxBytes,
+ bool insecure,
+ int stack_bytes = 24 * 1024,
+ int core = 1);
+
+ // Verify if the response is ready
+ bool isResponseReady() const { return ready; }
+
+ // Get last HTTP response (status + headers)
+ const std::string& lastResponse() const { return response; }
+
+ // Resets the internal state
+ void reset() { response.clear(); ready = false; }
+
+ // ===================================
+
+private:
+ static void getTask(void* pv);
+ static std::string getJsonBody(HTTPClient& http, int bodyMaxBytes);
+
+ std::atomic<bool> ready{false}; // response is ready
+ std::atomic<bool> inFlight{false}; // avoid double requests
+ std::string response; // http response
+
+ inline static const char* headerKeys[] = {
+ // General
+ "Location","Server","Date","Content-Type","Content-Length",
+ "Connection","Keep-Alive","Cache-Control","Expires","Last-Modified",
+ "ETag","Vary","Transfer-Encoding","Content-Encoding","Content-Language",
+ "Content-Disposition","Accept-Ranges","Content-Range","Set-Cookie",
+ // auth / quotas
+ "WWW-Authenticate","Retry-After",
+ "X-RateLimit-Limit","X-RateLimit-Remaining","X-RateLimit-Reset",
+ // CORS
+ "Access-Control-Allow-Origin","Access-Control-Allow-Credentials",
+ "Access-Control-Allow-Methods","Access-Control-Allow-Headers",
+ "Access-Control-Expose-Headers","Access-Control-Max-Age",
+ // Security
+ "Strict-Transport-Security","Content-Security-Policy",
+ "X-Content-Type-Options","X-Frame-Options","Referrer-Policy",
+ "Permissions-Policy",
+ // perfs
+ "Alt-Svc","Server-Timing"
+ };
+
+};