Skip to content

Commit e76e475

Browse files
committed
Issue witnessmenow#11: moved http response parsing out of sendPostToTelegram()
1 parent a6a1f43 commit e76e475

File tree

2 files changed

+43
-38
lines changed

2 files changed

+43
-38
lines changed

src/UniversalTelegramBot.cpp

Lines changed: 42 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -117,12 +117,51 @@ String UniversalTelegramBot::sendGetToTelegram(const String& command) {
117117
return mess;
118118
}
119119

120+
bool UniversalTelegramBot::readHTTPAnswer(String &body, String &headers) {
121+
int ch_count = 0;
122+
long now = millis();
123+
bool finishedHeaders = false;
124+
bool currentLineIsBlank = true;
125+
bool responseReceived = false;
126+
127+
while (millis() - now < waitForResponse) {
128+
while (client->available()) {
129+
char c = client->read();
130+
responseReceived = true;
131+
132+
if (!finishedHeaders) {
133+
if (currentLineIsBlank && c == '\n') {
134+
finishedHeaders = true;
135+
} else {
136+
headers += c;
137+
}
138+
} else {
139+
if (ch_count < maxMessageLength) {
140+
body += c;
141+
ch_count++;
142+
}
143+
}
144+
145+
if (c == '\n') currentLineIsBlank = true;
146+
else if (c != '\r') currentLineIsBlank = false;
147+
}
148+
149+
if (responseReceived) {
150+
#ifdef _debug
151+
Serial.println();
152+
Serial.println(body);
153+
Serial.println();
154+
#endif
155+
break;
156+
}
157+
}
158+
return responseReceived;
159+
}
160+
120161
String UniversalTelegramBot::sendPostToTelegram(const String& command, JsonObject payload) {
121162

122163
String body;
123164
String headers;
124-
long now;
125-
bool responseReceived = false;
126165

127166
// Connect with api.telegram.org if not already connected
128167
if (!client->connected()) {
@@ -160,42 +199,7 @@ String UniversalTelegramBot::sendPostToTelegram(const String& command, JsonObjec
160199
Serial.println(String("Posting:") + out);
161200
#endif
162201

163-
int ch_count = 0;
164-
now = millis();
165-
bool finishedHeaders = false;
166-
bool currentLineIsBlank = true;
167-
while (millis() - now < waitForResponse) {
168-
while (client->available()) {
169-
char c = client->read();
170-
responseReceived = true;
171-
172-
if (!finishedHeaders) {
173-
if (currentLineIsBlank && c == '\n') {
174-
finishedHeaders = true;
175-
} else {
176-
headers += c;
177-
}
178-
} else {
179-
if (ch_count < maxMessageLength) {
180-
body += c;
181-
ch_count++;
182-
}
183-
}
184-
185-
if (c == '\n') currentLineIsBlank = true;
186-
else if (c != '\r') currentLineIsBlank = false;
187-
188-
}
189-
190-
if (responseReceived) {
191-
#ifdef _debug
192-
Serial.println();
193-
Serial.println(body);
194-
Serial.println();
195-
#endif
196-
break;
197-
}
198-
}
202+
readHTTPAnswer(body, headers);
199203
}
200204

201205
return body;

src/UniversalTelegramBot.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ class UniversalTelegramBot {
7979
GetNextBuffer getNextBufferCallback,
8080
GetNextBufferLen getNextBufferLenCallback);
8181

82+
bool readHTTPAnswer(String &body, String &headers);
8283
bool getMe();
8384

8485
bool sendSimpleMessage(const String& chat_id, const String& text, const String& parse_mode);

0 commit comments

Comments
 (0)