Skip to content
This repository was archived by the owner on Mar 17, 2025. It is now read-only.

Commit 2d8e1eb

Browse files
Separating WiFiClient.
We can now pass in a reference to a WiFi client from calling code. This way, we can set up the client to authenticate with a fingerprint, a certificate, or use an insecure client. The fingerprint no longer needs to be compiled with the library, therefore, the library won't need to be updated whenever the cert changes. This commit also fixes broken error message retrieval.
1 parent 90c0ce4 commit 2d8e1eb

8 files changed

+29
-22
lines changed

src/Firebase.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ std::string makeFirebaseURL(const std::string& path, const std::string& auth) {
3333

3434
} // namespace
3535

36-
Firebase::Firebase(const std::string& host, const std::string& auth) : host_(host), auth_(auth) {
37-
http_.reset(FirebaseHttpClient::create());
36+
Firebase::Firebase(WiFiClient* client, const std::string& host, const std::string& auth) : host_(host), auth_(auth) {
37+
http_.reset(FirebaseHttpClient::create(client));
3838
http_->setReuseConnection(true);
3939
}
4040

src/Firebase.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
// Firebase REST API client.
3333
class Firebase {
3434
public:
35-
Firebase(const std::string& host, const std::string& auth = "");
35+
Firebase(WiFiClient* client, const std::string& host, const std::string& auth = "");
3636

3737
const std::string& auth() const;
3838

src/FirebaseArduino.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,22 +19,23 @@
1919
// This is needed to compile std::string on esp8266.
2020
template class std::basic_string<char>;
2121

22-
void FirebaseArduino::begin(const String& host, const String& auth) {
22+
void FirebaseArduino::begin(WiFiClient* client, const String& host, const String& auth) {
2323
host_ = host.c_str();
2424
auth_ = auth.c_str();
25+
client_ = client;
2526
}
2627

2728
void FirebaseArduino::initStream() {
2829
if (stream_http_.get() == nullptr) {
29-
stream_http_.reset(FirebaseHttpClient::create());
30+
stream_http_.reset(FirebaseHttpClient::create(client_));
3031
stream_http_->setReuseConnection(true);
3132
stream_.reset(new FirebaseStream(stream_http_));
3233
}
3334
}
3435

3536
void FirebaseArduino::initRequest() {
3637
if (req_http_.get() == nullptr) {
37-
req_http_.reset(FirebaseHttpClient::create());
38+
req_http_.reset(FirebaseHttpClient::create(client_));
3839
req_http_->setReuseConnection(true);
3940
req_.reset(new FirebaseRequest(req_http_));
4041
}
@@ -197,8 +198,9 @@ bool FirebaseArduino::failed() {
197198
return error_.code() != 0;
198199
}
199200

200-
const String& FirebaseArduino::error() {
201-
return error_.message().c_str();
201+
void FirebaseArduino::error(std::string* buf) {
202+
std::string err = error_.message();
203+
*buf = err;
202204
}
203205

204206
FirebaseArduino Firebase;

src/FirebaseArduino.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class FirebaseArduino {
3737
* \param host Your firebase db host, usually X.firebaseio.com.
3838
* \param auth Optional credentials for the db, a secret or token.
3939
*/
40-
virtual void begin(const String& host, const String& auth = "");
40+
virtual void begin(WiFiClient* client, const String& host, const String& auth = "");
4141

4242
/**
4343
* Appends the integer value to the node at path.
@@ -221,7 +221,8 @@ class FirebaseArduino {
221221
/**
222222
* \return Error message from last command if failed() is true.
223223
*/
224-
virtual const String& error();
224+
virtual void error(std::string* buf);
225+
225226
private:
226227
std::string host_;
227228
std::string auth_;
@@ -230,6 +231,7 @@ class FirebaseArduino {
230231
std::shared_ptr<FirebaseRequest> req_;
231232
std::shared_ptr<FirebaseHttpClient> stream_http_;
232233
std::shared_ptr<FirebaseStream> stream_;
234+
WiFiClient* client_;
233235

234236
void initStream();
235237
void initRequest();

src/FirebaseCloudMessaging.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@ FirebaseCloudMessage FirebaseCloudMessage::SimpleNotification(
88
return message;
99
}
1010

11-
FirebaseCloudMessaging::FirebaseCloudMessaging(const std::string& server_key) {
11+
FirebaseCloudMessaging::FirebaseCloudMessaging(WiFiClient* client, const std::string& server_key) {
1212
auth_header_ = "key=";
1313
auth_header_ += server_key;
14+
client_ = client;
1415
}
1516

1617
const FirebaseError FirebaseCloudMessaging::SendMessageToUser(
@@ -63,7 +64,7 @@ const FirebaseError FirebaseCloudMessaging::SendMessageToTopic(
6364

6465
const FirebaseError FirebaseCloudMessaging::SendPayload(
6566
const char* payload) {
66-
std::shared_ptr<FirebaseHttpClient> client(FirebaseHttpClient::create());
67+
std::shared_ptr<FirebaseHttpClient> client(FirebaseHttpClient::create(client_));
6768
client->begin("http://fcm.googleapis.com/fcm/send");
6869
client->addHeader("Authorization", auth_header_.c_str());
6970
client->addHeader("Content-Type", "application/json");

src/FirebaseCloudMessaging.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ struct FirebaseCloudMessage {
6464
// Firebase REST API client.
6565
class FirebaseCloudMessaging {
6666
public:
67-
FirebaseCloudMessaging(const std::string& server_key);
67+
FirebaseCloudMessaging(WiFiClient* client, const std::string& server_key);
6868
const FirebaseError SendMessageToUser(const std::string& registration_id,
6969
const FirebaseCloudMessage& message);
7070
const FirebaseError SendMessageToUsers(const std::vector<std::string>& registration_ids,
@@ -82,5 +82,6 @@ class FirebaseCloudMessaging {
8282
const void AddToJson(const FirebaseCloudMessage& message, JsonObject& json) const;
8383

8484
std::string auth_header_;
85+
WiFiClient* client_;
8586
};
8687
#endif // firebase_cloud_messaging_h

src/FirebaseHttpClient.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,15 @@
55

66
#include "Arduino.h"
77
#include "Stream.h"
8+
#include <WiFiClient.h>
89

910
struct HttpStatus {
1011
static const int TEMPORARY_REDIRECT = 307;
1112
};
1213

1314
class FirebaseHttpClient {
1415
public:
15-
static FirebaseHttpClient* create();
16+
static FirebaseHttpClient* create(WiFiClient* client);
1617

1718
virtual void setReuseConnection(bool reuse) = 0;
1819
virtual void begin(const std::string& url) = 0;
@@ -39,7 +40,4 @@ class FirebaseHttpClient {
3940
static const uint16_t kFirebasePort = 443;
4041
};
4142

42-
static const char kFirebaseFingerprint[] =
43-
"B6 F5 80 C8 B1 DA 61 C1 07 9D 80 42 D8 A9 1F AF 9F C8 96 7D"; // 2019-04
44-
4543
#endif // FIREBASE_HTTP_CLIENT_H

src/FirebaseHttpClient_Esp8266.cpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,19 +36,21 @@ class ForceReuseHTTPClient : public HTTPClient {
3636

3737
class FirebaseHttpClientEsp8266 : public FirebaseHttpClient {
3838
public:
39-
FirebaseHttpClientEsp8266() {}
39+
FirebaseHttpClientEsp8266(WiFiClient* client) {
40+
client_ = client;
41+
}
4042

4143
void setReuseConnection(bool reuse) override {
4244
http_.setReuse(reuse);
4345
http_.forceReuse(reuse);
4446
}
4547

4648
void begin(const std::string& url) override {
47-
http_.begin(url.c_str(), kFirebaseFingerprint);
49+
http_.begin(*client_, url.c_str());
4850
}
4951

5052
void begin(const std::string& host, const std::string& path) override {
51-
http_.begin(host.c_str(), kFirebasePort, path.c_str(), kFirebaseFingerprint);
53+
http_.begin(*client_, host.c_str(), kFirebasePort, path.c_str());
5254
}
5355

5456
void end() override {
@@ -89,9 +91,10 @@ class FirebaseHttpClientEsp8266 : public FirebaseHttpClient {
8991

9092
private:
9193
ForceReuseHTTPClient http_;
94+
WiFiClient* client_;
9295
};
9396

94-
FirebaseHttpClient* FirebaseHttpClient::create() {
95-
return new FirebaseHttpClientEsp8266();
97+
FirebaseHttpClient* FirebaseHttpClient::create(WiFiClient* client) {
98+
return new FirebaseHttpClientEsp8266(client);
9699
}
97100

0 commit comments

Comments
 (0)