From ab01d8be1001837d199ba1831e518a151fe5b256 Mon Sep 17 00:00:00 2001 From: Markus Sattler Date: Wed, 25 Nov 2015 14:29:15 +0100 Subject: [PATCH] add url phasing for begin change deconstruct call to delete --- .../src/ESP8266httpClient.cpp | 85 +++++++++++++++++-- .../ESP8266httpClient/src/ESP8266httpClient.h | 4 + 2 files changed, 82 insertions(+), 7 deletions(-) diff --git a/libraries/ESP8266httpClient/src/ESP8266httpClient.cpp b/libraries/ESP8266httpClient/src/ESP8266httpClient.cpp index 69becd195..2c22b328f 100644 --- a/libraries/ESP8266httpClient/src/ESP8266httpClient.cpp +++ b/libraries/ESP8266httpClient/src/ESP8266httpClient.cpp @@ -54,12 +54,12 @@ httpClient::~httpClient() { if(_tcps) { _tcps->stop(); - _tcps->~WiFiClientSecure(); + delete _tcps; _tcps = NULL; _tcp = NULL; } else if(_tcp) { _tcp->stop(); - _tcp->~WiFiClient(); + delete _tcp; _tcp = NULL; } @@ -68,6 +68,78 @@ httpClient::~httpClient() { } } +/** + * phasing the url for all needed informations + * @param url const char * + * @param httpsFingerprint const char * + */ +void httpClient::begin(const char *url, const char * httpsFingerprint) { + begin(String(url), String(httpsFingerprint)); +} + +/** + * phasing the url for all needed informations + * @param url String + * @param httpsFingerprint String + */ +void httpClient::begin(String url, String httpsFingerprint) { + + DEBUG_HTTPCLIENT("[HTTP-Client][begin] url: %s\n", url.c_str()); + + _httpsFingerprint = httpsFingerprint; + _returnCode = 0; + _size = -1; + + _Headers = ""; + + String protocol; + // check for : (http: or https: + int index = url.indexOf(':'); + int index2; + bool hasPort = false; + if(index) { + protocol = url.substring(0, index); + url.remove(0, (index + 3)); // remove http:// or https:// + + index = url.indexOf(':'); + index2 = url.indexOf('/'); + + if(index >= 0 && ((index2 >= 0 && index < index2) || index2 == 0)) { // do we have a port? + _host = url.substring(0, index); // hostname + url.remove(0, (index + 1)); // remove hostname + : + + index = url.indexOf('/'); + _port = url.substring(0, index).toInt(); // get port + url.remove(0, index); // remove port + hasPort = true; + } else { + index = index2; + _host = url.substring(0, index); + url.remove(0, index); // remove hostname + } + + _url = url; + + if(protocol.equalsIgnoreCase("http")) { + _https = false; + if(!hasPort) { + _port = 80; + } + } else if(protocol.equalsIgnoreCase("https")) { + _https = true; + if(!hasPort) { + _port = 443; + } + } else { + DEBUG_HTTPCLIENT("[HTTP-Client][begin] protocol: %s unknown?!\n", protocol.c_str()); + return; + } + } + + DEBUG_HTTPCLIENT("[HTTP-Client][begin] host: %s port: %d url: %s https: %d httpsFingerprint: %s\n", _host.c_str(), _port, _url.c_str(), _https, _httpsFingerprint.c_str()); + +} + /** * begin * @param host const char * @@ -226,7 +298,6 @@ WiFiClient * httpClient::getStreamPtr(void) { return NULL; } -WiFiClient * getStreamPtr(void); /** * write all message body / payload to Stream * @param stream Stream * @@ -378,7 +449,7 @@ bool httpClient::connect(void) { if(_https) { DEBUG_HTTPCLIENT("[HTTP-Client] connect https...\n"); if(_tcps) { - _tcps->~WiFiClient(); + delete _tcps; _tcps = NULL; _tcp = NULL; } @@ -387,18 +458,18 @@ bool httpClient::connect(void) { } else { DEBUG_HTTPCLIENT("[HTTP-Client] connect http...\n"); if(_tcp) { - _tcp->~WiFiClient(); + delete _tcp; _tcp = NULL; } _tcp = new WiFiClient(); } if(!_tcp->connect(_host.c_str(), _port)) { - DEBUG_HTTPCLIENT("[HTTP-Client] failed connect to %s:%u.\n", _host.c_str(), _port); + DEBUG_HTTPCLIENT("[HTTP-Client] failed connect to %s:%u\n", _host.c_str(), _port); return false; } - DEBUG_HTTPCLIENT("[HTTP-Client] connected to %s:%u.\n", _host.c_str(), _port); + DEBUG_HTTPCLIENT("[HTTP-Client] connected to %s:%u\n", _host.c_str(), _port); if(_https && _httpsFingerprint.length() > 0) { if(_tcps->verify(_httpsFingerprint.c_str(), _host.c_str())) { diff --git a/libraries/ESP8266httpClient/src/ESP8266httpClient.h b/libraries/ESP8266httpClient/src/ESP8266httpClient.h index 1f3d33bd5..3b21c18ea 100644 --- a/libraries/ESP8266httpClient/src/ESP8266httpClient.h +++ b/libraries/ESP8266httpClient/src/ESP8266httpClient.h @@ -48,8 +48,12 @@ class httpClient { httpClient(); ~httpClient(); + void begin(const char *url, const char * httpsFingerprint = ""); + void begin(String url, String httpsFingerprint = ""); + void begin(const char *host, uint16_t port, const char * url = "/", bool https = false, const char * httpsFingerprint = ""); void begin(String host, uint16_t port, String url = "/", bool https = false, String httpsFingerprint = ""); + void end(void); bool connected(void);