Compare commits

..
4 Commits
Author SHA1 Message Date
Sergey Lyubka f3189519d8 Merge pull request #3816 from cesanta/flash
SPI flash support, with xiao nrf52 and portenta-h7
2026-12-03 12:46:50 +00:00
Sergio R. Caprile f03c00a516 Merge pull request #3813 from cesanta/tight
tighten MQTT and HTTP handlers
2026-12-03 09:38:58 -03:00
Sergey Lyubka d7a756a24a Merge pull request #3815 from cesanta/flash1
Introduce struct mg_flash::{read_fn,init_fn}
2026-12-03 11:39:41 +00:00
Sergio R. Caprile 24f5519463 tighten MQTT and HTTP handlers 2026-12-02 14:31:43 -03:00
4 changed files with 36 additions and 16 deletions

No files matched your search

+10 -8
View File
@@ -5397,7 +5397,7 @@ static void http_cb(struct mg_connection *c, int ev, void *ev_data) {
while (c->pfn_data == NULL && ofs < c->recv.len) { while (c->pfn_data == NULL && ofs < c->recv.len) {
const char *buf = (char *) c->recv.buf + ofs; const char *buf = (char *) c->recv.buf + ofs;
int n = mg_http_parse(buf, c->recv.len - ofs, &hm); int n = mg_http_parse(buf, c->recv.len - ofs, &hm);
struct mg_str *te; // Transfer - encoding header struct mg_str *hcl, *te; // Content-Length and Transfer-encoding headers
bool is_chunked = false, is_http_1_0 = false; bool is_chunked = false, is_http_1_0 = false;
size_t old_len = c->recv.len; size_t old_len = c->recv.len;
if (n < 0) { if (n < 0) {
@@ -5417,8 +5417,11 @@ static void http_cb(struct mg_connection *c, int ev, void *ev_data) {
c->pfn = NULL; c->pfn = NULL;
return; return;
} }
if (ev == MG_EV_CLOSE) { // If client did not set Content-Length hcl = mg_http_get_header(&hm, "Content-Length");
hm.message.len = c->recv.len - ofs; // and closes now, deliver MSG if (ev == MG_EV_CLOSE && (hcl == NULL || c->is_client)) {
// - a server responded to HEAD with optional Content-Length (no body)
// - a client sent no Content-Length and closed --> deliver MSG
hm.message.len = c->recv.len - ofs;
hm.body.len = hm.message.len - (size_t) (hm.body.buf - hm.message.buf); hm.body.len = hm.message.len - (size_t) (hm.body.buf - hm.message.buf);
} }
is_http_1_0 = is_http_1_0 =
@@ -5432,7 +5435,7 @@ static void http_cb(struct mg_connection *c, int ev, void *ev_data) {
mg_error(c, "Invalid Transfer-Encoding"); // See #2460 mg_error(c, "Invalid Transfer-Encoding"); // See #2460
return; return;
} }
} else if (mg_http_get_header(&hm, "Content-length") == NULL) { } else if (hcl == NULL) {
// #2593: HTTP packets must contain either Transfer-Encoding or // #2593: HTTP packets must contain either Transfer-Encoding or
// Content-length // Content-length
bool is_response = mg_ncasecmp(hm.method.buf, "HTTP/", 5) == 0; bool is_response = mg_ncasecmp(hm.method.buf, "HTTP/", 5) == 0;
@@ -8591,9 +8594,8 @@ int mg_mqtt_parse(const uint8_t *buf, size_t len, uint8_t version,
m->ack = p[1]; m->ack = p[1];
p += 2; p += 2;
if (version == 5) { if (version == 5) {
if (p >= end) return MQTT_MALFORMED;
len_len = (uint32_t) decode_varint(p, (size_t) (end - p), &tmp); len_len = (uint32_t) decode_varint(p, (size_t) (end - p), &tmp);
if (!len_len) return MQTT_MALFORMED; if (len_len == 0) return MQTT_MALFORMED;
m->props_size = (size_t) tmp; m->props_size = (size_t) tmp;
m->props_start = (size_t) (p + len_len - buf); m->props_start = (size_t) (p + len_len - buf);
p += len_len + m->props_size; p += len_len + m->props_size;
@@ -8624,9 +8626,9 @@ int mg_mqtt_parse(const uint8_t *buf, size_t len, uint8_t version,
p += 2; p += 2;
} }
if (p > end) return MQTT_MALFORMED; if (p > end) return MQTT_MALFORMED;
if (version == 5 && p + 2 < end) { if (version == 5) {
len_len = (uint32_t) decode_varint(p, (size_t) (end - p), &tmp); len_len = (uint32_t) decode_varint(p, (size_t) (end - p), &tmp);
if (!len_len) return MQTT_MALFORMED; if (len_len == 0) return MQTT_MALFORMED;
m->props_size = (size_t) tmp; m->props_size = (size_t) tmp;
m->props_start = (size_t) (p + len_len - buf); m->props_start = (size_t) (p + len_len - buf);
p += len_len + m->props_size; p += len_len + m->props_size;
+7 -4
View File
@@ -1152,7 +1152,7 @@ static void http_cb(struct mg_connection *c, int ev, void *ev_data) {
while (c->pfn_data == NULL && ofs < c->recv.len) { while (c->pfn_data == NULL && ofs < c->recv.len) {
const char *buf = (char *) c->recv.buf + ofs; const char *buf = (char *) c->recv.buf + ofs;
int n = mg_http_parse(buf, c->recv.len - ofs, &hm); int n = mg_http_parse(buf, c->recv.len - ofs, &hm);
struct mg_str *te; // Transfer - encoding header struct mg_str *hcl, *te; // Content-Length and Transfer-encoding headers
bool is_chunked = false, is_http_1_0 = false; bool is_chunked = false, is_http_1_0 = false;
size_t old_len = c->recv.len; size_t old_len = c->recv.len;
if (n < 0) { if (n < 0) {
@@ -1172,8 +1172,11 @@ static void http_cb(struct mg_connection *c, int ev, void *ev_data) {
c->pfn = NULL; c->pfn = NULL;
return; return;
} }
if (ev == MG_EV_CLOSE) { // If client did not set Content-Length hcl = mg_http_get_header(&hm, "Content-Length");
hm.message.len = c->recv.len - ofs; // and closes now, deliver MSG if (ev == MG_EV_CLOSE && (hcl == NULL || c->is_client)) {
// - a server responded to HEAD with optional Content-Length (no body)
// - a client sent no Content-Length and closed --> deliver MSG
hm.message.len = c->recv.len - ofs;
hm.body.len = hm.message.len - (size_t) (hm.body.buf - hm.message.buf); hm.body.len = hm.message.len - (size_t) (hm.body.buf - hm.message.buf);
} }
is_http_1_0 = is_http_1_0 =
@@ -1187,7 +1190,7 @@ static void http_cb(struct mg_connection *c, int ev, void *ev_data) {
mg_error(c, "Invalid Transfer-Encoding"); // See #2460 mg_error(c, "Invalid Transfer-Encoding"); // See #2460
return; return;
} }
} else if (mg_http_get_header(&hm, "Content-length") == NULL) { } else if (hcl == NULL) {
// #2593: HTTP packets must contain either Transfer-Encoding or // #2593: HTTP packets must contain either Transfer-Encoding or
// Content-length // Content-length
bool is_response = mg_ncasecmp(hm.method.buf, "HTTP/", 5) == 0; bool is_response = mg_ncasecmp(hm.method.buf, "HTTP/", 5) == 0;
+3 -4
View File
@@ -440,9 +440,8 @@ int mg_mqtt_parse(const uint8_t *buf, size_t len, uint8_t version,
m->ack = p[1]; m->ack = p[1];
p += 2; p += 2;
if (version == 5) { if (version == 5) {
if (p >= end) return MQTT_MALFORMED;
len_len = (uint32_t) decode_varint(p, (size_t) (end - p), &tmp); len_len = (uint32_t) decode_varint(p, (size_t) (end - p), &tmp);
if (!len_len) return MQTT_MALFORMED; if (len_len == 0) return MQTT_MALFORMED;
m->props_size = (size_t) tmp; m->props_size = (size_t) tmp;
m->props_start = (size_t) (p + len_len - buf); m->props_start = (size_t) (p + len_len - buf);
p += len_len + m->props_size; p += len_len + m->props_size;
@@ -473,9 +472,9 @@ int mg_mqtt_parse(const uint8_t *buf, size_t len, uint8_t version,
p += 2; p += 2;
} }
if (p > end) return MQTT_MALFORMED; if (p > end) return MQTT_MALFORMED;
if (version == 5 && p + 2 < end) { if (version == 5) {
len_len = (uint32_t) decode_varint(p, (size_t) (end - p), &tmp); len_len = (uint32_t) decode_varint(p, (size_t) (end - p), &tmp);
if (!len_len) return MQTT_MALFORMED; if (len_len == 0) return MQTT_MALFORMED;
m->props_size = (size_t) tmp; m->props_size = (size_t) tmp;
m->props_start = (size_t) (p + len_len - buf); m->props_start = (size_t) (p + len_len - buf);
p += len_len + m->props_size; p += len_len + m->props_size;
+16
View File
@@ -817,6 +817,12 @@ static void test_mqtt_parse(void) {
struct mg_mqtt_prop prop; struct mg_mqtt_prop prop;
// MQTT5 PUBLISH with Subscription Identifier = 5 // MQTT5 PUBLISH with Subscription Identifier = 5
const uint8_t sub_id_pkt[] = {0x30, 0x06, 0x00, 0x01, 'x', 0x02, 0x0B, 0x05}; const uint8_t sub_id_pkt[] = {0x30, 0x06, 0x00, 0x01, 'x', 0x02, 0x0B, 0x05};
// MQTT5 PUBLISH with no properties and no payload
const uint8_t pub_empty[] = {0x30, 0x04, 0x00, 0x01, 'x', 0x00};
// MQTT5 PUBLISH with no properties and a one-byte payload
const uint8_t pub_byte[] = {0x30, 0x05, 0x00, 0x01, 'x', 0x00, 'y'};
// MQTT5 PUBLISH without its mandatory Property Length
const uint8_t pub_no_props[] = {0x30, 0x03, 0x00, 0x01, 'x'};
// MQTT5 CONNACK with Server Keep Alive = 60 // MQTT5 CONNACK with Server Keep Alive = 60
const uint8_t connack_pkt[] = {0x20, 0x06, 0x00, 0x00, 0x03, 0x13, 0x00, 0x3C}; const uint8_t connack_pkt[] = {0x20, 0x06, 0x00, 0x00, 0x03, 0x13, 0x00, 0x3C};
@@ -825,6 +831,13 @@ static void test_mqtt_parse(void) {
ASSERT(mg_mqtt_parse(sub_id_pkt, sizeof(sub_id_pkt), 5, &mm) == MQTT_OK); ASSERT(mg_mqtt_parse(sub_id_pkt, sizeof(sub_id_pkt), 5, &mm) == MQTT_OK);
mg_mqtt_next_prop(&mm, &prop, 0); mg_mqtt_next_prop(&mm, &prop, 0);
ASSERT(mg_mqtt_parse(pub_empty, sizeof(pub_empty), 5, &mm) == MQTT_OK);
ASSERT(mm.props_size == 0 && mm.data.len == 0);
ASSERT(mg_mqtt_parse(pub_byte, sizeof(pub_byte), 5, &mm) == MQTT_OK);
ASSERT(mm.props_size == 0 && mm.data.len == 1 && mm.data.buf[0] == 'y');
ASSERT(mg_mqtt_parse(pub_no_props, sizeof(pub_no_props), 5, &mm) ==
MQTT_MALFORMED);
ASSERT(mg_mqtt_parse(connack_pkt, sizeof(connack_pkt), 5, &mm) == MQTT_OK); ASSERT(mg_mqtt_parse(connack_pkt, sizeof(connack_pkt), 5, &mm) == MQTT_OK);
ASSERT(mm.ack == 0); ASSERT(mm.ack == 0);
ASSERT(mg_mqtt_next_prop(&mm, &prop, 0) > 0); ASSERT(mg_mqtt_next_prop(&mm, &prop, 0) > 0);
@@ -1277,6 +1290,9 @@ static void test_http_server(void) {
"POST /body HTTP/1.1\r\n" "POST /body HTTP/1.1\r\n"
"Content-Length: 4\r\n\r\nkuku") == 200); "Content-Length: 4\r\n\r\nkuku") == 200);
ASSERT(cmpbody(buf, "kuku") == 0); ASSERT(cmpbody(buf, "kuku") == 0);
ASSERT(fetch(&mgr, buf, url, "%s",
"POST /body HTTP/1.1\r\n"
"Content-Length: 4\r\n\r\nku") == 0);
ASSERT(fetch(&mgr, buf, url, "GET /ssi HTTP/1.1\r\n\r\n") == 301); ASSERT(fetch(&mgr, buf, url, "GET /ssi HTTP/1.1\r\n\r\n") == 301);
ASSERT(fetch(&mgr, buf, url, "GET /ssi/ HTTP/1.1\r\n\r\n") == 200); ASSERT(fetch(&mgr, buf, url, "GET /ssi/ HTTP/1.1\r\n\r\n") == 200);