Skip to content

Commit cc1951a

Browse files
committed
get rid of pointer, reverting to first proposal
1 parent d97e0fb commit cc1951a

File tree

3 files changed

+71
-68
lines changed

3 files changed

+71
-68
lines changed

libraries/ESP8266HTTPClient/examples/BasicHttpsClient/BasicHttpsClient.ino

+8-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,14 @@ void loop() {
6161

6262
HTTPClient https;
6363
https.setTimeout(4000); // or: client->setTimeout(4000);
64-
client->setNegociationTimeout(10000);
64+
client->setHandshakeTimeout(10000);
65+
66+
#if 1
67+
constexpr int sslbufsize = 1024;
68+
bool mfln = client->probeMaxFragmentLength(jigsaw_host, jigsaw_port, sslbufsize);
69+
Serial.printf("Can reduce SSL footprint to %d bytes in RAM: %s\n", sslbufsize, mfln ? "yes" : "no");
70+
if (mfln) { client->setBufferSizes(sslbufsize, sslbufsize); }
71+
#endif
6572

6673
Serial.print("[HTTPS] begin...\n");
6774
if (https.begin(*client, jigsaw_host, jigsaw_port)) { // HTTPS

libraries/ESP8266WiFi/src/WiFiClientSecureBearSSL.cpp

+19-29
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,6 @@ extern "C" {
6969
namespace BearSSL {
7070

7171
void WiFiClientSecureCtx::_clear() {
72-
// TLS handshake may take more than the 5 second default timeout
73-
_negociationTimeout = _userFacingStream? _userFacingStream->getNegociationTimeout(): 15000;
74-
updateStreamTimeout();
75-
7672
_sc = nullptr;
7773
_sc_svr = nullptr;
7874
_eng = nullptr;
@@ -84,7 +80,7 @@ void WiFiClientSecureCtx::_clear() {
8480
_now = 0; // You can override or ensure time() is correct w/configTime
8581
_ta = nullptr;
8682
setBufferSizes(16384, 512); // Minimum safe
87-
_handshake_done = false;
83+
_set_handshake_done(false); // refreshes _timeout
8884
_recvapp_buf = nullptr;
8985
_recvapp_len = 0;
9086
_oom_err = false;
@@ -104,7 +100,7 @@ void WiFiClientSecureCtx::_clearAuthenticationSettings() {
104100
}
105101

106102

107-
WiFiClientSecureCtx::WiFiClientSecureCtx(const WiFiClientSecure* alter) : WiFiClient(), _userFacingStream(alter) {
103+
WiFiClientSecureCtx::WiFiClientSecureCtx() : WiFiClient() {
108104
_clear();
109105
_clearAuthenticationSettings();
110106
_certStore = nullptr; // Don't want to remove cert store on a clear, should be long lived
@@ -122,11 +118,10 @@ WiFiClientSecureCtx::~WiFiClientSecureCtx() {
122118
stack_thunk_del_ref();
123119
}
124120

125-
WiFiClientSecureCtx::WiFiClientSecureCtx(const WiFiClientSecure* alter,
126-
ClientContext* client,
121+
WiFiClientSecureCtx::WiFiClientSecureCtx(ClientContext* client,
127122
const X509List *chain, const PrivateKey *sk,
128123
int iobuf_in_size, int iobuf_out_size, ServerSessions *cache,
129-
const X509List *client_CA_ta, int tls_min, int tls_max):_userFacingStream(alter) {
124+
const X509List *client_CA_ta, int tls_min, int tls_max) {
130125
_clear();
131126
_clearAuthenticationSettings();
132127
stack_thunk_add_ref();
@@ -143,12 +138,11 @@ WiFiClientSecureCtx::WiFiClientSecureCtx(const WiFiClientSecure* alter,
143138
}
144139
}
145140

146-
WiFiClientSecureCtx::WiFiClientSecureCtx(const WiFiClientSecure* alter,
147-
ClientContext *client,
141+
WiFiClientSecureCtx::WiFiClientSecureCtx(ClientContext *client,
148142
const X509List *chain,
149143
unsigned cert_issuer_key_type, const PrivateKey *sk,
150144
int iobuf_in_size, int iobuf_out_size, ServerSessions *cache,
151-
const X509List *client_CA_ta, int tls_min, int tls_max): _userFacingStream(alter) {
145+
const X509List *client_CA_ta, int tls_min, int tls_max) {
152146
_clear();
153147
_clearAuthenticationSettings();
154148
stack_thunk_add_ref();
@@ -207,7 +201,13 @@ bool WiFiClientSecureCtx::stop(unsigned int maxWaitMs) {
207201
}
208202

209203
bool WiFiClientSecureCtx::flush(unsigned int maxWaitMs) {
204+
auto savedNormal = _normalTimeout;
205+
auto savedHandshake = _handshakeTimeout;
206+
_normalTimeout = maxWaitMs;
207+
_handshakeTimeout = maxWaitMs;
210208
(void) _run_until(BR_SSL_SENDAPP);
209+
_normalTimeout = savedNormal;
210+
_handshakeTimeout = savedHandshake;
211211
return WiFiClient::flush(maxWaitMs);
212212
}
213213

@@ -248,10 +248,7 @@ void WiFiClientSecureCtx::_freeSSL() {
248248
_recvapp_buf = nullptr;
249249
_recvapp_len = 0;
250250
// This connection is toast
251-
_handshake_done = false;
252-
253-
_negociationTimeout = _userFacingStream? _userFacingStream->getNegociationTimeout(): 15000;
254-
updateStreamTimeout();
251+
_set_handshake_done(false); // refreshes _timeout
255252
}
256253

257254
bool WiFiClientSecureCtx::_clientConnected() {
@@ -466,7 +463,7 @@ size_t WiFiClientSecureCtx::peekBytes(uint8_t *buffer, size_t length) {
466463
return 0;
467464
}
468465

469-
updateStreamTimeout();
466+
_updateStreamTimeout();
470467
_startMillis = millis();
471468
while ((_pollRecvBuffer() < (int)length) && ((millis() - _startMillis) < _timeout)) {
472469
yield();
@@ -491,8 +488,8 @@ int WiFiClientSecureCtx::_run_until(unsigned target, bool blocking) {
491488

492489
// _run_until() is called prior to inherited read/write methods
493490
// -> refreshing _timeout here, which is also used by ancestors
494-
updateStreamTimeout();
495-
esp8266::polledTimeout::oneShotMs loopTimeout(_timeout);
491+
DEBUG_BSSL("_run_until starts, timeout=%lu\n", _updateStreamTimeout());
492+
esp8266::polledTimeout::oneShotMs loopTimeout(_updateStreamTimeout());
496493

497494
for (int no_work = 0; blocking || no_work < 2;) {
498495
optimistic_yield(100);
@@ -611,15 +608,15 @@ int WiFiClientSecureCtx::_run_until(unsigned target, bool blocking) {
611608
}
612609

613610
bool WiFiClientSecureCtx::_wait_for_handshake() {
614-
_handshake_done = false;
611+
_set_handshake_done(false); // refreshes _timeout
615612
while (!_handshake_done && _clientConnected()) {
616613
int ret = _run_until(BR_SSL_SENDAPP);
617614
if (ret < 0) {
618615
DEBUG_BSSL("_wait_for_handshake: failed\n");
619616
break;
620617
}
621618
if (br_ssl_engine_current_state(_eng) & BR_SSL_SENDAPP) {
622-
_handshake_done = true;
619+
_set_handshake_done(true); // refreshes _timeout
623620
}
624621
optimistic_yield(1000);
625622
}
@@ -1215,9 +1212,7 @@ bool WiFiClientSecureCtx::_connectSSL(const char* hostName) {
12151212
_x509_insecure = nullptr;
12161213
_x509_knownkey = nullptr;
12171214

1218-
// reduce timeout after successful handshake to fail fast if server stop accepting our data for whathever reason
1219-
if (ret) _negociationTimeout = 0;
1220-
updateStreamTimeout();
1215+
// _timeout has been refreshed to normal operation as _handshake_done turned to true
12211216

12221217
return ret;
12231218
}
@@ -1683,9 +1678,4 @@ bool WiFiClientSecure::probeMaxFragmentLength(IPAddress ip, uint16_t port, uint1
16831678
return _SendAbort(probe, supportsLen);
16841679
}
16851680

1686-
void WiFiClientSecureCtx::updateStreamTimeout ()
1687-
{
1688-
_timeout = std::max(_userFacingStream? _userFacingStream->getTimeout(): 5000, _negociationTimeout);
1689-
}
1690-
16911681
}; // namespace BearSSL

libraries/ESP8266WiFi/src/WiFiClientSecureBearSSL.h

+44-38
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class WiFiClientSecure;
3535

3636
class WiFiClientSecureCtx : public WiFiClient {
3737
public:
38-
WiFiClientSecureCtx(const WiFiClientSecure* alter);
38+
WiFiClientSecureCtx();
3939
WiFiClientSecureCtx(const WiFiClientSecureCtx &rhs) = delete;
4040
~WiFiClientSecureCtx() override;
4141

@@ -149,6 +149,10 @@ class WiFiClientSecureCtx : public WiFiClient {
149149
// consume bytes after use (see peekBuffer)
150150
virtual void peekConsume (size_t consume) override;
151151

152+
void setNormalTimeout (unsigned long timeout) { _normalTimeout = timeout; }
153+
void setHandshakeTimeout (unsigned long timeout) { _handshakeTimeout = timeout; }
154+
unsigned long getHandshakeTimeout () const { return _handshakeTimeout; }
155+
152156
protected:
153157
bool _connectSSL(const char *hostName); // Do initial SSL handshake
154158

@@ -218,12 +222,10 @@ class WiFiClientSecureCtx : public WiFiClient {
218222

219223
// Methods for handling server.available() call which returns a client connection.
220224
friend class WiFiClientSecure; // access to private context constructors
221-
WiFiClientSecureCtx(const WiFiClientSecure* alter,
222-
ClientContext *client, const X509List *chain, unsigned cert_issuer_key_type,
225+
WiFiClientSecureCtx(ClientContext *client, const X509List *chain, unsigned cert_issuer_key_type,
223226
const PrivateKey *sk, int iobuf_in_size, int iobuf_out_size, ServerSessions *cache,
224227
const X509List *client_CA_ta, int tls_min, int tls_max);
225-
WiFiClientSecureCtx(const WiFiClientSecure* alter,
226-
ClientContext* client, const X509List *chain, const PrivateKey *sk,
228+
WiFiClientSecureCtx(ClientContext* client, const X509List *chain, const PrivateKey *sk,
227229
int iobuf_in_size, int iobuf_out_size, ServerSessions *cache,
228230
const X509List *client_CA_ta, int tls_min, int tls_max);
229231

@@ -241,10 +243,11 @@ class WiFiClientSecureCtx : public WiFiClient {
241243
uint8_t *_streamLoad(Stream& stream, size_t size);
242244

243245
// timeout management
244-
unsigned long _negociationTimeout = 0; // negociation timeout
245-
const WiFiClientSecure* _userFacingStream = nullptr; // user-facing WiFiClientSecure
246-
void setStream (const WiFiClientSecure* upStream) { _userFacingStream = upStream; }
247-
void updateStreamTimeout ();
246+
247+
unsigned long _updateStreamTimeout () { return _timeout = _handshake_done? _normalTimeout: _handshakeTimeout; }
248+
void _set_handshake_done (bool handshake_done) { _handshake_done = handshake_done; _updateStreamTimeout(); }
249+
250+
unsigned long _normalTimeout = 5000, _handshakeTimeout = 15000;
248251

249252
}; // class WiFiClientSecureCtx
250253

@@ -265,35 +268,33 @@ class WiFiClientSecure : public WiFiClient {
265268

266269
public:
267270

268-
WiFiClientSecure():_ctx(new WiFiClientSecureCtx(this)) { _owned = _ctx.get(); }
269-
WiFiClientSecure(const WiFiClientSecure &rhs): WiFiClient(), _ctx(rhs._ctx), _userNegociationTimeout(rhs._userNegociationTimeout) {
270-
if (_ctx) { _owned = _ctx.get(); _ctx->setStream(this); }
271-
}
271+
WiFiClientSecure():_ctx(new WiFiClientSecureCtx()) { _owned = _ctx.get(); }
272+
WiFiClientSecure(const WiFiClientSecure &rhs): WiFiClient(), _ctx(rhs._ctx) { if (_ctx) _owned = _ctx.get(); }
272273
~WiFiClientSecure() override { _ctx = nullptr; }
273274

274275
WiFiClientSecure& operator=(const WiFiClientSecure&) = default;
275276

276277
std::unique_ptr<WiFiClient> clone() const override { return std::unique_ptr<WiFiClient>(new WiFiClientSecure(*this)); }
277278

278279
uint8_t status() override { return _ctx->status(); }
279-
int connect(IPAddress ip, uint16_t port) override { return _ctx->connect(ip, port); }
280-
int connect(const String& host, uint16_t port) override { return _ctx->connect(host, port); }
281-
int connect(const char* name, uint16_t port) override { return _ctx->connect(name, port); }
282-
283-
uint8_t connected() override { return _ctx->connected(); }
284-
size_t write(const uint8_t *buf, size_t size) override { return _ctx->write(buf, size); }
285-
size_t write_P(PGM_P buf, size_t size) override { return _ctx->write_P(buf, size); }
286-
size_t write(const char *buf) { return write((const uint8_t*)buf, strlen(buf)); }
287-
size_t write_P(const char *buf) { return write_P((PGM_P)buf, strlen_P(buf)); }
288-
size_t write(Stream& stream) /* Note this is not virtual */ { return _ctx->write(stream); }
289-
int read(uint8_t *buf, size_t size) override { return _ctx->read(buf, size); }
290-
int available() override { return _ctx->available(); }
291-
int availableForWrite() override { return _ctx->availableForWrite(); }
292-
int read() override { return _ctx->read(); }
293-
int peek() override { return _ctx->peek(); }
294-
size_t peekBytes(uint8_t *buffer, size_t length) override { return _ctx->peekBytes(buffer, length); }
295-
bool flush(unsigned int maxWaitMs) { return _ctx->flush(maxWaitMs); }
296-
bool stop(unsigned int maxWaitMs) { return _ctx->stop(maxWaitMs); }
280+
int connect(IPAddress ip, uint16_t port) override { uto(); return _ctx->connect(ip, port); }
281+
int connect(const String& host, uint16_t port) override { uto(); return _ctx->connect(host, port); }
282+
int connect(const char* name, uint16_t port) override { uto(); return _ctx->connect(name, port); }
283+
284+
uint8_t connected() override { uto(); return _ctx->connected(); }
285+
size_t write(const uint8_t *buf, size_t size) override { uto(); return _ctx->write(buf, size); }
286+
size_t write_P(PGM_P buf, size_t size) override { uto(); return _ctx->write_P(buf, size); }
287+
size_t write(const char *buf) { uto(); return write((const uint8_t*)buf, strlen(buf)); }
288+
size_t write_P(const char *buf) { uto(); return write_P((PGM_P)buf, strlen_P(buf)); }
289+
size_t write(Stream& stream) /* Note this is not virtual */ { uto(); return _ctx->write(stream); }
290+
int read(uint8_t *buf, size_t size) override { uto(); return _ctx->read(buf, size); }
291+
int available() override { uto(); return _ctx->available(); }
292+
int availableForWrite() override { uto(); return _ctx->availableForWrite(); }
293+
int read() override { uto(); return _ctx->read(); }
294+
int peek() override { uto(); return _ctx->peek(); }
295+
size_t peekBytes(uint8_t *buffer, size_t length) override { uto(); return _ctx->peekBytes(buffer, length); }
296+
bool flush(unsigned int maxWaitMs) { uto(); return _ctx->flush(maxWaitMs); }
297+
bool stop(unsigned int maxWaitMs) { uto(); return _ctx->stop(maxWaitMs); }
297298
void flush() override { (void)flush(0); }
298299
void stop() override { (void)stop(0); }
299300

@@ -362,7 +363,7 @@ class WiFiClientSecure : public WiFiClient {
362363
virtual bool hasPeekBufferAPI () const override { return true; }
363364

364365
// return number of byte accessible by peekBuffer()
365-
virtual size_t peekAvailable () override { return _ctx->available(); }
366+
virtual size_t peekAvailable () override { return available(); }
366367

367368
// return a pointer to available data buffer (size = peekAvailable())
368369
// semantic forbids any kind of read() before calling peekConsume()
@@ -371,28 +372,33 @@ class WiFiClientSecure : public WiFiClient {
371372
// consume bytes after use (see peekBuffer)
372373
virtual void peekConsume (size_t consume) override { return _ctx->peekConsume(consume); }
373374

374-
// allowing to change timeout during negociation
375-
void setNegociationTimeout (unsigned long timeout) { _userNegociationTimeout = timeout; }
376-
unsigned long getNegociationTimeout () const { return _userNegociationTimeout; }
375+
// allowing user to set timeout used during handshake
376+
void setHandshakeTimeout (unsigned long timeout) { _ctx->setHandshakeTimeout(timeout); }
377+
unsigned long getHandshakeTimeout () const { return _ctx->getHandshakeTimeout(); }
377378

378379
private:
379380
std::shared_ptr<WiFiClientSecureCtx> _ctx;
380-
unsigned long _userNegociationTimeout = 15000; // negociation timeout initializer
381381

382382
// Methods for handling server.available() call which returns a client connection.
383383
friend class WiFiServerSecure; // Server needs to access these constructors
384384
WiFiClientSecure(ClientContext *client, const X509List *chain, unsigned cert_issuer_key_type,
385385
const PrivateKey *sk, int iobuf_in_size, int iobuf_out_size, ServerSessions *cache,
386386
const X509List *client_CA_ta, int tls_min, int tls_max):
387-
_ctx(new WiFiClientSecureCtx(this, client, chain, cert_issuer_key_type, sk, iobuf_in_size, iobuf_out_size, cache, client_CA_ta, tls_min, tls_max)) {
387+
_ctx(new WiFiClientSecureCtx(client, chain, cert_issuer_key_type, sk, iobuf_in_size, iobuf_out_size, cache, client_CA_ta, tls_min, tls_max)) {
388388
}
389389

390390
WiFiClientSecure(ClientContext* client, const X509List *chain, const PrivateKey *sk,
391391
int iobuf_in_size, int iobuf_out_size, ServerSessions *cache,
392392
const X509List *client_CA_ta, int tls_min, int tls_max):
393-
_ctx(new WiFiClientSecureCtx(this, client, chain, sk, iobuf_in_size, iobuf_out_size, cache, client_CA_ta, tls_min, tls_max)) {
393+
_ctx(new WiFiClientSecureCtx(client, chain, sk, iobuf_in_size, iobuf_out_size, cache, client_CA_ta, tls_min, tls_max)) {
394394
}
395395

396+
// (because Stream::setTimeout() is not virtual,)
397+
// forward user timeout from Stream:: to SSL context
398+
// this is internally called on every user operations
399+
inline void uto () { _ctx->setNormalTimeout(_timeout); }
400+
401+
396402
}; // class WiFiClientSecure
397403

398404
}; // namespace BearSSL

0 commit comments

Comments
 (0)