Skip to content

Commit

Permalink
Add mg_mqtt_next_unsub
Browse files Browse the repository at this point in the history
  • Loading branch information
cpq committed Mar 9, 2021
1 parent ff9d0e4 commit 829827d
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 10 deletions.
23 changes: 17 additions & 6 deletions mongoose.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -2007,20 +2007,31 @@ 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,
uint8_t *qos, int pos) {
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;
if ((size_t) pos >= msg->dgram.len) return -1;

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) {
Expand Down
19 changes: 15 additions & 4 deletions src/mqtt.c
Original file line number Diff line number Diff line change
Expand Up @@ -176,20 +176,31 @@ 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,
uint8_t *qos, int pos) {
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;
if ((size_t) pos >= msg->dgram.len) return -1;

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) {
Expand Down

0 comments on commit 829827d

Please sign in to comment.