From 667816ebe003c7381d657fcdb09ede499e346438 Mon Sep 17 00:00:00 2001 From: Jeroen88 Date: Thu, 11 Apr 2019 14:21:04 +0200 Subject: [PATCH] BearSSL Max Fragment Length Negotation and Node.js server (#5929) * Minor bug fixes in Maximum Fragment Length Negotation example, mainly giving background processes some time in fetch() * Minor layout changes to pass travis tests * Use PolledTimeout for timeout --- .../BearSSL_MaxFragmentLength.ino | 20 ++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/libraries/ESP8266WiFi/examples/BearSSL_MaxFragmentLength/BearSSL_MaxFragmentLength.ino b/libraries/ESP8266WiFi/examples/BearSSL_MaxFragmentLength/BearSSL_MaxFragmentLength.ino index 19ca6b3f7..f05210283 100644 --- a/libraries/ESP8266WiFi/examples/BearSSL_MaxFragmentLength/BearSSL_MaxFragmentLength.ino +++ b/libraries/ESP8266WiFi/examples/BearSSL_MaxFragmentLength/BearSSL_MaxFragmentLength.ino @@ -5,6 +5,7 @@ // Released to the public domain #include +#include #ifndef STASSID #define STASSID "your-ssid" @@ -17,17 +18,22 @@ const char *pass = STAPSK; void fetch(BearSSL::WiFiClientSecure *client) { client->write("GET / HTTP/1.0\r\nHost: tls.mbed.org\r\nUser-Agent: ESP8266\r\n\r\n"); client->flush(); - uint32_t to = millis() + 5000; + using oneShot = esp8266::polledTimeout::oneShot; + oneShot timeout(5000); do { char tmp[32]; - memset(tmp, 0, 32); int rlen = client->read((uint8_t*)tmp, sizeof(tmp) - 1); yield(); if (rlen < 0) { break; } + if (rlen == 0) { + delay(10); // Give background processes some time + continue; + } + tmp[rlen] = '\0'; Serial.print(tmp); - } while (millis() < to); + } while (!timeout); client->stop(); Serial.printf("\n-------\n"); } @@ -73,11 +79,11 @@ int fetchMaxFragmentLength() { BearSSL::WiFiClientSecure client; client.setInsecure(); - bool mfln = client.probeMaxFragmentLength("tls.mbed.org", 443, 1024); + bool mfln = client.probeMaxFragmentLength("tls.mbed.org", 443, 512); Serial.printf("\nConnecting to https://tls.mbed.org\n"); Serial.printf("MFLN supported: %s\n", mfln ? "yes" : "no"); if (mfln) { - client.setBufferSizes(1024, 1024); + client.setBufferSizes(512, 512); } client.connect("tls.mbed.org", 443); if (client.connected()) { @@ -125,6 +131,6 @@ void loop() { yield(); Serial.printf("\n\n"); - Serial.printf("Default SSL: %d bytes used\n", a); - Serial.printf("1024 byte MFLN SSL: %d bytes used\n", b); + Serial.printf("Default SSL: %d bytes used\n", a); + Serial.printf("512 byte MFLN SSL: %d bytes used\n", b); }