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

BUGFIX Read response body according to Content-Length bytes #244

Open
wants to merge 1 commit 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 21 additions & 6 deletions src/UniversalTelegramBot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,22 +102,37 @@ bool UniversalTelegramBot::readHTTPAnswer(String &body, String &headers) {
bool finishedHeaders = false;
bool currentLineIsBlank = true;
bool responseReceived = false;
int toRead = 0;

while (millis() - now < longPoll * 1000 + waitForResponse) {
while (client->available()) {
char c = client->read();
responseReceived = true;

if (!finishedHeaders) {
if (currentLineIsBlank && c == '\n') {
finishedHeaders = true;
String headerLC = String(headers);
headerLC.toLowerCase();
int ind1 = headerLC.indexOf("content-length");
if (ind1 != -1) {
int ind2 = headerLC.indexOf("\r", ind1 + 15);
if (ind2 != -1) {
toRead = headerLC.substring(ind1 + 15, ind2).toInt();
#ifdef TELEGRAM_DEBUG
Serial.print("Content-Length: ");
Serial.println(toRead);
Serial.println();
#endif
}
}
} else {
headers += c;
}
} else {
if (ch_count < maxMessageLength) {
body += c;
ch_count++;
responseReceived = toRead > 0 ? ch_count == toRead : true;
}
}

Expand All @@ -126,14 +141,14 @@ bool UniversalTelegramBot::readHTTPAnswer(String &body, String &headers) {
}

if (responseReceived) {
#ifdef TELEGRAM_DEBUG
Serial.println();
Serial.println(body);
Serial.println();
#endif
break;
}
}
#ifdef TELEGRAM_DEBUG
Serial.println();
Serial.println(body);
Serial.println();
#endif
return responseReceived;
}

Expand Down