Embed gzipped web assets at build time and type LED edit commands.
Generate the web UI headers from c/web/ via scripts/embed_file.py (minify + gzip) and serve them with Content-Encoding, replacing the checked-in web_*.h blobs. Move the LED driver/presets into lib/leds, and parse preset edits into a typed cmd_preset_t on core0 instead of passing raw JSON to core1. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
b3e9fc9663
commit
bf0429c505
19 files changed
+395
-2205
No files matched your search
@@ -53,7 +53,10 @@ struct httpserver_conn {
|
||||
/* streaming static response */
|
||||
const uint8_t *static_body;
|
||||
size_t static_len;
|
||||
size_t static_sent;
|
||||
size_t static_sent; /* bytes queued via tcp_write */
|
||||
size_t tx_acked; /* bytes ACKed (from tcp_sent) */
|
||||
size_t tx_total; /* hdr_len + static_len */
|
||||
bool write_done;
|
||||
char hdr[HS_HDR_BUF];
|
||||
int hdr_len;
|
||||
int hdr_sent;
|
||||
@@ -364,43 +367,60 @@ static bool parse_request_line(const char *req, char *method, size_t method_len,
|
||||
}
|
||||
|
||||
static err_t pump_static(httpserver_conn_t *conn) {
|
||||
if (!conn->pcb) {
|
||||
if (!conn->pcb || !conn->static_body) {
|
||||
return ERR_OK;
|
||||
}
|
||||
|
||||
while (conn->hdr_sent < conn->hdr_len) {
|
||||
u16_t left = (u16_t)(conn->hdr_len - conn->hdr_sent);
|
||||
u16_t space = tcp_sndbuf(conn->pcb);
|
||||
if (space == 0) {
|
||||
return ERR_OK;
|
||||
}
|
||||
u16_t n = left < space ? left : space;
|
||||
if (n > HS_SEND_CHUNK) {
|
||||
n = HS_SEND_CHUNK;
|
||||
}
|
||||
if (tcp_write(conn->pcb, conn->hdr + conn->hdr_sent, n, TCP_WRITE_FLAG_COPY) != ERR_OK) {
|
||||
return close_conn(conn, conn->pcb, ERR_ABRT);
|
||||
}
|
||||
conn->hdr_sent += n;
|
||||
}
|
||||
while (conn->static_sent < conn->static_len) {
|
||||
u16_t left = (u16_t)(conn->static_len - conn->static_sent);
|
||||
size_t left = (size_t)(conn->hdr_len - conn->hdr_sent);
|
||||
u16_t space = tcp_sndbuf(conn->pcb);
|
||||
if (space == 0) {
|
||||
tcp_output(conn->pcb);
|
||||
return ERR_OK;
|
||||
}
|
||||
u16_t n = left < space ? left : space;
|
||||
u16_t n = left < space ? (u16_t)left : space;
|
||||
if (n > HS_SEND_CHUNK) {
|
||||
n = HS_SEND_CHUNK;
|
||||
}
|
||||
/* body is const flash — may use COPY for safety across chips */
|
||||
if (tcp_write(conn->pcb, conn->static_body + conn->static_sent, n, TCP_WRITE_FLAG_COPY) != ERR_OK) {
|
||||
err_t err = tcp_write(conn->pcb, conn->hdr + conn->hdr_sent, n, TCP_WRITE_FLAG_COPY);
|
||||
if (err == ERR_MEM) {
|
||||
tcp_output(conn->pcb);
|
||||
return ERR_OK;
|
||||
}
|
||||
if (err != ERR_OK) {
|
||||
return close_conn(conn, conn->pcb, ERR_ABRT);
|
||||
}
|
||||
conn->hdr_sent += n;
|
||||
}
|
||||
|
||||
while (conn->static_sent < conn->static_len) {
|
||||
size_t left = conn->static_len - conn->static_sent;
|
||||
u16_t space = tcp_sndbuf(conn->pcb);
|
||||
if (space == 0) {
|
||||
tcp_output(conn->pcb);
|
||||
return ERR_OK;
|
||||
}
|
||||
u16_t n = left < space ? (u16_t)left : space;
|
||||
if (n > HS_SEND_CHUNK) {
|
||||
n = HS_SEND_CHUNK;
|
||||
}
|
||||
err_t err = tcp_write(conn->pcb, conn->static_body + conn->static_sent, n, TCP_WRITE_FLAG_COPY);
|
||||
if (err == ERR_MEM) {
|
||||
tcp_output(conn->pcb);
|
||||
return ERR_OK;
|
||||
}
|
||||
if (err != ERR_OK) {
|
||||
return close_conn(conn, conn->pcb, ERR_ABRT);
|
||||
}
|
||||
conn->static_sent += n;
|
||||
}
|
||||
|
||||
tcp_output(conn->pcb);
|
||||
if (conn->static_sent >= conn->static_len && conn->hdr_sent >= conn->hdr_len) {
|
||||
conn->write_done = true;
|
||||
|
||||
/* Only close after peer has ACKed every queued byte — closing earlier
|
||||
* truncates the response and triggers ERR_CONTENT_LENGTH_MISMATCH. */
|
||||
if (conn->tx_acked >= conn->tx_total) {
|
||||
return close_conn(conn, conn->pcb, ERR_OK);
|
||||
}
|
||||
return ERR_OK;
|
||||
@@ -572,10 +592,10 @@ static err_t try_dispatch(httpserver_conn_t *conn) {
|
||||
static err_t on_sent(void *arg, struct tcp_pcb *pcb, u16_t len) {
|
||||
httpserver_conn_t *conn = arg;
|
||||
(void)pcb;
|
||||
(void)len;
|
||||
if (conn->mode != CONN_HTTP) {
|
||||
return ERR_OK;
|
||||
}
|
||||
conn->tx_acked += len;
|
||||
if (conn->static_body) {
|
||||
return pump_static(conn);
|
||||
}
|
||||
@@ -613,10 +633,11 @@ static err_t on_poll(void *arg, struct tcp_pcb *pcb) {
|
||||
if (conn->mode == CONN_WS) {
|
||||
return ERR_OK;
|
||||
}
|
||||
if (conn->static_body && conn->static_sent < conn->static_len) {
|
||||
/* Keep pumping / waiting for ACK on static responses */
|
||||
if (conn->static_body) {
|
||||
return pump_static(conn);
|
||||
}
|
||||
if (conn->dispatched && !conn->static_body) {
|
||||
if (conn->dispatched) {
|
||||
return close_conn(conn, pcb, ERR_OK);
|
||||
}
|
||||
return close_conn(conn, pcb, ERR_OK);
|
||||
@@ -708,30 +729,46 @@ bool httpserver_listen(httpserver_t *s, uint16_t port) {
|
||||
}
|
||||
|
||||
void httpserver_send_static(httpserver_conn_t *conn, int status, const char *content_type,
|
||||
const uint8_t *body, size_t body_len) {
|
||||
const uint8_t *body, size_t body_len,
|
||||
const char *content_encoding) {
|
||||
if (!conn || conn->mode != CONN_HTTP || !conn->pcb) {
|
||||
return;
|
||||
}
|
||||
if (!content_type) {
|
||||
content_type = "application/octet-stream";
|
||||
}
|
||||
conn->hdr_len = snprintf(conn->hdr, sizeof(conn->hdr),
|
||||
"HTTP/1.1 %d %s\r\n"
|
||||
"Content-Type: %s\r\n"
|
||||
"Content-Length: %u\r\n"
|
||||
"Connection: close\r\n"
|
||||
"\r\n",
|
||||
status, status_text(status), content_type, (unsigned)body_len);
|
||||
if (content_encoding && content_encoding[0]) {
|
||||
conn->hdr_len = snprintf(conn->hdr, sizeof(conn->hdr),
|
||||
"HTTP/1.1 %d %s\r\n"
|
||||
"Content-Type: %s\r\n"
|
||||
"Content-Encoding: %s\r\n"
|
||||
"Content-Length: %u\r\n"
|
||||
"Connection: close\r\n"
|
||||
"\r\n",
|
||||
status, status_text(status), content_type, content_encoding,
|
||||
(unsigned)body_len);
|
||||
} else {
|
||||
conn->hdr_len = snprintf(conn->hdr, sizeof(conn->hdr),
|
||||
"HTTP/1.1 %d %s\r\n"
|
||||
"Content-Type: %s\r\n"
|
||||
"Content-Length: %u\r\n"
|
||||
"Connection: close\r\n"
|
||||
"\r\n",
|
||||
status, status_text(status), content_type, (unsigned)body_len);
|
||||
}
|
||||
conn->hdr_sent = 0;
|
||||
conn->static_body = body ? body : (const uint8_t *)"";
|
||||
conn->static_len = body_len;
|
||||
conn->static_sent = 0;
|
||||
conn->tx_acked = 0;
|
||||
conn->tx_total = (size_t)conn->hdr_len + body_len;
|
||||
conn->write_done = false;
|
||||
pump_static(conn);
|
||||
}
|
||||
|
||||
void httpserver_send(httpserver_conn_t *conn, int status, const char *content_type,
|
||||
const void *body, size_t body_len) {
|
||||
httpserver_send_static(conn, status, content_type, (const uint8_t *)body, body_len);
|
||||
httpserver_send_static(conn, status, content_type, (const uint8_t *)body, body_len, NULL);
|
||||
}
|
||||
|
||||
void httpserver_send_html(httpserver_conn_t *conn, const char *html) {
|
||||
|
||||
Reference in new issue
Block a user