Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HTTPClient lib - add HTTPCLIENT_NOSECURE build flag #9893

Merged
merged 3 commits into from
Jun 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 21 additions & 7 deletions libraries/HTTPClient/src/HTTPClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,8 @@

#include <Arduino.h>
#include <esp32-hal-log.h>

#ifdef HTTPCLIENT_1_1_COMPATIBLE
#include <NetworkClient.h>
#include <NetworkClientSecure.h>
#endif

#include <StreamString.h>
#include <base64.h>

#include "HTTPClient.h"

/// Cookie jar support
Expand All @@ -56,6 +49,7 @@ class TransportTraits {
}
};

#ifndef HTTPCLIENT_NOSECURE
class TLSTraits : public TransportTraits {
public:
TLSTraits(const char *CAcert, const char *clicert = nullptr, const char *clikey = nullptr) : _cacert(CAcert), _clicert(clicert), _clikey(clikey) {}
Expand All @@ -81,6 +75,7 @@ class TLSTraits : public TransportTraits {
const char *_clicert;
const char *_clikey;
};
#endif // HTTPCLIENT_NOSECURE
#endif // HTTPCLIENT_1_1_COMPATIBLE

/**
Expand Down Expand Up @@ -145,6 +140,12 @@ bool HTTPClient::begin(NetworkClient &client, String url) {

_port = (protocol == "https" ? 443 : 80);
_secure = (protocol == "https");

#ifdef HTTPCLIENT_NOSECURE
if (_secure) {
return false;
}
#endif // HTTPCLIENT_NOSECURE
return beginInternal(url, protocol.c_str());
}

Expand Down Expand Up @@ -174,10 +175,16 @@ bool HTTPClient::begin(NetworkClient &client, String host, uint16_t port, String
_uri = uri;
_protocol = (https ? "https" : "http");
_secure = https;

#ifdef HTTPCLIENT_NOSECURE
return _secure ? false : true;
#else
return true;
#endif // HTTPCLIENT_NOSECURE
}

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

return true;
}
#endif // HTTPCLIENT_NOSECURE

/**
* parsing the url for all needed parameters
Expand All @@ -214,7 +222,11 @@ bool HTTPClient::begin(String url) {
clear();
_port = 80;
if (!beginInternal(url, "http")) {
#ifdef HTTPCLIENT_NOSECURE
return false;
#else
return begin(url, (const char *)NULL);
#endif // HTTPCLIENT_NOSECURE
}
_transportTraits = TransportTraitsPtr(new TransportTraits());
if (!_transportTraits) {
Expand Down Expand Up @@ -299,6 +311,7 @@ bool HTTPClient::begin(String host, uint16_t port, String uri) {
return true;
}

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

/**
Expand Down
17 changes: 16 additions & 1 deletion libraries/HTTPClient/src/HTTPClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@
#include <memory>
#include <Arduino.h>
#include <NetworkClient.h>
#ifndef HTTPCLIENT_NOSECURE
#include <NetworkClientSecure.h>
#endif // HTTPCLIENT_NOSECURE

/// Cookie jar support
#include <vector>
Expand Down Expand Up @@ -182,10 +184,23 @@ class HTTPClient {

#ifdef HTTPCLIENT_1_1_COMPATIBLE
bool begin(String url);
bool begin(String url, const char *CAcert);
bool begin(String host, uint16_t port, String uri = "/");
#ifndef HTTPCLIENT_NOSECURE
bool begin(String url, const char *CAcert);
bool begin(String host, uint16_t port, String uri, const char *CAcert);
bool begin(String host, uint16_t port, String uri, const char *CAcert, const char *cli_cert, const char *cli_key);
#else
bool begin(String url, const char *CAcert) {
return false;
};
bool begin(String host, uint16_t port, String uri, const char *CAcert) {
return false;
};
bool begin(String host, uint16_t port, String uri, const char *CAcert, const char *cli_cert, const char *cli_key) {
return false;
};
#endif // HTTPCLIENT_NOSECURE

#endif

void end(void);
Expand Down
Loading