Skip to content

Commit 1e7b968

Browse files
committed
add examples/BasicHttpClient/BasicHttpClient.ino
fix get size only fingerprint when strlen > 0
1 parent 5e1a656 commit 1e7b968

File tree

3 files changed

+99
-12
lines changed

3 files changed

+99
-12
lines changed
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/**
2+
* BasicHttpClient.ino
3+
*
4+
* Created on: 24.05.2015
5+
*
6+
*/
7+
8+
#include <Arduino.h>
9+
10+
#include <ESP8266WiFi.h>
11+
#include <ESP8266WiFiMulti.h>
12+
13+
#include <ESP8266httpClient.h>
14+
15+
16+
ESP8266WiFiMulti WiFiMulti;
17+
18+
void setup() {
19+
Serial.begin(115200);
20+
21+
22+
Serial.println();
23+
Serial.println();
24+
Serial.println();
25+
26+
27+
for(uint8_t t = 4; t > 0; t--) {
28+
Serial.printf("[SETUP] WAIT %d...\n", t);
29+
Serial.flush();
30+
delay(1000);
31+
}
32+
33+
WiFiMulti.addAP("SSID", "PASSWORD");
34+
35+
//WiFi.disconnect();
36+
while(WiFiMulti.run() != WL_CONNECTED) {
37+
delay(100);
38+
}
39+
}
40+
41+
void loop() {
42+
if((WiFiMulti.run() == WL_CONNECTED)) {
43+
httpClient http;
44+
45+
Serial.print("[HTTP] begin...\n");
46+
47+
http.begin("192.168.1.12", 80, "/test.html");
48+
49+
Serial.print("[HTTP] GET...\n");
50+
if(http.GET()) {
51+
52+
Serial.print("[HTTP] GET... ok.\n");
53+
54+
size_t len = http.getSize();
55+
56+
uint8_t buff[128] = { 0 };
57+
WiFiClient stream = http.getStream();
58+
while(http.connected() && len > 0) {
59+
size_t size = stream.available();
60+
int c = stream.readBytes(buff, ((size > 128) ? 128 : size));
61+
62+
Serial.write(buff, c);
63+
len -= c;
64+
65+
delay(0);
66+
}
67+
Serial.println();
68+
Serial.print("[HTTP] connection closed or file end.\n");
69+
70+
} else {
71+
72+
Serial.print("[HTTP] GET... fail.\n");
73+
}
74+
75+
delay(10000);
76+
}
77+
}

libraries/ESP8266httpClient/src/ESP8266httpClient.cpp

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ httpClient::httpClient() {
3636
_currentHeaders = NULL;
3737

3838
_returnCode = 0;
39-
_size = 0;
39+
_size = -1;
4040
}
4141

4242
httpClient::~httpClient() {
@@ -51,11 +51,18 @@ httpClient::~httpClient() {
5151
}
5252

5353
void httpClient::begin(const char *host, uint16_t port, const char * url, bool https, const char * httpsFingerprint) {
54+
5455
_host = host;
5556
_port = port;
5657
_url = url;
5758
_https = https;
5859
_httpsFingerprint = httpsFingerprint;
60+
61+
_returnCode = 0;
62+
_size = -1;
63+
64+
_Headers = "";
65+
5966
}
6067

6168
void httpClient::begin(String host, uint16_t port, String url, bool https, String httpsFingerprint) {
@@ -121,6 +128,15 @@ int httpClient::POST(String payload) {
121128
return POST((uint8_t *) payload.c_str(), payload.length());
122129
}
123130

131+
132+
/**
133+
* size of message body / payload
134+
* @return -1 if no info or > 0 when Content-Length is set by server
135+
*/
136+
int httpClient::getSize(void) {
137+
return _size;
138+
}
139+
124140
/**
125141
* returns the stram of the tcp connection
126142
* @return WiFiClient
@@ -194,13 +210,7 @@ bool httpClient::hasHeader(const char* name) {
194210
return false;
195211
}
196212

197-
/**
198-
* size of message body / payload
199-
* @return 0 if no info or > 0 when Content-Length is set by server
200-
*/
201-
size_t httpClient::getSize(void) {
202-
return _size;
203-
}
213+
204214

205215
/**
206216
* init TCP connection and handle ssl verify if needed
@@ -209,7 +219,7 @@ size_t httpClient::getSize(void) {
209219
bool httpClient::connect(void) {
210220

211221
if(connected()) {
212-
DEBUG_HTTPCLIENT("[HTTP-Client] connect. already connected!\n");
222+
DEBUG_HTTPCLIENT("[HTTP-Client] connect. already connected, reuse!\n");
213223
return true;
214224
}
215225

@@ -229,7 +239,7 @@ bool httpClient::connect(void) {
229239

230240
DEBUG_HTTPCLIENT("[HTTP-Client] connected to %s:%u.\n", _host.c_str(), _port);
231241

232-
if(_https) {
242+
if(_https && _httpsFingerprint.length() > 0) {
233243
if(_tcps->verify(_httpsFingerprint.c_str(), _host.c_str())) {
234244
DEBUG_HTTPCLIENT("[HTTP-Client] https certificate matches\n");
235245
} else {

libraries/ESP8266httpClient/src/ESP8266httpClient.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ class httpClient {
5959
bool hasHeader(const char* name); // check if header exists
6060

6161

62-
size_t getSize(void);
62+
int getSize(void);
6363

6464
WiFiClient & getStream(void);
6565

@@ -89,7 +89,7 @@ class httpClient {
8989
size_t _headerKeysCount;
9090

9191
int _returnCode;
92-
size_t _size;
92+
int _size;
9393

9494

9595
bool connect(void);

0 commit comments

Comments
 (0)