Skip to content

Commit 8ae8afb

Browse files
authored
Merge pull request #89 from bonitoo-io/feat/mfn
feat(ESP8266): Add Max Fragment Length Negotiation for TLS connection
2 parents 92cd781 + 70dafb9 commit 8ae8afb

File tree

2 files changed

+56
-5
lines changed

2 files changed

+56
-5
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
11
# Changelog
2+
## Version 3.4.0 (in progres)
3+
### Features
4+
- [#88](https://github.com/tobiasschuerg/InfluxDB-Client-for-Arduino/pull/88) - ESP8266 only - Added Max Fragment Length Negotiation for TLS communicaton to reduce memory allocation. If server supports MFLN, it saves ~15kB. Standalone InfluxDB OSS server doesn't support MFLN, Cloud yes. To leverage MFLN for standalone OSS, a reverse proxy needs to be used.
5+
6+
### Documentation
7+
- [#87](https://github.com/tobiasschuerg/InfluxDB-Client-for-Arduino/pull/87) - Fixed include file name in the Readme
28

39
## Version 3.3.0 (2020-07-07)
410
- [NEW] Added possibility skip server certification validation (`setInsecure()` method)

src/InfluxDbClient.cpp

Lines changed: 50 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,9 @@ static const char RetryAfter[] = "Retry-After";
5050
static const char TransferEnconding[] = "Transfer-Encoding";
5151

5252
static String escapeJSONString(String &value);
53-
53+
#if defined(ESP8266)
54+
bool checkMFLN(BearSSL::WiFiClientSecure *client, String url);
55+
#endif
5456
static String precisionToString(WritePrecision precision, uint8_t version = 2) {
5557
switch(precision) {
5658
case WritePrecision::US:
@@ -220,6 +222,7 @@ bool InfluxDBClient::init() {
220222
if (_insecure) {
221223
wifiClientSec->setInsecure();
222224
}
225+
checkMFLN(wifiClientSec, _serverUrl);
223226
#elif defined(ESP32)
224227
WiFiClientSecure *wifiClientSec = new WiFiClientSecure;
225228
if(_certInfo && strlen_P(_certInfo) > 0) {
@@ -236,6 +239,52 @@ bool InfluxDBClient::init() {
236239
return true;
237240
}
238241

242+
// parse URL for host and port and call probeMaxFragmentLength
243+
#if defined(ESP8266)
244+
bool checkMFLN(BearSSL::WiFiClientSecure *client, String url) {
245+
int index = url.indexOf(':');
246+
if(index < 0) {
247+
return false;
248+
}
249+
String protocol = url.substring(0, index);
250+
int port = -1;
251+
url.remove(0, (index + 3)); // remove http:// or https://
252+
253+
if (protocol == "http") {
254+
// set default port for 'http'
255+
port = 80;
256+
} else if (protocol == "https") {
257+
// set default port for 'https'
258+
port = 443;
259+
} else {
260+
return false;
261+
}
262+
index = url.indexOf('/');
263+
String host = url.substring(0, index);
264+
url.remove(0, index); // remove host
265+
// check Authorization
266+
index = host.indexOf('@');
267+
if(index >= 0) {
268+
host.remove(0, index + 1); // remove auth part including @
269+
}
270+
// get port
271+
index = host.indexOf(':');
272+
if(index >= 0) {
273+
String portS = host;
274+
host = host.substring(0, index); // hostname
275+
portS.remove(0, (index + 1)); // remove hostname + :
276+
port = portS.toInt(); // get port
277+
}
278+
INFLUXDB_CLIENT_DEBUG("probeMaxFragmentLength to %s:%d\n", host.c_str(), port);
279+
bool mfln = client->probeMaxFragmentLength(host, port, 1024);
280+
INFLUXDB_CLIENT_DEBUG(" MFLN:%s\n", mfln ? "yes" : "no");
281+
if (mfln) {
282+
client->setBufferSizes(1024, 1024);
283+
}
284+
return mfln;
285+
}
286+
#endif //ESP8266
287+
239288
InfluxDBClient::~InfluxDBClient() {
240289
if(_pointsBuffer) {
241290
delete [] _pointsBuffer;
@@ -248,10 +297,6 @@ InfluxDBClient::~InfluxDBClient() {
248297
}
249298

250299
void InfluxDBClient::clean() {
251-
// if(_wifiClient) {
252-
// delete _wifiClient;
253-
// _wifiClient = nullptr;
254-
// }
255300
_wifiClient = nullptr;
256301
#if defined(ESP8266)
257302
if(_cert) {

0 commit comments

Comments
 (0)