The ESP-IDF project, docs, tools, tests and web UI move from v2/ to the root, and the Makefile, README, build scripts and test runner drop the v2/ prefix. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
139 lines
4.1 KiB
C
139 lines
4.1 KiB
C
#include "web.h"
|
|
|
|
#include <stddef.h>
|
|
#include <stdint.h>
|
|
#include <string.h>
|
|
#include "esp_log.h"
|
|
|
|
static const char *TAG = "web";
|
|
|
|
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";
|
|
}
|
|
|
|
// The web UI is one blob linked into the app image (www_dist/www.pack, see tools/build_web.sh):
|
|
// "WWW1", then per file u16 name length, u32 data length, the name, the gzipped data; a zero name
|
|
// length ends it.
|
|
extern const uint8_t www_pack_start[] asm("_binary_www_pack_start");
|
|
extern const uint8_t www_pack_end[] asm("_binary_www_pack_end");
|
|
|
|
static bool find_file(const char *path, const uint8_t **data, uint32_t *len)
|
|
{
|
|
const uint8_t *p = www_pack_start, *end = www_pack_end;
|
|
if (end - p < 6 || memcmp(p, "WWW1", 4) != 0) {
|
|
return false;
|
|
}
|
|
p += 4;
|
|
size_t want = strlen(path);
|
|
while (end - p >= 2) {
|
|
uint16_t nlen;
|
|
memcpy(&nlen, p, 2);
|
|
if (nlen == 0 || end - p < 6 + nlen) {
|
|
return false;
|
|
}
|
|
uint32_t dlen;
|
|
memcpy(&dlen, p + 2, 4);
|
|
const uint8_t *name = p + 6, *body = name + nlen;
|
|
if (end - body < (ptrdiff_t)dlen) {
|
|
return false;
|
|
}
|
|
if (nlen == want && memcmp(name, path, want) == 0) {
|
|
*data = body;
|
|
*len = dlen;
|
|
return true;
|
|
}
|
|
p = body + dlen;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
// path is the URL-relative name, e.g. "static/zones.js"
|
|
static esp_err_t send_gz(httpd_req_t *req, const char *path, bool immutable)
|
|
{
|
|
const uint8_t *data;
|
|
uint32_t len;
|
|
if (strstr(path, "..")) {
|
|
return httpd_resp_send_err(req, HTTPD_400_BAD_REQUEST, "bad path");
|
|
}
|
|
if (!find_file(path, &data, &len)) {
|
|
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");
|
|
return httpd_resp_send(req, (const char *)data, len);
|
|
}
|
|
|
|
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;
|
|
}
|