Skip to content

Commit 1079f4c

Browse files
vortigontme-no-devpre-commit-ci-lite[bot]
authored
HTTPClient lib - add HTTPCLIENT_NOSECURE build flag (#9893)
* 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. * Update HTTPClient.cpp * ci(pre-commit): Apply automatic fixes --------- Co-authored-by: Me No Dev <[email protected]> Co-authored-by: pre-commit-ci-lite[bot] <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com>
1 parent d708438 commit 1079f4c

File tree

2 files changed

+37
-8
lines changed

2 files changed

+37
-8
lines changed

Diff for: libraries/HTTPClient/src/HTTPClient.cpp

+21-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,6 +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+
if (_secure) {
146+
return false;
147+
}
148+
#endif // HTTPCLIENT_NOSECURE
148149
return beginInternal(url, protocol.c_str());
149150
}
150151

@@ -174,10 +175,16 @@ bool HTTPClient::begin(NetworkClient &client, String host, uint16_t port, String
174175
_uri = uri;
175176
_protocol = (https ? "https" : "http");
176177
_secure = https;
178+
179+
#ifdef HTTPCLIENT_NOSECURE
180+
return _secure ? false : true;
181+
#else
177182
return true;
183+
#endif // HTTPCLIENT_NOSECURE
178184
}
179185

180186
#ifdef HTTPCLIENT_1_1_COMPATIBLE
187+
#ifndef HTTPCLIENT_NOSECURE
181188
bool HTTPClient::begin(String url, const char *CAcert) {
182189
if (_client && !_tcpDeprecated) {
183190
log_d("mix up of new and deprecated api");
@@ -199,6 +206,7 @@ bool HTTPClient::begin(String url, const char *CAcert) {
199206

200207
return true;
201208
}
209+
#endif // HTTPCLIENT_NOSECURE
202210

203211
/**
204212
* parsing the url for all needed parameters
@@ -214,7 +222,11 @@ bool HTTPClient::begin(String url) {
214222
clear();
215223
_port = 80;
216224
if (!beginInternal(url, "http")) {
225+
#ifdef HTTPCLIENT_NOSECURE
226+
return false;
227+
#else
217228
return begin(url, (const char *)NULL);
229+
#endif // HTTPCLIENT_NOSECURE
218230
}
219231
_transportTraits = TransportTraitsPtr(new TransportTraits());
220232
if (!_transportTraits) {
@@ -299,6 +311,7 @@ bool HTTPClient::begin(String host, uint16_t port, String uri) {
299311
return true;
300312
}
301313

314+
#ifndef HTTPCLIENT_NOSECURE
302315
bool HTTPClient::begin(String host, uint16_t port, String uri, const char *CAcert) {
303316
if (_client && !_tcpDeprecated) {
304317
log_d("mix up of new and deprecated api");
@@ -338,6 +351,7 @@ bool HTTPClient::begin(String host, uint16_t port, String uri, const char *CAcer
338351
_transportTraits = TransportTraitsPtr(new TLSTraits(CAcert, cli_cert, cli_key));
339352
return true;
340353
}
354+
#endif // HTTPCLIENT_NOSECURE
341355
#endif // HTTPCLIENT_1_1_COMPATIBLE
342356

343357
/**

Diff for: libraries/HTTPClient/src/HTTPClient.h

+16-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,23 @@ 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) {
194+
return false;
195+
};
196+
bool begin(String host, uint16_t port, String uri, const char *CAcert) {
197+
return false;
198+
};
199+
bool begin(String host, uint16_t port, String uri, const char *CAcert, const char *cli_cert, const char *cli_key) {
200+
return false;
201+
};
202+
#endif // HTTPCLIENT_NOSECURE
203+
189204
#endif
190205

191206
void end(void);

0 commit comments

Comments
 (0)