Skip to content

Commit 2783a24

Browse files
committed
BearSSL memory analysis
1 parent a04f510 commit 2783a24

File tree

5 files changed

+234
-68
lines changed

5 files changed

+234
-68
lines changed

SpotifyClient.cpp

+46-49
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,20 @@
2121
SOFTWARE.
2222
*/
2323

24-
#include <ESP8266HTTPClient.h>
24+
#pragma once
2525
#include <ESP8266WiFi.h>
26+
#include <ESP8266HTTPClient.h>
2627
#include <ESP8266mDNS.h>
2728

2829
#include "SpotifyClient.h"
2930

3031
#define min(X, Y) (((X) < (Y)) ? (X) : (Y))
3132

32-
SpotifyClient::SpotifyClient(String clientId, String clientSecret, String redirectUri) {
33+
SpotifyClient::SpotifyClient(String clientId, String clientSecret, String redirectUri, WiFiClientSecure *wifiClient) {
3334
this->clientId = clientId;
3435
this->clientSecret = clientSecret;
3536
this->redirectUri = redirectUri;
37+
this->wifiClient = wifiClient;
3638
}
3739

3840
uint16_t SpotifyClient::update(SpotifyData *data, SpotifyAuth *auth) {
@@ -41,14 +43,13 @@ uint16_t SpotifyClient::update(SpotifyData *data, SpotifyAuth *auth) {
4143
level = 0;
4244
isDataCall = true;
4345
currentParent = "";
44-
WiFiClientSecure client = WiFiClientSecure();
4546
JsonStreamingParser parser;
4647
parser.setListener(this);
4748

4849
String host = "api.spotify.com";
4950
const int port = 443;
5051
String url = "/v1/me/player/currently-playing";
51-
if (!client.connect(host.c_str(), port)) {
52+
if (!wifiClient->connect(host.c_str(), port)) {
5253
Serial.println("connection failed");
5354
return 0;
5455
}
@@ -60,10 +61,10 @@ uint16_t SpotifyClient::update(SpotifyData *data, SpotifyAuth *auth) {
6061
"Connection: close\r\n\r\n";
6162
// This will send the request to the server
6263
Serial.println(request);
63-
client.print(request);
64+
wifiClient->print(request);
6465

6566
int retryCounter = 0;
66-
while (!client.available()) {
67+
while (!wifiClient->available()) {
6768
executeCallback();
6869
retryCounter++;
6970
if (retryCounter > 10) {
@@ -77,21 +78,20 @@ uint16_t SpotifyClient::update(SpotifyData *data, SpotifyAuth *auth) {
7778
char c = ' ';
7879

7980
int size = 0;
80-
client.setNoDelay(false);
81-
// while(client.connected()) {
81+
wifiClient->setNoDelay(false);
8282
uint16_t httpCode = 0;
83-
while (client.connected() || client.available()) {
84-
while ((size = client.available()) > 0) {
83+
while (wifiClient->connected() || wifiClient->available()) {
84+
while ((size = wifiClient->available()) > 0) {
8585

8686
if (isBody) {
8787
uint16_t len = min(bufLen, size);
88-
c = client.readBytes(buf, len);
88+
c = wifiClient->readBytes(buf, len);
8989
for (uint16_t i = 0; i < len; i++) {
9090
parser.parse(buf[i]);
9191
// Serial.print((char)buf[i]);
9292
}
9393
} else {
94-
String line = client.readStringUntil('\r');
94+
String line = wifiClient->readStringUntil('\r');
9595
Serial.println(line);
9696
if (line.startsWith("HTTP/1.")) {
9797
httpCode = line.substring(9, line.indexOf(' ', 9)).toInt();
@@ -121,14 +121,13 @@ uint16_t SpotifyClient::playerCommand(SpotifyAuth *auth, String method, String c
121121
level = 0;
122122
isDataCall = true;
123123
currentParent = "";
124-
WiFiClientSecure client = WiFiClientSecure();
125124
JsonStreamingParser parser;
126125
parser.setListener(this);
127126

128127
String host = "api.spotify.com";
129128
const int port = 443;
130129
String url = "/v1/me/player/" + command;
131-
if (!client.connect(host.c_str(), port)) {
130+
if (!wifiClient->connect(host.c_str(), port)) {
132131
Serial.println("connection failed");
133132
return 0;
134133
}
@@ -141,10 +140,10 @@ uint16_t SpotifyClient::playerCommand(SpotifyAuth *auth, String method, String c
141140
"Connection: close\r\n\r\n";
142141
// This will send the request to the server
143142
Serial.println(request);
144-
client.print(request);
143+
wifiClient->print(request);
145144

146145
int retryCounter = 0;
147-
while (!client.available()) {
146+
while (!wifiClient->available()) {
148147
executeCallback();
149148

150149
retryCounter++;
@@ -159,20 +158,19 @@ uint16_t SpotifyClient::playerCommand(SpotifyAuth *auth, String method, String c
159158
char c = ' ';
160159

161160
int size = 0;
162-
client.setNoDelay(false);
163-
// while(client.connected()) {
161+
wifiClient->setNoDelay(false);
164162
uint16_t httpCode = 0;
165-
while (client.connected() || client.available()) {
166-
while ((size = client.available()) > 0) {
163+
while (wifiClient->connected() || wifiClient->available()) {
164+
while ((size = wifiClient->available()) > 0) {
167165
if (isBody) {
168166
uint16_t len = min(bufLen, size);
169-
c = client.readBytes(buf, len);
167+
c = wifiClient->readBytes(buf, len);
170168
for (uint16_t i = 0; i < len; i++) {
171169
parser.parse(buf[i]);
172170
// Serial.print((char)buf[i]);
173171
}
174172
} else {
175-
String line = client.readStringUntil('\r');
173+
String line = wifiClient->readStringUntil('\r');
176174
Serial.println(line);
177175
if (line.startsWith("HTTP/1.")) {
178176
httpCode = line.substring(9, line.indexOf(' ', 9)).toInt();
@@ -194,13 +192,16 @@ void SpotifyClient::getToken(SpotifyAuth *auth, String grantType, String code) {
194192
isDataCall = false;
195193
JsonStreamingParser parser;
196194
parser.setListener(this);
197-
WiFiClientSecure client;
195+
198196
// https://accounts.spotify.com/api/token
199197
const char *host = "accounts.spotify.com";
200198
const int port = 443;
201199
String url = "/api/token";
202-
if (!client.connect(host, port)) {
203-
Serial.println("connection failed");
200+
wifiClient->setInsecure();
201+
wifiClient->connect(host, port);
202+
if (!wifiClient->connected()) {
203+
Serial.printf("Connection to %s:%d failed; returning.\n", host, port);
204+
wifiClient->stop();
204205
return;
205206
}
206207

@@ -221,39 +222,33 @@ void SpotifyClient::getToken(SpotifyAuth *auth, String grantType, String code) {
221222
"Connection: close\r\n\r\n" +
222223
content;
223224
Serial.println(request);
224-
client.print(request);
225+
wifiClient->print(request);
225226

226-
int retryCounter = 0;
227-
while (!client.available()) {
228-
executeCallback();
229-
retryCounter++;
230-
if (retryCounter > 10) {
231-
return;
232-
}
233-
delay(10);
234-
}
235-
236-
int pos = 0;
237-
boolean isBody = false;
238227
char c;
239-
240-
int size = 0;
241-
client.setNoDelay(false);
242-
while (client.connected() || client.available()) {
243-
while ((size = client.available()) > 0) {
244-
c = client.read();
228+
boolean isBody = false;
229+
unsigned long timeoutMillis = 10000UL;
230+
unsigned long startMillis = millis();
231+
while (wifiClient->connected() || wifiClient->available()) {
232+
if (wifiClient->available()) {
233+
if ((millis() - startMillis) > timeoutMillis) {
234+
Serial.printf("HTTP timeout after %ds.\n", (timeoutMillis/1000));
235+
wifiClient->stop();
236+
ESP.restart();
237+
}
238+
c = wifiClient->read();
239+
Serial.print(c);
245240
if (c == '{' || c == '[') {
246241
isBody = true;
247242
}
248243
if (isBody) {
249244
parser.parse(c);
250-
Serial.print(c);
251-
} else {
252-
Serial.print(c);
253245
}
254246
}
255247
executeCallback();
248+
// give WiFi and TCP/IP libraries a chance to handle pending events
249+
yield();
256250
}
251+
wifiClient->stop();
257252

258253
this->data = nullptr;
259254
}
@@ -284,7 +279,7 @@ String SpotifyClient::startConfigPortal(const String mDnsName) {
284279

285280
oneWayCode = server.arg("code");
286281
Serial.printf("Code: %s\n", oneWayCode.c_str());
287-
String message = "<html><head></head><body>Succesfully authentiated This device with Spotify. Restart your device now</body></html>";
282+
String message = "<html><head></head><body>Succesfully authentiated this device with Spotify. Restart your device now</body></html>";
288283
server.send(200, "text/html", message);
289284
});
290285

@@ -295,7 +290,7 @@ String SpotifyClient::startConfigPortal(const String mDnsName) {
295290
if (!MDNS.begin(mDnsName)) {
296291
Serial.println("Error setting up MDNS responder!");
297292
}
298-
Serial.println("mDNS responder started");
293+
Serial.println("MDNS responder started");
299294
Serial.println("Open browser at http://" + mDnsName + ".local");
300295

301296
while (oneWayCode == "") {
@@ -306,6 +301,8 @@ String SpotifyClient::startConfigPortal(const String mDnsName) {
306301

307302
Serial.println("Stopping HTTP server");
308303
server.stop();
304+
Serial.println("Stopping MDNS responder");
305+
MDNS.close();
309306
return oneWayCode;
310307
}
311308

SpotifyClient.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include <JsonListener.h>
2727
#include <JsonStreamingParser.h>
2828
#include <ESP8266WebServer.h>
29+
#include <WiFiClientSecure.h>
2930
#include <FS.h>
3031
#include <base64.h>
3132

@@ -139,14 +140,15 @@ class SpotifyClient: public JsonListener {
139140
String clientId;
140141
String clientSecret;
141142
String redirectUri;
143+
WiFiClientSecure *wifiClient;
142144
ESP8266WebServer server;
143145

144146
String getRootPath();
145147
void executeCallback();
146148

147149
public:
148150

149-
SpotifyClient(String clientId, String clientSecret, String redirectUri);
151+
SpotifyClient(String clientId, String clientSecret, String redirectUri, WiFiClientSecure *wifiClient);
150152

151153
uint16_t update(SpotifyData *data, SpotifyAuth *auth);
152154

0 commit comments

Comments
 (0)