Skip to content

Commit 81d3bb3

Browse files
committed
Return error when HTTPClient::begin is called with HTTPS URL without certificate fingerprint (#1941)
1 parent 53d190f commit 81d3bb3

File tree

2 files changed

+23
-23
lines changed

2 files changed

+23
-23
lines changed

libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.cpp

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -87,33 +87,43 @@ HTTPClient::~HTTPClient()
8787
}
8888
}
8989

90+
void HTTPClient::clear()
91+
{
92+
_returnCode = 0;
93+
_size = -1;
94+
_headers = "";
95+
}
96+
9097

9198
bool HTTPClient::begin(String url, String httpsFingerprint)
9299
{
100+
_transportTraits.reset(nullptr);
93101
if (httpsFingerprint.length() == 0) {
94102
return false;
95103
}
96-
if (!begin(url)) {
104+
if (!beginInternal(url, "https")) {
97105
return false;
98106
}
99107
_transportTraits = TransportTraitsPtr(new TLSTraits(httpsFingerprint));
100108
DEBUG_HTTPCLIENT("[HTTP-Client][begin] httpsFingerprint: %s\n", httpsFingerprint.c_str());
101109
return true;
102110
}
103111

104-
void HTTPClient::clear()
105-
{
106-
_returnCode = 0;
107-
_size = -1;
108-
_headers = "";
109-
}
110-
111-
112112
/**
113113
* parsing the url for all needed parameters
114114
* @param url String
115115
*/
116116
bool HTTPClient::begin(String url)
117+
{
118+
_transportTraits.reset(nullptr);
119+
if (!beginInternal(url, "http")) {
120+
return false;
121+
}
122+
_transportTraits = TransportTraitsPtr(new TransportTraits());
123+
return true;
124+
}
125+
126+
bool HTTPClient::beginInternal(String url, const char* expectedProtocol)
117127
{
118128
DEBUG_HTTPCLIENT("[HTTP-Client][begin] url: %s\n", url.c_str());
119129
bool hasPort = false;
@@ -148,25 +158,14 @@ bool HTTPClient::begin(String url)
148158
_host = host.substring(0, index); // hostname
149159
host.remove(0, (index + 1)); // remove hostname + :
150160
_port = host.toInt(); // get port
151-
hasPort = true;
152161
} else {
153162
_host = host;
154163
}
155164
_uri = url;
156-
157-
if(_protocol.equalsIgnoreCase("http")) {
158-
if(!hasPort) {
159-
_port = 80;
160-
}
161-
} else if(_protocol.equalsIgnoreCase("https")) {
162-
if(!hasPort) {
163-
_port = 443;
164-
}
165-
} else {
166-
DEBUG_HTTPCLIENT("[HTTP-Client][begin] protocol: %s unknown?!\n", _protocol.c_str());
165+
if (_protocol != expectedProtocol) {
166+
DEBUG_HTTPCLIENT("[HTTP-Client][begin] unexpected protocol: %s, expected %s\n", _protocol.c_str(), expectedProtocol);
167167
return false;
168168
}
169-
_transportTraits = TransportTraitsPtr(new TransportTraits());
170169
DEBUG_HTTPCLIENT("[HTTP-Client][begin] host: %s port: %d url: %s\n", _host.c_str(), _port, _uri.c_str());
171170
return true;
172171
}
@@ -785,7 +784,7 @@ bool HTTPClient::connect(void)
785784
}
786785

787786
if (!_transportTraits) {
788-
DEBUG_HTTPCLIENT("[HTTP-Client] _transportTraits is null (HTTPClient::begin not called?)\n");
787+
DEBUG_HTTPCLIENT("[HTTP-Client] connect: HTTPClient::begin was not called or returned error\n");
789788
return false;
790789
}
791790

libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,7 @@ class HTTPClient
186186
String value;
187187
};
188188

189+
bool beginInternal(String url, const char* expectedProtocol);
189190
void clear();
190191
int returnError(int error);
191192
bool connect(void);

0 commit comments

Comments
 (0)