#include "web.h" #include #include #include #include #include "esp_app_desc.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; } bool web_file(const char *path, const uint8_t **data, uint32_t *len) { return find_file(path, data, len); } void web_each(const char *prefix, void (*fn)(const char *name, void *ctx), void *ctx) { const uint8_t *p = www_pack_start + 4, *end = www_pack_end; if (end - www_pack_start < 6 || memcmp(www_pack_start, "WWW1", 4) != 0) { return; } size_t plen = strlen(prefix); while (end - p >= 6) { uint16_t nlen; uint32_t dlen; memcpy(&nlen, p, 2); memcpy(&dlen, p + 2, 4); if (nlen == 0 || end - p < 6 + nlen + (ptrdiff_t)dlen) { return; } char name[128]; if (nlen < sizeof(name) && nlen > plen && memcmp(p + 6, prefix, plen) == 0) { memcpy(name, p + 6 + plen, nlen - plen); name[nlen - plen] = 0; fn(name, ctx); } p += 6 + nlen + dlen; } } // Every file changes only with the firmware, so the build's hash is the ETag: browsers revalidate on // each load (no-cache) and get a 304 until the next update, instead of running a stale UI for a day. static const char *etag(void) { static char tag[20]; if (!tag[0]) { const uint8_t *sha = esp_app_get_description()->app_elf_sha256; snprintf(tag, sizeof(tag), "\"%02x%02x%02x%02x%02x%02x%02x%02x\"", sha[0], sha[1], sha[2], sha[3], sha[4], sha[5], sha[6], sha[7]); } return tag; } // path is the URL-relative name, e.g. "static/zones.js" static esp_err_t send_gz(httpd_req_t *req, const char *path) { 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_hdr(req, "Cache-Control", "no-cache"); httpd_resp_set_hdr(req, "ETag", etag()); char inm[24]; if (httpd_req_get_hdr_value_str(req, "If-None-Match", inm, sizeof(inm)) == ESP_OK && !strcmp(inm, etag())) { httpd_resp_set_status(req, "304 Not Modified"); return httpd_resp_send(req, NULL, 0); } httpd_resp_set_type(req, mime_for(path)); httpd_resp_set_hdr(req, "Content-Encoding", "gzip"); 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); } static esp_err_t index_handler(httpd_req_t *req) { return send_gz(req, "index.html"); } static esp_err_t audio_page_handler(httpd_req_t *req) { return send_gz(req, "audio.html"); } 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); } 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; }