Add mg_mqtt_next_unsub

This commit is contained in:
cpq 2021-03-09 09:54:02 +00:00
parent ff9d0e462e
commit 829827d46f
2 changed files with 32 additions and 10 deletions

View File

@ -1189,7 +1189,7 @@ static size_t get_chunk_length(const char *buf, size_t len, size_t *ll) {
static void walkchunks(struct mg_connection *c, struct mg_http_message *hm,
int reqlen) {
size_t off = 0, bl, ll;
while (off < c->recv.len - reqlen) {
while (off + reqlen < c->recv.len) {
char *buf = (char *) &c->recv.buf[reqlen];
size_t memo = c->recv.len;
size_t cl = get_chunk_length(&buf[off], memo - reqlen - off, &ll);
@ -1202,7 +1202,7 @@ static void walkchunks(struct mg_connection *c, struct mg_http_message *hm,
if (cl <= 5) {
// Zero chunk - last one. Prepare body - cut off chunk lengths
off = bl = 0;
while (off < c->recv.len - reqlen) {
while (off + reqlen < c->recv.len) {
char *buf = (char *) &c->recv.buf[reqlen];
size_t memo = c->recv.len;
size_t cl = get_chunk_length(&buf[off], memo - reqlen - off, &ll);
@ -2007,7 +2007,7 @@ int mg_mqtt_parse(const uint8_t *buf, size_t len, struct mg_mqtt_message *m) {
return MQTT_OK;
}
int mg_mqtt_next_sub(struct mg_mqtt_message *msg, struct mg_str *topic,
static int mg_mqtt_next_topic(struct mg_mqtt_message *msg, struct mg_str *topic,
uint8_t *qos, int pos) {
unsigned char *buf = (unsigned char *) msg->dgram.ptr + pos;
int new_pos;
@ -2015,12 +2015,23 @@ int mg_mqtt_next_sub(struct mg_mqtt_message *msg, struct mg_str *topic,
topic->len = buf[0] << 8 | buf[1];
topic->ptr = (char *) buf + 2;
new_pos = pos + 2 + topic->len + 1;
new_pos = pos + 2 + topic->len + (qos == NULL ? 0 : 1);
if ((size_t) new_pos > msg->dgram.len) return -1;
*qos = buf[2 + topic->len];
if (qos != NULL) *qos = buf[2 + topic->len];
return new_pos;
}
int mg_mqtt_next_sub(struct mg_mqtt_message *msg, struct mg_str *topic,
uint8_t *qos, int pos) {
uint8_t tmp;
return mg_mqtt_next_topic(msg, topic, qos == NULL ? &tmp : qos, pos);
}
int mg_mqtt_next_unsub(struct mg_mqtt_message *msg, struct mg_str *topic,
int pos) {
return mg_mqtt_next_topic(msg, topic, NULL, pos);
}
static void mqtt_cb(struct mg_connection *c, int ev, void *ev_data,
void *fn_data) {
if (ev == MG_EV_READ) {

View File

@ -176,7 +176,7 @@ int mg_mqtt_parse(const uint8_t *buf, size_t len, struct mg_mqtt_message *m) {
return MQTT_OK;
}
int mg_mqtt_next_sub(struct mg_mqtt_message *msg, struct mg_str *topic,
static int mg_mqtt_next_topic(struct mg_mqtt_message *msg, struct mg_str *topic,
uint8_t *qos, int pos) {
unsigned char *buf = (unsigned char *) msg->dgram.ptr + pos;
int new_pos;
@ -184,12 +184,23 @@ int mg_mqtt_next_sub(struct mg_mqtt_message *msg, struct mg_str *topic,
topic->len = buf[0] << 8 | buf[1];
topic->ptr = (char *) buf + 2;
new_pos = pos + 2 + topic->len + 1;
new_pos = pos + 2 + topic->len + (qos == NULL ? 0 : 1);
if ((size_t) new_pos > msg->dgram.len) return -1;
*qos = buf[2 + topic->len];
if (qos != NULL) *qos = buf[2 + topic->len];
return new_pos;
}
int mg_mqtt_next_sub(struct mg_mqtt_message *msg, struct mg_str *topic,
uint8_t *qos, int pos) {
uint8_t tmp;
return mg_mqtt_next_topic(msg, topic, qos == NULL ? &tmp : qos, pos);
}
int mg_mqtt_next_unsub(struct mg_mqtt_message *msg, struct mg_str *topic,
int pos) {
return mg_mqtt_next_topic(msg, topic, NULL, pos);
}
static void mqtt_cb(struct mg_connection *c, int ev, void *ev_data,
void *fn_data) {
if (ev == MG_EV_READ) {