Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mqtt: WIP: Read variable header indepentenly from payload #13465

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
48 changes: 46 additions & 2 deletions lib/mqtt.c
Original file line number Diff line number Diff line change
Expand Up @@ -623,10 +623,12 @@ static CURLcode mqtt_read_publish(struct Curl_easy *data, bool *done)
CURLcode result = CURLE_OK;
struct connectdata *conn = data->conn;
ssize_t nread;
ssize_t nprocessed = 0;
size_t remlen;
struct mqtt_conn *mqtt = &conn->proto.mqtt;
struct MQTT *mq = data->req.p.mqtt;
unsigned char packet;
ssize_t topiclen;

switch(mqtt->state) {
MQTT_SUBACK_COMING:
Expand Down Expand Up @@ -691,10 +693,52 @@ static CURLcode mqtt_read_publish(struct Curl_easy *data, bool *done)
goto end;
}

/* if QoS is set, message contains packet id */
result = Curl_client_write(data, CLIENTWRITE_BODY, buffer, nread);
/* read the variable header */

/* read topic */
if(nread < 2) {
failf(data, "packet too short");
result = CURLE_WEIRD_SERVER_REPLY;
goto end;
}
nprocessed += 2;
topiclen = (unsigned char)buffer[0] << 8 | (unsigned char)buffer[1];
if(nread < nprocessed + topiclen) {
failf(data, "topic length longer than packet");
result = CURLE_WEIRD_SERVER_REPLY;
goto end;
}
result = Curl_client_write(data, CLIENTWRITE_HEADER,
&buffer[nprocessed], topiclen);
if(result)
goto end;
nprocessed += topiclen;

/* if QoS is set, message contains packet id */
if(mq->firstbyte & 0x06) {
if(nread < nprocessed + 2) {
failf(data, "packet too short");
result = CURLE_WEIRD_SERVER_REPLY;
goto end;
}
result = Curl_client_write(data, CLIENTWRITE_HEADER,
&buffer[nprocessed], 2);
if(result)
goto end;
nprocessed += 2;
}

/* read payload */
if(nread - nprocessed > 0) {
result = Curl_client_write(data, CLIENTWRITE_BODY,
&buffer[nprocessed], nread - nprocessed);
if(result)
goto end;
}
else {
/* zero length payload are valid in MQTT */
infof(data, "Zero length payload");
}

mq->npacket -= nread;
if(!mq->npacket)
Expand Down