This repository was archived by the owner on Mar 17, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 492
/
Copy pathFirebase.cpp
219 lines (188 loc) · 6.22 KB
/
Firebase.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
//
// Copyright 2015 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#include "Firebase.h"
// Detect whether stable version of HTTP library is installed instead of
// master branch and patch in missing status and methods.
#ifndef HTTP_CODE_TEMPORARY_REDIRECT
#define HTTP_CODE_TEMPORARY_REDIRECT 307
#define USE_ESP_ARDUINO_CORE_2_0_0
#endif
namespace {
const char* kFirebaseFingerprint = "7A 54 06 9B DC 7A 25 B3 86 8D 66 53 48 2C 0B 96 42 C7 B3 0A";
const uint16_t kFirebasePort = 443;
String makeFirebaseURL(const String& path, const String& auth) {
String url;
if (path[0] != '/') {
url = "/";
}
url += path + ".json";
if (auth.length() > 0) {
url += "?auth=" + auth;
}
return url;
}
} // namespace
Firebase::Firebase() {
}
Firebase::Firebase(const String& host, const String& auth) {
begin(host, auth);
}
void Firebase::begin(const String& host, const String& auth) {
host_ = host;
auth_ = auth;
http_.setReuse(true);
}
Firebase& Firebase::host(const String& host) {
host_ = host;
return *this;
}
Firebase& Firebase::auth(const String& auth) {
auth_ = auth;
return *this;
}
FirebaseGet Firebase::get(const String& path) {
return FirebaseGet(host_, auth_, path, &http_);
}
FirebaseSet Firebase::set(const String& path, const String& value) {
return FirebaseSet(host_, auth_, path, value, &http_);
}
FirebasePush Firebase::push(const String& path, const String& value) {
return FirebasePush(host_, auth_, path, value, &http_);
}
FirebaseRemove Firebase::remove(const String& path) {
return FirebaseRemove(host_, auth_, path, &http_);
}
FirebaseStream Firebase::stream(const String& path) {
// TODO: create new client dedicated to stream.
return FirebaseStream(host_, auth_, path, &http_);
}
// FirebaseCall
FirebaseCall::FirebaseCall(const String& host, const String& auth,
const char* method, const String& path,
const String& data, HTTPClient* http) {
begin(host, auth, method, path, data, http);
}
void FirebaseCall::begin(const String& host, const String& auth,
const char* method, const String& path,
const String& data, HTTPClient* http) {
http_ = http;
String url = makeFirebaseURL(path, auth);
http_->setReuse(true);
http_->begin(host, kFirebasePort, url, true, kFirebaseFingerprint);
bool followRedirect = false;
if (method == "STREAM") {
method = "GET";
http_->addHeader("Accept", "text/event-stream");
followRedirect = true;
}
if (followRedirect) {
const char* headers[] = {"Location"};
http_->collectHeaders(headers, 1);
}
int status = http_->sendRequest(method, (uint8_t*)data.c_str(), data.length());
// TODO: Add a max redirect check
if (followRedirect) {
while (status == HTTP_CODE_TEMPORARY_REDIRECT) {
String location = http_->header("Location");
http_->setReuse(false);
http_->end();
http_->setReuse(true);
http_->begin(location, kFirebaseFingerprint);
status = http_->sendRequest("GET", (uint8_t*)NULL, 0);
}
}
if (status != 200) {
#ifdef USE_ESP_ARDUINO_CORE_2_0_0
error_ = FirebaseError(status, String(method) + " " + url + ": " + status);
#else
error_ = FirebaseError(status, String(method) + " " + url + ": " + HTTPClient::errorToString(status));
#endif
}
// if not streaming.
if (!followRedirect) {
response_ = http_->getString();
}
}
// FirebaseGet
FirebaseGet::FirebaseGet(const String& host, const String& auth,
const String& path,
HTTPClient* http)
: FirebaseCall(host, auth, "GET", path, "", http) {
if (!error()) {
// TODO: parse json
json_ = response();
}
}
// FirebaseSet
FirebaseSet::FirebaseSet(const String& host, const String& auth,
const String& path, const String& value,
HTTPClient* http)
: FirebaseCall(host, auth, "PUT", path, value, http) {
if (!error()) {
// TODO: parse json
json_ = response();
}
}
// FirebasePush
FirebasePush::FirebasePush(const String& host, const String& auth,
const String& path, const String& value,
HTTPClient* http) {
begin(host, auth, path, value, http);
}
void FirebasePush::begin(const String& host, const String& auth,
const String& path, const String& value,
HTTPClient* http) {
FirebaseCall::begin(host, auth, "POST", path, value, http);
if (!error()) {
// TODO: parse name
name_ = response();
}
}
// FirebasePush
FirebaseRemove::FirebaseRemove(const String& host, const String& auth,
const String& path,
HTTPClient* http)
: FirebaseCall(host, auth, "DELETE", path, "", http) {
}
// FirebaseStream
FirebaseStream::FirebaseStream(const String& host, const String& auth,
const String& path,
HTTPClient* http)
: FirebaseCall(host, auth, "STREAM", path, "", http) {
}
void FirebaseStream::begin(const String& host, const String& auth,
const String& path,
HTTPClient* http) {
FirebaseCall::begin(host, auth, "STREAM", path, "", http);
}
bool FirebaseStream::available() {
return http_->getStreamPtr()->available();
}
FirebaseStream::Event FirebaseStream::read(String& event) {
auto client = http_->getStreamPtr();
Event type;
String typeStr = client->readStringUntil('\n').substring(7);
if (typeStr == "put") {
type = Event::PUT;
} else if (typeStr == "patch") {
type = Event::PATCH;
} else {
type = Event::UNKNOWN;
}
event = client->readStringUntil('\n').substring(6);
client->readStringUntil('\n'); // consume separator
return type;
}