-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathhttp_client.cpp
More file actions
59 lines (54 loc) · 1.95 KB
/
http_client.cpp
File metadata and controls
59 lines (54 loc) · 1.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include "http_client.h"
// Forward declaration of logging function implemented in main sketch
// Provide default parameter to match original definition.
void WriteLog(String msg, bool NewLine = true);
bool HttpGet(const char* host, uint16_t port, const char* path, String& outBody) {
WiFiClient client;
WriteLog(String("HTTP poll: connecting ") + host + ":" + port + " path " + path, true);
if (!client.connect(host, port)) {
WriteLog("HTTP connect failed", true);
return false;
}
client.print(String("GET ") + path + " HTTP/1.1\r\n" \
+ "Host: " + host + "\r\n" \
+ "Connection: close\r\n" \
+ "Cache-Control: no-cache\r\n\r\n");
unsigned long start = millis();
while(!client.available() && millis() - start < 3000) {
delay(10);
}
if(!client.available()) {
WriteLog("HTTP timeout waiting for response", true);
client.stop();
return false;
}
// Read full response (small, due to jq filter). Limit to 4096 for safety.
String response;
while(client.available()) {
char c = client.read();
response += c;
if(response.length() > 4096) {
WriteLog("Response exceeded 4KB limit", true);
client.stop();
return false;
}
}
client.stop();
int bodyIndex = response.indexOf("\r\n\r\n");
if(bodyIndex < 0) {
WriteLog("Malformed response (no header terminator)", true);
return false;
}
outBody = response.substring(bodyIndex + 4);
outBody.trim();
if(outBody.isEmpty()) {
WriteLog("Empty body", true);
return false;
}
// Quick sanity snippet
String snippet; snippet.reserve(80);
int n = outBody.length() < 80 ? outBody.length() : 80;
for(int i=0;i<n;i++){ char ch = outBody[i]; snippet += (ch >=32 && ch<127)?ch:'.'; }
WriteLog("Body " + String(outBody.length()) + "B snippet: " + snippet, true);
return true;
}