From 1e7b9688a53927f6f67dac80bac3fb51328ab4a2 Mon Sep 17 00:00:00 2001 From: Markus Sattler Date: Sun, 22 Nov 2015 11:09:48 +0100 Subject: [PATCH] add examples/BasicHttpClient/BasicHttpClient.ino fix get size only fingerprint when strlen > 0 --- .../BasicHttpClient/BasicHttpClient.ino | 77 +++++++++++++++++++ .../src/ESP8266httpClient.cpp | 30 +++++--- .../ESP8266httpClient/src/ESP8266httpClient.h | 4 +- 3 files changed, 99 insertions(+), 12 deletions(-) create mode 100644 libraries/ESP8266httpClient/examples/BasicHttpClient/BasicHttpClient.ino diff --git a/libraries/ESP8266httpClient/examples/BasicHttpClient/BasicHttpClient.ino b/libraries/ESP8266httpClient/examples/BasicHttpClient/BasicHttpClient.ino new file mode 100644 index 000000000..23a5e49da --- /dev/null +++ b/libraries/ESP8266httpClient/examples/BasicHttpClient/BasicHttpClient.ino @@ -0,0 +1,77 @@ +/** + * BasicHttpClient.ino + * + * Created on: 24.05.2015 + * + */ + +#include + +#include +#include + +#include + + +ESP8266WiFiMulti WiFiMulti; + +void setup() { + Serial.begin(115200); + + + Serial.println(); + Serial.println(); + Serial.println(); + + + for(uint8_t t = 4; t > 0; t--) { + Serial.printf("[SETUP] WAIT %d...\n", t); + Serial.flush(); + delay(1000); + } + + WiFiMulti.addAP("SSID", "PASSWORD"); + + //WiFi.disconnect(); + while(WiFiMulti.run() != WL_CONNECTED) { + delay(100); + } +} + +void loop() { + if((WiFiMulti.run() == WL_CONNECTED)) { + httpClient http; + + Serial.print("[HTTP] begin...\n"); + + http.begin("192.168.1.12", 80, "/test.html"); + + Serial.print("[HTTP] GET...\n"); + if(http.GET()) { + + Serial.print("[HTTP] GET... ok.\n"); + + size_t len = http.getSize(); + + uint8_t buff[128] = { 0 }; + WiFiClient stream = http.getStream(); + while(http.connected() && len > 0) { + size_t size = stream.available(); + int c = stream.readBytes(buff, ((size > 128) ? 128 : size)); + + Serial.write(buff, c); + len -= c; + + delay(0); + } + Serial.println(); + Serial.print("[HTTP] connection closed or file end.\n"); + + } else { + + Serial.print("[HTTP] GET... fail.\n"); + } + + delay(10000); + } +} diff --git a/libraries/ESP8266httpClient/src/ESP8266httpClient.cpp b/libraries/ESP8266httpClient/src/ESP8266httpClient.cpp index 8be06cf3f..125f363e1 100644 --- a/libraries/ESP8266httpClient/src/ESP8266httpClient.cpp +++ b/libraries/ESP8266httpClient/src/ESP8266httpClient.cpp @@ -36,7 +36,7 @@ httpClient::httpClient() { _currentHeaders = NULL; _returnCode = 0; - _size = 0; + _size = -1; } httpClient::~httpClient() { @@ -51,11 +51,18 @@ httpClient::~httpClient() { } void httpClient::begin(const char *host, uint16_t port, const char * url, bool https, const char * httpsFingerprint) { + _host = host; _port = port; _url = url; _https = https; _httpsFingerprint = httpsFingerprint; + + _returnCode = 0; + _size = -1; + + _Headers = ""; + } void httpClient::begin(String host, uint16_t port, String url, bool https, String httpsFingerprint) { @@ -121,6 +128,15 @@ int httpClient::POST(String payload) { return POST((uint8_t *) payload.c_str(), payload.length()); } + +/** + * size of message body / payload + * @return -1 if no info or > 0 when Content-Length is set by server + */ +int httpClient::getSize(void) { + return _size; +} + /** * returns the stram of the tcp connection * @return WiFiClient @@ -194,13 +210,7 @@ bool httpClient::hasHeader(const char* name) { return false; } -/** - * size of message body / payload - * @return 0 if no info or > 0 when Content-Length is set by server - */ -size_t httpClient::getSize(void) { - return _size; -} + /** * init TCP connection and handle ssl verify if needed @@ -209,7 +219,7 @@ size_t httpClient::getSize(void) { bool httpClient::connect(void) { if(connected()) { - DEBUG_HTTPCLIENT("[HTTP-Client] connect. already connected!\n"); + DEBUG_HTTPCLIENT("[HTTP-Client] connect. already connected, reuse!\n"); return true; } @@ -229,7 +239,7 @@ bool httpClient::connect(void) { DEBUG_HTTPCLIENT("[HTTP-Client] connected to %s:%u.\n", _host.c_str(), _port); - if(_https) { + if(_https && _httpsFingerprint.length() > 0) { if(_tcps->verify(_httpsFingerprint.c_str(), _host.c_str())) { DEBUG_HTTPCLIENT("[HTTP-Client] https certificate matches\n"); } else { diff --git a/libraries/ESP8266httpClient/src/ESP8266httpClient.h b/libraries/ESP8266httpClient/src/ESP8266httpClient.h index 1ffe8f094..57f186912 100644 --- a/libraries/ESP8266httpClient/src/ESP8266httpClient.h +++ b/libraries/ESP8266httpClient/src/ESP8266httpClient.h @@ -59,7 +59,7 @@ class httpClient { bool hasHeader(const char* name); // check if header exists - size_t getSize(void); + int getSize(void); WiFiClient & getStream(void); @@ -89,7 +89,7 @@ class httpClient { size_t _headerKeysCount; int _returnCode; - size_t _size; + int _size; bool connect(void);