Files
led-controller/v2/main/web.c
T
jimmyandClaude Sonnet 5.5 f7ac4ab290 feat(v2): audio page, beat endpoints and an Audio menu button
Restores the live audio section from esp32-eth: /audio page, /audio.wav
stream, /beats event feed, POST /tap and /time, and the mic and beat state
in /status. The web build adds an Audio entry to the header menu and the
mobile dropdown for the v2 copy of index.html only, so the Pi UI is unchanged.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-27 22:30:26 +13:00

121 lines
3.4 KiB
C

#include "web.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "esp_log.h"
static const char *TAG = "web";
#define CHUNK 2048
static const struct {
const char *ext;
const char *type;
} MIME[] = {
{ ".html", "text/html; charset=utf-8" },
{ ".js", "application/javascript; charset=utf-8" },
{ ".css", "text/css; charset=utf-8" },
{ ".json", "application/json" },
{ ".svg", "image/svg+xml" },
{ ".png", "image/png" },
{ ".ico", "image/x-icon" },
{ ".woff2", "font/woff2" },
};
static const char *mime_for(const char *path)
{
const char *dot = strrchr(path, '.');
for (size_t i = 0; dot && i < sizeof(MIME) / sizeof(MIME[0]); i++) {
if (strcmp(dot, MIME[i].ext) == 0) {
return MIME[i].type;
}
}
return "application/octet-stream";
}
// path is the URL-relative name, e.g. "static/zones.js"; the file on flash is "/www/<path>.gz"
static esp_err_t send_gz(httpd_req_t *req, const char *path, bool immutable)
{
char fs[128];
if (strstr(path, "..") || snprintf(fs, sizeof(fs), "/www/%s.gz", path) >= (int)sizeof(fs)) {
return httpd_resp_send_err(req, HTTPD_400_BAD_REQUEST, "bad path");
}
FILE *f = fopen(fs, "rb");
if (!f) {
return httpd_resp_send_404(req);
}
httpd_resp_set_type(req, mime_for(path));
httpd_resp_set_hdr(req, "Content-Encoding", "gzip");
httpd_resp_set_hdr(req, "Cache-Control", immutable ? "public, max-age=86400" : "no-cache");
char *buf = malloc(CHUNK);
if (!buf) {
fclose(f);
return httpd_resp_send_500(req);
}
esp_err_t ret = ESP_OK;
size_t n;
while (ret == ESP_OK && (n = fread(buf, 1, CHUNK, f)) > 0) {
ret = httpd_resp_send_chunk(req, buf, n);
}
if (ret == ESP_OK) {
ret = httpd_resp_send_chunk(req, NULL, 0);
}
free(buf);
fclose(f);
return ret;
}
esp_err_t web_send_page(httpd_req_t *req, const char *name)
{
return send_gz(req, name, false);
}
static esp_err_t index_handler(httpd_req_t *req)
{
return send_gz(req, "index.html", false);
}
static esp_err_t audio_page_handler(httpd_req_t *req)
{
return send_gz(req, "audio.html", false);
}
static esp_err_t static_handler(httpd_req_t *req)
{
char path[96];
const char *uri = req->uri + 1; // drop the leading '/'
size_t n = strcspn(uri, "?");
if (n >= sizeof(path)) {
return httpd_resp_send_err(req, HTTPD_400_BAD_REQUEST, "uri too long");
}
memcpy(path, uri, n);
path[n] = 0;
return send_gz(req, path, true);
}
static esp_err_t favicon_handler(httpd_req_t *req)
{
httpd_resp_set_status(req, "204 No Content");
return httpd_resp_send(req, NULL, 0);
}
esp_err_t web_register(httpd_handle_t server)
{
const httpd_uri_t uris[] = {
{ .uri = "/", .method = HTTP_GET, .handler = index_handler },
{ .uri = "/audio", .method = HTTP_GET, .handler = audio_page_handler },
{ .uri = "/static/*", .method = HTTP_GET, .handler = static_handler },
{ .uri = "/favicon.ico", .method = HTTP_GET, .handler = favicon_handler },
};
for (size_t i = 0; i < sizeof(uris) / sizeof(uris[0]); i++) {
esp_err_t err = httpd_register_uri_handler(server, &uris[i]);
if (err != ESP_OK) {
ESP_LOGE(TAG, "register %s: %s", uris[i].uri, esp_err_to_name(err));
return err;
}
}
return ESP_OK;
}