Skip to content

Commit fce9703

Browse files
committed
Merge pull request #1082 from Links2004/httpClient
add url phasing for begin
2 parents 56a3733 + ab01d8b commit fce9703

File tree

2 files changed

+82
-7
lines changed

2 files changed

+82
-7
lines changed

libraries/ESP8266httpClient/src/ESP8266httpClient.cpp

+78-7
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,12 @@ httpClient::~httpClient() {
5454

5555
if(_tcps) {
5656
_tcps->stop();
57-
_tcps->~WiFiClientSecure();
57+
delete _tcps;
5858
_tcps = NULL;
5959
_tcp = NULL;
6060
} else if(_tcp) {
6161
_tcp->stop();
62-
_tcp->~WiFiClient();
62+
delete _tcp;
6363
_tcp = NULL;
6464
}
6565

@@ -68,6 +68,78 @@ httpClient::~httpClient() {
6868
}
6969
}
7070

71+
/**
72+
* phasing the url for all needed informations
73+
* @param url const char *
74+
* @param httpsFingerprint const char *
75+
*/
76+
void httpClient::begin(const char *url, const char * httpsFingerprint) {
77+
begin(String(url), String(httpsFingerprint));
78+
}
79+
80+
/**
81+
* phasing the url for all needed informations
82+
* @param url String
83+
* @param httpsFingerprint String
84+
*/
85+
void httpClient::begin(String url, String httpsFingerprint) {
86+
87+
DEBUG_HTTPCLIENT("[HTTP-Client][begin] url: %s\n", url.c_str());
88+
89+
_httpsFingerprint = httpsFingerprint;
90+
_returnCode = 0;
91+
_size = -1;
92+
93+
_Headers = "";
94+
95+
String protocol;
96+
// check for : (http: or https:
97+
int index = url.indexOf(':');
98+
int index2;
99+
bool hasPort = false;
100+
if(index) {
101+
protocol = url.substring(0, index);
102+
url.remove(0, (index + 3)); // remove http:// or https://
103+
104+
index = url.indexOf(':');
105+
index2 = url.indexOf('/');
106+
107+
if(index >= 0 && ((index2 >= 0 && index < index2) || index2 == 0)) { // do we have a port?
108+
_host = url.substring(0, index); // hostname
109+
url.remove(0, (index + 1)); // remove hostname + :
110+
111+
index = url.indexOf('/');
112+
_port = url.substring(0, index).toInt(); // get port
113+
url.remove(0, index); // remove port
114+
hasPort = true;
115+
} else {
116+
index = index2;
117+
_host = url.substring(0, index);
118+
url.remove(0, index); // remove hostname
119+
}
120+
121+
_url = url;
122+
123+
if(protocol.equalsIgnoreCase("http")) {
124+
_https = false;
125+
if(!hasPort) {
126+
_port = 80;
127+
}
128+
} else if(protocol.equalsIgnoreCase("https")) {
129+
_https = true;
130+
if(!hasPort) {
131+
_port = 443;
132+
}
133+
} else {
134+
DEBUG_HTTPCLIENT("[HTTP-Client][begin] protocol: %s unknown?!\n", protocol.c_str());
135+
return;
136+
}
137+
}
138+
139+
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());
140+
141+
}
142+
71143
/**
72144
* begin
73145
* @param host const char *
@@ -226,7 +298,6 @@ WiFiClient * httpClient::getStreamPtr(void) {
226298
return NULL;
227299
}
228300

229-
WiFiClient * getStreamPtr(void);
230301
/**
231302
* write all message body / payload to Stream
232303
* @param stream Stream *
@@ -378,7 +449,7 @@ bool httpClient::connect(void) {
378449
if(_https) {
379450
DEBUG_HTTPCLIENT("[HTTP-Client] connect https...\n");
380451
if(_tcps) {
381-
_tcps->~WiFiClient();
452+
delete _tcps;
382453
_tcps = NULL;
383454
_tcp = NULL;
384455
}
@@ -387,18 +458,18 @@ bool httpClient::connect(void) {
387458
} else {
388459
DEBUG_HTTPCLIENT("[HTTP-Client] connect http...\n");
389460
if(_tcp) {
390-
_tcp->~WiFiClient();
461+
delete _tcp;
391462
_tcp = NULL;
392463
}
393464
_tcp = new WiFiClient();
394465
}
395466

396467
if(!_tcp->connect(_host.c_str(), _port)) {
397-
DEBUG_HTTPCLIENT("[HTTP-Client] failed connect to %s:%u.\n", _host.c_str(), _port);
468+
DEBUG_HTTPCLIENT("[HTTP-Client] failed connect to %s:%u\n", _host.c_str(), _port);
398469
return false;
399470
}
400471

401-
DEBUG_HTTPCLIENT("[HTTP-Client] connected to %s:%u.\n", _host.c_str(), _port);
472+
DEBUG_HTTPCLIENT("[HTTP-Client] connected to %s:%u\n", _host.c_str(), _port);
402473

403474
if(_https && _httpsFingerprint.length() > 0) {
404475
if(_tcps->verify(_httpsFingerprint.c_str(), _host.c_str())) {

libraries/ESP8266httpClient/src/ESP8266httpClient.h

+4
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,12 @@ class httpClient {
4848
httpClient();
4949
~httpClient();
5050

51+
void begin(const char *url, const char * httpsFingerprint = "");
52+
void begin(String url, String httpsFingerprint = "");
53+
5154
void begin(const char *host, uint16_t port, const char * url = "/", bool https = false, const char * httpsFingerprint = "");
5255
void begin(String host, uint16_t port, String url = "/", bool https = false, String httpsFingerprint = "");
56+
5357
void end(void);
5458

5559
bool connected(void);

0 commit comments

Comments
 (0)