Skip to content

Commit 81003a1

Browse files
committed
HTTPClient lib - add HTTPCLIENT_NOSECURE build flag
`HTTPCLIENT_NOSECURE` build flag disables TLS support in HTTPClient library by excluding `NetworkClientSecure.h` header. This allows linker to strip down mbedTLS lind and certificates bundle, which in turn reduces firmware image for about ~80kib.
1 parent 6b22339 commit 81003a1

File tree

2 files changed

+30
-8
lines changed

2 files changed

+30
-8
lines changed

libraries/HTTPClient/src/HTTPClient.cpp

+20-7
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,8 @@
2828

2929
#include <Arduino.h>
3030
#include <esp32-hal-log.h>
31-
32-
#ifdef HTTPCLIENT_1_1_COMPATIBLE
33-
#include <NetworkClient.h>
34-
#include <NetworkClientSecure.h>
35-
#endif
36-
3731
#include <StreamString.h>
3832
#include <base64.h>
39-
4033
#include "HTTPClient.h"
4134

4235
/// Cookie jar support
@@ -56,6 +49,7 @@ class TransportTraits {
5649
}
5750
};
5851

52+
#ifndef HTTPCLIENT_NOSECURE
5953
class TLSTraits : public TransportTraits {
6054
public:
6155
TLSTraits(const char *CAcert, const char *clicert = nullptr, const char *clikey = nullptr) : _cacert(CAcert), _clicert(clicert), _clikey(clikey) {}
@@ -81,6 +75,7 @@ class TLSTraits : public TransportTraits {
8175
const char *_clicert;
8276
const char *_clikey;
8377
};
78+
#endif // HTTPCLIENT_NOSECURE
8479
#endif // HTTPCLIENT_1_1_COMPATIBLE
8580

8681
/**
@@ -145,7 +140,12 @@ bool HTTPClient::begin(NetworkClient &client, String url) {
145140

146141
_port = (protocol == "https" ? 443 : 80);
147142
_secure = (protocol == "https");
143+
144+
#ifdef HTTPCLIENT_NOSECURE
145+
return _secure ? false : beginInternal(url, protocol.c_str());
146+
#else
148147
return beginInternal(url, protocol.c_str());
148+
#endif // HTTPCLIENT_NOSECURE
149149
}
150150

151151
/**
@@ -174,10 +174,16 @@ bool HTTPClient::begin(NetworkClient &client, String host, uint16_t port, String
174174
_uri = uri;
175175
_protocol = (https ? "https" : "http");
176176
_secure = https;
177+
178+
#ifdef HTTPCLIENT_NOSECURE
179+
return _secure ? false : true;
180+
#else
177181
return true;
182+
#endif // HTTPCLIENT_NOSECURE
178183
}
179184

180185
#ifdef HTTPCLIENT_1_1_COMPATIBLE
186+
#ifndef HTTPCLIENT_NOSECURE
181187
bool HTTPClient::begin(String url, const char *CAcert) {
182188
if (_client && !_tcpDeprecated) {
183189
log_d("mix up of new and deprecated api");
@@ -199,6 +205,7 @@ bool HTTPClient::begin(String url, const char *CAcert) {
199205

200206
return true;
201207
}
208+
#endif // HTTPCLIENT_NOSECURE
202209

203210
/**
204211
* parsing the url for all needed parameters
@@ -214,7 +221,11 @@ bool HTTPClient::begin(String url) {
214221
clear();
215222
_port = 80;
216223
if (!beginInternal(url, "http")) {
224+
#ifdef HTTPCLIENT_NOSECURE
225+
return false;
226+
#else
217227
return begin(url, (const char *)NULL);
228+
#endif // HTTPCLIENT_NOSECURE
218229
}
219230
_transportTraits = TransportTraitsPtr(new TransportTraits());
220231
if (!_transportTraits) {
@@ -299,6 +310,7 @@ bool HTTPClient::begin(String host, uint16_t port, String uri) {
299310
return true;
300311
}
301312

313+
#ifndef HTTPCLIENT_NOSECURE
302314
bool HTTPClient::begin(String host, uint16_t port, String uri, const char *CAcert) {
303315
if (_client && !_tcpDeprecated) {
304316
log_d("mix up of new and deprecated api");
@@ -338,6 +350,7 @@ bool HTTPClient::begin(String host, uint16_t port, String uri, const char *CAcer
338350
_transportTraits = TransportTraitsPtr(new TLSTraits(CAcert, cli_cert, cli_key));
339351
return true;
340352
}
353+
#endif // HTTPCLIENT_NOSECURE
341354
#endif // HTTPCLIENT_1_1_COMPATIBLE
342355

343356
/**

libraries/HTTPClient/src/HTTPClient.h

+10-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@
3434
#include <memory>
3535
#include <Arduino.h>
3636
#include <NetworkClient.h>
37+
#ifndef HTTPCLIENT_NOSECURE
3738
#include <NetworkClientSecure.h>
39+
#endif // HTTPCLIENT_NOSECURE
3840

3941
/// Cookie jar support
4042
#include <vector>
@@ -182,10 +184,17 @@ class HTTPClient {
182184

183185
#ifdef HTTPCLIENT_1_1_COMPATIBLE
184186
bool begin(String url);
185-
bool begin(String url, const char *CAcert);
186187
bool begin(String host, uint16_t port, String uri = "/");
188+
#ifndef HTTPCLIENT_NOSECURE
189+
bool begin(String url, const char *CAcert);
187190
bool begin(String host, uint16_t port, String uri, const char *CAcert);
188191
bool begin(String host, uint16_t port, String uri, const char *CAcert, const char *cli_cert, const char *cli_key);
192+
#else
193+
bool begin(String url, const char *CAcert){ return false; };
194+
bool begin(String host, uint16_t port, String uri, const char *CAcert){ return false; };
195+
bool begin(String host, uint16_t port, String uri, const char *CAcert, const char *cli_cert, const char *cli_key){ return false; };
196+
#endif // HTTPCLIENT_NOSECURE
197+
189198
#endif
190199

191200
void end(void);

0 commit comments

Comments
 (0)